diff --git a/core/src/mouse/event.rs b/core/src/mouse/event.rs index 7f5585ada0..e6aa77b6b4 100644 --- a/core/src/mouse/event.rs +++ b/core/src/mouse/event.rs @@ -18,8 +18,10 @@ pub enum Event { /// The mouse cursor was moved CursorMoved { - /// The new position of the mouse cursor + /// The new position of the mouse cursor (window-relative) position: Point, + /// The position of the mouse cursor in screen space (absolute) + screen_position: Point, }, /// A mouse button was pressed. diff --git a/examples/delineate/src/main.rs b/examples/delineate/src/main.rs index 97bd7d6282..a6141508f0 100644 --- a/examples/delineate/src/main.rs +++ b/examples/delineate/src/main.rs @@ -144,7 +144,7 @@ impl Example { fn subscription(&self) -> Subscription { event::listen_with(|event, _status, _window| match event { - Event::Mouse(mouse::Event::CursorMoved { position }) => { + Event::Mouse(mouse::Event::CursorMoved { position, .. }) => { Some(Message::MouseMoved(position)) } Event::Window(window::Event::Resized { .. }) => { diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index b98b827347..4733808ff5 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -346,6 +346,7 @@ pub fn main() -> Result<(), winit::error::EventLoopError> { if let Some(event) = conversion::window_event( event, window.scale_factor() as f32, + window.outer_position().ok(), *modifiers, ) { events.push(event); diff --git a/test/src/emulator.rs b/test/src/emulator.rs index 0bd653d7b4..6fc349332c 100644 --- a/test/src/emulator.rs +++ b/test/src/emulator.rs @@ -313,6 +313,7 @@ impl Emulator

{ for event in &events { if let core::Event::Mouse(mouse::Event::CursorMoved { position, + .. }) = event { self.cursor = mouse::Cursor::Available(*position); diff --git a/test/src/instruction.rs b/test/src/instruction.rs index 99d0d505e0..298b68f31e 100644 --- a/test/src/instruction.rs +++ b/test/src/instruction.rs @@ -49,7 +49,7 @@ impl Interaction { pub fn from_event(event: &Event) -> Option { Some(match event { Event::Mouse(mouse) => Self::Mouse(match mouse { - mouse::Event::CursorMoved { position } => { + mouse::Event::CursorMoved { position, .. } => { Mouse::Move(Target::Point(*position)) } mouse::Event::ButtonPressed(button) => Mouse::Press { @@ -223,14 +223,14 @@ impl Interaction { /// Returns a list of runtime events representing the [`Interaction`]. /// - /// The `find_target` closure must convert a [`Target`] into its screen - /// coordinates. + /// The `find_target` closure must convert a [`Target`] into a position + /// in the viewport. pub fn events( &self, find_target: impl FnOnce(&Target) -> Option, ) -> Option> { let mouse_move_ = - |to| Event::Mouse(mouse::Event::CursorMoved { position: to }); + |to| Event::Mouse(mouse::Event::CursorMoved { position: to, screen_position: to }); let mouse_press = |button| Event::Mouse(mouse::Event::ButtonPressed(button)); diff --git a/tester/src/recorder.rs b/tester/src/recorder.rs index 3baefd250e..e66ba65001 100644 --- a/tester/src/recorder.rs +++ b/tester/src/recorder.rs @@ -402,9 +402,10 @@ fn record( } let interaction = - if let Event::Mouse(mouse::Event::CursorMoved { position }) = event { + if let Event::Mouse(mouse::Event::CursorMoved { position, screen_position }) = event { Interaction::from_event(&Event::Mouse(mouse::Event::CursorMoved { position: *position - (bounds.position() - Point::ORIGIN), + screen_position: *screen_position, })) } else { Interaction::from_event(event) diff --git a/widget/src/image/viewer.rs b/widget/src/image/viewer.rs index 29da5d6de0..0970fbb91d 100644 --- a/widget/src/image/viewer.rs +++ b/widget/src/image/viewer.rs @@ -239,7 +239,7 @@ where shell.capture_event(); } } - Event::Mouse(mouse::Event::CursorMoved { position }) => { + Event::Mouse(mouse::Event::CursorMoved { position, .. }) => { let state = tree.state.downcast_mut::(); if let Some(origin) = state.cursor_grabbed_at { diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 0783f7c297..6531149ff8 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -822,7 +822,7 @@ where | Event::Touch(touch::Event::FingerLost { .. }) => { state::(tree).is_dragging = false; } - Event::Mouse(mouse::Event::CursorMoved { position }) + Event::Mouse(mouse::Event::CursorMoved { position, .. }) | Event::Touch(touch::Event::FingerMoved { position, .. }) => { let state = state::(tree); diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 250918ab78..6ef1f2d815 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -141,6 +141,7 @@ pub fn window_attributes( pub fn window_event( event: winit::event::WindowEvent, scale_factor: f32, + window_position: Option>, modifiers: winit::keyboard::ModifiersState, ) -> Option { use winit::event::Ime; @@ -161,8 +162,18 @@ pub fn window_event( WindowEvent::CursorMoved { position, .. } => { let position = position.to_logical::(f64::from(scale_factor)); + let window_position_logical = window_position + .unwrap_or_default() + .to_logical::(f64::from(scale_factor)); + + let screen_position = Point::new( + (window_position_logical.x + position.x) as f32, + (window_position_logical.y + position.y) as f32, + ); + Some(Event::Mouse(mouse::Event::CursorMoved { position: Point::new(position.x as f32, position.y as f32), + screen_position, })) } WindowEvent::CursorEntered { .. } => { diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 8be0e924ce..3cbc9d2acb 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -972,6 +972,7 @@ async fn run_instance

( if let Some(event) = conversion::window_event( window_event, window.state.scale_factor(), + window.raw.outer_position().ok(), window.state.modifiers(), ) { events.push((id, event));