Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
// Copyright (c) 2017 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/native_browser_view_mac.h"
|
|
|
|
|
|
|
|
#include "brightray/browser/inspectable_web_contents_view.h"
|
|
|
|
#include "skia/ext/skia_utils_mac.h"
|
|
|
|
#include "ui/gfx/geometry/rect.h"
|
|
|
|
|
2017-08-09 00:00:00 +00:00
|
|
|
using base::scoped_nsobject;
|
|
|
|
|
2017-04-12 11:40:31 +00:00
|
|
|
// Match view::Views behavior where the view sticks to the top-left origin.
|
|
|
|
const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
|
|
|
|
NSViewMaxXMargin | NSViewMinYMargin;
|
|
|
|
|
2017-08-09 00:00:00 +00:00
|
|
|
@interface DragRegionView : NSView
|
2017-08-09 18:57:57 +00:00
|
|
|
|
|
|
|
@property (assign) NSPoint initialLocation;
|
|
|
|
|
2017-08-09 00:00:00 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DragRegionView
|
|
|
|
|
|
|
|
- (BOOL)mouseDownCanMoveWindow
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDown:(NSEvent *)event
|
|
|
|
{
|
2017-08-09 18:57:57 +00:00
|
|
|
if ([self.window respondsToSelector:@selector(performWindowDragWithEvent:)]) {
|
|
|
|
[self.window performWindowDragWithEvent:event];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.initialLocation = [event locationInWindow];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDragged:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
if ([self.window respondsToSelector:@selector(performWindowDragWithEvent:)]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSPoint currentLocation;
|
|
|
|
NSPoint newOrigin;
|
|
|
|
|
|
|
|
NSRect screenFrame = [[NSScreen mainScreen] frame];
|
|
|
|
NSRect windowFrame = [self.window frame];
|
|
|
|
|
|
|
|
currentLocation = [NSEvent mouseLocation];
|
|
|
|
newOrigin.x = currentLocation.x - self.initialLocation.x;
|
|
|
|
newOrigin.y = currentLocation.y - self.initialLocation.y;
|
|
|
|
|
|
|
|
// Don't let window get dragged up under the menu bar
|
|
|
|
if( (newOrigin.y+windowFrame.size.height) > (screenFrame.origin.y+screenFrame.size.height) ){
|
|
|
|
newOrigin.y=screenFrame.origin.y + (screenFrame.size.height-windowFrame.size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the window to the new location
|
|
|
|
[self.window setFrameOrigin:newOrigin];
|
2017-08-09 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debugging tips:
|
|
|
|
// Uncomment the following four lines to color DragRegionViews bright red
|
|
|
|
- (void)drawRect:(NSRect)aRect
|
|
|
|
{
|
|
|
|
[[NSColor redColor] set];
|
|
|
|
NSRectFill([self bounds]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
NativeBrowserViewMac::NativeBrowserViewMac(
|
|
|
|
brightray::InspectableWebContentsView* web_contents_view)
|
2017-04-12 11:40:31 +00:00
|
|
|
: NativeBrowserView(web_contents_view) {
|
|
|
|
auto* view = GetInspectableWebContentsView()->GetNativeView();
|
|
|
|
view.autoresizingMask = kDefaultAutoResizingMask;
|
|
|
|
}
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
|
|
|
|
NativeBrowserViewMac::~NativeBrowserViewMac() {}
|
|
|
|
|
2017-04-12 11:40:31 +00:00
|
|
|
void NativeBrowserViewMac::SetAutoResizeFlags(uint8_t flags) {
|
|
|
|
NSAutoresizingMaskOptions autoresizing_mask = kDefaultAutoResizingMask;
|
|
|
|
if (flags & kAutoResizeWidth) {
|
|
|
|
autoresizing_mask |= NSViewWidthSizable;
|
|
|
|
}
|
|
|
|
if (flags & kAutoResizeHeight) {
|
|
|
|
autoresizing_mask |= NSViewHeightSizable;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* view = GetInspectableWebContentsView()->GetNativeView();
|
|
|
|
view.autoresizingMask = autoresizing_mask;
|
|
|
|
}
|
|
|
|
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
void NativeBrowserViewMac::SetBounds(const gfx::Rect& bounds) {
|
|
|
|
auto* view = GetInspectableWebContentsView()->GetNativeView();
|
|
|
|
auto* superview = view.superview;
|
|
|
|
const auto superview_height = superview ? superview.frame.size.height : 0;
|
|
|
|
view.frame =
|
|
|
|
NSMakeRect(bounds.x(), superview_height - bounds.y() - bounds.height(),
|
|
|
|
bounds.width(), bounds.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeBrowserViewMac::SetBackgroundColor(SkColor color) {
|
|
|
|
auto* view = GetInspectableWebContentsView()->GetNativeView();
|
|
|
|
view.wantsLayer = YES;
|
|
|
|
view.layer.backgroundColor = skia::CGColorCreateFromSkColor(color);
|
|
|
|
}
|
|
|
|
|
2017-08-09 00:00:00 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 17:47:30 +00:00
|
|
|
// static
|
|
|
|
NativeBrowserView* NativeBrowserView::Create(
|
|
|
|
brightray::InspectableWebContentsView* web_contents_view) {
|
|
|
|
return new NativeBrowserViewMac(web_contents_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|