From 53633fcaeb9c9d14177540b7eaf772e9df9941ee Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 14 Apr 2025 11:24:01 +0200 Subject: [PATCH] fix: fallback to old MacOS context menu behavior if no frame is present (#46619) * fix: fallback to old MacOS context menu behavior if no frame is present Co-authored-by: georgexu99 Co-authored-by: erickzhao Co-authored-by: clavin Co-authored-by: Keeley Hammond * docs: add additional option for focusedFrame Co-authored-by: Keeley Hammond * fix: handle frame found, but no view update from rfh Co-authored-by: Keeley Hammond * fix: fix conditional Co-authored-by: Calvin Co-authored-by: Keeley Hammond --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Keeley Hammond Co-authored-by: Keeley Hammond --- docs/api/menu.md | 2 +- shell/browser/api/electron_api_menu_mac.mm | 37 ++++++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/docs/api/menu.md b/docs/api/menu.md index 949720dc0e8c..fef600f68dd6 100644 --- a/docs/api/menu.md +++ b/docs/api/menu.md @@ -74,7 +74,7 @@ The `menu` object has the following instance methods: * `options` Object (optional) * `window` [BaseWindow](base-window.md) (optional) - Default is the focused window. * `frame` [WebFrameMain](web-frame-main.md) (optional) - Provide the relevant frame - if you want certain OS-level features such as Writing Tools on macOS to function correctly. Typically, this should be `params.frame` from the [`context-menu` event](web-contents.md#event-context-menu) on a WebContents. + if you want certain OS-level features such as Writing Tools on macOS to function correctly. Typically, this should be `params.frame` from the [`context-menu` event](web-contents.md#event-context-menu) on a WebContents, or the [`focusedFrame` property](web-contents.md#contentsfocusedframe-readonly) of a WebContents. * `x` number (optional) - Default is the current mouse cursor position. Must be declared if `y` is declared. * `y` number (optional) - Default is the current mouse cursor position. diff --git a/shell/browser/api/electron_api_menu_mac.mm b/shell/browser/api/electron_api_menu_mac.mm index f5dffd4492e2..76a34b1a6a7f 100644 --- a/shell/browser/api/electron_api_menu_mac.mm +++ b/shell/browser/api/electron_api_menu_mac.mm @@ -164,19 +164,36 @@ void MenuMac::PopupOnUI(const base::WeakPtr& native_window, static_cast(rfh->GetView()); RenderWidgetHostViewCocoa* cocoa_view = rwhvm->GetInProcessNSView(); view = cocoa_view; + + // TODO: ui::ShowContextMenu does not dispatch the event correctly + // if no frame is found. Fix this to remove if/else condition. + NSEvent* dummy_event = + [NSEvent mouseEventWithType:NSEventTypeRightMouseDown + location:position + modifierFlags:0 + timestamp:0 + windowNumber:nswindow.windowNumber + context:nil + eventNumber:0 + clickCount:1 + pressure:0]; + ui::ShowContextMenu(menu, dummy_event, view, true); + return; } } - NSEvent* dummy_event = [NSEvent mouseEventWithType:NSEventTypeRightMouseDown - location:position - modifierFlags:0 - timestamp:0 - windowNumber:nswindow.windowNumber - context:nil - eventNumber:0 - clickCount:1 - pressure:0]; - ui::ShowContextMenu(menu, dummy_event, view, true); + // Make sure events can be pumped while the menu is up. + base::CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow; + + // One of the events that could be pumped is |window.close()|. + // User-initiated event-tracking loops protect against this by + // setting flags in -[CrApplication sendEvent:], but since + // web-content menus are initiated by IPC message the setup has to + // be done manually. + base::mac::ScopedSendingEvent sendingEventScoper; + + // Don't emit unresponsive event when showing menu. + [menu popUpMenuPositioningItem:item atLocation:position inView:view]; } void MenuMac::ClosePopupAt(int32_t window_id) {