Split cursor move handlers

This commit is contained in:
dAxpeDDa 2023-06-04 01:08:03 +02:00 committed by daxpedda
parent 7500a88230
commit 587fa67571
4 changed files with 132 additions and 108 deletions

View file

@ -244,23 +244,67 @@ 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_cursor_move( canvas.on_cursor_move(
move |pointer_id, position, delta, active_modifiers, buttons, button| { {
let modifiers_changed = let runner = self.runner.clone();
(has_focus_clone.get() && modifiers.get() != active_modifiers).then(|| { let has_focus = has_focus.clone();
let modifiers = self.modifiers.clone();
move |active_modifiers| {
if has_focus.get() && modifiers.get() != active_modifiers {
modifiers.set(active_modifiers); modifiers.set(active_modifiers);
Event::WindowEvent { runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
event: WindowEvent::ModifiersChanged(active_modifiers.into()), event: WindowEvent::ModifiersChanged(active_modifiers.into()),
} })
}); }
}
},
{
let runner = self.runner.clone();
let button_event = button.map(|button| { move |pointer_id, position, delta| {
if buttons.contains(button.into()) { runner.send_events(
[
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
position,
},
},
Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(pointer_id)),
event: DeviceEvent::MouseMotion {
delta: (delta.x, delta.y),
},
},
]
.into_iter(),
);
}
},
{
let runner = self.runner.clone();
move |device_id, location, force| {
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Touch(Touch {
id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
phase: TouchPhase::Moved,
force: Some(force),
location,
}),
});
}
},
{
let runner = self.runner.clone();
move |pointer_id, position: crate::dpi::PhysicalPosition<f64>, buttons, button| {
let button_event = if buttons.contains(button.into()) {
Event::WindowEvent { Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
event: WindowEvent::MouseInput { event: WindowEvent::MouseInput {
@ -278,13 +322,13 @@ impl<T> EventLoopWindowTarget<T> {
button, button,
}, },
} }
} };
});
runner.send_events( // A chorded button event may come in without any prior CursorMoved events,
modifiers_changed // therefore we should send a CursorMoved event to make sure that the
.into_iter() // user code has the correct cursor position.
.chain([ runner.send_events(
[
Event::WindowEvent { Event::WindowEvent {
window_id: RootWindowId(id), window_id: RootWindowId(id),
event: WindowEvent::CursorMoved { event: WindowEvent::CursorMoved {
@ -292,27 +336,11 @@ impl<T> EventLoopWindowTarget<T> {
position, position,
}, },
}, },
Event::DeviceEvent { button_event,
device_id: RootDeviceId(DeviceId(pointer_id)), ]
event: DeviceEvent::MouseMotion { .into_iter(),
delta: (delta.x, delta.y), );
}, }
},
])
.chain(button_event),
);
},
move |device_id, location, force| {
runner_touch.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Touch(Touch {
id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
phase: TouchPhase::Moved,
force: Some(force),
location,
}),
});
}, },
prevent_default, prevent_default,
); );

View file

@ -267,28 +267,31 @@ impl Canvas {
} }
} }
pub fn on_cursor_move<M, T>( pub fn on_cursor_move<MOD, M, T, B>(
&mut self, &mut self,
modifier_handler: MOD,
mouse_handler: M, mouse_handler: M,
touch_handler: T, touch_handler: T,
button_handler: B,
prevent_default: bool, prevent_default: bool,
) where ) where
M: 'static MOD: 'static + FnMut(ModifiersState),
+ FnMut( M: 'static + FnMut(i32, PhysicalPosition<f64>, PhysicalPosition<f64>),
i32,
PhysicalPosition<f64>,
PhysicalPosition<f64>,
ModifiersState,
ButtonsState,
Option<MouseButton>,
),
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
B: 'static + FnMut(i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
{ {
match &mut self.mouse_state { match &mut self.mouse_state {
MouseState::HasPointerEvent(h) => { MouseState::HasPointerEvent(h) => h.on_cursor_move(
h.on_cursor_move(&self.common, mouse_handler, touch_handler, prevent_default) &self.common,
modifier_handler,
mouse_handler,
touch_handler,
button_handler,
prevent_default,
),
MouseState::NoPointerEvent(h) => {
h.on_cursor_move(&self.common, modifier_handler, mouse_handler)
} }
MouseState::NoPointerEvent(h) => h.on_cursor_move(&self.common, mouse_handler),
} }
} }

View file

