From 493837588d1545715f9e5c7f0da71a21c4a4487a Mon Sep 17 00:00:00 2001 From: abrahamanavhoeba-alt Date: Sun, 16 Nov 2025 11:30:22 -0500 Subject: [PATCH] Fix critical macOS crash and add icon size configuration This commit addresses three vital issues: 1. Fix issue #23 - macOS app crashes when using JS API - Fixed incorrect NSPoint extraction from backingAlignedRect (line 70) - The method returns NSRect, not NSPoint - now properly extracting .origin - Fixed callback type mismatch in drop handler (line 273) - Changed from Fn(DragResult) to Fn(DragResult, CursorPosition) 2. Fix issue #41 - Large drag preview images - Added optional icon_size field to Options struct - Allows users to specify custom icon dimensions (width, height) - Implemented on both macOS (using setSize) and Windows (SHDRAGIMAGE) - Maintains backward compatibility - None uses original size Note: These fixes compile successfully but require manual GUI testing on respective platforms to fully verify functionality. Related issues: #23, #41 --- crates/drag/src/lib.rs | 4 ++++ crates/drag/src/platform_impl/macos/mod.rs | 17 +++++++++++++---- crates/drag/src/platform_impl/windows/mod.rs | 11 +++++++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/drag/src/lib.rs b/crates/drag/src/lib.rs index 2de7588..cb4a6e6 100644 --- a/crates/drag/src/lib.rs +++ b/crates/drag/src/lib.rs @@ -163,6 +163,10 @@ unsafe impl objc::Encode for DragMode { pub struct Options { pub skip_animatation_on_cancel_or_failure: bool, pub mode: DragMode, + /// Optional icon size (width, height) in pixels. + /// If set, the drag preview icon will be scaled to this size. + /// If None, the original image size will be used. + pub icon_size: Option<(u32, u32)>, } /// An image definition. diff --git a/crates/drag/src/platform_impl/macos/mod.rs b/crates/drag/src/platform_impl/macos/mod.rs index f232590..011130b 100644 --- a/crates/drag/src/platform_impl/macos/mod.rs +++ b/crates/drag/src/platform_impl/macos/mod.rs @@ -67,7 +67,8 @@ pub fn start_drag); - + if operation == 0 { // NSDragOperationNone callback_closure(DragResult::Cancel, mouse_location); @@ -269,7 +278,7 @@ pub fn start_drag)); + drop(Box::from_raw(*callback as *mut Box)); } } diff --git a/crates/drag/src/platform_impl/windows/mod.rs b/crates/drag/src/platform_impl/windows/mod.rs index ee9ee4f..7e9d434 100644 --- a/crates/drag/src/platform_impl/windows/mod.rs +++ b/crates/drag/src/platform_impl/windows/mod.rs @@ -239,7 +239,7 @@ pub fn start_drag(&CLSID_DragDropHelper) { @@ -280,7 +280,7 @@ pub fn start_drag(&CLSID_DragDropHelper) { @@ -312,7 +312,7 @@ pub fn start_drag Option { +fn get_drag_image(image: Image, icon_size: Option<(u32, u32)>) -> Option { let hbitmap = match image { Image::Raw(bytes) => image::read_bytes_to_hbitmap(&bytes).ok(), Image::File(path) => image::read_path_to_hbitmap(&path).ok(), @@ -320,7 +320,10 @@ fn get_drag_image(image: Image) -> Option { hbitmap.map(|hbitmap| unsafe { // get image size let mut bitmap: BITMAP = BITMAP::default(); - let (width, height) = if 0 + let (width, height) = if let Some((w, h)) = icon_size { + // Use specified icon size + (w as i32, h as i32) + } else if 0 == GetObjectW( hbitmap, std::mem::size_of::() as i32,