summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/dev_tools_controller.mm
blob: 346292e28b8edba18da196acbae5b50ea0b77a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/dev_tools_controller.h"

#include <algorithm>

#include <Cocoa/Cocoa.h>

#include "chrome/browser/browser_process.h"
#include "chrome/browser/debugger/devtools_window.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#import "chrome/browser/ui/cocoa/view_id_util.h"
#include "chrome/common/pref_names.h"
#include "content/browser/tab_contents/tab_contents.h"

namespace {

// Minimal height of devtools pane or content pane when devtools are docked
// to the browser window.
const int kMinDevToolsHeight = 50;
const int kMinDevToolsWidth = 150;
const int kMinContentsSize = 50;

}  // end namespace


@interface DevToolsController (Private)
- (void)showDevToolsContents:(TabContents*)devToolsContents
                 withProfile:(Profile*)profile;
- (void)showDevToolsContainer:(Profile*)profile;
- (void)hideDevToolsContainer:(Profile*)profile;
- (void)resizeDevTools:(CGFloat)size;
@end


@implementation DevToolsController

- (id)initWithDelegate:(id<TabContentsControllerDelegate>)delegate {
  if ((self = [super init])) {
    splitView_.reset([[NSSplitView alloc] initWithFrame:NSZeroRect]);
    [splitView_ setDividerStyle:NSSplitViewDividerStyleThin];
    [splitView_ setVertical:NO];
    [splitView_ setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
    [splitView_ setDelegate:self];

    dockToRight_ = NO;

    contentsController_.reset(
        [[TabContentsController alloc] initWithContents:NULL
                                               delegate:delegate]);
  }
  return self;
}

- (void)dealloc {
  [splitView_ setDelegate:nil];
  [super dealloc];
}

- (NSView*)view {
  return splitView_.get();
}

- (NSSplitView*)splitView {
  return splitView_.get();
}

- (void)updateDevToolsForTabContents:(TabContents*)contents
                         withProfile:(Profile*)profile {
  // Get current devtools content.
  TabContentsWrapper* devToolsTab = contents ?
      DevToolsWindow::GetDevToolsContents(contents) : NULL;
  TabContents* devToolsContents = devToolsTab ?
      devToolsTab->tab_contents() : NULL;

  [self showDevToolsContents:devToolsContents withProfile:profile];
}

- (void)setDockToRight:(BOOL)dockToRight
           withProfile:(Profile*)profile {
  if (dockToRight_ == dockToRight)
    return;

  NSArray* subviews = [splitView_ subviews];
  if ([subviews count] == 2) {
    [self hideDevToolsContainer:profile];
    dockToRight_ = dockToRight;
    [self showDevToolsContainer:profile];
  } else {
    dockToRight_ = dockToRight;
  }
}

- (void)ensureContentsVisible {
  [contentsController_ ensureContentsVisible];
}

- (void)showDevToolsContents:(TabContents*)devToolsContents
                 withProfile:(Profile*)profile {
  [contentsController_ ensureContentsSizeDoesNotChange];

  NSArray* subviews = [splitView_ subviews];
  if (devToolsContents) {
    // |devToolsView| is a TabContentsViewCocoa object, whose ViewID was
    // set to VIEW_ID_TAB_CONTAINER initially, so we need to change it to
    // VIEW_ID_DEV_TOOLS_DOCKED here.
    view_id_util::SetID(devToolsContents->GetNativeView(),
                        VIEW_ID_DEV_TOOLS_DOCKED);
    [self showDevToolsContainer:profile];
  } else {
    if ([subviews count] > 1) {
      [self hideDevToolsContainer:profile];
    }
  }

  [contentsController_ changeTabContents:devToolsContents];
}

- (void)showDevToolsContainer:(Profile*)profile {
  NSArray* subviews = [splitView_ subviews];
  DCHECK_GE([subviews count], 1u);

  CGFloat splitOffset = 0;

  CGFloat contentSize =
      dockToRight_ ? NSWidth([splitView_ frame])
                   : NSHeight([splitView_ frame]);

  if ([subviews count] == 1) {
    // Load the default split offset.
    splitOffset = profile->GetPrefs()->
        GetInteger(prefs::kDevToolsSplitLocation);

    if (splitOffset < 0)
      splitOffset = contentSize * 1 / 3;

    [splitView_ addSubview:[contentsController_ view]];
  } else {
    DCHECK_EQ([subviews count], 2u);
    // If devtools are already visible, keep the current size.
    splitOffset = dockToRight_ ? NSWidth([[subviews objectAtIndex:1] frame])
                               : NSHeight([[subviews objectAtIndex:1] frame]);
  }

  // Make sure |splitOffset| isn't too large or too small.
  CGFloat minSize = dockToRight_ ? kMinDevToolsWidth: kMinDevToolsHeight;
  splitOffset = std::max(minSize, splitOffset);
  splitOffset = std::min(static_cast<CGFloat>(contentSize - kMinContentsSize),
                         splitOffset);

  if (splitOffset < 0)
    splitOffset = contentSize * 1 / 3;

  DCHECK_GE(splitOffset, 0) << "kMinWebHeight needs to be smaller than "
                            << "smallest available tab contents space.";

  [splitView_ setVertical: dockToRight_];
  [self resizeDevTools:splitOffset];
}

- (void)hideDevToolsContainer:(Profile*)profile {
  NSArray* subviews = [splitView_ subviews];
  NSView* oldDevToolsContentsView = [subviews objectAtIndex:1];

  // Store split offset when hiding devtools window only.
  int splitOffset = dockToRight_ ? NSWidth([oldDevToolsContentsView frame])
                                 : NSHeight([oldDevToolsContentsView frame]);
  profile->GetPrefs()->SetInteger(
      prefs::kDevToolsSplitLocation, splitOffset);

  [oldDevToolsContentsView removeFromSuperview];
  [splitView_ adjustSubviews];
}

- (void)resizeDevTools:(CGFloat)size {
  NSArray* subviews = [splitView_ subviews];

  // It seems as if |-setPosition:ofDividerAtIndex:| should do what's needed,
  // but I can't figure out how to use it. Manually resize web and devtools.
  // TODO(alekseys): either make setPosition:ofDividerAtIndex: work or to add a
  // category on NSSplitView to handle manual resizing.
  NSView* webView = [subviews objectAtIndex:0];
  NSRect webFrame = [webView frame];
  NSView* devToolsView = [subviews objectAtIndex:1];
  NSRect devToolsFrame = [devToolsView frame];

  if (dockToRight_)
    devToolsFrame.size.width = size;
  else
    devToolsFrame.size.height = size;

  if (dockToRight_) {
    webFrame.size.width =
        NSWidth([splitView_ frame]) - ([splitView_ dividerThickness] + size);
  } else {
    webFrame.size.height =
        NSHeight([splitView_ frame]) - ([splitView_ dividerThickness] + size);
  }

  [webView setFrame:webFrame];
  [devToolsView setFrame:devToolsFrame];

  [splitView_ adjustSubviews];
}

// NSSplitViewDelegate protocol.
- (BOOL)splitView:(NSSplitView *)splitView
    shouldAdjustSizeOfSubview:(NSView *)subview {
  // Return NO for the devTools view to indicate that it should not be resized
  // automatically. It preserves the height set by the user and also keeps
  // view height the same while changing tabs when one of the tabs shows infobar
  // and others are not.
  if ([[splitView_ subviews] indexOfObject:subview] == 1)
    return NO;
  return YES;
}

@end