fix: context-menu event with BaseWindows (#44954)

fix: context-menu event with BaseWindows

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-12-04 16:03:24 -08:00 committed by GitHub
parent b1b01463fb
commit 3007f859da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 78 additions and 26 deletions

View file

@ -24,8 +24,6 @@ class DelayedNativeViewHost : public views::NativeViewHost {
void ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) override;
gfx::NativeView GetNativeView() { return native_view_; }
private:
gfx::NativeView native_view_;
};

View file

@ -201,26 +201,29 @@ void SwizzleSwipeWithEvent(NSView* view, SEL swiz_selector) {
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.
// We're looking for the NativeViewHost that contains the WebContentsView.
// There can be two possible NativeViewHosts - one containing the
// WebContentsView (present for BrowserWindows) and the one containing the
// VibrantView (present when vibrancy is set). We want the one containing
// the WebContentsView if it exists.
const auto& children = shell_->GetContentsView()->children();
if (children.empty())
return;
const auto it = std::ranges::find_if(children, [&](views::View* child) {
if (std::strcmp(child->GetClassName(), "NativeViewHost") == 0) {
auto* nvh = static_cast<views::NativeViewHost*>(child);
return nvh->native_view().GetNativeNSView() != [self vibrantView];
}
return false;
});
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;
if (it != children.end()) {
auto ns_view = static_cast<electron::DelayedNativeViewHost*>(*it)
->native_view()
.GetNativeNSView();
if (ns_view) {
[static_cast<ElectronInspectableWebContentsView*>(ns_view)
redispatchContextMenuEvent:base::apple::OwnedNSEvent(event)];
}
}
}
[super sendEvent:event];