Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow contextmenu event in draggable regions #37395

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion filenames.gni
Expand Up @@ -153,7 +153,7 @@ filenames = {
"shell/browser/notifications/mac/notification_presenter_mac.mm",
"shell/browser/relauncher_mac.cc",
"shell/browser/ui/certificate_trust_mac.mm",
"shell/browser/ui/cocoa/delayed_native_view_host.cc",
"shell/browser/ui/cocoa/delayed_native_view_host.mm",
"shell/browser/ui/cocoa/delayed_native_view_host.h",
"shell/browser/ui/cocoa/electron_bundle_mover.h",
"shell/browser/ui/cocoa/electron_bundle_mover.mm",
Expand Down
10 changes: 9 additions & 1 deletion shell/browser/api/electron_api_web_contents.h
Expand Up @@ -436,7 +436,13 @@ class WebContents : public ExclusiveAccessContext,
// content::RenderWidgetHost::InputEventObserver:
void OnInputEvent(const blink::WebInputEvent& event) override;

SkRegion* draggable_region() { return draggable_region_.get(); }
SkRegion* draggable_region() {
return force_non_draggable_ ? nullptr : draggable_region_.get();
}

void SetForceNonDraggable(bool force_non_draggable) {
force_non_draggable_ = force_non_draggable;
}

// disable copy
WebContents(const WebContents&) = delete;
Expand Down Expand Up @@ -820,6 +826,8 @@ class WebContents : public ExclusiveAccessContext,

std::unique_ptr<SkRegion> draggable_region_;

bool force_non_draggable_ = false;

base::WeakPtrFactory<WebContents> weak_factory_{this};
};

Expand Down
21 changes: 0 additions & 21 deletions shell/browser/ui/cocoa/delayed_native_view_host.cc

This file was deleted.

1 change: 1 addition & 0 deletions shell/browser/ui/cocoa/delayed_native_view_host.h
Expand Up @@ -23,6 +23,7 @@ class DelayedNativeViewHost : public views::NativeViewHost {
// views::View:
void ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) override;
bool OnMousePressed(const ui::MouseEvent& event) override;

private:
gfx::NativeView native_view_;
Expand Down
37 changes: 37 additions & 0 deletions shell/browser/ui/cocoa/delayed_native_view_host.mm
@@ -0,0 +1,37 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/browser/ui/cocoa/delayed_native_view_host.h"
#include "shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h"

namespace electron {

DelayedNativeViewHost::DelayedNativeViewHost(gfx::NativeView native_view)
: native_view_(native_view) {}

DelayedNativeViewHost::~DelayedNativeViewHost() = default;

void DelayedNativeViewHost::ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) {
NativeViewHost::ViewHierarchyChanged(details);
if (details.is_add && GetWidget())
Attach(native_view_);
}

bool DelayedNativeViewHost::OnMousePressed(const ui::MouseEvent& ui_event) {
// NativeViewHost::OnMousePressed normally isn't called, but
// NativeWidgetMacNSWindow specifically carves out an event here for
// right-mouse-button clicks. We want to forward them to the web content, so
// handle them here.
// See:
// https://source.chromium.org/chromium/chromium/src/+/main:components/remote_cocoa/app_shim/native_widget_mac_nswindow.mm;l=415-421;drc=a5af91924bafb85426e091c6035801990a6dc697
ElectronInspectableWebContentsView* inspectable_web_contents_view =
(ElectronInspectableWebContentsView*)native_view_.GetNativeNSView();
[inspectable_web_contents_view
redispatchContextMenuEvent:ui_event.native_event()];

return true;
}

} // namespace electron
Expand Up @@ -46,6 +46,8 @@ using electron::InspectableWebContentsViewMac;
(const DevToolsContentsResizingStrategy&)strategy;
- (void)setTitle:(NSString*)title;

- (void)redispatchContextMenuEvent:(NSEvent*)theEvent;

@end

#endif // ELECTRON_SHELL_BROWSER_UI_COCOA_ELECTRON_INSPECTABLE_WEB_CONTENTS_VIEW_H_
23 changes: 23 additions & 0 deletions shell/browser/ui/cocoa/electron_inspectable_web_contents_view.mm
Expand Up @@ -5,10 +5,12 @@
#include "shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h"

#include "content/public/browser/render_widget_host_view.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/ui/cocoa/event_dispatching_window.h"
#include "shell/browser/ui/inspectable_web_contents.h"
#include "shell/browser/ui/inspectable_web_contents_view_delegate.h"
#include "shell/browser/ui/inspectable_web_contents_view_mac.h"
#include "ui/base/cocoa/base_view.h"
#include "ui/gfx/mac/scoped_cocoa_disable_screen_updates.h"

@implementation ElectronInspectableWebContentsView
Expand Down Expand Up @@ -273,6 +275,27 @@ - (void)parentWindowBecameMain:(NSNotification*)notification {
[self notifyDevToolsFocused];
}

- (void)redispatchContextMenuEvent:(NSEvent*)event {
DCHECK(event.type == NSEventTypeRightMouseDown ||
(event.type == NSEventTypeLeftMouseDown &&
(event.modifierFlags & NSEventModifierFlagControl)));
content::WebContents* contents =
inspectableWebContentsView_->inspectable_web_contents()->GetWebContents();
electron::api::WebContents* api_contents =
electron::api::WebContents::From(contents);
if (api_contents) {
// Temporarily pretend that the WebContents is fully non-draggable while we
// re-send the mouse event. This allows the re-dispatched event to "land"
// on the WebContents, instead of "falling through" back to the window.
api_contents->SetForceNonDraggable(true);
BaseView* contentsView = (BaseView*)contents->GetRenderWidgetHostView()
->GetNativeView()
.GetNativeNSView();
[contentsView mouseEvent:event];
api_contents->SetForceNonDraggable(false);
}
}

#pragma mark - NSWindowDelegate

- (void)windowWillClose:(NSNotification*)notification {
Expand Down