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:
2025-06-09 20:09:24 -05:00
parent 73634440cf
commit aa039c7291
5 changed files with 92 additions and 92 deletions
+3 -18
View File
@@ -1,20 +1,5 @@
// Linux platform implementation for cursor handling
use super::Result;
use super::stub_impl::create_platform_stubs;
// Initialize cursor handling
pub fn init() -> Result<()> {
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
}
// Generate stub implementations for Linux
create_platform_stubs!("Linux");
+3 -18
View File
@@ -1,20 +1,5 @@
// macOS platform implementation for cursor handling
use super::Result;
use super::stub_impl::create_platform_stubs;
// Initialize cursor handling
pub fn init() -> Result<()> {
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
}
// Generate stub implementations for macOS
create_platform_stubs!("macOS");
+3
View File
@@ -3,6 +3,9 @@
// Common types for cursor handling
pub mod types;
// Shared stub implementations for unimplemented platforms
mod stub_impl;
// Windows implementation
#[cfg(target_os = "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
SystemParametersInfoW(SPI_SETCURSORS, 0, null_mut(), 0);
// 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);
}
// Use the shared cursor restoration logic
restore_default_cursor_internal();
}
}
@@ -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
unsafe fn apply_cursor_to_all_system_cursors(cursor: winapi::shared::windef::HCURSOR) -> bool {
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)
];
// Apply to priority cursors first
// Apply to priority cursors first (with error logging)
for &cursor_id in priority_cursors.iter() {
// Make a copy of the cursor
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
if !apply_cursor_to_system_cursor(cursor, cursor_id, true) {
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() {
// Skip the priority cursors we already processed
if priority_cursors.contains(&cursor_id) {
continue;
}
// Make a copy of the cursor
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
if !apply_cursor_to_system_cursor(cursor, cursor_id, false) {
success = false;
}
}
@@ -587,9 +579,8 @@ unsafe fn apply_cursor_to_all_system_cursors(cursor: winapi::shared::windef::HCU
success
}
// Restore the system cursor temporarily (without affecting cleanup state)
pub fn restore_cursor_temporarily() {
unsafe {
// Helper function for common cursor restoration logic
unsafe fn restore_default_cursor_internal() {
// Force Windows to reload default cursor settings
SystemParametersInfoW(SPI_SETCURSORS, 0, null_mut(), 0);
@@ -602,6 +593,12 @@ pub fn restore_cursor_temporarily() {
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)