Skip to content

Commit

Permalink
move enable_web_page_scroll into prevent_default option
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Sep 3, 2022
1 parent 6ae6207 commit e635978
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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`.
- 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_page_scroll` method.
- 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.
Expand Down
15 changes: 0 additions & 15 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ 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_page_scroll(self) -> Self;

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

/// Whether `event.preventDefault` should be automatically called to prevent event propagation
Expand All @@ -43,12 +34,6 @@ pub trait WindowBuilderExtWebSys {
}

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

self
}

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

Expand Down
18 changes: 8 additions & 10 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Canvas {
on_fullscreen_change: Option<EventListener>,
on_dark_mode: Option<MediaQueryListHandle>,
mouse_state: MouseState,
disable_web_scroll: Option<[EventListener; 5]>,
disable_mobile_scroll: Option<[EventListener; 3]>,
}

struct Common {
Expand Down Expand Up @@ -82,11 +82,8 @@ impl Canvas {
wants_fullscreen: Rc::new(RefCell::new(false)),
};

let disable_web_scroll = if !attr.enable_web_page_scroll && attr.prevent_default {
let disable_mobile_scroll = if attr.prevent_default {
Some([
common.add_event("pointermove", move |event: Event| {
event.prevent_default();
}),
common.add_event("pointermove", move |event: Event| {
event.prevent_default();
}),
Expand All @@ -96,9 +93,6 @@ impl Canvas {
common.add_event("touchend", move |event: Event| {
event.prevent_default();
}),
common.add_event("wheel", move |event: Event| {
event.prevent_default();
}),
])
} else {
None
Expand All @@ -114,7 +108,7 @@ impl Canvas {
on_fullscreen_change: None,
on_dark_mode: None,
mouse_state,
disable_web_scroll,
disable_mobile_scroll,
common,
})
}
Expand Down Expand Up @@ -300,11 +294,15 @@ impl Canvas {
}
}

pub fn on_mouse_wheel<F>(&mut self, mut handler: F, _prevent_default: bool)
pub fn on_mouse_wheel<F>(&mut self, mut handler: F, prevent_default: bool)
where
F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState),
{
self.on_mouse_wheel = Some(self.common.add_event("wheel", move |event: WheelEvent| {
if prevent_default {
event.prevent_default();
}

if let Some(delta) = event::mouse_scroll_delta(&event) {
handler(0, delta, event::mouse_modifiers(&event));
}
Expand Down
2 changes: 0 additions & 2 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub(crate) canvas: Option<backend::RawCanvasType>,
pub(crate) prevent_default: bool,
pub(crate) focusable: bool,
pub(crate) enable_web_page_scroll: bool,
}

impl Default for PlatformSpecificWindowBuilderAttributes {
Expand All @@ -413,7 +412,6 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
canvas: None,
prevent_default: true,
focusable: true,
enable_web_page_scroll: false,
}
}
}

0 comments on commit e635978

Please sign in to comment.