Web: don't block pen input (#3813)

This commit is contained in:
daxpedda 2024-07-23 15:32:20 +02:00 committed by GitHub
parent c9c260ca08
commit 5ec934b1b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 32 additions and 135 deletions

View file

@ -86,3 +86,4 @@ changelog entry.
### Fixed ### Fixed
- On MacOS, fix building with `feature = "rwh_04"`. - On MacOS, fix building with `feature = "rwh_04"`.
- On Web, pen events are now routed through to `WindowEvent::Cursor*`.

View file

@ -244,21 +244,10 @@ impl Shared {
return; return;
} }
let pointer_type = event.pointer_type();
if pointer_type != "mouse" {
return;
}
// chorded button event // chorded button event
let device_id = RootDeviceId(DeviceId(event.pointer_id())); let device_id = RootDeviceId(DeviceId(event.pointer_id()));
if let Some(button) = backend::event::mouse_button(&event) { if let Some(button) = backend::event::mouse_button(&event) {
debug_assert_eq!(
pointer_type, "mouse",
"expect pointer type of a chorded button event to be a mouse"
);
let state = if backend::event::mouse_buttons(&event).contains(button.into()) { let state = if backend::event::mouse_buttons(&event).contains(button.into()) {
ElementState::Pressed ElementState::Pressed
} else { } else {
@ -322,10 +311,6 @@ impl Shared {
return; return;
} }
if event.pointer_type() != "mouse" {
return;
}
let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())), device_id: RootDeviceId(DeviceId(event.pointer_id())),
@ -345,10 +330,6 @@ impl Shared {
return; return;
} }
if event.pointer_type() != "mouse" {
return;
}
let button = backend::event::mouse_button(&event).expect("no mouse button pressed"); let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())), device_id: RootDeviceId(DeviceId(event.pointer_id())),

View file

