From 964e342f69223a955269f7797381deff7b4d6cd7 Mon Sep 17 00:00:00 2001 From: dAxpeDDa Date: Sun, 4 Jun 2023 01:50:30 +0200 Subject: [PATCH] Prevent text selection --- CHANGELOG.md | 1 + .../web/event_loop/window_target.rs | 35 ++++++------------- src/platform_impl/web/web_sys/canvas.rs | 16 ++++++--- src/platform_impl/web/web_sys/pointer.rs | 15 ++++++++ 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b58731d..49502a63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ And please only add new entries to the top of this list, right below the `# Unre - On Web, send mouse position on button release as well. - On Web, fix touch input not gaining or loosing focus. - **Breaking:** On Web, dropped support for Safari versions below 13. +- On Web, prevent clicks on the canvas to select text. # 0.28.6 diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index da35477c..ef2e6478 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -349,14 +349,8 @@ impl EventLoopWindowTarget { { let runner = self.runner.clone(); let modifiers = self.modifiers.clone(); - let has_focus = has_focus.clone(); move |pointer_id, position, button, active_modifiers| { - let focus_changed = (!has_focus.replace(true)).then_some(Event::WindowEvent { - window_id: RootWindowId(id), - event: WindowEvent::Focused(true), - }); - let modifiers_changed = (modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { @@ -368,7 +362,7 @@ impl EventLoopWindowTarget { // A mouse down 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(focus_changed.into_iter().chain(modifiers_changed).chain([ + runner.send_events(modifiers_changed.into_iter().chain([ Event::WindowEvent { window_id: RootWindowId(id), event: WindowEvent::CursorMoved { @@ -389,28 +383,21 @@ impl EventLoopWindowTarget { }, { let runner = self.runner.clone(); - let has_focus = has_focus.clone(); move |device_id, location, force| { - let focus_changed = (!has_focus.replace(true)).then_some(Event::WindowEvent { + runner.send_event(Event::WindowEvent { window_id: RootWindowId(id), - event: WindowEvent::Focused(true), - }); - - runner.send_events(focus_changed.into_iter().chain(iter::once( - Event::WindowEvent { - window_id: RootWindowId(id), - event: WindowEvent::Touch(Touch { - id: device_id as u64, - device_id: RootDeviceId(DeviceId(device_id)), - phase: TouchPhase::Started, - force: Some(force), - location, - }), - }, - ))); + event: WindowEvent::Touch(Touch { + id: device_id as u64, + device_id: RootDeviceId(DeviceId(device_id)), + phase: TouchPhase::Started, + force: Some(force), + location, + }), + }) } }, + prevent_default, ); canvas.on_mouse_release( diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index 0e8d4b97..8ba5c24e 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -235,13 +235,21 @@ impl Canvas { .on_mouse_release(&self.common, mouse_handler, touch_handler) } - pub fn on_mouse_press(&mut self, mouse_handler: M, touch_handler: T) - where + pub fn on_mouse_press( + &mut self, + mouse_handler: M, + touch_handler: T, + prevent_default: bool, + ) where M: 'static + FnMut(i32, PhysicalPosition, MouseButton, ModifiersState), T: 'static + FnMut(i32, PhysicalPosition, Force), { - self.pointer_handler - .on_mouse_press(&self.common, mouse_handler, touch_handler) + self.pointer_handler.on_mouse_press( + &self.common, + mouse_handler, + touch_handler, + prevent_default, + ) } pub fn on_cursor_move( diff --git a/src/platform_impl/web/web_sys/pointer.rs b/src/platform_impl/web/web_sys/pointer.rs index ff1aadb7..bb0c786f 100644 --- a/src/platform_impl/web/web_sys/pointer.rs +++ b/src/platform_impl/web/web_sys/pointer.rs @@ -104,6 +104,7 @@ impl PointerHandler { canvas_common: &Common, mut mouse_handler: M, mut touch_handler: T, + prevent_default: bool, ) where M: 'static + FnMut(i32, PhysicalPosition, MouseButton, ModifiersState), T: 'static + FnMut(i32, PhysicalPosition, Force), @@ -112,6 +113,13 @@ impl PointerHandler { self.on_pointer_press = Some(canvas_common.add_user_event( "pointerdown", move |event: PointerEvent| { + if prevent_default { + // prevent text selection + event.prevent_default(); + // but still focus element + let _ = canvas.focus(); + } + match event.pointer_type().as_str() { "touch" => { touch_handler( @@ -192,6 +200,13 @@ impl PointerHandler { "expect pointer type of a chorded button event to be a mouse" ); + if prevent_default { + // prevent text selection + event.prevent_default(); + // but still focus element + let _ = canvas.focus(); + } + button_handler( id, event::mouse_position(&event).to_physical(super::scale_factor()),