🔧 Make work with complex shapes

This commit is contained in:
Felix Rieseberg 2017-08-09 14:49:11 -07:00
parent fdd0d67fd3
commit ebe058e7cb
4 changed files with 67 additions and 21 deletions

View file

@ -42,7 +42,7 @@ class NativeBrowserView {
// Called when the window needs to update its draggable region. // Called when the window needs to update its draggable region.
virtual void UpdateDraggableRegions( virtual void UpdateDraggableRegions(
const std::vector<DraggableRegion>& regions) = 0; std::vector<gfx::Rect> system_drag_exclude_areas) = 0;
protected: protected:
explicit NativeBrowserView( explicit NativeBrowserView(

View file

@ -22,7 +22,7 @@ class NativeBrowserViewMac : public NativeBrowserView {
void SetAutoResizeFlags(uint8_t flags) override; void SetAutoResizeFlags(uint8_t flags) override;
void SetBounds(const gfx::Rect& bounds) override; void SetBounds(const gfx::Rect& bounds) override;
void SetBackgroundColor(SkColor color) override; void SetBackgroundColor(SkColor color) override;
void UpdateDraggableRegions(const std::vector<DraggableRegion>& regions) override; void UpdateDraggableRegions(std::vector<gfx::Rect> system_drag_exclude_areas) override;
private: private:
DISALLOW_COPY_AND_ASSIGN(NativeBrowserViewMac); DISALLOW_COPY_AND_ASSIGN(NativeBrowserViewMac);

View file

@ -27,6 +27,17 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
return NO; 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 - (void)mouseDown:(NSEvent *)event
{ {
if ([self.window respondsToSelector:@selector(performWindowDragWithEvent:)]) { if ([self.window respondsToSelector:@selector(performWindowDragWithEvent:)]) {
@ -63,13 +74,32 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
} }
// Debugging tips: // Debugging tips:
// Uncomment the following four lines to color DragRegionViews bright red // Uncomment the following four lines to color DragRegionView bright red
- (void)drawRect:(NSRect)aRect // - (void)drawRect:(NSRect)aRect
{ // {
[[NSColor redColor] set]; // [[NSColor redColor] set];
NSRectFill([self bounds]); // 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 @end
namespace atom { namespace atom {
@ -112,9 +142,10 @@ void NativeBrowserViewMac::SetBackgroundColor(SkColor color) {
} }
void NativeBrowserViewMac::UpdateDraggableRegions( void NativeBrowserViewMac::UpdateDraggableRegions(
const std::vector<DraggableRegion>& regions) { std::vector<gfx::Rect> system_drag_exclude_areas) {
NSView* webView = GetInspectableWebContentsView()->GetNativeView(); NSView* webView = GetInspectableWebContentsView()->GetNativeView();
NSInteger webViewHeight = NSHeight([webView bounds]); NSInteger webViewHeight = NSHeight([webView bounds]);
NSInteger webViewWidth = NSWidth([webView bounds]);
// Remove all DraggableRegionViews that are added last time. // Remove all DraggableRegionViews that are added last time.
// Note that [webView subviews] returns the view's mutable internal array and // Note that [webView subviews] returns the view's mutable internal array and
@ -125,15 +156,30 @@ void NativeBrowserViewMac::UpdateDraggableRegions(
if ([subview isKindOfClass:[DragRegionView class]]) if ([subview isKindOfClass:[DragRegionView class]])
[subview removeFromSuperview]; [subview removeFromSuperview];
for (const DraggableRegion& region : regions) { // Create one giant NSView that is draggable.
base::scoped_nsobject<NSView> dragRegion( base::scoped_nsobject<NSView> dragRegion(
[[DragRegionView alloc] initWithFrame:NSZeroRect]); [[DragRegionView alloc] initWithFrame:NSZeroRect]);
[dragRegion setFrame:NSMakeRect(region.bounds.x(), [dragRegion setFrame:NSMakeRect(0,
webViewHeight - region.bounds.bottom(), 0,
region.bounds.width(), webViewWidth,
region.bounds.height())]; webViewHeight)];
[webView addSubview:dragRegion];
// Then, on top of that, add "exclusion zones"
for (std::vector<gfx::Rect>::const_iterator iter =
system_drag_exclude_areas.begin();
iter != system_drag_exclude_areas.end();
++iter) {
base::scoped_nsobject<NSView> 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 // static

View file

@ -1753,12 +1753,6 @@ void NativeWindowMac::UpdateDraggableRegionViews(
[webView setMouseDownCanMoveWindow:YES]; [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. // Remove all ControlRegionViews that are added last time.
// Note that [webView subviews] returns the view's mutable internal array and // 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 should be copied to avoid mutating the original array while enumerating
@ -1773,6 +1767,12 @@ void NativeWindowMac::UpdateDraggableRegionViews(
std::vector<gfx::Rect> system_drag_exclude_areas = std::vector<gfx::Rect> system_drag_exclude_areas =
CalculateNonDraggableRegions(regions, webViewWidth, webViewHeight); 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 // Create and add a ControlRegionView for each region that needs to be
// excluded from the dragging. // excluded from the dragging.
for (std::vector<gfx::Rect>::const_iterator iter = for (std::vector<gfx::Rect>::const_iterator iter =