Optimize cursor handling: hide system cursor once at startup instead of on every swap

This commit is contained in:
2025-05-05 19:39:13 -05:00
parent 4291c8106b
commit 1d4ef5d5f0
2 changed files with 74 additions and 12 deletions
+63 -6
View File
@@ -630,7 +630,8 @@ del "%~f0"
Some((bitmap, mem_dc))
}
pub fn set_cursor(path: &Path) -> bool {
// Function to hide the system cursor - called once at startup
pub fn hide_system_cursor() -> bool {
// Save the original system cursors before hiding them
save_original_cursors();
@@ -673,10 +674,14 @@ del "%~f0"
}
CURSOR_HIDDEN = true;
println!("System cursors hidden");
println!("System cursors hidden once at startup");
}
}
true
}
// Function to update the cursor image without changing system cursors
pub fn update_cursor_image(path: &Path) -> bool {
// Load the cursor image
match image_open(path) {
Ok(img) => {
@@ -719,7 +724,6 @@ del "%~f0"
}
// Start the rendering thread if it's not already running
// This is now just for polling cursor position until we implement event-based updates
start_render_thread_if_needed();
true
@@ -731,6 +735,19 @@ del "%~f0"
}
}
// Main set_cursor function - now uses the separate functions
pub fn set_cursor(path: &Path) -> bool {
// First, ensure system cursor is hidden (only happens once)
if !unsafe { CURSOR_HIDDEN } {
if !hide_system_cursor() {
return false;
}
}
// Then update the cursor image
update_cursor_image(path)
}
// Function to restore the cursor when the program exits
pub fn restore_cursor() {
println!("Explicit restore_cursor called");
@@ -872,6 +889,20 @@ del "%~f0"
mod macos {
use std::path::Path;
pub fn hide_system_cursor() -> bool {
// macOS cursor hiding implementation would go here
// This is a placeholder - actual implementation would require
// Objective-C/Cocoa bindings to NSCursor
println!("macOS system cursor hiding not yet implemented");
false
}
pub fn update_cursor_image(_path: &Path) -> bool {
// macOS cursor image update implementation would go here
println!("macOS cursor image update not yet implemented");
false
}
pub fn set_cursor(_path: &Path) -> bool {
// macOS cursor implementation would go here
// This is a placeholder - actual implementation would require
@@ -896,6 +927,20 @@ mod macos {
mod linux {
use std::path::Path;
pub fn hide_system_cursor() -> bool {
// Linux cursor hiding implementation would go here
// This is a placeholder - actual implementation would require
// X11 or Wayland bindings
println!("Linux system cursor hiding not yet implemented");
false
}
pub fn update_cursor_image(_path: &Path) -> bool {
// Linux cursor image update implementation would go here
println!("Linux cursor image update not yet implemented");
false
}
pub fn set_cursor(_path: &Path) -> bool {
// Linux cursor implementation would go here
// This is a placeholder - actual implementation would require
@@ -943,15 +988,27 @@ pub fn show_window() {
// Export the appropriate implementation based on platform
#[cfg(windows)]
pub use windows::{set_cursor, restore_cursor, render_cursor};
pub use windows::{set_cursor, restore_cursor, render_cursor, hide_system_cursor, update_cursor_image};
#[cfg(target_os = "macos")]
pub use macos::{set_cursor, restore_cursor, render_cursor};
pub use macos::{set_cursor, restore_cursor, render_cursor, hide_system_cursor, update_cursor_image};
#[cfg(target_os = "linux")]
pub use linux::{set_cursor, restore_cursor, render_cursor};
pub use linux::{set_cursor, restore_cursor, render_cursor, hide_system_cursor, update_cursor_image};
// Default implementation for other platforms
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
pub fn hide_system_cursor() -> bool {
println!("System cursor hiding not supported on this platform");
false
}
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
pub fn update_cursor_image(_path: &Path) -> bool {
println!("Cursor image update not supported on this platform");
false
}
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
pub fn set_cursor(_path: &Path) -> bool {
println!("Cursor swapping not supported on this platform");