From eac1a7ff68fd03dea343efeff09ef345d48c51df Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 27 Feb 2025 20:44:46 +0100 Subject: [PATCH] fix: context-menu event emitted in draggable regions (#45813) * fix: context-menu event emitted in draggable regions * fix: only trigger on mouse release --- ...electron_desktop_window_tree_host_linux.cc | 20 +++++++++++++++++++ .../electron_desktop_window_tree_host_linux.h | 1 + spec/api-web-contents-spec.ts | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/shell/browser/ui/electron_desktop_window_tree_host_linux.cc b/shell/browser/ui/electron_desktop_window_tree_host_linux.cc index 98546cc0acf3..7f79c7223ef8 100644 --- a/shell/browser/ui/electron_desktop_window_tree_host_linux.cc +++ b/shell/browser/ui/electron_desktop_window_tree_host_linux.cc @@ -12,6 +12,7 @@ #include "base/feature_list.h" #include "base/i18n/rtl.h" +#include "shell/browser/api/electron_api_web_contents.h" #include "shell/browser/native_window_features.h" #include "shell/browser/native_window_views.h" #include "shell/browser/ui/views/client_frame_view_linux.h" @@ -241,4 +242,23 @@ void ElectronDesktopWindowTreeHostLinux::UpdateFrameHints() { SizeConstraintsChanged(); } } + +void ElectronDesktopWindowTreeHostLinux::DispatchEvent(ui::Event* event) { + if (event->IsMouseEvent()) { + auto* mouse_event = static_cast(event); + bool is_mousedown = mouse_event->type() == ui::EventType::kMousePressed; + bool is_system_menu_trigger = + is_mousedown && + (mouse_event->IsRightMouseButton() || + (mouse_event->IsLeftMouseButton() && mouse_event->IsControlDown())); + if (is_system_menu_trigger) { + electron::api::WebContents::SetDisableDraggableRegions(true); + views::DesktopWindowTreeHostLinux::DispatchEvent(event); + electron::api::WebContents::SetDisableDraggableRegions(false); + return; + } + } + views::DesktopWindowTreeHostLinux::DispatchEvent(event); +} + } // namespace electron diff --git a/shell/browser/ui/electron_desktop_window_tree_host_linux.h b/shell/browser/ui/electron_desktop_window_tree_host_linux.h index 38d295b835e4..d1ee9a33a8b3 100644 --- a/shell/browser/ui/electron_desktop_window_tree_host_linux.h +++ b/shell/browser/ui/electron_desktop_window_tree_host_linux.h @@ -60,6 +60,7 @@ class ElectronDesktopWindowTreeHostLinux // views::DesktopWindowTreeHostLinux: void UpdateFrameHints() override; + void DispatchEvent(ui::Event* event) override; private: void UpdateWindowState(ui::PlatformWindowState new_state); diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents-spec.ts index ed6fd5ef73c2..5d7721acce3c 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -2861,18 +2861,18 @@ describe('webContents module', () => { await once(w.webContents, 'context-menu'); await setTimeout(100); - expect(contextMenuEmitCount).to.equal(1); }); - it('emits when right-clicked in page in a draggable region', async () => { + ifit(process.platform !== 'win32')('emits when right-clicked in page in a draggable region', async () => { const w = new BrowserWindow({ show: false }); await w.loadFile(path.join(fixturesPath, 'pages', 'draggable-page.html')); const promise = once(w.webContents, 'context-menu') as Promise<[any, Electron.ContextMenuParams]>; // Simulate right-click to create context-menu event. - const opts = { x: 0, y: 0, button: 'right' as const }; + const midPoint = w.getBounds().width / 2; + const opts = { x: midPoint, y: midPoint, button: 'right' as const }; w.webContents.sendInputEvent({ ...opts, type: 'mouseDown' }); w.webContents.sendInputEvent({ ...opts, type: 'mouseUp' });