Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WindowBuilderExtWebSys::with_prevent_default disables scrolling on both mobile and desktop (previously just desktop) #2216

Merged
merged 5 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 21 additions & 15 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ impl<T> EventLoopWindowTarget<T> {
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 {
Expand Down Expand Up @@ -153,22 +156,25 @@ impl<T> EventLoopWindowTarget<T> {
});

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| {
Expand Down
26 changes: 24 additions & 2 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ mod pointer_handler;
#[allow(dead_code)]
pub struct Canvas {
common: Common,
on_touch_start: Option<EventListenerHandle<dyn FnMut(Event)>>,
on_touch_end: Option<EventListenerHandle<dyn FnMut(Event)>>,
on_focus: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
on_blur: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
on_keyboard_release: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<F>(&mut self, mut handler: F)
where
F: 'static + FnMut(),
Expand Down Expand Up @@ -262,12 +282,14 @@ impl Canvas {
}
}

pub fn on_cursor_move<F>(&mut self, handler: F)
pub fn on_cursor_move<F>(&mut self, handler: F, prevent_default: bool)
where
F: 'static + FnMut(i32, PhysicalPosition<f64>, PhysicalPosition<f64>, 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),
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/platform_impl/web/web_sys/canvas/pointer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,20 @@ impl PointerHandler {
));
}

pub fn on_cursor_move<F>(&mut self, canvas_common: &super::Common, mut handler: F)
where
pub fn on_cursor_move<F>(
&mut self,
canvas_common: &super::Common,
mut handler: F,
prevent_default: bool,
) where
F: 'static + FnMut(i32, PhysicalPosition<f64>, PhysicalPosition<f64>, 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()),
Expand Down