2018-03-06 05:07:06 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_browser_window.h"
|
2018-03-06 05:07:06 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-03-06 05:07:06 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2018-03-06 05:44:36 +00:00
|
|
|
#include "base/mac/scoped_nsobject.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_browser_view.h"
|
|
|
|
#include "shell/browser/native_window_mac.h"
|
|
|
|
#include "shell/browser/ui/inspectable_web_contents_view.h"
|
2018-03-06 05:44:36 +00:00
|
|
|
|
2018-03-06 05:07:06 +00:00
|
|
|
@interface NSView (WebContentsView)
|
|
|
|
- (void)setMouseDownCanMoveWindow:(BOOL)can_move;
|
|
|
|
@end
|
|
|
|
|
2018-03-06 05:44:36 +00:00
|
|
|
@interface ControlRegionView : NSView
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ControlRegionView
|
|
|
|
|
|
|
|
- (BOOL)mouseDownCanMoveWindow {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSView*)hitTest:(NSPoint)aPoint {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2018-03-06 05:07:06 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2018-10-19 13:50:30 +00:00
|
|
|
void BrowserWindow::OverrideNSWindowContentView(InspectableWebContents* iwc) {
|
2018-07-10 01:43:42 +00:00
|
|
|
// Make NativeWindow use a NSView as content view.
|
|
|
|
static_cast<NativeWindowMac*>(window())->OverrideNSWindowContentView();
|
|
|
|
// Add webview to contentView.
|
2019-01-09 20:25:19 +00:00
|
|
|
NSView* webView = iwc->GetView()->GetNativeView().GetNativeNSView();
|
|
|
|
NSView* contentView =
|
|
|
|
[window()->GetNativeWindow().GetNativeNSWindow() contentView];
|
2018-07-10 01:43:42 +00:00
|
|
|
[webView setFrame:[contentView bounds]];
|
2018-12-10 19:05:30 +00:00
|
|
|
|
|
|
|
// ensure that buttons view is floated to top of view hierarchy
|
|
|
|
NSArray* subviews = [contentView subviews];
|
|
|
|
NSView* last = subviews.lastObject;
|
|
|
|
[contentView addSubview:webView positioned:NSWindowBelow relativeTo:last];
|
|
|
|
|
2018-07-10 01:43:42 +00:00
|
|
|
[contentView viewDidMoveToWindow];
|
|
|
|
}
|
|
|
|
|
2018-03-06 05:07:06 +00:00
|
|
|
void BrowserWindow::UpdateDraggableRegions(
|
2019-06-03 17:43:04 +00:00
|
|
|
const std::vector<mojom::DraggableRegionPtr>& regions) {
|
2018-03-06 05:07:06 +00:00
|
|
|
if (window_->has_frame())
|
|
|
|
return;
|
2018-03-06 05:44:36 +00:00
|
|
|
|
2020-01-30 00:55:27 +00:00
|
|
|
if (!web_contents())
|
|
|
|
return;
|
|
|
|
|
2018-03-06 05:44:36 +00:00
|
|
|
// All ControlRegionViews should be added as children of the WebContentsView,
|
|
|
|
// because WebContentsView will be removed and re-added when entering and
|
|
|
|
// leaving fullscreen mode.
|
2019-01-09 20:25:19 +00:00
|
|
|
NSView* webView = web_contents()->GetNativeView().GetNativeNSView();
|
2018-03-06 05:44:36 +00:00
|
|
|
NSInteger webViewWidth = NSWidth([webView bounds]);
|
|
|
|
NSInteger webViewHeight = NSHeight([webView bounds]);
|
|
|
|
|
2018-03-06 05:07:06 +00:00
|
|
|
if ([webView respondsToSelector:@selector(setMouseDownCanMoveWindow:)]) {
|
|
|
|
[webView setMouseDownCanMoveWindow:YES];
|
|
|
|
}
|
2018-03-06 05:44:36 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// it.
|
|
|
|
base::scoped_nsobject<NSArray> subviews([[webView subviews] copy]);
|
|
|
|
for (NSView* subview in subviews.get())
|
|
|
|
if ([subview isKindOfClass:[ControlRegionView class]])
|
|
|
|
[subview removeFromSuperview];
|
|
|
|
|
|
|
|
// Draggable regions is implemented by having the whole web view draggable
|
|
|
|
// (mouseDownCanMoveWindow) and overlaying regions that are not draggable.
|
2020-01-09 20:13:28 +00:00
|
|
|
if (&draggable_regions_ != ®ions) {
|
|
|
|
draggable_regions_.clear();
|
|
|
|
for (const auto& r : regions)
|
|
|
|
draggable_regions_.push_back(r.Clone());
|
|
|
|
}
|
2020-10-27 21:28:43 +00:00
|
|
|
|
|
|
|
auto browser_views = window_->browser_views();
|
|
|
|
for (NativeBrowserView* view : browser_views) {
|
|
|
|
view->UpdateDraggableRegions(draggable_regions_);
|
|
|
|
}
|
|
|
|
|
2018-03-19 11:21:03 +00:00
|
|
|
std::vector<gfx::Rect> drag_exclude_rects;
|
2018-03-06 05:44:36 +00:00
|
|
|
if (regions.empty()) {
|
2018-03-19 11:21:03 +00:00
|
|
|
drag_exclude_rects.push_back(gfx::Rect(0, 0, webViewWidth, webViewHeight));
|
2018-03-06 05:44:36 +00:00
|
|
|
} else {
|
2018-03-19 11:21:03 +00:00
|
|
|
drag_exclude_rects = CalculateNonDraggableRegions(
|
2018-03-06 05:44:36 +00:00
|
|
|
DraggableRegionsToSkRegion(regions), webViewWidth, webViewHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and add a ControlRegionView for each region that needs to be
|
|
|
|
// excluded from the dragging.
|
2018-03-19 11:21:03 +00:00
|
|
|
for (const auto& rect : drag_exclude_rects) {
|
2018-03-06 05:44:36 +00:00
|
|
|
base::scoped_nsobject<NSView> controlRegion(
|
|
|
|
[[ControlRegionView alloc] initWithFrame:NSZeroRect]);
|
2018-03-19 11:21:03 +00:00
|
|
|
[controlRegion setFrame:NSMakeRect(rect.x(), webViewHeight - rect.bottom(),
|
|
|
|
rect.width(), rect.height())];
|
2018-03-06 05:44:36 +00:00
|
|
|
[webView addSubview:controlRegion];
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppKit will not update its cache of mouseDownCanMoveWindow unless something
|
|
|
|
// changes. Previously we tried adding an NSView and removing it, but for some
|
|
|
|
// reason it required reposting the mouse-down event, and didn't always work.
|
|
|
|
// Calling the below seems to be an effective solution.
|
|
|
|
[[webView window] setMovableByWindowBackground:NO];
|
|
|
|
[[webView window] setMovableByWindowBackground:YES];
|
2018-03-06 05:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|