I just needed to do something so I felt useful today
- Eliminated duplicate platform stubs: Created shared macro for macOS/Linux implementations - Extracted cursor restoration logic: Shared helper function for Windows API calls - Refactored cursor application: Helper function for copy/apply/error handling pattern - Reduced ~70 lines of duplicate code while maintaining all functionality
This commit is contained in:
@@ -1,20 +1,5 @@
|
|||||||
// Linux platform implementation for cursor handling
|
// Linux platform implementation for cursor handling
|
||||||
use super::Result;
|
use super::stub_impl::create_platform_stubs;
|
||||||
|
|
||||||
// Initialize cursor handling
|
// Generate stub implementations for Linux
|
||||||
pub fn init() -> Result<()> {
|
create_platform_stubs!("Linux");
|
||||||
println!("Linux cursor initialization not yet implemented");
|
|
||||||
Ok(()) // Return OK for now to not block platform-agnostic code
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_cursor(path: &str) -> Result<()> {
|
|
||||||
// Linux cursor implementation would go here
|
|
||||||
println!("Linux cursor setting not yet implemented for path: {}", path);
|
|
||||||
Ok(()) // Return OK for now to not block platform-agnostic code
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn restore_default_cursor() -> Result<()> {
|
|
||||||
// Linux cursor restoration would go here
|
|
||||||
println!("Linux cursor restoration not yet implemented");
|
|
||||||
Ok(()) // Return OK for now to not block platform-agnostic code
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,20 +1,5 @@
|
|||||||
// macOS platform implementation for cursor handling
|
// macOS platform implementation for cursor handling
|
||||||
use super::Result;
|
use super::stub_impl::create_platform_stubs;
|
||||||
|
|
||||||
// Initialize cursor handling
|
// Generate stub implementations for macOS
|
||||||
pub fn init() -> Result<()> {
|
create_platform_stubs!("macOS");
|
||||||
println!("macOS cursor initialization not yet implemented");
|
|
||||||
Ok(()) // Return OK for now to not block platform-agnostic code
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_cursor(path: &str) -> Result<()> {
|
|
||||||
// macOS cursor implementation would go here
|
|
||||||
println!("macOS cursor setting not yet implemented for path: {}", path);
|
|
||||||
Ok(()) // Return OK for now to not block platform-agnostic code
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn restore_default_cursor() -> Result<()> {
|
|
||||||
// macOS cursor restoration would go here
|
|
||||||
println!("macOS cursor restoration not yet implemented");
|
|
||||||
Ok(()) // Return OK for now to not block platform-agnostic code
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
// Common types for cursor handling
|
// Common types for cursor handling
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
// Shared stub implementations for unimplemented platforms
|
||||||
|
mod stub_impl;
|
||||||
|
|
||||||
// Windows implementation
|
// Windows implementation
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
mod windows;
|
mod windows;
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
// Shared stub implementations for unimplemented platforms (macOS, Linux)
|
||||||
|
|
||||||
|
/// Creates a stub implementation for a platform that doesn't have cursor support yet
|
||||||
|
macro_rules! create_platform_stubs {
|
||||||
|
($platform_name:expr) => {
|
||||||
|
use super::Result;
|
||||||
|
use image::RgbaImage;
|
||||||
|
|
||||||
|
// Initialize cursor handling
|
||||||
|
pub fn init() -> Result<()> {
|
||||||
|
println!("{} cursor initialization not yet implemented", $platform_name);
|
||||||
|
Ok(()) // Return OK for now to not block platform-agnostic code
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set cursor from image data
|
||||||
|
pub fn set_cursor(_image: &RgbaImage) -> Result<()> {
|
||||||
|
println!("{} cursor setting not yet implemented", $platform_name);
|
||||||
|
Ok(()) // Return OK for now to not block platform-agnostic code
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore default cursor
|
||||||
|
pub fn restore_default_cursor() -> Result<()> {
|
||||||
|
println!("{} cursor restoration not yet implemented", $platform_name);
|
||||||
|
Ok(()) // Return OK for now to not block platform-agnostic code
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export the macro for use by platform modules
|
||||||
|
pub(crate) use create_platform_stubs;
|
||||||
@@ -347,17 +347,8 @@ pub fn cleanup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force Windows to reload default cursor settings
|
// Use the shared cursor restoration logic
|
||||||
SystemParametersInfoW(SPI_SETCURSORS, 0, null_mut(), 0);
|
restore_default_cursor_internal();
|
||||||
|
|
||||||
// Also try the A version for maximum compatibility
|
|
||||||
super::state::reset_system_cursors();
|
|
||||||
|
|
||||||
// Reset to default arrow cursor
|
|
||||||
let arrow_cursor = LoadCursorW(null_mut(), MAKEINTRESOURCEW(IDC_ARROW as u16));
|
|
||||||
if !arrow_cursor.is_null() {
|
|
||||||
SetCursor(arrow_cursor);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -509,6 +500,37 @@ pub fn update_cursor_info(image: &RgbaImage) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to apply a cursor to a specific system cursor ID
|
||||||
|
unsafe fn apply_cursor_to_system_cursor(cursor: winapi::shared::windef::HCURSOR, cursor_id: DWORD, log_errors: bool) -> bool {
|
||||||
|
// Make a copy of the cursor
|
||||||
|
let cursor_copy = CopyIcon(cursor);
|
||||||
|
if cursor_copy.is_null() {
|
||||||
|
if log_errors {
|
||||||
|
let error = std::io::Error::last_os_error();
|
||||||
|
eprintln!(
|
||||||
|
"Failed to copy cursor for ID: {} - Error: {}",
|
||||||
|
cursor_id, error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the system cursor
|
||||||
|
if SetSystemCursor(cursor_copy, cursor_id) == 0 {
|
||||||
|
if log_errors {
|
||||||
|
let error = std::io::Error::last_os_error();
|
||||||
|
eprintln!(
|
||||||
|
"Failed to set cursor for ID: {} - Error: {}",
|
||||||
|
cursor_id, error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
DestroyIcon(cursor_copy); // Clean up the copy if we failed to set it
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
// Apply a cursor to all system cursors
|
// Apply a cursor to all system cursors
|
||||||
unsafe fn apply_cursor_to_all_system_cursors(cursor: winapi::shared::windef::HCURSOR) -> bool {
|
unsafe fn apply_cursor_to_all_system_cursors(cursor: winapi::shared::windef::HCURSOR) -> bool {
|
||||||
let mut success = true;
|
let mut success = true;
|
||||||
@@ -530,51 +552,21 @@ unsafe fn apply_cursor_to_all_system_cursors(cursor: winapi::shared::windef::HCU
|
|||||||
32649, // IDC_HAND (links)
|
32649, // IDC_HAND (links)
|
||||||
];
|
];
|
||||||
|
|
||||||
// Apply to priority cursors first
|
// Apply to priority cursors first (with error logging)
|
||||||
for &cursor_id in priority_cursors.iter() {
|
for &cursor_id in priority_cursors.iter() {
|
||||||
// Make a copy of the cursor
|
if !apply_cursor_to_system_cursor(cursor, cursor_id, true) {
|
||||||
let cursor_copy = CopyIcon(cursor);
|
|
||||||
if cursor_copy.is_null() {
|
|
||||||
let error = std::io::Error::last_os_error();
|
|
||||||
eprintln!(
|
|
||||||
"Failed to copy cursor for priority ID: {} - Error: {}",
|
|
||||||
cursor_id, error
|
|
||||||
);
|
|
||||||
success = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the system cursor
|
|
||||||
if SetSystemCursor(cursor_copy, cursor_id) == 0 {
|
|
||||||
let error = std::io::Error::last_os_error();
|
|
||||||
eprintln!(
|
|
||||||
"Failed to set cursor for priority ID: {} - Error: {}",
|
|
||||||
cursor_id, error
|
|
||||||
);
|
|
||||||
DestroyIcon(cursor_copy); // Clean up the copy if we failed to set it
|
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now apply to the remaining cursors
|
// Now apply to the remaining cursors (without error logging to reduce spam)
|
||||||
for &cursor_id in SYSTEM_CURSORS.iter() {
|
for &cursor_id in SYSTEM_CURSORS.iter() {
|
||||||
// Skip the priority cursors we already processed
|
// Skip the priority cursors we already processed
|
||||||
if priority_cursors.contains(&cursor_id) {
|
if priority_cursors.contains(&cursor_id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make a copy of the cursor
|
if !apply_cursor_to_system_cursor(cursor, cursor_id, false) {
|
||||||
let cursor_copy = CopyIcon(cursor);
|
|
||||||
if cursor_copy.is_null() {
|
|
||||||
// Don't log errors for non-priority cursors to reduce console spam
|
|
||||||
success = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the system cursor
|
|
||||||
if SetSystemCursor(cursor_copy, cursor_id) == 0 {
|
|
||||||
// Don't log errors for non-priority cursors to reduce console spam
|
|
||||||
DestroyIcon(cursor_copy); // Clean up the copy if we failed to set it
|
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -587,9 +579,8 @@ unsafe fn apply_cursor_to_all_system_cursors(cursor: winapi::shared::windef::HCU
|
|||||||
success
|
success
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore the system cursor temporarily (without affecting cleanup state)
|
// Helper function for common cursor restoration logic
|
||||||
pub fn restore_cursor_temporarily() {
|
unsafe fn restore_default_cursor_internal() {
|
||||||
unsafe {
|
|
||||||
// Force Windows to reload default cursor settings
|
// Force Windows to reload default cursor settings
|
||||||
SystemParametersInfoW(SPI_SETCURSORS, 0, null_mut(), 0);
|
SystemParametersInfoW(SPI_SETCURSORS, 0, null_mut(), 0);
|
||||||
|
|
||||||
@@ -602,6 +593,12 @@ pub fn restore_cursor_temporarily() {
|
|||||||
SetCursor(arrow_cursor);
|
SetCursor(arrow_cursor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore the system cursor temporarily (without affecting cleanup state)
|
||||||
|
pub fn restore_cursor_temporarily() {
|
||||||
|
unsafe {
|
||||||
|
restore_default_cursor_internal();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore the system cursor permanently (full cleanup)
|
// Restore the system cursor permanently (full cleanup)
|
||||||
|
|||||||
Reference in New Issue
Block a user