Send position on button release

This commit is contained in:
dAxpeDDa 2023-06-04 01:08:47 +02:00 committed by daxpedda
parent 587fa67571
commit 61bd8b8254
5 changed files with 54 additions and 35 deletions

View file

@ -63,6 +63,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- **Breaking:** On Web, `instant` is now replaced by `web_time`. - **Breaking:** On Web, `instant` is now replaced by `web_time`.
- On Windows, port to `windows-sys` version 0.48.0. - On Windows, port to `windows-sys` version 0.48.0.
- On Web, fix pen treated as mouse input. - On Web, fix pen treated as mouse input.
- On Web, send mouse position on button release as well.
# 0.28.6 # 0.28.6

View file

@ -400,14 +400,15 @@ impl<T> EventLoopWindowTarget<T> {
}, },
); );
let runner = self.runner.clone();
let runner_touch = self.runner.clone();
let modifiers = self.modifiers.clone();
let has_focus_clone = has_focus.clone();
canvas.on_mouse_release( canvas.on_mouse_release(
move |pointer_id, button, active_modifiers| { {
let runner = self.runner.clone();
let modifiers = self.modifiers.clone();
let has_focus = has_focus.clone();
move |pointer_id, position, button, active_modifiers| {
let modifiers_changed = let modifiers_changed =
(has_focus_clone.get() && modifiers.get() != active_modifiers).then(|| { (has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers); modifiers.set(active_modifiers);
Event::WindowEvent { Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
@ -415,7 +416,17 @@ impl<T> EventLoopWindowTarget<T> {
} }
}); });
runner.send_events(modifiers_changed.into_iter().chain(iter::once( // A mouse up event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
// user code has the correct cursor position.
runner.send_events(modifiers_changed.into_iter().chain([
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
position,
},
},
Event::WindowEvent { Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
event: WindowEvent::MouseInput { event: WindowEvent::MouseInput {
@ -424,8 +435,12 @@ impl<T> EventLoopWindowTarget<T> {
button, button,
}, },
}, },
))); ]));
}
}, },
{
let runner_touch = self.runner.clone();
move |device_id, location, force| { move |device_id, location, force| {
runner_touch.send_event(Event::WindowEvent { runner_touch.send_event(Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
@ -437,6 +452,7 @@ impl<T> EventLoopWindowTarget<T> {
location, location,
}), }),
}); });
}
}, },
); );

View file

@ -243,7 +243,7 @@ impl Canvas {
pub fn on_mouse_release<M, T>(&mut self, mouse_handler: M, touch_handler: T) pub fn on_mouse_release<M, T>(&mut self, mouse_handler: M, touch_handler: T)
where where
M: 'static + FnMut(i32, MouseButton, ModifiersState), M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{ {
match &mut self.mouse_state { match &mut self.mouse_state {

View file

@ -84,7 +84,7 @@ impl MouseHandler {
pub fn on_mouse_release<F>(&mut self, canvas_common: &super::Common, mut handler: F) pub fn on_mouse_release<F>(&mut self, canvas_common: &super::Common, mut handler: F)
where where
F: 'static + FnMut(i32, MouseButton, ModifiersState), F: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
{ {
let on_mouse_leave_handler = self.on_mouse_leave_handler.clone(); let on_mouse_leave_handler = self.on_mouse_leave_handler.clone();
let mouse_capture_state = self.mouse_capture_state.clone(); let mouse_capture_state = self.mouse_capture_state.clone();
@ -110,6 +110,7 @@ impl MouseHandler {
event.stop_propagation(); event.stop_propagation();
handler( handler(
0, 0,
event::mouse_position(&event).to_physical(super::super::scale_factor()),
event::mouse_button(&event).expect("no mouse button released"), event::mouse_button(&event).expect("no mouse button released"),
event::mouse_modifiers(&event), event::mouse_modifiers(&event),
); );

View file

@ -75,7 +75,7 @@ impl PointerHandler {
mut mouse_handler: M, mut mouse_handler: M,
mut touch_handler: T, mut touch_handler: T,
) where ) where
M: 'static + FnMut(i32, MouseButton, ModifiersState), M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{ {
let canvas = canvas_common.raw.clone(); let canvas = canvas_common.raw.clone();
@ -91,6 +91,7 @@ impl PointerHandler {
), ),
"mouse" => mouse_handler( "mouse" => mouse_handler(
event.pointer_id(), event.pointer_id(),
event::mouse_position(&event).to_physical(super::super::scale_factor()),
event::mouse_button(&event).expect("no mouse button released"), event::mouse_button(&event).expect("no mouse button released"),
event::mouse_modifiers(&event), event::mouse_modifiers(&event),
), ),