From 015333e778e2d071eca8fedc99abb80c93a69e80 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 5 Nov 2020 21:39:53 +0900 Subject: [PATCH] fix: make draggable region work when devtools is open --- .../api/electron_api_browser_window_mac.mm | 20 +-------- .../electron_inspectable_web_contents_view.h | 8 ++++ .../electron_inspectable_web_contents_view.mm | 44 ++++++++++++++++++- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/shell/browser/api/electron_api_browser_window_mac.mm b/shell/browser/api/electron_api_browser_window_mac.mm index 4f043931ec7fe..d874bb3168da0 100644 --- a/shell/browser/api/electron_api_browser_window_mac.mm +++ b/shell/browser/api/electron_api_browser_window_mac.mm @@ -12,27 +12,9 @@ #include "base/mac/scoped_nsobject.h" #include "shell/browser/native_browser_view.h" #include "shell/browser/native_window_mac.h" +#include "shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h" #include "shell/browser/ui/inspectable_web_contents_view.h" -@interface NSView (WebContentsView) -- (void)setMouseDownCanMoveWindow:(BOOL)can_move; -@end - -@interface ControlRegionView : NSView -@end - -@implementation ControlRegionView - -- (BOOL)mouseDownCanMoveWindow { - return NO; -} - -- (NSView*)hitTest:(NSPoint)aPoint { - return nil; -} - -@end - namespace electron { namespace api { diff --git a/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h b/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h index 691c25f639332..ba247812bb8b1 100644 --- a/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h +++ b/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.h @@ -17,12 +17,20 @@ class InspectableWebContentsViewMac; using electron::InspectableWebContentsViewMac; +@interface NSView (WebContentsView) +- (void)setMouseDownCanMoveWindow:(BOOL)can_move; +@end + +@interface ControlRegionView : NSView +@end + @interface ElectronInspectableWebContentsView : BaseView { @private electron::InspectableWebContentsViewMac* inspectableWebContentsView_; base::scoped_nsobject fake_view_; base::scoped_nsobject devtools_window_; + base::scoped_nsobject devtools_mask_; BOOL devtools_visible_; BOOL devtools_docked_; BOOL devtools_is_first_responder_; diff --git a/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.mm b/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.mm index 0b25862211bd3..54997092d4f1e 100644 --- a/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.mm +++ b/shell/browser/ui/cocoa/electron_inspectable_web_contents_view.mm @@ -11,6 +11,18 @@ #include "shell/browser/ui/inspectable_web_contents_view_mac.h" #include "ui/gfx/mac/scoped_cocoa_disable_screen_updates.h" +@implementation ControlRegionView + +- (BOOL)mouseDownCanMoveWindow { + return NO; +} + +- (NSView*)hitTest:(NSPoint)aPoint { + return nil; +} + +@end + @implementation ElectronInspectableWebContentsView - (instancetype)initWithInspectableWebContentsViewMac: @@ -48,6 +60,9 @@ - (instancetype)initWithInspectableWebContentsViewMac: [self addSubview:contentsView]; } + // This will float above devtools to exclude it from dragging. + devtools_mask_.reset([[ControlRegionView alloc] initWithFrame:NSZeroRect]); + // See https://code.google.com/p/chromium/issues/detail?id=348490. [self setWantsLayer:YES]; @@ -84,6 +99,12 @@ - (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate { devtools_visible_ = visible; if (devtools_docked_) { if (visible) { + // The devToolsView is placed under the contentsView, so it has to be + // draggable to make draggable region of contentsView work. + [devToolsView setMouseDownCanMoveWindow:YES]; + // This view will exclude the actual devtools part from dragging. + [self addSubview:devtools_mask_.get()]; + // Place the devToolsView under contentsView, notice that we didn't set // sizes for them until the setContentsResizingStrategy message. [self addSubview:devToolsView positioned:NSWindowBelow relativeTo:nil]; @@ -94,6 +115,7 @@ - (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate { } else { gfx::ScopedCocoaDisableScreenUpdates disabler; [devToolsView removeFromSuperview]; + [devtools_mask_ removeFromSuperview]; [self adjustSubviews]; } } else { @@ -182,7 +204,7 @@ - (void)adjustSubviews { NSView* devToolsView = [[self subviews] objectAtIndex:0]; NSView* contentsView = [[self subviews] objectAtIndex:1]; - DCHECK_EQ(2u, [[self subviews] count]); + DCHECK_EQ(3u, [[self subviews] count]); gfx::Rect new_devtools_bounds; gfx::Rect new_contents_bounds; @@ -191,6 +213,26 @@ - (void)adjustSubviews { &new_devtools_bounds, &new_contents_bounds); [devToolsView setFrame:[self flipRectToNSRect:new_devtools_bounds]]; [contentsView setFrame:[self flipRectToNSRect:new_contents_bounds]]; + + // Move mask to the devtools area to exclude it from dragging. + NSRect cf = contentsView.frame; + NSRect sb = [self bounds]; + NSRect devtools_frame; + if (cf.size.height < sb.size.height) { // bottom docked + devtools_frame.origin.x = 0; + devtools_frame.origin.y = 0; + devtools_frame.size.width = sb.size.width; + devtools_frame.size.height = sb.size.height - cf.size.height; + } else { // left or right docked + if (cf.origin.x > 0) // left docked + devtools_frame.origin.x = 0; + else // right docked. + devtools_frame.origin.x = cf.size.width; + devtools_frame.origin.y = 0; + devtools_frame.size.width = sb.size.width - cf.size.width; + devtools_frame.size.height = sb.size.height; + } + [devtools_mask_ setFrame:devtools_frame]; } - (void)setTitle:(NSString*)title {