diff --git a/src/platform/macos/view.rs b/src/platform/macos/view.rs index 07cd39c6..90c07d11 100644 --- a/src/platform/macos/view.rs +++ b/src/platform/macos/view.rs @@ -278,6 +278,26 @@ impl BaseviewView { this.window_handler.use_handler(|h| h.on_event(event)).unwrap_or(EventStatus::Ignored) } + /// Reports the mouse position of `event` as a `CursorMoved` event. + /// + /// Button presses report it first too: mouse moves are only tracked while + /// the window is key, so without it the first click into an inactive window + /// would come with no known position, and handlers that need one (such as + /// egui-baseview) would drop it. + fn report_position(this: ViewRef, event: &NSEvent) { + let point = this.view.convertPoint_fromView(event.locationInWindow(), None); + + let position = LogicalPosition { x: point.x, y: point.y }; + + Self::trigger_event( + this, + Event::Mouse(MouseEvent::CursorMoved { + position: position.to_physical(this.state.scale_factor.get()), + modifiers: make_modifiers(event.modifierFlags()), + }), + ); + } + fn trigger_frame(this: ViewRef) { let result = this.window_handler.use_handler(|h| { this.state.redraw_requested.set(false); @@ -521,17 +541,7 @@ impl ViewImpl for BaseviewView { } fn mouse_moved(this: ViewRef, event: &NSEvent) { - let point = this.view.convertPoint_fromView(event.locationInWindow(), None); - - let position = LogicalPosition { x: point.x, y: point.y }; - - Self::trigger_event( - this, - Event::Mouse(MouseEvent::CursorMoved { - position: position.to_physical(this.state.scale_factor.get()), - modifiers: make_modifiers(event.modifierFlags()), - }), - ); + Self::report_position(this, event); // SAFETY: Our superclass is NSView let _: () = unsafe { msg_send![super(this.view, NSView::class()), mouseMoved: event] }; @@ -649,6 +659,7 @@ impl ViewImpl for BaseviewView { } fn mouse_down(this: ViewRef, event: &NSEvent) { + Self::report_position(this, event); Self::trigger_event( this, Event::Mouse(ButtonPressed { @@ -669,6 +680,7 @@ impl ViewImpl for BaseviewView { } fn right_mouse_down(this: ViewRef, event: &NSEvent) { + Self::report_position(this, event); Self::trigger_event( this, Event::Mouse(ButtonPressed { @@ -689,6 +701,7 @@ impl ViewImpl for BaseviewView { } fn other_mouse_down(this: ViewRef, event: &NSEvent) { + Self::report_position(this, event); Self::trigger_event( this, Event::Mouse(ButtonPressed {