From 7bfece11449fd9e07a17f81f0c655d32e465ed19 Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Wed, 9 Aug 2017 14:49:11 -0700 Subject: [PATCH] :wrench: Make work with complex shapes --- atom/browser/native_browser_view.h | 2 +- atom/browser/native_browser_view_mac.h | 2 +- atom/browser/native_browser_view_mac.mm | 72 ++++++++++++++++++++----- atom/browser/native_window_mac.mm | 12 ++--- 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/atom/browser/native_browser_view.h b/atom/browser/native_browser_view.h index 47741aaf5ca..11c4dd228d1 100644 --- a/atom/browser/native_browser_view.h +++ b/atom/browser/native_browser_view.h @@ -42,7 +42,7 @@ class NativeBrowserView { // Called when the window needs to update its draggable region. virtual void UpdateDraggableRegions( - const std::vector& regions) = 0; + std::vector system_drag_exclude_areas) = 0; protected: explicit NativeBrowserView( diff --git a/atom/browser/native_browser_view_mac.h b/atom/browser/native_browser_view_mac.h index 9b87728571f..600a3df8da2 100644 --- a/atom/browser/native_browser_view_mac.h +++ b/atom/browser/native_browser_view_mac.h @@ -22,7 +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; + void UpdateDraggableRegions(std::vector system_drag_exclude_areas) 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 5e8db8cf554..589278dbced 100644 --- a/atom/browser/native_browser_view_mac.mm +++ b/atom/browser/native_browser_view_mac.mm @@ -27,6 +27,17 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask = return NO; } +- (NSView *)hitTest:(NSPoint)aPoint +{ + // Pass-through events that don't hit one of the exclusion zones + for (NSView *exlusion_zones in [self subviews]) { + if ([exlusion_zones hitTest:aPoint]) + return nil; + } + + return self; +} + - (void)mouseDown:(NSEvent *)event { if ([self.window respondsToSelector:@selector(performWindowDragWithEvent:)]) { @@ -63,13 +74,32 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask = } // Debugging tips: -// Uncomment the following four lines to color DragRegionViews bright red -- (void)drawRect:(NSRect)aRect -{ - [[NSColor redColor] set]; - NSRectFill([self bounds]); +// Uncomment the following four lines to color DragRegionView bright red +// - (void)drawRect:(NSRect)aRect +// { +// [[NSColor redColor] set]; +// NSRectFill([self bounds]); +// } + +@end + +@interface ExcludeDragRegionView : NSView +@end + +@implementation ExcludeDragRegionView + +- (BOOL)mouseDownCanMoveWindow { + return NO; } +// Debugging tips: +// Uncomment the following four lines to color ExcludeDragRegionView bright red +// - (void)drawRect:(NSRect)aRect +// { +// [[NSColor greenColor] set]; +// NSRectFill([self bounds]); +// } + @end namespace atom { @@ -112,9 +142,10 @@ void NativeBrowserViewMac::SetBackgroundColor(SkColor color) { } void NativeBrowserViewMac::UpdateDraggableRegions( - const std::vector& regions) { + std::vector system_drag_exclude_areas) { NSView* webView = GetInspectableWebContentsView()->GetNativeView(); NSInteger webViewHeight = NSHeight([webView bounds]); + NSInteger webViewWidth = NSWidth([webView bounds]); // Remove all DraggableRegionViews that are added last time. // Note that [webView subviews] returns the view's mutable internal array and @@ -125,15 +156,30 @@ void NativeBrowserViewMac::UpdateDraggableRegions( if ([subview isKindOfClass:[DragRegionView class]]) [subview removeFromSuperview]; - for (const DraggableRegion& region : regions) { - base::scoped_nsobject dragRegion( + // Create one giant NSView that is draggable. + 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]; + [dragRegion setFrame:NSMakeRect(0, + 0, + webViewWidth, + webViewHeight)]; + + // Then, on top of that, add "exclusion zones" + for (std::vector::const_iterator iter = + system_drag_exclude_areas.begin(); + iter != system_drag_exclude_areas.end(); + ++iter) { + base::scoped_nsobject controlRegion( + [[ExcludeDragRegionView alloc] initWithFrame:NSZeroRect]); + [controlRegion setFrame:NSMakeRect(iter->x(), + webViewHeight - iter->bottom(), + iter->width(), + iter->height())]; + [dragRegion addSubview:controlRegion]; } + + // Add the DragRegion to the WebView + [webView addSubview:dragRegion]; } // static diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 679624fa985..fcaf04aa761 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -1753,12 +1753,6 @@ 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 @@ -1773,6 +1767,12 @@ void NativeWindowMac::UpdateDraggableRegionViews( std::vector system_drag_exclude_areas = CalculateNonDraggableRegions(regions, webViewWidth, webViewHeight); + // Call down to a BrowserView, if it exists, and inform it about the + // draggable areas + if (browser_view_) { + browser_view_->UpdateDraggableRegions(system_drag_exclude_areas); + } + // Create and add a ControlRegionView for each region that needs to be // excluded from the dragging. for (std::vector::const_iterator iter =