From 813290130ea255b2cb45a66234a422519d13f667 Mon Sep 17 00:00:00 2001 From: Jason Tsai Date: Thu, 1 Jun 2023 17:42:04 +0800 Subject: [PATCH] fix(maxOS): modify event's mouse location in drag_window (#743) --- .changes/drag-window-cursor-position.md | 5 ++++ src/platform_impl/macos/window.rs | 32 +++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 .changes/drag-window-cursor-position.md diff --git a/.changes/drag-window-cursor-position.md b/.changes/drag-window-cursor-position.md new file mode 100644 index 000000000..c18e5f02f --- /dev/null +++ b/.changes/drag-window-cursor-position.md @@ -0,0 +1,5 @@ +--- +"tao": patch +--- + +On macOS, fix the unexpected shifting of the window when dragging after closing the share dialog. diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 211d8b5d0..3db5044fd 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -41,12 +41,14 @@ use crate::{ use cocoa::{ appkit::{ self, CGFloat, NSApp, NSApplication, NSApplicationPresentationOptions, NSColor, NSEvent, - NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton, - NSWindowCollectionBehavior, NSWindowOrderingMode, NSWindowStyleMask, + NSEventModifierFlags, NSEventSubtype, NSEventType, NSRequestUserAttentionType, NSScreen, + NSView, NSWindow, NSWindowButton, NSWindowCollectionBehavior, NSWindowOrderingMode, + NSWindowStyleMask, }, base::{id, nil}, foundation::{ - NSArray, NSAutoreleasePool, NSDictionary, NSPoint, NSRect, NSSize, NSString, NSUInteger, + NSArray, NSAutoreleasePool, NSDictionary, NSInteger, NSPoint, NSRect, NSSize, NSString, + NSTimeInterval, NSUInteger, }, }; use core_graphics::display::{CGDisplay, CGDisplayMode}; @@ -839,7 +841,29 @@ impl UnownedWindow { #[inline] pub fn drag_window(&self) -> Result<(), ExternalError> { unsafe { - let event: id = msg_send![NSApp(), currentEvent]; + let mut event: id = msg_send![NSApp(), currentEvent]; + + let event_type: NSUInteger = msg_send![event, type]; + if event_type == 0x15 { + let mouse_location: NSPoint = msg_send![class!(NSEvent), mouseLocation]; + let event_modifier_flags: NSEventModifierFlags = msg_send![event, modifierFlags]; + let event_timestamp: NSTimeInterval = msg_send![event, timestamp]; + let event_window_number: NSInteger = msg_send![event, windowNumber]; + + event = msg_send![ + class!(NSEvent), + mouseEventWithType: NSEventType::NSLeftMouseDown + location: mouse_location + modifierFlags: event_modifier_flags + timestamp: event_timestamp + windowNumber: event_window_number + context: nil + eventNumber: NSEventSubtype::NSWindowExposedEventType + clickCount: 1 + pressure: 1.0 + ]; + } + let _: () = msg_send![*self.ns_window, performWindowDragWithEvent: event]; }