Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/src/mouse/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/delineate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Example {

fn subscription(&self) -> Subscription<Message> {
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 { .. }) => {
Expand Down
1 change: 1 addition & 0 deletions examples/integration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ impl<P: Program + 'static> Emulator<P> {
for event in &events {
if let core::Event::Mouse(mouse::Event::CursorMoved {
position,
..
}) = event
{
self.cursor = mouse::Cursor::Available(*position);
Expand Down
8 changes: 4 additions & 4 deletions test/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Interaction {
pub fn from_event(event: &Event) -> Option<Self> {
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 {
Expand Down Expand Up @@ -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<Point>,
) -> Option<Vec<Event>> {
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));
Expand Down
3 changes: 2 additions & 1 deletion tester/src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,10 @@ fn record<Message>(
}

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)
Expand Down
2 changes: 1 addition & 1 deletion widget/src/image/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<State>();

if let Some(origin) = state.cursor_grabbed_at {
Expand Down
2 changes: 1 addition & 1 deletion widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ where
| Event::Touch(touch::Event::FingerLost { .. }) => {
state::<Renderer>(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::<Renderer>(tree);

Expand Down
11 changes: 11 additions & 0 deletions winit/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub fn window_attributes(
pub fn window_event(
event: winit::event::WindowEvent,
scale_factor: f32,
window_position: Option<winit::dpi::PhysicalPosition<i32>>,
modifiers: winit::keyboard::ModifiersState,
) -> Option<Event> {
use winit::event::Ime;
Expand All @@ -161,8 +162,18 @@ pub fn window_event(
WindowEvent::CursorMoved { position, .. } => {
let position = position.to_logical::<f64>(f64::from(scale_factor));

let window_position_logical = window_position
.unwrap_or_default()
.to_logical::<f64>(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 { .. } => {
Expand Down
1 change: 1 addition & 0 deletions winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ async fn run_instance<P>(
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));
Expand Down