From 0c549e56e071be75c5f8efef621e7886389ef2c9 Mon Sep 17 00:00:00 2001 From: 73v3 Date: Tue, 19 May 2026 06:43:04 +1000 Subject: [PATCH] Fix macOS cursor grab warp position --- src/native/macos.rs | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/native/macos.rs b/src/native/macos.rs index 29b3e2ef7..273a3c9ca 100644 --- a/src/native/macos.rs +++ b/src/native/macos.rs @@ -144,12 +144,46 @@ impl MacosDisplay { fn move_mouse_inside_window(&self, _window: *mut Object) { unsafe { - let frame: NSRect = msg_send![self.window, frame]; - let origin = self.transform_mouse_point(&frame.origin); + // Use the drawable/content view, not the outer NSWindow frame. + // NSWindow frame includes titlebar/borders and uses AppKit's bottom-left + // screen coordinate system. CGWarpMouseCursorPosition expects CoreGraphics + // screen coordinates, whose Y axis is flipped. + let bounds: NSRect = msg_send![self.view, bounds]; + + let center_in_view = NSPoint { + x: bounds.origin.x + bounds.size.width * 0.5, + y: bounds.origin.y + bounds.size.height * 0.5, + }; + + let center_in_window: NSPoint = msg_send![ + self.view, + convertPoint: center_in_view + toView: nil + ]; + + let center_rect = NSRect { + origin: center_in_window, + size: NSSize { + width: 0.0, + height: 0.0, + }, + }; + + let center_on_screen: NSRect = msg_send![ + self.window, + convertRectToScreen: center_rect + ]; + + // AppKit screen coordinates are bottom-left origin. + // CoreGraphics cursor warp coordinates are top-left origin. + let main_screen: ObjcId = msg_send![class!(NSScreen), mainScreen]; + let main_screen_frame: NSRect = msg_send![main_screen, frame]; + let point = NSPoint { - x: (origin.0 as f64) + (frame.size.width / 2.0), - y: (origin.1 as f64) + (frame.size.height / 2.0), + x: center_on_screen.origin.x, + y: main_screen_frame.size.height - center_on_screen.origin.y, }; + CGWarpMouseCursorPosition(point); } } @@ -1249,7 +1283,8 @@ where (*view).set_ivar("display_ptr", &mut display as *mut _ as *mut c_void); // Tell the view to accept file drops. Without this, dragging files onto the window will do nothing. - let dragged_types: ObjcId = msg_send![class!(NSArray), arrayWithObject: NSPasteboardTypeFileURL]; + let dragged_types: ObjcId = + msg_send![class!(NSArray), arrayWithObject: NSPasteboardTypeFileURL]; let () = msg_send![view, registerForDraggedTypes: dragged_types]; display.window = window;