diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ca139c0..02f7e559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Unreleased` header. - On Windows, fix consecutive calls to `window.set_fullscreen(Some(Fullscreen::Borderless(None)))` resulting in losing previous window state when eventually exiting fullscreen using `window.set_fullscreen(None)`. - On Wayland, fix resize being sent on focus change. - On Windows, fix `set_ime_cursor_area`. +- On Web, fix context menu not being disabled by `with_prevent_default(true)`. # 0.29.4 diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 16cd5093..11da79fc 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -647,6 +647,8 @@ impl EventLoopWindowTarget { let runner = self.runner.clone(); canvas.on_animation_frame(move || runner.request_redraw(RootWindowId(id))); + + canvas.on_context_menu(prevent_default); } pub fn available_monitors(&self) -> VecDequeIter { diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index f2cffd63..5cece388 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -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}; @@ -41,6 +42,7 @@ pub struct Canvas { on_intersect: Option, animation_frame_handler: AnimationFrameHandler, on_touch_end: Option>, + on_context_menu: Option>, } pub struct Common { @@ -150,6 +152,7 @@ impl Canvas { on_intersect: None, animation_frame_handler: AnimationFrameHandler::new(window), on_touch_end: None, + on_context_menu: None, }) } @@ -438,6 +441,17 @@ impl Canvas { self.animation_frame_handler.on_animation_frame(f) } + 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) { fullscreen::request_fullscreen(self.document(), self.raw()); } @@ -511,6 +525,7 @@ impl Canvas { self.on_intersect = None; self.animation_frame_handler.cancel(); self.on_touch_end = None; + self.on_context_menu = None; } }