2014-07-02 08:21:47 +00:00
|
|
|
#include "browser/mac/bry_inspectable_web_contents_view.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
#include "browser/inspectable_web_contents_impl.h"
|
|
|
|
#include "browser/inspectable_web_contents_view_mac.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2015-08-26 07:56:39 +00:00
|
|
|
#import "ui/base/cocoa/underlay_opengl_hosting_window.h"
|
2014-07-02 08:21:47 +00:00
|
|
|
#include "ui/gfx/mac/scoped_ns_disable_screen_updates.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
|
|
|
|
using namespace brightray;
|
|
|
|
|
|
|
|
@implementation BRYInspectableWebContentsView
|
|
|
|
|
2014-07-02 08:54:04 +00:00
|
|
|
- (instancetype)initWithInspectableWebContentsViewMac:(InspectableWebContentsViewMac*)view {
|
2013-03-14 13:03:50 +00:00
|
|
|
self = [super init];
|
|
|
|
if (!self)
|
|
|
|
return nil;
|
|
|
|
|
2014-07-02 08:54:04 +00:00
|
|
|
inspectableWebContentsView_ = view;
|
2014-07-01 12:57:49 +00:00
|
|
|
devtools_visible_ = NO;
|
2014-07-02 08:21:47 +00:00
|
|
|
devtools_docked_ = NO;
|
2015-09-15 03:04:46 +00:00
|
|
|
devtools_is_first_responder_ = NO;
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(viewDidBecomeFirstResponder:)
|
|
|
|
name:kViewDidBecomeFirstResponder
|
|
|
|
object:nil];
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2014-07-02 08:54:04 +00:00
|
|
|
auto contents = inspectableWebContentsView_->inspectable_web_contents()->GetWebContents();
|
2014-07-28 01:34:13 +00:00
|
|
|
auto contentsView = contents->GetNativeView();
|
2014-07-02 08:21:47 +00:00
|
|
|
[contentsView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
|
|
|
|
[self addSubview:contentsView];
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2014-07-28 04:40:38 +00:00
|
|
|
// See https://code.google.com/p/chromium/issues/detail?id=348490.
|
|
|
|
[self setWantsLayer:YES];
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
- (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
|
|
|
|
[self adjustSubviews];
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
- (IBAction)showDevTools:(id)sender {
|
2014-07-01 12:57:49 +00:00
|
|
|
inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools();
|
2013-03-14 13:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setDevToolsVisible:(BOOL)visible {
|
2014-07-02 08:21:47 +00:00
|
|
|
if (visible == devtools_visible_)
|
2013-03-27 16:15:46 +00:00
|
|
|
return;
|
2014-07-02 08:21:47 +00:00
|
|
|
|
2015-06-05 06:33:37 +00:00
|
|
|
auto inspectable_web_contents = inspectableWebContentsView_->inspectable_web_contents();
|
|
|
|
auto webContents = inspectable_web_contents->GetWebContents();
|
|
|
|
auto devToolsWebContents = inspectable_web_contents->GetDevToolsWebContents();
|
2014-07-28 01:34:13 +00:00
|
|
|
auto devToolsView = devToolsWebContents->GetNativeView();
|
2014-07-02 08:21:47 +00:00
|
|
|
|
2014-12-07 06:16:00 +00:00
|
|
|
if (visible && devtools_docked_) {
|
|
|
|
webContents->SetAllowOtherViews(true);
|
|
|
|
devToolsWebContents->SetAllowOtherViews(true);
|
|
|
|
} else {
|
|
|
|
webContents->SetAllowOtherViews(false);
|
|
|
|
}
|
|
|
|
|
2014-07-01 12:57:49 +00:00
|
|
|
devtools_visible_ = visible;
|
2014-07-02 08:21:47 +00:00
|
|
|
if (devtools_docked_) {
|
|
|
|
if (visible) {
|
|
|
|
// Place the devToolsView under contentsView, notice that we didn't set
|
|
|
|
// sizes for them until the setContentsResizingStrategy message.
|
|
|
|
[self addSubview:devToolsView positioned:NSWindowBelow relativeTo:nil];
|
2014-09-01 11:18:12 +00:00
|
|
|
[self adjustSubviews];
|
2014-08-07 02:48:02 +00:00
|
|
|
|
|
|
|
// Focus on web view.
|
|
|
|
devToolsWebContents->RestoreFocus();
|
2014-07-02 08:21:47 +00:00
|
|
|
} else {
|
|
|
|
gfx::ScopedNSDisableScreenUpdates disabler;
|
|
|
|
[devToolsView removeFromSuperview];
|
|
|
|
[self adjustSubviews];
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-02 08:37:05 +00:00
|
|
|
if (visible) {
|
2014-07-02 08:21:47 +00:00
|
|
|
[devtools_window_ makeKeyAndOrderFront:nil];
|
2014-07-02 08:37:05 +00:00
|
|
|
} else {
|
|
|
|
[[self window] makeKeyAndOrderFront:nil];
|
2014-11-03 14:12:33 +00:00
|
|
|
[devtools_window_ setDelegate:nil];
|
2014-11-03 14:05:16 +00:00
|
|
|
[devtools_window_ close];
|
2014-07-02 08:21:47 +00:00
|
|
|
devtools_window_.reset();
|
2014-07-02 08:37:05 +00:00
|
|
|
}
|
2014-07-02 08:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-01 12:57:49 +00:00
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
- (BOOL)isDevToolsVisible {
|
|
|
|
return devtools_visible_;
|
|
|
|
}
|
|
|
|
|
2015-09-15 03:04:46 +00:00
|
|
|
- (BOOL)isDevToolsFocused {
|
|
|
|
if (devtools_docked_) {
|
|
|
|
return [[self window] isKeyWindow] && devtools_is_first_responder_;
|
|
|
|
} else {
|
|
|
|
return [devtools_window_ isKeyWindow];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
- (void)setIsDocked:(BOOL)docked {
|
|
|
|
// Revert to no-devtools state.
|
|
|
|
[self setDevToolsVisible:NO];
|
|
|
|
|
|
|
|
// Switch to new state.
|
|
|
|
devtools_docked_ = docked;
|
|
|
|
if (!docked) {
|
2015-06-05 06:33:37 +00:00
|
|
|
auto inspectable_web_contents = inspectableWebContentsView_->inspectable_web_contents();
|
|
|
|
auto devToolsWebContents = inspectable_web_contents->GetDevToolsWebContents();
|
2014-07-28 01:34:13 +00:00
|
|
|
auto devToolsView = devToolsWebContents->GetNativeView();
|
2014-07-01 12:57:49 +00:00
|
|
|
|
|
|
|
auto styleMask = NSTitledWindowMask | NSClosableWindowMask |
|
|
|
|
NSMiniaturizableWindowMask | NSResizableWindowMask |
|
|
|
|
NSTexturedBackgroundWindowMask |
|
|
|
|
NSUnifiedTitleAndToolbarWindowMask;
|
|
|
|
devtools_window_.reset([[UnderlayOpenGLHostingWindow alloc]
|
|
|
|
initWithContentRect:NSMakeRect(0, 0, 800, 600)
|
|
|
|
styleMask:styleMask
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES]);
|
|
|
|
[devtools_window_ setDelegate:self];
|
2014-07-04 13:34:47 +00:00
|
|
|
[devtools_window_ setFrameAutosaveName:@"brightray.devtools"];
|
2014-07-01 12:57:49 +00:00
|
|
|
[devtools_window_ setTitle:@"Developer Tools"];
|
|
|
|
[devtools_window_ setReleasedWhenClosed:NO];
|
|
|
|
[devtools_window_ setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge];
|
|
|
|
[devtools_window_ setContentBorderThickness:24 forEdge:NSMaxYEdge];
|
|
|
|
|
|
|
|
NSView* contentView = [devtools_window_ contentView];
|
|
|
|
devToolsView.frame = contentView.bounds;
|
|
|
|
devToolsView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
|
|
|
|
|
|
[contentView addSubview:devToolsView];
|
2013-03-27 16:15:46 +00:00
|
|
|
}
|
2014-07-02 08:21:47 +00:00
|
|
|
[self setDevToolsVisible:YES];
|
|
|
|
}
|
2013-03-27 16:15:46 +00:00
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
- (void)setContentsResizingStrategy:(const DevToolsContentsResizingStrategy&)strategy {
|
|
|
|
strategy_.CopyFrom(strategy);
|
|
|
|
[self adjustSubviews];
|
2013-11-05 02:29:53 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
- (void)adjustSubviews {
|
|
|
|
if (![[self subviews] count])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (![self isDevToolsVisible] || devtools_window_) {
|
|
|
|
DCHECK_EQ(1u, [[self subviews] count]);
|
|
|
|
NSView* contents = [[self subviews] objectAtIndex:0];
|
|
|
|
[contents setFrame:[self bounds]];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSView* devToolsView = [[self subviews] objectAtIndex:0];
|
|
|
|
NSView* contentsView = [[self subviews] objectAtIndex:1];
|
|
|
|
|
|
|
|
DCHECK_EQ(2u, [[self subviews] count]);
|
|
|
|
|
|
|
|
gfx::Rect new_devtools_bounds;
|
|
|
|
gfx::Rect new_contents_bounds;
|
|
|
|
ApplyDevToolsContentsResizingStrategy(
|
|
|
|
strategy_, gfx::Size(NSSizeToCGSize([self bounds].size)),
|
|
|
|
&new_devtools_bounds, &new_contents_bounds);
|
|
|
|
[devToolsView setFrame:[self flipRectToNSRect:new_devtools_bounds]];
|
|
|
|
[contentsView setFrame:[self flipRectToNSRect:new_contents_bounds]];
|
2013-03-27 16:15:46 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 12:55:07 +00:00
|
|
|
- (void)setTitle:(NSString*)title {
|
|
|
|
[devtools_window_ setTitle:title];
|
2015-08-07 09:25:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 03:04:46 +00:00
|
|
|
- (void)viewDidBecomeFirstResponder:(NSNotification*)notification {
|
|
|
|
auto inspectable_web_contents = inspectableWebContentsView_->inspectable_web_contents();
|
|
|
|
if (!inspectable_web_contents)
|
|
|
|
return;
|
|
|
|
auto webContents = inspectable_web_contents->GetWebContents();
|
|
|
|
auto webContentsView = webContents->GetNativeView();
|
|
|
|
|
|
|
|
NSView* view = [notification object];
|
|
|
|
if ([[webContentsView subviews] containsObject:view]) {
|
|
|
|
devtools_is_first_responder_ = NO;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto devToolsWebContents = inspectable_web_contents->GetDevToolsWebContents();
|
|
|
|
if (!devToolsWebContents)
|
|
|
|
return;
|
|
|
|
auto devToolsView = devToolsWebContents->GetNativeView();
|
|
|
|
|
|
|
|
if ([[devToolsView subviews] containsObject:view]) {
|
|
|
|
devtools_is_first_responder_ = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 12:57:49 +00:00
|
|
|
#pragma mark - NSWindowDelegate
|
2013-04-09 19:11:16 +00:00
|
|
|
|
2014-07-01 12:57:49 +00:00
|
|
|
- (void)windowWillClose:(NSNotification*)notification {
|
2014-07-02 08:21:47 +00:00
|
|
|
inspectableWebContentsView_->inspectable_web_contents()->CloseDevTools();
|
2013-04-09 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 09:54:37 +00:00
|
|
|
- (void)windowDidBecomeMain:(NSNotification*)notification {
|
|
|
|
content::WebContents* web_contents =
|
2015-06-05 06:33:37 +00:00
|
|
|
inspectableWebContentsView_->inspectable_web_contents()->GetDevToolsWebContents();
|
2014-09-09 09:54:37 +00:00
|
|
|
if (!web_contents)
|
|
|
|
return;
|
|
|
|
|
|
|
|
web_contents->RestoreFocus();
|
|
|
|
|
|
|
|
content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
|
|
|
|
if (rwhv)
|
|
|
|
rwhv->SetActive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidResignMain:(NSNotification*)notification {
|
|
|
|
content::WebContents* web_contents =
|
2015-06-05 06:33:37 +00:00
|
|
|
inspectableWebContentsView_->inspectable_web_contents()->GetDevToolsWebContents();
|
2014-09-09 09:54:37 +00:00
|
|
|
if (!web_contents)
|
|
|
|
return;
|
|
|
|
|
|
|
|
web_contents->StoreFocus();
|
|
|
|
|
|
|
|
content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
|
|
|
|
if (rwhv)
|
|
|
|
rwhv->SetActive(false);
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
@end
|