From 82d0380ea6999b42d73487c1c94b7be169b1fdd5 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Fri, 2 Jun 2023 13:16:04 +0200 Subject: [PATCH] Ignore pen input on Web --- CHANGELOG.md | 1 + .../web/web_sys/canvas/pointer_handler.rs | 70 +++++++++++-------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ec503f4..becb07ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ And please only add new entries to the top of this list, right below the `# Unre - On Web, handle coalesced pointer events, which increases the resolution of pointer inputs. - **Breaking:** On Web, `instant` is now replaced by `web_time`. - On Windows, port to `windows-sys` version 0.48.0. +- On Web, fix pen treated as mouse input. # 0.28.6 diff --git a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs index 086875e9..80713ce9 100644 --- a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs +++ b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs @@ -41,7 +41,7 @@ impl PointerHandler { // touch events are handled separately // handling them here would produce duplicate mouse events, inconsistent with // other platforms. - if event.pointer_type() == "touch" { + if event.pointer_type() != "mouse" { return; } @@ -60,7 +60,7 @@ impl PointerHandler { // touch events are handled separately // handling them here would produce duplicate mouse events, inconsistent with // other platforms. - if event.pointer_type() == "touch" { + if event.pointer_type() != "mouse" { return; } @@ -82,19 +82,19 @@ impl PointerHandler { self.on_pointer_release = Some(canvas_common.add_user_event( "pointerup", move |event: PointerEvent| { - if event.pointer_type() == "touch" { - touch_handler( + match event.pointer_type().as_str() { + "touch" => touch_handler( event.pointer_id(), event::touch_position(&event, &canvas) .to_physical(super::super::scale_factor()), Force::Normalized(event.pressure() as f64), - ); - } else { - mouse_handler( + ), + "mouse" => mouse_handler( event.pointer_id(), event::mouse_button(&event).expect("no mouse button released"), event::mouse_modifiers(&event), - ); + ), + _ => (), } }, )); @@ -113,25 +113,29 @@ impl PointerHandler { self.on_pointer_press = Some(canvas_common.add_user_event( "pointerdown", move |event: PointerEvent| { - if event.pointer_type() == "touch" { - touch_handler( - event.pointer_id(), - event::touch_position(&event, &canvas) - .to_physical(super::super::scale_factor()), - Force::Normalized(event.pressure() as f64), - ); - } else { - mouse_handler( - event.pointer_id(), - event::mouse_position(&event).to_physical(super::super::scale_factor()), - event::mouse_button(&event).expect("no mouse button pressed"), - event::mouse_modifiers(&event), - ); + match event.pointer_type().as_str() { + "touch" => { + touch_handler( + event.pointer_id(), + event::touch_position(&event, &canvas) + .to_physical(super::super::scale_factor()), + Force::Normalized(event.pressure() as f64), + ); + } + "mouse" => { + mouse_handler( + event.pointer_id(), + event::mouse_position(&event).to_physical(super::super::scale_factor()), + event::mouse_button(&event).expect("no mouse button pressed"), + event::mouse_modifiers(&event), + ); - // 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()); + // 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()); + } + _ => (), } }, )); @@ -169,16 +173,22 @@ impl PointerHandler { fn has_get_coalesced_events(this: &PointerEventExt) -> JsValue; } - if event.pointer_type() == "touch" && prevent_default { - // prevent scroll on mobile web - event.prevent_default(); + match event.pointer_type().as_str() { + "touch" => { + if prevent_default { + // prevent scroll on mobile web + event.prevent_default(); + } + } + "mouse" => (), + _ => return, } let event: PointerEventExt = event.unchecked_into(); let id = event.pointer_id(); // cache buttons if the pointer is a mouse - let mouse = (event.pointer_type() != "touch").then(|| { + let mouse = (event.pointer_type() == "mouse").then(|| { ( event::mouse_modifiers(&event), event::mouse_buttons(&event),