Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Add positions to mouse click events. #1289

Closed
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
1 change: 1 addition & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub enum WindowEvent {
state: ElementState,
button: MouseButton,
modifiers: ModifiersState,
position: LogicalPosition,
},

/// Touchpad pressure event.
Expand Down
8 changes: 6 additions & 2 deletions src/platform_impl/linux/wayland/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ pub fn implement_pointer<T: 'static>(
let mut sink = sink.lock().unwrap();
let store = store.lock().unwrap();
let mut cursor_manager = cursor_manager.lock().unwrap();
let mut cursor_pos = (0.0, 0.0).into();
match evt {
PtrEvent::Enter {
surface,
surface_x,
surface_y,
..
} => {
cursor_pos = (surface_x, surface_y).into();
let wid = store.find_wid(&surface);
if let Some(wid) = wid {
mouse_focus = Some(wid);
Expand All @@ -69,7 +71,7 @@ pub fn implement_pointer<T: 'static>(
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
),
position: (surface_x, surface_y).into(),
position: cursor_pos,
modifiers: modifiers_tracker.lock().unwrap().clone(),
},
wid,
Expand Down Expand Up @@ -97,13 +99,14 @@ pub fn implement_pointer<T: 'static>(
surface_y,
..
} => {
cursor_pos = (surface_x, surface_y).into();
if let Some(wid) = mouse_focus {
sink.send_window_event(
WindowEvent::CursorMoved {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
),
position: (surface_x, surface_y).into(),
position: cursor_pos,
modifiers: modifiers_tracker.lock().unwrap().clone(),
},
wid,
Expand Down Expand Up @@ -131,6 +134,7 @@ pub fn implement_pointer<T: 'static>(
),
state,
button,
position: cursor_pos,
modifiers: modifiers_tracker.lock().unwrap().clone(),
},
wid,
Expand Down
12 changes: 12 additions & 0 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ impl<T: 'static> EventProcessor<T> {
} else {
Released
};

let dpi_factor = self
.with_window(xev.event, |window| window.hidpi_factor())
.unwrap_or(1.0);
let position = LogicalPosition::from_physical(
(xev.event_x as f64, xev.event_y as f64),
dpi_factor,
);
match xev.detail as u32 {
ffi::Button1 => callback(Event::WindowEvent {
window_id,
Expand All @@ -659,6 +667,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Left,
modifiers,
position,
},
}),
ffi::Button2 => callback(Event::WindowEvent {
Expand All @@ -668,6 +677,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Middle,
modifiers,
position,
},
}),
ffi::Button3 => callback(Event::WindowEvent {
Expand All @@ -677,6 +687,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Right,
modifiers,
position,
},
}),

Expand Down Expand Up @@ -710,6 +721,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Other(x as u8),
modifiers,
position,
},
}),
}
Expand Down
6 changes: 4 additions & 2 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,29 @@ impl<T> WindowTarget<T> {
});

let runner = self.runner.clone();
canvas.on_mouse_press(move |pointer_id, button, modifiers| {
canvas.on_mouse_press(move |pointer_id, button, modifiers, position| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(id),
event: WindowEvent::MouseInput {
device_id: DeviceId(device::Id(pointer_id)),
state: ElementState::Pressed,
button,
modifiers,
position,
},
});
});

