Skip to content

Commit

Permalink
On Web, fix context menu not being disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored and kchibisov committed Dec 24, 2023
1 parent 380dc4c commit b3f8404
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Unreleased` header.

# Unreleased

- On Web, fix context menu not being disabled by `with_prevent_default(true)`.

# 0.29.5

- On macOS, remove spurious error logging when handling `Fn`.
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ impl<T> EventLoopWindowTarget<T> {
canvas.on_animation_frame(move || runner.request_redraw(RootWindowId(id)));

canvas.on_touch_end();

canvas.on_context_menu(prevent_default);
}

pub fn available_monitors(&self) -> VecDequeIter<MonitorHandle> {
Expand Down
17 changes: 16 additions & 1 deletion src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::sync::{Arc, Mutex};
use smol_str::SmolStr;
use wasm_bindgen::{closure::Closure, JsCast};
use web_sys::{
CssStyleDeclaration, Document, Event, FocusEvent, HtmlCanvasElement, KeyboardEvent, WheelEvent,
CssStyleDeclaration, Document, Event, FocusEvent, HtmlCanvasElement, KeyboardEvent,
PointerEvent, WheelEvent,
};

use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct Canvas {
on_intersect: Option<IntersectionObserverHandle>,
animation_frame_handler: AnimationFrameHandler,
on_touch_end: Option<EventListenerHandle<dyn FnMut(Event)>>,
on_context_menu: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
}

pub struct Common {
Expand Down Expand Up @@ -152,6 +154,7 @@ impl Canvas {
on_intersect: None,
animation_frame_handler: AnimationFrameHandler::new(window),
on_touch_end: None,
on_context_menu: None,
})
}

Expand Down Expand Up @@ -446,6 +449,17 @@ impl Canvas {
self.on_touch_end = Some(self.common.add_transient_event("touchend", |_| {}));
}

pub(crate) fn on_context_menu(&mut self, prevent_default: bool) {
self.on_context_menu = Some(self.common.add_event(
"contextmenu",
move |event: PointerEvent| {
if prevent_default {
event.prevent_default();
}
},
));
}

pub fn request_fullscreen(&self) {
self.common.fullscreen_handler.request_fullscreen()
}
Expand Down Expand Up @@ -524,6 +538,7 @@ impl Canvas {
self.animation_frame_handler.cancel();
self.on_touch_end = None;
self.common.fullscreen_handler.cancel();
self.on_context_menu = None;
}
}

Expand Down

0 comments on commit b3f8404

Please sign in to comment.