@ -266,21 +266,6 @@ impl ActiveEventLoop {
}); });
canvas.on_cursor_move( canvas.on_cursor_move(
{
let runner = self.runner.clone();
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);
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
})
}
}
},
{ {
let runner = self.runner.clone(); let runner = self.runner.clone();
let has_focus = has_focus.clone(); let has_focus = has_focus.clone();
@ -380,20 +365,6 @@ impl ActiveEventLoop {
); );
canvas.on_mouse_press( canvas.on_mouse_press(
{
let runner = self.runner.clone();
let modifiers = self.modifiers.clone();
move |active_modifiers| {
if modifiers.get() != active_modifiers {
modifiers.set(active_modifiers);
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
})
}
}
},
{ {
let runner = self.runner.clone(); let runner = self.runner.clone();
let modifiers = self.modifiers.clone(); let modifiers = self.modifiers.clone();
@ -458,21 +429,6 @@ impl ActiveEventLoop {
); );
canvas.on_mouse_release( canvas.on_mouse_release(
{
let runner = self.runner.clone();
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);
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
});
}
}
},
{ {
let runner = self.runner.clone(); let runner = self.runner.clone();
let has_focus = has_focus.clone(); let has_focus = has_focus.clone();

View file

@ -328,51 +328,29 @@ impl Canvas {
self.pointer_handler.on_cursor_enter(&self.common, handler) self.pointer_handler.on_cursor_enter(&self.common, handler)
} }
pub fn on_mouse_release<MOD, M, T>( pub fn on_mouse_release<M, T>(&mut self, mouse_handler: M, touch_handler: T)
&mut self, where
modifier_handler: MOD,
mouse_handler: M,
touch_handler: T,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton), M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{ {
self.pointer_handler.on_mouse_release( self.pointer_handler.on_mouse_release(&self.common, mouse_handler, touch_handler)
&self.common,
modifier_handler,
mouse_handler,
touch_handler,
)
} }
pub fn on_mouse_press<MOD, M, T>( pub fn on_mouse_press<M, T>(&mut self, mouse_handler: M, touch_handler: T)
&mut self, where
modifier_handler: MOD,
mouse_handler: M,
touch_handler: T,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton), M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{ {
self.pointer_handler.on_mouse_press( self.pointer_handler.on_mouse_press(
&self.common, &self.common,
modifier_handler,
mouse_handler, mouse_handler,
touch_handler, touch_handler,
Rc::clone(&self.prevent_default), Rc::clone(&self.prevent_default),
) )
} }
pub fn on_cursor_move<MOD, M, T, B>( pub fn on_cursor_move<M, T, B>(&mut self, mouse_handler: M, touch_handler: T, button_handler: B)
&mut self, where
modifier_handler: MOD,
mouse_handler: M,
touch_handler: T,
button_handler: B,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>), M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>), + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
@ -380,7 +358,6 @@ impl Canvas {
{ {
self.pointer_handler.on_cursor_move( self.pointer_handler.on_cursor_move(
&self.common, &self.common,
modifier_handler,
mouse_handler, mouse_handler,
touch_handler, touch_handler,
button_handler, button_handler,

View file

@ -44,7 +44,7 @@ impl PointerHandler {
// touch events are handled separately // touch events are handled separately
// handling them here would produce duplicate mouse events, inconsistent with // handling them here would produce duplicate mouse events, inconsistent with
// other platforms. // other platforms.
let pointer_id = (event.pointer_type() == "mouse").then(|| event.pointer_id()); let pointer_id = (event.pointer_type() != "touch").then(|| event.pointer_id());
handler(modifiers, pointer_id); handler(modifiers, pointer_id);
})); }));
@ -61,20 +61,18 @@ impl PointerHandler {
// touch events are handled separately // touch events are handled separately
// handling them here would produce duplicate mouse events, inconsistent with // handling them here would produce duplicate mouse events, inconsistent with
// other platforms. // other platforms.
let pointer_id = (event.pointer_type() == "mouse").then(|| event.pointer_id()); let pointer_id = (event.pointer_type() != "touch").then(|| event.pointer_id());
handler(modifiers, pointer_id); handler(modifiers, pointer_id);
})); }));
} }
pub fn on_mouse_release<MOD, M, T>( pub fn on_mouse_release<M, T>(
&mut self, &mut self,
canvas_common: &Common, canvas_common: &Common,
mut modifier_handler: MOD,
mut mouse_handler: M, mut mouse_handler: M,
mut touch_handler: T, mut touch_handler: T,
) where ) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton), M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{ {
@ -90,26 +88,23 @@ impl PointerHandler {
event::mouse_position(&event).to_physical(super::scale_factor(&window)), event::mouse_position(&event).to_physical(super::scale_factor(&window)),
Force::Normalized(event.pressure() as f64), Force::Normalized(event.pressure() as f64),
), ),
"mouse" => mouse_handler( _ => mouse_handler(
modifiers, modifiers,
event.pointer_id(), event.pointer_id(),
event::mouse_position(&event).to_physical(super::scale_factor(&window)), event::mouse_position(&event).to_physical(super::scale_factor(&window)),
event::mouse_button(&event).expect("no mouse button released"), event::mouse_button(&event).expect("no mouse button released"),
), ),
_ => modifier_handler(modifiers),
} }
})); }));
} }
pub fn on_mouse_press<MOD, M, T>( pub fn on_mouse_press<M, T>(
&mut self, &mut self,
canvas_common: &Common, canvas_common: &Common,
mut modifier_handler: MOD,
mut mouse_handler: M, mut mouse_handler: M,
mut touch_handler: T, mut touch_handler: T,
prevent_default: Rc<Cell<bool>>, prevent_default: Rc<Cell<bool>>,
) where ) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton), M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force), T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{ {
@ -125,8 +120,9 @@ impl PointerHandler {
} }
let modifiers = event::mouse_modifiers(&event); let modifiers = event::mouse_modifiers(&event);
let pointer_type = &event.pointer_type();
match event.pointer_type().as_str() { match pointer_type.as_str() {
"touch" => { "touch" => {
touch_handler( touch_handler(
modifiers, modifiers,
@ -135,7 +131,7 @@ impl PointerHandler {
Force::Normalized(event.pressure() as f64), Force::Normalized(event.pressure() as f64),
); );
}, },
"mouse" => { _ => {
mouse_handler( mouse_handler(
modifiers, modifiers,
event.pointer_id(), event.pointer_id(),
@ -143,27 +139,27 @@ impl PointerHandler {
event::mouse_button(&event).expect("no mouse button pressed"), event::mouse_button(&event).expect("no mouse button pressed"),
); );
// Error is swallowed here since the error would occur every time the mouse if pointer_type == "mouse" {
// is clicked when the cursor is grabbed, and there // Error is swallowed here since the error would occur every time the
// is probably not a situation where this could // mouse is clicked when the cursor is
// fail, that we care if it fails. // grabbed, and there is probably not a
let _e = canvas.set_pointer_capture(event.pointer_id()); // situation where this could fail, that we
// care if it fails.
let _e = canvas.set_pointer_capture(event.pointer_id());
}
}, },
_ => modifier_handler(modifiers),
} }
})); }));
} }
pub fn on_cursor_move<MOD, M, T, B>( pub fn on_cursor_move<M, T, B>(
&mut self, &mut self,
canvas_common: &Common, canvas_common: &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, mut button_handler: B,
prevent_default: Rc<Cell<bool>>, prevent_default: Rc<Cell<bool>>,
) where ) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>), M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>), + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
@ -175,23 +171,10 @@ impl PointerHandler {
Some(canvas_common.add_event("pointermove", move |event: PointerEvent| { Some(canvas_common.add_event("pointermove", move |event: PointerEvent| {
let modifiers = event::mouse_modifiers(&event); let modifiers = event::mouse_modifiers(&event);
let pointer_type = event.pointer_type();
if let "touch" | "mouse" = pointer_type.as_str() {
} else {
modifier_handler(modifiers);
return;
}
let id = event.pointer_id(); let id = event.pointer_id();
// chorded button event // chorded button event
if let Some(button) = event::mouse_button(&event) { if let Some(button) = event::mouse_button(&event) {
debug_assert_eq!(
pointer_type, "mouse",
"expect pointer type of a chorded button event to be a mouse"
);
if prevent_default.get() { if prevent_default.get() {
// prevent text selection // prevent text selection
event.prevent_default(); event.prevent_default();
@ -212,13 +195,7 @@ impl PointerHandler {
// pointer move event // pointer move event
let scale = super::scale_factor(&window); let scale = super::scale_factor(&window);
match pointer_type.as_str() { match event.pointer_type().as_str() {
"mouse" => mouse_handler(
modifiers,
id,
&mut event::pointer_move_event(event)
.map(|event| event::mouse_position(&event).to_physical(scale)),
),
"touch" => touch_handler( "touch" => touch_handler(
modifiers, modifiers,
id, id,
@ -229,7 +206,12 @@ impl PointerHandler {
) )
}), }),
), ),
_ => unreachable!("didn't return early before"), _ => mouse_handler(
modifiers,
id,
&mut event::pointer_move_event(event)
.map(|event| event::mouse_position(&event).to_physical(scale)),
),
}; };
})); }));
} }