From 0fca8b088d4c7fc3feb83567d284f169afc2f15c Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Sun, 4 Sep 2022 13:45:30 +1000 Subject: [PATCH] WindowBuilderExtWebSys::with_prevent_default disables scrolling on both mobile and desktop (previously just desktop) (#2216) * Disable scrolling on web by default but provide method in builder to enable it * rename enable_web_scroll -> enable_web_page_scroll * move enable_web_page_scroll into prevent_default option * final approach * Mark prevent_default change as breaking Co-authored-by: Mads Marquart --- CHANGELOG.md | 1 + .../web/event_loop/window_target.rs | 36 +++++++++++-------- src/platform_impl/web/web_sys/canvas.rs | 26 ++++++++++++-- .../web/web_sys/canvas/pointer_handler.rs | 11 ++++-- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf982763..0e648fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ And please only add new entries to the top of this list, right below the `# Unre - On Windows, added `WindowExtWindows::set_undecorated_shadow` and `WindowBuilderExtWindows::with_undecorated_shadow` to draw the drop shadow behind a borderless window. - On Windows, fixed default window features (ie snap, animations, shake, etc.) when decorations are disabled. - **Breaking:** On macOS, add support for two-finger touchpad magnification and rotation gestures with new events `WindowEvent::TouchpadMagnify` and `WindowEvent::TouchpadRotate`. +- **Breaking:** On web, the `WindowBuilderExtWebSys::with_prevent_default` setting (enabled by default), now additionally prevents scrolling of the webpage in mobile browsers, previously it only disabled scrolling on desktop. - On Wayland, `wayland-csd-adwaita` now uses `ab_glyph` instead of `crossfont` to render the title for decorations. - On Wayland, a new `wayland-csd-adwaita-crossfont` feature was added to use `crossfont` instead of `ab_glyph` for decorations. - On Wayland, if not otherwise specified use upstream automatic CSD theme selection. diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 6b15e3dd..c9863bf9 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -63,6 +63,9 @@ impl EventLoopWindowTarget { let mut canvas = canvas.borrow_mut(); canvas.set_attribute("data-raw-handle", &id.0.to_string()); + canvas.on_touch_start(prevent_default); + canvas.on_touch_end(prevent_default); + let runner = self.runner.clone(); canvas.on_blur(move || { runner.send_event(Event::WindowEvent { @@ -153,22 +156,25 @@ impl EventLoopWindowTarget { }); let runner = self.runner.clone(); - canvas.on_cursor_move(move |pointer_id, position, delta, modifiers| { - runner.send_event(Event::WindowEvent { - window_id: RootWindowId(id), - event: WindowEvent::CursorMoved { + canvas.on_cursor_move( + move |pointer_id, position, delta, modifiers| { + runner.send_event(Event::WindowEvent { + window_id: RootWindowId(id), + event: WindowEvent::CursorMoved { + device_id: RootDeviceId(DeviceId(pointer_id)), + position, + modifiers, + }, + }); + runner.send_event(Event::DeviceEvent { device_id: RootDeviceId(DeviceId(pointer_id)), - position, - modifiers, - }, - }); - runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(DeviceId(pointer_id)), - event: DeviceEvent::MouseMotion { - delta: (delta.x, delta.y), - }, - }); - }); + event: DeviceEvent::MouseMotion { + delta: (delta.x, delta.y), + }, + }); + }, + prevent_default, + ); let runner = self.runner.clone(); canvas.on_mouse_press(move |pointer_id, position, button, modifiers| { diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index ebae6722..2e875874 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -21,6 +21,8 @@ mod pointer_handler; #[allow(dead_code)] pub struct Canvas { common: Common, + on_touch_start: Option>, + on_touch_end: Option>, on_focus: Option>, on_blur: Option>, on_keyboard_release: Option>, @@ -79,6 +81,8 @@ impl Canvas { raw: canvas, wants_fullscreen: Rc::new(RefCell::new(false)), }, + on_touch_start: None, + on_touch_end: None, on_blur: None, on_focus: None, on_keyboard_release: None, @@ -132,6 +136,22 @@ impl Canvas { &self.common.raw } + pub fn on_touch_start(&mut self, prevent_default: bool) { + self.on_touch_start = Some(self.common.add_event("touchstart", move |event: Event| { + if prevent_default { + event.prevent_default(); + } + })); + } + + pub fn on_touch_end(&mut self, prevent_default: bool) { + self.on_touch_end = Some(self.common.add_event("touchend", move |event: Event| { + if prevent_default { + event.prevent_default(); + } + })); + } + pub fn on_blur(&mut self, mut handler: F) where F: 'static + FnMut(), @@ -262,12 +282,14 @@ impl Canvas { } } - pub fn on_cursor_move(&mut self, handler: F) + pub fn on_cursor_move(&mut self, handler: F, prevent_default: bool) where F: 'static + FnMut(i32, PhysicalPosition, PhysicalPosition, ModifiersState), { match &mut self.mouse_state { - MouseState::HasPointerEvent(h) => h.on_cursor_move(&self.common, handler), + MouseState::HasPointerEvent(h) => { + h.on_cursor_move(&self.common, handler, prevent_default) + } MouseState::NoPointerEvent(h) => h.on_cursor_move(&self.common, handler), } } 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 85a99eb8..e907f54e 100644 --- a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs +++ b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs @@ -88,13 +88,20 @@ impl PointerHandler { )); } - pub fn on_cursor_move(&mut self, canvas_common: &super::Common, mut handler: F) - where + pub fn on_cursor_move( + &mut self, + canvas_common: &super::Common, + mut handler: F, + prevent_default: bool, + ) where F: 'static + FnMut(i32, PhysicalPosition, PhysicalPosition, ModifiersState), { self.on_cursor_move = Some(canvas_common.add_event( "pointermove", move |event: PointerEvent| { + if prevent_default { + event.prevent_default(); + } handler( event.pointer_id(), event::mouse_position(&event).to_physical(super::super::scale_factor()),