Skip to content

Commit

Permalink
Disable scrolling on web by default but provide method in builder to …
Browse files Browse the repository at this point in the history
…enable it
  • Loading branch information
rukai committed Mar 5, 2022
1 parent b7e7755 commit bc5680f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- Added `Window::is_resizable`.
- Added `Window::is_decorated`.
- On X11, fix for repeated event loop iteration when `ControlFlow` was `Wait`
- On web, by default, when the canvas is focused, scrolling of the webpage is disabled. It can however be enabled via the `WindowBuilderExtWebSys::enable_web_scroll` method.
- On Wayland, report unaccelerated mouse deltas in `DeviceEvent::MouseMotion`.
- **Breaking:** Bump `ndk` version to 0.6, ndk-sys to `v0.3`, `ndk-glue` to `0.6`.
- Remove no longer needed `WINIT_LINK_COLORSYNC` environment variable.
Expand Down
15 changes: 15 additions & 0 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,25 @@ pub trait WindowExtWebSys {
}

pub trait WindowBuilderExtWebSys {
/// Enable scrolling of the web page the canvas is in when the canvas is focused.
///
/// Scrolling is disabled by default because the scroll input on many mobile devices
/// is the same as click and dragging which is a very common input method for many applications.
///
/// So only call this method if you know that you will never need to handle mouse wheel inputs
/// or click and dragging.
fn enable_web_scroll(self) -> Self;

fn with_canvas(self, canvas: Option<HtmlCanvasElement>) -> Self;
}

impl WindowBuilderExtWebSys for WindowBuilder {
fn enable_web_scroll(mut self) -> Self {
self.platform_specific.enable_web_scroll = true;

self
}

fn with_canvas(mut self, canvas: Option<HtmlCanvasElement>) -> Self {
self.platform_specific.canvas = canvas;

Expand Down
35 changes: 30 additions & 5 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Canvas {
on_fullscreen_change: Option<EventListenerHandle<dyn FnMut(Event)>>,
on_dark_mode: Option<MediaQueryListHandle>,
mouse_state: MouseState,
disable_web_scroll: Option<[EventListenerHandle<dyn FnMut(Event)>; 5]>,
}

struct Common {
Expand Down Expand Up @@ -72,11 +73,34 @@ impl Canvas {
MouseState::NoPointerEvent(mouse_handler::MouseHandler::new())
};

let common = Common {
raw: canvas,
wants_fullscreen: Rc::new(RefCell::new(false)),
};

let disable_web_scroll = if !attr.enable_web_scroll {
Some([
common.add_event("pointermove", move |event: Event| {
event.prevent_default();
}),
common.add_event("pointermove", move |event: Event| {
event.prevent_default();
}),
common.add_event("touchstart", move |event: Event| {
event.prevent_default();
}),
common.add_event("touchend", move |event: Event| {
event.prevent_default();
}),
common.add_event("wheel", move |event: Event| {
event.prevent_default();
}),
])
} else {
None
};

Ok(Canvas {
common: Common {
raw: canvas,
wants_fullscreen: Rc::new(RefCell::new(false)),
},
on_blur: None,
on_focus: None,
on_keyboard_release: None,
Expand All @@ -86,6 +110,8 @@ impl Canvas {
on_fullscreen_change: None,
on_dark_mode: None,
mouse_state,
disable_web_scroll,
common,
})
}

Expand Down Expand Up @@ -266,7 +292,6 @@ impl Canvas {
F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState),
{
self.on_mouse_wheel = Some(self.common.add_event("wheel", move |event: WheelEvent| {
event.prevent_default();
if let Some(delta) = event::mouse_scroll_delta(&event) {
handler(0, delta, event::mouse_modifiers(&event));
}
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,5 @@ impl Id {
#[derive(Default, Clone)]
pub struct PlatformSpecificBuilderAttributes {
pub(crate) canvas: Option<backend::RawCanvasType>,
pub(crate) enable_web_scroll: bool,
}

0 comments on commit bc5680f

Please sign in to comment.