let runner = self.runner.clone();
canvas.on_mouse_release(move |pointer_id, button, modifiers| {
canvas.on_mouse_release(move |pointer_id, button, modifiers, position| {
runner.send_event(Event::WindowEvent {
window_id: WindowId(id),
event: WindowEvent::MouseInput {
device_id: DeviceId(device::Id(pointer_id)),
state: ElementState::Released,
button,
modifiers,
position,
},
});
});
Expand Down
6 changes: 4 additions & 2 deletions src/platform_impl/web/stdweb/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,28 @@ impl Canvas {

pub fn on_mouse_release<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(i32, MouseButton, ModifiersState),
F: 'static + FnMut(i32, MouseButton, ModifiersState, LogicalPosition),
{
self.on_mouse_release = Some(self.add_user_event(move |event: PointerUpEvent| {
handler(
event.pointer_id(),
event::mouse_button(&event),
event::mouse_modifiers(&event),
event::mouse_position(&event),
);
}));
}

pub fn on_mouse_press<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(i32, MouseButton, ModifiersState),
F: 'static + FnMut(i32, MouseButton, ModifiersState, LogicalPosition),
{
self.on_mouse_press = Some(self.add_user_event(move |event: PointerDownEvent| {
handler(
event.pointer_id(),
event::mouse_button(&event),
event::mouse_modifiers(&event),
event::mouse_position(&event),
);
}));
}
Expand Down
6 changes: 4 additions & 2 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Canvas {

pub fn on_mouse_release<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(i32, MouseButton, ModifiersState),
F: 'static + FnMut(i32, MouseButton, ModifiersState, LogicalPosition),
{
self.on_mouse_release = Some(self.add_user_event(
"pointerup",
Expand All @@ -195,14 +195,15 @@ impl Canvas {
event.pointer_id(),
event::mouse_button(&event),
event::mouse_modifiers(&event),
event::mouse_position(&event),
);
},
));
}

pub fn on_mouse_press<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(i32, MouseButton, ModifiersState),
F: 'static + FnMut(i32, MouseButton, ModifiersState, LogicalPosition),
{
self.on_mouse_press = Some(self.add_user_event(
"pointerdown",
Expand All @@ -211,6 +212,7 @@ impl Canvas {
event.pointer_id(),
event::mouse_button(&event),
event::mouse_modifiers(&event),
event::mouse_position(&event),
);
},
));
Expand Down
23 changes: 23 additions & 0 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,13 @@ fn normalize_pointer_pressure(pressure: u32) -> Option<Force> {
}
}

fn event_position(window: HWND, lparam: LPARAM) -> LogicalPosition {
let x = windowsx::GET_X_LPARAM(lparam) as f64;
let y = windowsx::GET_Y_LPARAM(lparam) as f64;
let dpi_factor = hwnd_scale_factor(window);
LogicalPosition::from_physical((x, y), dpi_factor)
}

/// Any window whose callback is configured to this function will have its events propagated
/// through the events loop of the thread the window was created in.
//
Expand Down Expand Up @@ -1321,6 +1328,7 @@ unsafe extern "system" fn public_window_callback<T>(
use crate::event::{ElementState::Pressed, MouseButton::Left, WindowEvent::MouseInput};

capture_mouse(window, &mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1329,6 +1337,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Pressed,
button: Left,
modifiers: event::get_key_mods(),
position,
},
});
0
Expand All @@ -1340,6 +1349,7 @@ unsafe extern "system" fn public_window_callback<T>(
};

release_mouse(&mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1348,6 +1358,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Released,
button: Left,
modifiers: event::get_key_mods(),
position,
},
});
0
Expand All @@ -1359,6 +1370,7 @@ unsafe extern "system" fn public_window_callback<T>(
};

capture_mouse(window, &mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1367,6 +1379,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Pressed,
button: Right,
modifiers: event::get_key_mods(),
position,
},
});
0
Expand All @@ -1378,6 +1391,7 @@ unsafe extern "system" fn public_window_callback<T>(
};

release_mouse(&mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1386,6 +1400,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Released,
button: Right,
modifiers: event::get_key_mods(),
position,
},
});
0
Expand All @@ -1397,6 +1412,7 @@ unsafe extern "system" fn public_window_callback<T>(
};

capture_mouse(window, &mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1405,6 +1421,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Pressed,
button: Middle,
modifiers: event::get_key_mods(),
position,
},
});
0
Expand All @@ -1416,6 +1433,7 @@ unsafe extern "system" fn public_window_callback<T>(
};

release_mouse(&mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1424,6 +1442,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Released,
button: Middle,
modifiers: event::get_key_mods(),
position,
},
});
0
Expand All @@ -1436,6 +1455,7 @@ unsafe extern "system" fn public_window_callback<T>(
let xbutton = winuser::GET_XBUTTON_WPARAM(wparam);

capture_mouse(window, &mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1444,6 +1464,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Pressed,
button: Other(xbutton as u8),
modifiers: event::get_key_mods(),
position,
},
});
0
Expand All @@ -1456,6 +1477,7 @@ unsafe extern "system" fn public_window_callback<T>(
let xbutton = winuser::GET_XBUTTON_WPARAM(wparam);

release_mouse(&mut *subclass_input.window_state.lock());
let position = event_position(window, lparam);

subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
Expand All @@ -1464,6 +1486,7 @@ unsafe extern "system" fn public_window_callback<T>(
state: Released,
button: Other(xbutton as u8),
modifiers: event::get_key_mods(),
position,
},
});
0
Expand Down