fix: ensure context-menu emitted for draggable regions (#44799)

* fix: ensure context-menu emitted for draggable regions

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: address suggestions from review

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2024-11-22 09:28:12 -05:00 committed by GitHub
parent bbdce52b12
commit d2bafe870e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 84 additions and 21 deletions

View file

@ -7,6 +7,8 @@
#include "base/strings/sys_string_conversions.h"
#include "electron/mas.h"
#include "shell/browser/native_window_mac.h"
#include "shell/browser/ui/cocoa/delayed_native_view_host.h"
#include "shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h"
#include "shell/browser/ui/cocoa/electron_preview_item.h"
#include "shell/browser/ui/cocoa/electron_touch_bar.h"
#include "shell/browser/ui/cocoa/root_view_mac.h"
@ -189,6 +191,42 @@ void SwizzleSwipeWithEvent(NSView* view, SEL swiz_selector) {
// NSWindow overrides.
- (void)sendEvent:(NSEvent*)event {
// Draggable regions only respond to left-click dragging, but the system will
// still suppress right-clicks in a draggable region. Forwarding right-clicks
// and ctrl+left-clicks allows the underlying views to respond to right-click
// to potentially bring up a frame context menu. WebContentsView is now a
// sibling view of the NSWindow contentView, so we need to intercept the event
// here as NativeWidgetMacNSWindow won't forward it to the WebContentsView
// anymore.
if (event.type == NSEventTypeRightMouseDown ||
(event.type == NSEventTypeLeftMouseDown &&
([event modifierFlags] & NSEventModifierFlagControl))) {
// The WebContentsView is added a sibling of BaseWindow's contentView at
// index 0 before it in the paint order - see
// https://github.com/electron/electron/pull/41256.
const auto& children = shell_->GetContentsView()->children();
if (children.empty())
return;
auto* wcv = children.front().get();
if (!wcv)
return;
auto ns_view = static_cast<electron::DelayedNativeViewHost*>(wcv)
->GetNativeView()
.GetNativeNSView();
if (!ns_view)
return;
[static_cast<ElectronInspectableWebContentsView*>(ns_view)
redispatchContextMenuEvent:base::apple::OwnedNSEvent(event)];
return;
}
[super sendEvent:event];
}
- (void)rotateWithEvent:(NSEvent*)event {
shell_->NotifyWindowRotateGesture(event.rotation);
}