Skip to content

Commit

Permalink
Web: don't block pen input (#3813)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored and kchibisov committed Aug 8, 2024
1 parent 4b1aa51 commit 9ca8c09
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 135 deletions.
1 change: 1 addition & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ changelog entry.
### Fixed

- On MacOS, fix building with `feature = "rwh_04"`.
- On Web, pen events are now routed through to `WindowEvent::Cursor*`.
19 changes: 0 additions & 19 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,10 @@ impl Shared {
return;
}

let pointer_type = event.pointer_type();

if pointer_type != "mouse" {
return;
}

// chorded button event
let device_id = RootDeviceId(DeviceId(event.pointer_id()));

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()) {
ElementState::Pressed
} else {
Expand Down Expand Up @@ -323,10 +312,6 @@ impl Shared {
return;
}

if event.pointer_type() != "mouse" {
return;
}

let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())),
Expand All @@ -346,10 +331,6 @@ impl Shared {
return;
}

if event.pointer_type() != "mouse" {
return;
}

let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())),
Expand Down
44 changes: 0 additions & 44 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,6 @@ impl ActiveEventLoop {
});

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 has_focus = has_focus.clone();
Expand Down Expand Up @@ -372,20 +357,6 @@ impl ActiveEventLoop {
);

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 modifiers = self.modifiers.clone();
Expand Down Expand Up @@ -450,21 +421,6 @@ impl ActiveEventLoop {
);

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 has_focus = has_focus.clone();
Expand Down
37 changes: 7 additions & 30 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,59 +329,36 @@ impl Canvas {
self.pointer_handler.on_cursor_enter(&self.common, handler)
}

pub fn on_mouse_release<MOD, M, T>(
&mut self,
modifier_handler: MOD,
mouse_handler: M,
touch_handler: T,
) where
MOD: 'static + FnMut(ModifiersState),
pub fn on_mouse_release<M, T>(&mut self, mouse_handler: M, touch_handler: T)
where
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{
self.pointer_handler.on_mouse_release(
&self.common,
modifier_handler,
mouse_handler,
touch_handler,
)
self.pointer_handler.on_mouse_release(&self.common, mouse_handler, touch_handler)
}

pub fn on_mouse_press<MOD, M, T>(
&mut self,
modifier_handler: MOD,
mouse_handler: M,
touch_handler: T,
) where
MOD: 'static + FnMut(ModifiersState),
pub fn on_mouse_press<M, T>(&mut self, mouse_handler: M, touch_handler: T)
where
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{
self.pointer_handler.on_mouse_press(
&self.common,
modifier_handler,
mouse_handler,
touch_handler,
Rc::clone(&self.prevent_default),
)
}

pub fn on_cursor_move<MOD, M, T, B>(
&mut self,
modifier_handler: MOD,
mouse_handler: M,
touch_handler: T,
button_handler: B,
) where
MOD: 'static + FnMut(ModifiersState),
pub fn on_cursor_move<M, T, B>(&mut self, mouse_handler: M, touch_handler: T, button_handler: B)
where
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
{
self.pointer_handler.on_cursor_move(
&self.common,
modifier_handler,
mouse_handler,
touch_handler,
button_handler,
Expand Down
66 changes: 24 additions & 42 deletions src/platform_impl/web/web_sys/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl PointerHandler {
// touch events are handled separately
// handling them here would produce duplicate mouse events, inconsistent with
// 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);
}));
Expand All @@ -61,20 +61,18 @@ impl PointerHandler {
// touch events are handled separately
// handling them here would produce duplicate mouse events, inconsistent with
// 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);
}));
}

pub fn on_mouse_release<MOD, M, T>(
pub fn on_mouse_release<M, T>(
&mut self,
canvas_common: &Common,
mut modifier_handler: MOD,
mut mouse_handler: M,
mut touch_handler: T,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{
Expand All @@ -90,26 +88,23 @@ impl PointerHandler {
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
Force::Normalized(event.pressure() as f64),
),
"mouse" => mouse_handler(
_ => mouse_handler(
modifiers,
event.pointer_id(),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
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,
canvas_common: &Common,
mut modifier_handler: MOD,
mut mouse_handler: M,
mut touch_handler: T,
prevent_default: Rc<Cell<bool>>,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
{
Expand All @@ -125,8 +120,9 @@ impl PointerHandler {
}

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_handler(
modifiers,
Expand All @@ -135,35 +131,35 @@ impl PointerHandler {
Force::Normalized(event.pressure() as f64),
);
},
"mouse" => {
_ => {
mouse_handler(
modifiers,
event.pointer_id(),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
event::mouse_button(&event).expect("no mouse button pressed"),
);

// Error is swallowed here since the error would occur every time the mouse
// is clicked when the cursor is grabbed, and there
// is probably not a situation where this could
// fail, that we care if it fails.
let _e = canvas.set_pointer_capture(event.pointer_id());
if pointer_type == "mouse" {
// Error is swallowed here since the error would occur every time the
// mouse is clicked when the cursor is
// grabbed, and there is probably not a
// 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,
canvas_common: &Common,
mut modifier_handler: MOD,
mut mouse_handler: M,
mut touch_handler: T,
mut button_handler: B,
prevent_default: Rc<Cell<bool>>,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
Expand All @@ -175,23 +171,10 @@ impl PointerHandler {
Some(canvas_common.add_event("pointermove", move |event: PointerEvent| {
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();

// chorded 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() {
// prevent text selection
event.prevent_default();
Expand All @@ -212,13 +195,7 @@ impl PointerHandler {

// pointer move event
let scale = super::scale_factor(&window);
match pointer_type.as_str() {
"mouse" => mouse_handler(
modifiers,
id,
&mut event::pointer_move_event(event)
.map(|event| event::mouse_position(&event).to_physical(scale)),
),
match event.pointer_type().as_str() {
"touch" => touch_handler(
modifiers,
id,
Expand All @@ -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)),
),
};
}));
}
Expand Down

0 comments on commit 9ca8c09

Please sign in to comment.