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 <georgexu99@electronjs.org>
Co-authored-by: erickzhao <erickzhao@electronjs.org>
Co-authored-by: clavin <calvin@electronjs.org>

Co-authored-by: Keeley Hammond <khammond@slack-corp.com>

* docs: add additional option for focusedFrame

Co-authored-by: Keeley Hammond <khammond@slack-corp.com>

* fix: handle frame found, but no view update from rfh

Co-authored-by: Keeley Hammond <khammond@slack-corp.com>

* fix: fix conditional

Co-authored-by: Calvin <clavin@users.noreply.github.com>

Co-authored-by: Keeley Hammond <vertedinde@electronjs.org>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: Keeley Hammond <vertedinde@electronjs.org>
This commit is contained in:
trop[bot] 2025-04-14 11:24:01 +02:00 committed by GitHub
parent 285d347b51
commit 53633fcaeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 11 deletions

View file

@ -164,19 +164,36 @@ void MenuMac::PopupOnUI(const base::WeakPtr<NativeWindow>& native_window,
static_cast<content::RenderWidgetHostViewMac*>(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) {