🔧 Draggable (using the window’s regions)
This commit is contained in:
parent
b81aab9eae
commit
a5dfb09037
5 changed files with 68 additions and 1 deletions
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
#include "base/macros.h"
|
#include "base/macros.h"
|
||||||
#include "third_party/skia/include/core/SkColor.h"
|
#include "third_party/skia/include/core/SkColor.h"
|
||||||
|
#include "atom/common/draggable_region.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace brightray {
|
namespace brightray {
|
||||||
class InspectableWebContentsView;
|
class InspectableWebContentsView;
|
||||||
|
@ -38,6 +40,10 @@ class NativeBrowserView {
|
||||||
virtual void SetBounds(const gfx::Rect& bounds) = 0;
|
virtual void SetBounds(const gfx::Rect& bounds) = 0;
|
||||||
virtual void SetBackgroundColor(SkColor color) = 0;
|
virtual void SetBackgroundColor(SkColor color) = 0;
|
||||||
|
|
||||||
|
// Called when the window needs to update its draggable region.
|
||||||
|
virtual void UpdateDraggableRegions(
|
||||||
|
const std::vector<DraggableRegion>& regions) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit NativeBrowserView(
|
explicit NativeBrowserView(
|
||||||
brightray::InspectableWebContentsView* web_contents_view);
|
brightray::InspectableWebContentsView* web_contents_view);
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
|
|
||||||
|
#include "atom/common/draggable_region.h"
|
||||||
|
#include "base/mac/scoped_nsobject.h"
|
||||||
#include "atom/browser/native_browser_view.h"
|
#include "atom/browser/native_browser_view.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -20,6 +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;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DISALLOW_COPY_AND_ASSIGN(NativeBrowserViewMac);
|
DISALLOW_COPY_AND_ASSIGN(NativeBrowserViewMac);
|
||||||
|
|
|
@ -8,10 +8,37 @@
|
||||||
#include "skia/ext/skia_utils_mac.h"
|
#include "skia/ext/skia_utils_mac.h"
|
||||||
#include "ui/gfx/geometry/rect.h"
|
#include "ui/gfx/geometry/rect.h"
|
||||||
|
|
||||||
|
using base::scoped_nsobject;
|
||||||
|
|
||||||
// Match view::Views behavior where the view sticks to the top-left origin.
|
// Match view::Views behavior where the view sticks to the top-left origin.
|
||||||
const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
|
const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
|
||||||
NSViewMaxXMargin | NSViewMinYMargin;
|
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 {
|
namespace atom {
|
||||||
|
|
||||||
NativeBrowserViewMac::NativeBrowserViewMac(
|
NativeBrowserViewMac::NativeBrowserViewMac(
|
||||||
|
@ -51,6 +78,31 @@ void NativeBrowserViewMac::SetBackgroundColor(SkColor color) {
|
||||||
view.layer.backgroundColor = skia::CGColorCreateFromSkColor(color);
|
view.layer.backgroundColor = skia::CGColorCreateFromSkColor(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NativeBrowserViewMac::UpdateDraggableRegions(
|
||||||
|
const std::vector<DraggableRegion>& 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<NSArray> 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<NSView> 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
|
// static
|
||||||
NativeBrowserView* NativeBrowserView::Create(
|
NativeBrowserView* NativeBrowserView::Create(
|
||||||
brightray::InspectableWebContentsView* web_contents_view) {
|
brightray::InspectableWebContentsView* web_contents_view) {
|
||||||
|
|
|
@ -154,7 +154,7 @@ class NativeWindowMac : public NativeWindow,
|
||||||
void UninstallView();
|
void UninstallView();
|
||||||
|
|
||||||
// Install the drag view, which will cover the whole window and decides
|
// 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<DraggableRegion>& regions);
|
void UpdateDraggableRegionViews(const std::vector<DraggableRegion>& regions);
|
||||||
|
|
||||||
void RegisterInputEventObserver(content::RenderViewHost* host);
|
void RegisterInputEventObserver(content::RenderViewHost* host);
|
||||||
|
|
|
@ -1753,6 +1753,12 @@ 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
|
||||||
|
|
Loading…
Reference in a new issue