2013-03-14 13:03:50 +00:00
|
|
|
#import "browser/mac/bry_inspectable_web_contents_view.h"
|
|
|
|
|
|
|
|
#import "browser/inspectable_web_contents_impl.h"
|
|
|
|
#import "browser/inspectable_web_contents_view_mac.h"
|
|
|
|
#import "browser/mac/bry_inspectable_web_contents_view_private.h"
|
|
|
|
|
2013-04-09 19:11:16 +00:00
|
|
|
#import "content/public/browser/render_widget_host_view.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
#import "content/public/browser/web_contents_view.h"
|
2013-11-25 21:28:57 +00:00
|
|
|
#import "ui/base/cocoa/underlay_opengl_hosting_window.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
|
|
|
|
using namespace brightray;
|
|
|
|
|
2013-12-12 15:33:50 +00:00
|
|
|
@interface GraySplitView : NSSplitView
|
2013-12-12 09:26:04 +00:00
|
|
|
- (NSColor*)dividerColor;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GraySplitView
|
|
|
|
- (NSColor*)dividerColor {
|
|
|
|
return [NSColor darkGrayColor];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
@interface BRYInspectableWebContentsViewPrivate : NSObject {
|
|
|
|
@public
|
|
|
|
InspectableWebContentsViewMac *inspectableWebContentsView;
|
2013-12-12 09:26:04 +00:00
|
|
|
GraySplitView *splitView;
|
2013-03-27 16:15:46 +00:00
|
|
|
NSWindow *window;
|
2013-04-08 17:53:53 +00:00
|
|
|
BOOL visible;
|
2013-03-14 13:03:50 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2013-03-27 16:15:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
NSRect devtoolsWindowFrame(NSView *referenceView) {
|
|
|
|
auto screenFrame = [referenceView.window convertRectToScreen:[referenceView convertRect:referenceView.bounds toView:nil]];
|
|
|
|
return NSInsetRect(screenFrame, NSWidth(screenFrame) / 6, NSHeight(screenFrame) / 6);
|
|
|
|
}
|
|
|
|
|
2013-04-09 19:11:16 +00:00
|
|
|
void SetActive(content::WebContents* web_contents, bool active) {
|
|
|
|
auto render_widget_host_view = web_contents->GetRenderWidgetHostView();
|
|
|
|
if (!render_widget_host_view)
|
|
|
|
return;
|
|
|
|
|
|
|
|
render_widget_host_view->SetActive(active);
|
|
|
|
}
|
|
|
|
|
2013-03-27 16:15:46 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
@implementation BRYInspectableWebContentsView
|
|
|
|
|
|
|
|
- (instancetype)initWithInspectableWebContentsViewMac:(InspectableWebContentsViewMac *)inspectableWebContentsView {
|
|
|
|
self = [super init];
|
|
|
|
if (!self)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
_private = [[BRYInspectableWebContentsViewPrivate alloc] init];
|
|
|
|
_private->inspectableWebContentsView = inspectableWebContentsView;
|
2013-12-12 09:26:04 +00:00
|
|
|
_private->splitView = [[GraySplitView alloc] init];
|
|
|
|
_private->splitView.delegate = self;
|
2013-03-14 13:03:50 +00:00
|
|
|
|
|
|
|
[self addSubview:_private->splitView];
|
|
|
|
_private->splitView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
|
|
_private->splitView.dividerStyle = NSSplitViewDividerStyleThin;
|
|
|
|
[_private->splitView addSubview:inspectableWebContentsView->inspectable_web_contents()->GetWebContents()->GetView()->GetNativeView()];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2013-03-27 16:15:46 +00:00
|
|
|
[_private->window release];
|
2013-03-14 17:06:58 +00:00
|
|
|
[_private->splitView release];
|
2013-03-14 13:03:50 +00:00
|
|
|
[_private release];
|
|
|
|
_private = nil;
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2014-04-23 03:20:12 +00:00
|
|
|
- (void)removeFromNotificationCenter {
|
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
- (IBAction)showDevTools:(id)sender {
|
|
|
|
_private->inspectableWebContentsView->inspectable_web_contents()->ShowDevTools();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setDevToolsVisible:(BOOL)visible {
|
2013-04-08 17:53:53 +00:00
|
|
|
if (_private->visible == visible)
|
2013-03-14 13:03:50 +00:00
|
|
|
return;
|
2013-04-08 17:53:53 +00:00
|
|
|
_private->visible = visible;
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2013-04-09 19:11:16 +00:00
|
|
|
if ([self isDocked]) {
|
2013-03-27 16:15:46 +00:00
|
|
|
if (visible) {
|
|
|
|
[_private->window makeKeyAndOrderFront:nil];
|
|
|
|
} else {
|
|
|
|
[_private->window orderOut:nil];
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-09 19:11:16 +00:00
|
|
|
auto devToolsWebContents = _private->inspectableWebContentsView->inspectable_web_contents()->devtools_web_contents();
|
|
|
|
auto devToolsView = devToolsWebContents->GetView()->GetNativeView();
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
if (visible) {
|
|
|
|
auto inspectedView = _private->inspectableWebContentsView->inspectable_web_contents()->GetWebContents()->GetView()->GetNativeView();
|
|
|
|
CGRect frame = NSRectToCGRect(inspectedView.frame);
|
|
|
|
CGRect inspectedViewFrame;
|
|
|
|
CGRect devToolsFrame;
|
2013-04-17 01:06:07 +00:00
|
|
|
CGFloat amount;
|
|
|
|
CGRectEdge edge;
|
|
|
|
if ([_private->splitView isVertical]) {
|
|
|
|
amount = CGRectGetWidth(frame) * 2 / 3;
|
|
|
|
edge = CGRectMaxXEdge;
|
|
|
|
} else {
|
|
|
|
amount = CGRectGetHeight(frame) * 2 / 3;
|
|
|
|
edge = CGRectMaxYEdge;
|
|
|
|
}
|
|
|
|
CGRectDivide(frame, &inspectedViewFrame, &devToolsFrame, amount, edge);
|
2013-03-14 13:03:50 +00:00
|
|
|
|
|
|
|
inspectedView.frame = NSRectFromCGRect(inspectedViewFrame);
|
|
|
|
devToolsView.frame = NSRectFromCGRect(devToolsFrame);
|
|
|
|
|
|
|
|
[_private->splitView addSubview:devToolsView];
|
|
|
|
} else {
|
|
|
|
[devToolsView removeFromSuperview];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_private->splitView adjustSubviews];
|
|
|
|
}
|
|
|
|
|
2013-11-05 02:29:53 +00:00
|
|
|
- (BOOL)isDevToolsVisible {
|
|
|
|
return _private->visible;
|
|
|
|
}
|
|
|
|
|
2013-03-27 15:19:15 +00:00
|
|
|
- (BOOL)setDockSide:(const std::string&)side {
|
|
|
|
if (side == "right") {
|
|
|
|
_private->splitView.vertical = YES;
|
2013-03-27 16:15:46 +00:00
|
|
|
[self moveToSplitView];
|
2013-03-27 15:19:15 +00:00
|
|
|
} else if (side == "bottom") {
|
|
|
|
_private->splitView.vertical = NO;
|
2013-03-27 16:15:46 +00:00
|
|
|
[self moveToSplitView];
|
|
|
|
} else if (side == "undocked") {
|
|
|
|
[self moveToWindow];
|
2013-03-27 15:19:15 +00:00
|
|
|
} else {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2013-03-27 16:15:46 +00:00
|
|
|
- (void)moveToWindow {
|
|
|
|
if (!_private->window) {
|
|
|
|
auto styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask | NSTexturedBackgroundWindowMask | NSUnifiedTitleAndToolbarWindowMask;
|
2013-11-25 21:28:57 +00:00
|
|
|
auto contentRect = [UnderlayOpenGLHostingWindow contentRectForFrameRect:devtoolsWindowFrame(_private->splitView) styleMask:styleMask];
|
|
|
|
_private->window = [[UnderlayOpenGLHostingWindow alloc] initWithContentRect:contentRect styleMask:styleMask backing:NSBackingStoreBuffered defer:YES];
|
2013-03-27 16:15:46 +00:00
|
|
|
_private->window.delegate = self;
|
|
|
|
_private->window.releasedWhenClosed = NO;
|
|
|
|
_private->window.title = @"Developer Tools";
|
|
|
|
[_private->window setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge];
|
|
|
|
[_private->window setContentBorderThickness:24 forEdge:NSMaxYEdge];
|
|
|
|
}
|
|
|
|
|
|
|
|
auto devToolsWebContents = _private->inspectableWebContentsView->inspectable_web_contents()->devtools_web_contents();
|
|
|
|
auto devToolsView = devToolsWebContents->GetView()->GetNativeView();
|
|
|
|
|
|
|
|
NSView *contentView = _private->window.contentView;
|
|
|
|
devToolsView.frame = contentView.bounds;
|
|
|
|
devToolsView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
|
|
|
|
|
|
[contentView addSubview:devToolsView];
|
|
|
|
[_private->window makeKeyAndOrderFront:nil];
|
|
|
|
[_private->splitView adjustSubviews];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)moveToSplitView {
|
|
|
|
[_private->window orderOut:nil];
|
|
|
|
|
|
|
|
auto devToolsWebContents = _private->inspectableWebContentsView->inspectable_web_contents()->devtools_web_contents();
|
|
|
|
auto devToolsView = devToolsWebContents->GetView()->GetNativeView();
|
|
|
|
|
|
|
|
[_private->splitView addSubview:devToolsView];
|
|
|
|
[_private->splitView adjustSubviews];
|
|
|
|
}
|
|
|
|
|
2013-04-09 19:11:16 +00:00
|
|
|
- (BOOL)isDocked {
|
|
|
|
auto devToolsWebContents = _private->inspectableWebContentsView->inspectable_web_contents()->devtools_web_contents();
|
|
|
|
if (!devToolsWebContents)
|
|
|
|
return NO;
|
|
|
|
auto devToolsView = devToolsWebContents->GetView()->GetNativeView();
|
|
|
|
|
|
|
|
return _private->window && devToolsView.window == _private->window;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)window:(NSWindow *)window didBecomeActive:(BOOL)active {
|
|
|
|
auto inspectable_contents = _private->inspectableWebContentsView->inspectable_web_contents();
|
|
|
|
|
|
|
|
// Changes to the active state of the window we create only affects the dev tools contents.
|
|
|
|
if (window == _private->window) {
|
|
|
|
SetActive(inspectable_contents->devtools_web_contents(), active);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes the window that hosts us always affect our main web contents. If the dev tools are also
|
|
|
|
// hosted in this window, they are affected too.
|
|
|
|
SetActive(inspectable_contents->GetWebContents(), active);
|
|
|
|
if (![self isDocked])
|
|
|
|
return;
|
|
|
|
SetActive(inspectable_contents->devtools_web_contents(), active);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - NSView
|
|
|
|
|
|
|
|
- (void)viewWillMoveToWindow:(NSWindow *)newWindow {
|
|
|
|
if (self.window) {
|
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:self name:NSWindowDidBecomeKeyNotification object:self.window];
|
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:self name:NSWindowDidResignKeyNotification object:self.window];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newWindow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:newWindow];
|
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:newWindow];
|
|
|
|
}
|
|
|
|
|
2013-12-12 09:26:04 +00:00
|
|
|
#pragma mark - NSSplitViewDelegate
|
|
|
|
|
|
|
|
-(void)splitViewWillResizeSubviews:(NSNotification *)notification {
|
|
|
|
[[_private->splitView window] disableScreenUpdatesUntilFlush];
|
|
|
|
}
|
|
|
|
|
2013-03-27 16:15:46 +00:00
|
|
|
#pragma mark - NSWindowDelegate
|
|
|
|
|
|
|
|
- (BOOL)windowShouldClose:(id)sender {
|
|
|
|
[_private->window orderOut:nil];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2013-04-09 19:11:16 +00:00
|
|
|
- (void)windowDidBecomeKey:(NSNotification *)notification {
|
|
|
|
[self window:notification.object didBecomeActive:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidResignKey:(NSNotification *)notification {
|
|
|
|
[self window:notification.object didBecomeActive:NO];
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BRYInspectableWebContentsViewPrivate
|
2013-11-05 02:29:53 +00:00
|
|
|
@end
|