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"
|
|
|
|
#include "content/public/browser/web_contents_view.h"
|
2013-11-25 21:28:57 +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;
|
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-02 08:21:47 +00:00
|
|
|
auto contentsView = contents->GetView()->GetNativeView();
|
|
|
|
[contentsView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
|
|
|
|
[self addSubview:contentsView];
|
2013-03-14 13:03:50 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
auto devToolsWebContents = inspectableWebContentsView_->inspectable_web_contents()->devtools_web_contents();
|
|
|
|
auto devToolsView = devToolsWebContents->GetView()->GetNativeView();
|
|
|
|
|
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];
|
|
|
|
[self update];
|
|
|
|
} else {
|
|
|
|
gfx::ScopedNSDisableScreenUpdates disabler;
|
|
|
|
devToolsWebContents->GetView()->RemoveOverlayView();
|
|
|
|
[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-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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setIsDocked:(BOOL)docked {
|
|
|
|
// Revert to no-devtools state.
|
|
|
|
[self setDevToolsVisible:NO];
|
|
|
|
|
|
|
|
// Switch to new state.
|
|
|
|
devtools_docked_ = docked;
|
|
|
|
if (!docked) {
|
2014-07-01 12:57:49 +00:00
|
|
|
auto devToolsWebContents = inspectableWebContentsView_->inspectable_web_contents()->devtools_web_contents();
|
|
|
|
auto devToolsView = devToolsWebContents->GetView()->GetNativeView();
|
|
|
|
|
|
|
|
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];
|
|
|
|
[devtools_window_ setFrameAutosaveName:@"brightray.developer.tools"];
|
|
|
|
[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 update];
|
2013-03-14 13:03:50 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 08:21:47 +00:00
|
|
|
- (void)update {
|
|
|
|
if (!devtools_docked_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto contents = inspectableWebContentsView_->inspectable_web_contents()->GetWebContents();
|
|
|
|
auto devToolsWebContents = inspectableWebContentsView_->inspectable_web_contents()->devtools_web_contents();
|
|
|
|
|
|
|
|
gfx::ScopedNSDisableScreenUpdates disabler;
|
|
|
|
devToolsWebContents->GetView()->SetOverlayView(
|
|
|
|
contents->GetView(),
|
|
|
|
gfx::Point(strategy_.insets().left(), strategy_.insets().top()));
|
|
|
|
[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)),
|
|
|
|
[self flipNSRectToRect:[devToolsView bounds]],
|
|
|
|
[self flipNSRectToRect:[contentsView bounds]],
|
|
|
|
&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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
@end
|