1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/dev_tools_controller.h"
10 #include <Cocoa/Cocoa.h>
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h"
15 #import "chrome/browser/ui/cocoa/view_id_util.h"
16 #include "chrome/common/pref_names.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_view.h"
19 #include "ui/base/cocoa/focus_tracker.h"
20 #include "ui/gfx/size_conversions.h"
22 using content::WebContents;
24 @interface DevToolsContainerView : NSView {
25 gfx::Insets contentsInsets_;
27 // Weak references. Ownership via -subviews.
28 NSView* devToolsView_;
29 NSView* contentsView_;
32 - (void)setContentsInsets:(const gfx::Insets&)insets;
33 - (void)adjustSubviews;
34 - (void)showDevTools:(NSView*)devToolsView;
40 @implementation DevToolsContainerView
42 - (void)setContentsInsets:(const gfx::Insets&)insets {
43 contentsInsets_ = insets;
46 - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
47 [self adjustSubviews];
50 - (void)showDevTools:(NSView*)devToolsView {
51 NSArray* subviews = [self subviews];
52 DCHECK_EQ(1u, [subviews count]);
53 contentsView_ = [subviews objectAtIndex:0];
54 devToolsView_ = devToolsView;
55 // Place DevTools under contents.
56 [self addSubview:devToolsView positioned:NSWindowBelow relativeTo:nil];
59 - (void)hideDevTools {
60 DCHECK_EQ(2u, [[self subviews] count]);
61 [devToolsView_ removeFromSuperview];
66 - (void)adjustSubviews {
67 if (![[self subviews] count])
71 DCHECK_EQ(1u, [[self subviews] count]);
72 NSView* contents = [[self subviews] objectAtIndex:0];
73 [contents setFrame:[self bounds]];
77 DCHECK_EQ(2u, [[self subviews] count]);
78 NSRect bounds = [self bounds];
80 [devToolsView_ setFrame:bounds];
82 CGFloat width = std::max(static_cast<CGFloat>(0),
83 NSWidth(bounds) - contentsInsets_.width());
84 CGFloat height = std::max(static_cast<CGFloat>(0),
85 NSHeight(bounds) - contentsInsets_.height());
86 CGFloat left = std::min(static_cast<CGFloat>(contentsInsets_.left()),
88 // Flip top and bottom for NSView geometry.
89 CGFloat top = std::min(static_cast<CGFloat>(contentsInsets_.bottom()),
91 [contentsView_ setFrame:NSMakeRect(left, top, width, height)];
96 @interface DevToolsController (Private)
97 - (void)showDevToolsView;
98 - (void)hideDevToolsView;
102 @implementation DevToolsController
105 if ((self = [super init])) {
106 devToolsContainerView_.reset(
107 [[DevToolsContainerView alloc] initWithFrame:NSZeroRect]);
108 [devToolsContainerView_
109 setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
115 return devToolsContainerView_.get();
118 - (void)updateDevToolsForWebContents:(WebContents*)contents
119 withProfile:(Profile*)profile {
120 DevToolsWindow* newDevToolsWindow = contents ?
121 DevToolsWindow::GetDockedInstanceForInspectedTab(contents) : NULL;
123 bool shouldHide = devToolsWindow_ && devToolsWindow_ != newDevToolsWindow;
124 bool shouldShow = newDevToolsWindow && devToolsWindow_ != newDevToolsWindow;
127 [self hideDevToolsView];
129 devToolsWindow_ = newDevToolsWindow;
130 if (devToolsWindow_) {
131 gfx::Insets insets = devToolsWindow_->GetContentsInsets();
132 devToolsWindow_->web_contents()->GetView()->SetOverlayView(
133 contents->GetView(), gfx::Point(insets.left(), insets.top()));
134 [devToolsContainerView_ setContentsInsets:insets];
136 gfx::Insets zeroInsets;
137 [devToolsContainerView_ setContentsInsets:zeroInsets];
141 [self showDevToolsView];
143 [devToolsContainerView_ adjustSubviews];
144 if (shouldHide || shouldShow)
145 [[devToolsContainerView_ window] disableScreenUpdatesUntilFlush];
148 - (void)showDevToolsView {
150 [[FocusTracker alloc] initWithWindow:[devToolsContainerView_ window]]);
152 // |devToolsView| is a WebContentsViewCocoa object, whose ViewID was
153 // set to VIEW_ID_TAB_CONTAINER initially, so we need to change it to
154 // VIEW_ID_DEV_TOOLS_DOCKED here.
155 NSView* devToolsView =
156 devToolsWindow_->web_contents()->GetView()->GetNativeView();
157 view_id_util::SetID(devToolsView, VIEW_ID_DEV_TOOLS_DOCKED);
159 [devToolsContainerView_ showDevTools:devToolsView];
162 - (void)hideDevToolsView {
163 devToolsWindow_->web_contents()->GetView()->RemoveOverlayView();
164 [devToolsContainerView_ hideDevTools];
165 [focusTracker_ restoreFocusInWindow:[devToolsContainerView_ window]];
166 focusTracker_.reset();