From a5dfb09037dd44d665b3eeec0f7a1ed7799a0433 Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Tue, 8 Aug 2017 17:00:00 -0700 Subject: [PATCH] =?UTF-8?q?:wrench:=20Draggable=20(using=20the=20window?= =?UTF-8?q?=E2=80=99s=20regions)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atom/browser/native_browser_view.h | 6 +++ atom/browser/native_browser_view_mac.h | 3 ++ atom/browser/native_browser_view_mac.mm | 52 +++++++++++++++++++++++++ atom/browser/native_window_mac.h | 2 +- atom/browser/native_window_mac.mm | 6 +++ 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/atom/browser/native_browser_view.h b/atom/browser/native_browser_view.h index 236f615c2b..47741aaf5c 100644 --- a/atom/browser/native_browser_view.h +++ b/atom/browser/native_browser_view.h @@ -7,6 +7,8 @@ #include "base/macros.h" #include "third_party/skia/include/core/SkColor.h" +#include "atom/common/draggable_region.h" +#include namespace brightray { class InspectableWebContentsView; @@ -38,6 +40,10 @@ class NativeBrowserView { virtual void SetBounds(const gfx::Rect& bounds) = 0; virtual void SetBackgroundColor(SkColor color) = 0; + // Called when the window needs to update its draggable region. + virtual void UpdateDraggableRegions( + const std::vector& regions) = 0; + protected: explicit NativeBrowserView( brightray::InspectableWebContentsView* web_contents_view); diff --git a/atom/browser/native_browser_view_mac.h b/atom/browser/native_browser_view_mac.h index 4e7aa429ce..9b87728571 100644 --- a/atom/browser/native_browser_view_mac.h +++ b/atom/browser/native_browser_view_mac.h @@ -7,6 +7,8 @@ #import +#include "atom/common/draggable_region.h" +#include "base/mac/scoped_nsobject.h" #include "atom/browser/native_browser_view.h" namespace atom { @@ -20,6 +22,7 @@ class NativeBrowserViewMac : public NativeBrowserView { void SetAutoResizeFlags(uint8_t flags) override; void SetBounds(const gfx::Rect& bounds) override; void SetBackgroundColor(SkColor color) override; + void UpdateDraggableRegions(const std::vector& regions) override; private: DISALLOW_COPY_AND_ASSIGN(NativeBrowserViewMac); diff --git a/atom/browser/native_browser_view_mac.mm b/atom/browser/native_browser_view_mac.mm index 2ce2adc1f4..096b87dbb4 100644 --- a/atom/browser/native_browser_view_mac.mm +++ b/atom/browser/native_browser_view_mac.mm @@ -8,10 +8,37 @@ #include "skia/ext/skia_utils_mac.h" #include "ui/gfx/geometry/rect.h" +using base::scoped_nsobject; + // Match view::Views behavior where the view sticks to the top-left origin. const NSAutoresizingMaskOptions kDefaultAutoResizingMask = NSViewMaxXMargin | NSViewMinYMargin; +@interface DragRegionView : NSView +@end + +@implementation DragRegionView + +- (BOOL)mouseDownCanMoveWindow +{ + return NO; +} + +- (void)mouseDown:(NSEvent *)event +{ + [self.window performWindowDragWithEvent:event]; +} + +// Debugging tips: +// Uncomment the following four lines to color DragRegionViews bright red +- (void)drawRect:(NSRect)aRect +{ + [[NSColor redColor] set]; + NSRectFill([self bounds]); +} + +@end + namespace atom { NativeBrowserViewMac::NativeBrowserViewMac( @@ -51,6 +78,31 @@ void NativeBrowserViewMac::SetBackgroundColor(SkColor color) { view.layer.backgroundColor = skia::CGColorCreateFromSkColor(color); } +void NativeBrowserViewMac::UpdateDraggableRegions( + const std::vector& regions) { + NSView* webView = GetInspectableWebContentsView()->GetNativeView(); + NSInteger webViewHeight = NSHeight([webView bounds]); + + // Remove all DraggableRegionViews that are added last time. + // Note that [webView subviews] returns the view's mutable internal array and + // it should be copied to avoid mutating the original array while enumerating + // it. + base::scoped_nsobject subviews([[webView subviews] copy]); + for (NSView* subview in subviews.get()) + if ([subview isKindOfClass:[DragRegionView class]]) + [subview removeFromSuperview]; + + for (const DraggableRegion& region : regions) { + base::scoped_nsobject dragRegion( + [[DragRegionView alloc] initWithFrame:NSZeroRect]); + [dragRegion setFrame:NSMakeRect(region.bounds.x(), + webViewHeight - region.bounds.bottom(), + region.bounds.width(), + region.bounds.height())]; + [webView addSubview:dragRegion]; + } +} + // static NativeBrowserView* NativeBrowserView::Create( brightray::InspectableWebContentsView* web_contents_view) { diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 3dfb88d539..45636eb702 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -154,7 +154,7 @@ class NativeWindowMac : public NativeWindow, void UninstallView(); // Install the drag view, which will cover the whole window and decides - // whehter we can drag. + // whether we can drag. void UpdateDraggableRegionViews(const std::vector& regions); void RegisterInputEventObserver(content::RenderViewHost* host); diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 2719c67295..679624fa98 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -1753,6 +1753,12 @@ void NativeWindowMac::UpdateDraggableRegionViews( [webView setMouseDownCanMoveWindow:YES]; } + // Call down to a BrowserView, if it exists, and inform it about the + // draggable areas + if (browser_view_) { + browser_view_->UpdateDraggableRegions(regions); + } + // Remove all ControlRegionViews that are added last time. // Note that [webView subviews] returns the view's mutable internal array and // it should be copied to avoid mutating the original array while enumerating