Skip to content

Commit

Permalink
fix: make draggable region work when devtools is open
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz authored and electron-bot committed Nov 9, 2020
1 parent ae3c79d commit 015333e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
20 changes: 1 addition & 19 deletions shell/browser/api/electron_api_browser_window_mac.mm
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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 <NSWindowDelegate> {
@private
electron::InspectableWebContentsViewMac* inspectableWebContentsView_;

base::scoped_nsobject<NSView> fake_view_;
base::scoped_nsobject<NSWindow> devtools_window_;
base::scoped_nsobject<ControlRegionView> devtools_mask_;
BOOL devtools_visible_;
BOOL devtools_docked_;
BOOL devtools_is_first_responder_;
Expand Down
44 changes: 43 additions & 1 deletion shell/browser/ui/cocoa/electron_inspectable_web_contents_view.mm
Expand Up @@ -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:
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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];
Expand All @@ -94,6 +115,7 @@ - (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate {
} else {
gfx::ScopedCocoaDisableScreenUpdates disabler;
[devToolsView removeFromSuperview];
[devtools_mask_ removeFromSuperview];
[self adjustSubviews];
}
} else {
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 015333e

Please sign in to comment.