@ -7,7 +7,6 @@ use crate::keyboard::ModifiersState;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use event::ButtonsState;
use web_sys::{EventTarget, MouseEvent}; use web_sys::{EventTarget, MouseEvent};
type MouseLeaveHandler = Rc<RefCell<Option<Box<dyn FnMut(i32, ModifiersState)>>>>; type MouseLeaveHandler = Rc<RefCell<Option<Box<dyn FnMut(i32, ModifiersState)>>>>;
@ -172,23 +171,22 @@ impl MouseHandler {
)); ));
} }
pub fn on_cursor_move<F>(&mut self, canvas_common: &super::Common, mut handler: F) pub fn on_cursor_move<MOD, M>(
where &mut self,
F: 'static canvas_common: &super::Common,
+ FnMut( mut modifier_handler: MOD,
i32, mut mouse_handler: M,
PhysicalPosition<f64>, ) where
PhysicalPosition<f64>, MOD: 'static + FnMut(ModifiersState),
ModifiersState, M: 'static + FnMut(i32, PhysicalPosition<f64>, PhysicalPosition<f64>),
ButtonsState,
Option<MouseButton>,
),
{ {
let mouse_capture_state = self.mouse_capture_state.clone(); let mouse_capture_state = self.mouse_capture_state.clone();
let canvas = canvas_common.raw.clone(); let canvas = canvas_common.raw.clone();
self.on_mouse_move = Some(canvas_common.add_window_mouse_event( self.on_mouse_move = Some(canvas_common.add_window_mouse_event(
"mousemove", "mousemove",
move |event: MouseEvent| { move |event: MouseEvent| {
modifier_handler(event::mouse_modifiers(&event));
let canvas = canvas.clone(); let canvas = canvas.clone();
let mouse_capture_state = mouse_capture_state.borrow(); let mouse_capture_state = mouse_capture_state.borrow();
let is_over_canvas = event let is_over_canvas = event
@ -213,13 +211,10 @@ impl MouseHandler {
event::mouse_position_by_client(&event, &canvas) event::mouse_position_by_client(&event, &canvas)
}; };
let mouse_delta = event::mouse_delta(&event); let mouse_delta = event::mouse_delta(&event);
handler( mouse_handler(
0, 0,
mouse_pos.to_physical(super::super::scale_factor()), mouse_pos.to_physical(super::super::scale_factor()),
mouse_delta.to_physical(super::super::scale_factor()), mouse_delta.to_physical(super::super::scale_factor()),
event::mouse_modifiers(&event),
event::mouse_buttons(&event),
event::mouse_button(&event),
); );
} }
} }

View file

@ -141,23 +141,19 @@ impl PointerHandler {
)); ));
} }
pub fn on_cursor_move<M, T>( pub fn on_cursor_move<MOD, M, T, B>(
&mut self, &mut self,
canvas_common: &super::Common, canvas_common: &super::Common,
mut modifier_handler: MOD,
mut mouse_handler: M, mut mouse_handler: M,
mut touch_handler: T, mut touch_handler: T,
mut button_handler: B,
prevent_default: bool, prevent_default: bool,
) where ) where
M: 'static MOD: 'static + FnMut(ModifiersState),
+ FnMut( M: 'static + FnMut(i32, PhysicalPosition<f64>, PhysicalPosition<f64>),
i32,
PhysicalPosition<f64>,
PhysicalPosition<f64>,
ModifiersState,
ButtonsState,
Option<MouseButton>,
),
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
B: 'static + FnMut(i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
{ {
let canvas = canvas_common.raw.clone(); let canvas = canvas_common.raw.clone();
self.on_cursor_move = Some(canvas_common.add_event( self.on_cursor_move = Some(canvas_common.add_event(
@ -173,7 +169,11 @@ impl PointerHandler {
fn has_get_coalesced_events(this: &PointerEventExt) -> JsValue; fn has_get_coalesced_events(this: &PointerEventExt) -> JsValue;
} }
match event.pointer_type().as_str() { modifier_handler(event::mouse_modifiers(&event));
let pointer_type = event.pointer_type();
match pointer_type.as_str() {
"touch" => { "touch" => {
if prevent_default { if prevent_default {
// prevent scroll on mobile web // prevent scroll on mobile web
@ -184,23 +184,32 @@ impl PointerHandler {
_ => return, _ => return,
} }
let event: PointerEventExt = event.unchecked_into();
let id = event.pointer_id(); let id = event.pointer_id();
// cache buttons if the pointer is a mouse
let mouse = (event.pointer_type() == "mouse").then(|| { // chorded button event
( if let Some(button) = event::mouse_button(&event) {
event::mouse_modifiers(&event), debug_assert_eq!(
pointer_type, "mouse",
"expect pointer type of a chorded button event to be a mouse"
);
button_handler(
id,
event::mouse_position(&event).to_physical(super::super::scale_factor()),
event::mouse_buttons(&event), event::mouse_buttons(&event),
event::mouse_button(&event), button,
) );
});
return;
}
// pointer move event
let event: PointerEventExt = event.unchecked_into();
// store coalesced events to extend it's lifetime // store coalesced events to extend it's lifetime
let events = (!event.has_get_coalesced_events().is_undefined()) let events = (!event.has_get_coalesced_events().is_undefined())
.then(|| event.get_coalesced_events()) .then(|| event.get_coalesced_events());
// if coalesced events is empty, it's a chorded button event
.filter(|events| events.length() != 0);
// make a single iterator depending on the availability of coalesced events // make a single iterator depending on the availability of coalesced events
let events = if let Some(events) = &events { let events = if let Some(events) = &events {
@ -214,30 +223,19 @@ impl PointerHandler {
}; };
for event in events { for event in events {
// coalesced events should always have the same source as the root event match pointer_type.as_str() {
debug_assert_eq!(id, event.pointer_id()); "mouse" => mouse_handler(
debug_assert_eq!(mouse.is_none(), event.pointer_type() == "touch");
if let Some((modifiers, buttons, button)) = mouse {
// coalesced events should have the same buttons
debug_assert_eq!(modifiers, event::mouse_modifiers(&event));
debug_assert_eq!(buttons, event::mouse_buttons(&event));
mouse_handler(
id, id,
event::mouse_position(&event).to_physical(super::super::scale_factor()), event::mouse_position(&event).to_physical(super::super::scale_factor()),
event::mouse_delta(&event).to_physical(super::super::scale_factor()), event::mouse_delta(&event).to_physical(super::super::scale_factor()),
modifiers, ),
buttons, "touch" => touch_handler(
button,
);
} else {
touch_handler(
id, id,
event::touch_position(&event, &canvas) event::touch_position(&event, &canvas)
.to_physical(super::super::scale_factor()), .to_physical(super::super::scale_factor()),
Force::Normalized(event.pressure() as f64), Force::Normalized(event.pressure() as f64),
); ),
_ => unreachable!("didn't return early before"),
} }
} }
}, },