Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: chrome/browser/cocoa/tab_contents_controller.mm

Issue 3228003: Sidebar view, implementation for Mac. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/cocoa/tab_contents_controller.h" 5 #import "chrome/browser/cocoa/tab_contents_controller.h"
6 6
7 #include "base/mac_util.h" 7 #include "base/mac_util.h"
8 #include "base/sys_string_conversions.h" 8 #include "base/sys_string_conversions.h"
9 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/bookmarks/bookmark_model.h" 10 #include "chrome/browser/bookmarks/bookmark_model.h"
11 #import "chrome/browser/cocoa/view_id_util.h" 11 #import "chrome/browser/cocoa/view_id_util.h"
12 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/renderer_host/render_view_host.h" 13 #include "chrome/browser/renderer_host/render_view_host.h"
14 #include "chrome/browser/renderer_host/render_widget_host_view.h" 14 #include "chrome/browser/renderer_host/render_widget_host_view.h"
15 #include "chrome/browser/tab_contents/tab_contents.h" 15 #include "chrome/browser/tab_contents/tab_contents.h"
16 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 17
18 // Default offset of the contents splitter in pixels. 18 // Default offset of the contents splitter in pixels.
19 static const int kDefaultContentsSplitOffset = 400; 19 static const int kDefaultContentsSplitOffset = 400;
20 20
21 // Never make the web part of the tab contents smaller than this (needed if the 21 // Never make the web part of the tab contents smaller than this (needed if the
22 // window is only a few pixels high). 22 // window is only a few pixels high/wide).
23 static const int kMinWebHeight = 50; 23 const int kMinWebHeight = 50;
24 const int kMinWebWidth = 50;
24 25
25 26
26 @implementation TabContentsController 27 @implementation TabContentsController
27 28
28 - (id)initWithNibName:(NSString*)name 29 - (id)initWithNibName:(NSString*)name
29 contents:(TabContents*)contents { 30 contents:(TabContents*)contents {
30 if ((self = [super initWithNibName:name 31 if ((self = [super initWithNibName:name
31 bundle:mac_util::MainAppBundle()])) { 32 bundle:mac_util::MainAppBundle()])) {
32 contents_ = contents; 33 contents_ = contents;
33 } 34 }
35 sidebarContents_ = NULL;
34 return self; 36 return self;
35 } 37 }
36 38
37 - (void)dealloc { 39 - (void)dealloc {
38 // make sure our contents have been removed from the window 40 // make sure our contents have been removed from the window
39 [[self view] removeFromSuperview]; 41 [[self view] removeFromSuperview];
40 [super dealloc]; 42 [super dealloc];
41 } 43 }
42 44
43 // Call when the tab view is properly sized and the render widget host view 45 // Call when the tab view is properly sized and the render widget host view
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 // Calling setContentView: here removes any first responder status 79 // Calling setContentView: here removes any first responder status
78 // the view may have, so avoid changing the view hierarchy unless 80 // the view may have, so avoid changing the view hierarchy unless
79 // the view is different. 81 // the view is different.
80 if (contents_ != updatedContents) { 82 if (contents_ != updatedContents) {
81 contents_ = updatedContents; 83 contents_ = updatedContents;
82 [self ensureContentsVisible]; 84 [self ensureContentsVisible];
83 } 85 }
84 } 86 }
85 87
86 - (void)showDevToolsContents:(TabContents*)devToolsContents { 88 - (void)showDevToolsContents:(TabContents*)devToolsContents {
87 NSArray* subviews = [contentsContainer_ subviews]; 89 NSArray* subviews = [devToolsContainer_ subviews];
88 if (devToolsContents) { 90 if (devToolsContents) {
89 DCHECK_GE([subviews count], 1u); 91 DCHECK_GE([subviews count], 1u);
90 92
91 // Load the default split offset. If we are already showing devtools, we 93 // Load the default split offset. If we are already showing devtools, we
92 // will replace the default with the current devtools height. 94 // will replace the default with the current devtools height.
93 CGFloat splitOffset = g_browser_process->local_state()->GetInteger( 95 CGFloat splitOffset = g_browser_process->local_state()->GetInteger(
94 prefs::kDevToolsSplitLocation); 96 prefs::kDevToolsSplitLocation);
95 if (splitOffset == -1) { 97 if (splitOffset == -1) {
96 // Initial load, set to default value. 98 // Initial load, set to default value.
97 splitOffset = kDefaultContentsSplitOffset; 99 splitOffset = kDefaultContentsSplitOffset;
98 } 100 }
99 101
100 // |devtoolsView| is a TabContentsViewCocoa object, whose ViewID was 102 // |devtoolsView| is a TabContentsViewCocoa object, whose ViewID was
101 // set to VIEW_ID_TAB_CONTAINER initially, so we need to change it to 103 // set to VIEW_ID_TAB_CONTAINER initially, so we need to change it to
102 // VIEW_ID_DEV_TOOLS_DOCKED here. 104 // VIEW_ID_DEV_TOOLS_DOCKED here.
103 NSView* devtoolsView = devToolsContents->GetNativeView(); 105 NSView* devtoolsView = devToolsContents->GetNativeView();
104 view_id_util::SetID(devtoolsView, VIEW_ID_DEV_TOOLS_DOCKED); 106 view_id_util::SetID(devtoolsView, VIEW_ID_DEV_TOOLS_DOCKED);
105 if ([subviews count] == 1) { 107 if ([subviews count] == 1) {
106 [contentsContainer_ addSubview:devtoolsView]; 108 [devToolsContainer_ addSubview:devtoolsView];
107 } else { 109 } else {
108 DCHECK_EQ([subviews count], 2u); 110 DCHECK_EQ([subviews count], 2u);
109 [contentsContainer_ replaceSubview:[subviews objectAtIndex:1] 111 [devToolsContainer_ replaceSubview:[subviews objectAtIndex:1]
110 with:devToolsContents->GetNativeView()]; 112 with:devToolsContents->GetNativeView()];
111 // If devtools are already visible, keep the current size. 113 // If devtools are already visible, keep the current size.
112 splitOffset = NSHeight([devtoolsView frame]); 114 splitOffset = NSHeight([devtoolsView frame]);
113 } 115 }
114 116
115 // Make sure |splitOffset| isn't too large or too small. 117 // Make sure |splitOffset| isn't too large or too small.
116 splitOffset = MIN(splitOffset, 118 splitOffset = MIN(splitOffset,
117 NSHeight([contentsContainer_ frame]) - kMinWebHeight); 119 NSHeight([devToolsContainer_ frame]) - kMinWebHeight);
118 DCHECK_GE(splitOffset, 0) << "kMinWebHeight needs to be smaller than " 120 DCHECK_GE(splitOffset, 0) << "kMinWebHeight needs to be smaller than "
119 << "smallest available tab contents space."; 121 << "smallest available tab contents space.";
120 splitOffset = MAX(0, splitOffset); 122 splitOffset = MAX(0, splitOffset);
121 123
122 // It seems as if |-setPosition:ofDividerAtIndex:| should do what's needed, 124 // It seems as if |-setPosition:ofDividerAtIndex:| should do what's needed,
123 // but I can't figure out how to use it. Manually resize web and devtools. 125 // but I can't figure out how to use it. Manually resize web and devtools.
124 NSRect devtoolsFrame = [devtoolsView frame]; 126 NSRect devtoolsFrame = [devtoolsView frame];
125 devtoolsFrame.size.height = splitOffset; 127 devtoolsFrame.size.height = splitOffset;
126 [devtoolsView setFrame:devtoolsFrame]; 128 [devtoolsView setFrame:devtoolsFrame];
127 129
128 NSRect webFrame = [[subviews objectAtIndex:0] frame]; 130 NSRect webFrame = [[subviews objectAtIndex:0] frame];
129 webFrame.size.height = NSHeight([contentsContainer_ frame]) - 131 webFrame.size.height = NSHeight([devToolsContainer_ frame]) -
130 [self devToolsHeight]; 132 [self devToolsHeight];
131 [[subviews objectAtIndex:0] setFrame:webFrame]; 133 [[subviews objectAtIndex:0] setFrame:webFrame];
132 134
133 [contentsContainer_ adjustSubviews]; 135 [devToolsContainer_ adjustSubviews];
134 } else { 136 } else {
135 if ([subviews count] > 1) { 137 if ([subviews count] > 1) {
136 NSView* oldDevToolsContentsView = [subviews objectAtIndex:1]; 138 NSView* oldDevToolsContentsView = [subviews objectAtIndex:1];
137 // Store split offset when hiding devtools window only. 139 // Store split offset when hiding devtools window only.
138 int splitOffset = NSHeight([oldDevToolsContentsView frame]); 140 int splitOffset = NSHeight([oldDevToolsContentsView frame]);
139 g_browser_process->local_state()->SetInteger( 141 g_browser_process->local_state()->SetInteger(
140 prefs::kDevToolsSplitLocation, splitOffset); 142 prefs::kDevToolsSplitLocation, splitOffset);
141 [oldDevToolsContentsView removeFromSuperview]; 143 [oldDevToolsContentsView removeFromSuperview];
142 } 144 }
143 } 145 }
144 } 146 }
145 147
146 - (CGFloat)devToolsHeight { 148 - (CGFloat)devToolsHeight {
147 NSArray* subviews = [contentsContainer_ subviews]; 149 NSArray* subviews = [devToolsContainer_ subviews];
148 if ([subviews count] < 2) 150 if ([subviews count] < 2)
149 return 0; 151 return 0;
150 return NSHeight([[subviews objectAtIndex:1] frame]) + 152 return NSHeight([[subviews objectAtIndex:1] frame]) +
151 [contentsContainer_ dividerThickness]; 153 [devToolsContainer_ dividerThickness];
154 }
155
156 // This function is very similar to showDevToolsContents.
157 // TODO(alekseys): refactor and move both to browser window.
158 // I (alekseys) intend to do it very soon.
159 - (void)showSidebarContents:(TabContents*)sidebarContents {
160 sidebarContents_ = sidebarContents;
161 NSArray* subviews = [contentsContainer_ subviews];
162 if (sidebarContents) {
163 DCHECK_GE([subviews count], 1u);
164
165 // Load the default split offset.
166 CGFloat sidebarWidth = g_browser_process->local_state()->GetInteger(
167 prefs::kExtensionSidebarWidth);
168 if (sidebarWidth == -1) {
169 // Initial load, set to default value.
170 sidebarWidth = NSWidth([contentsContainer_ frame]) / 7;
171 }
172
173 // |sidebarView| is a TabContentsViewCocoa object, whose ViewID was
174 // set to VIEW_ID_TAB_CONTAINER initially, so we need to change it to
175 // VIEW_ID_SIDE_BAR_CONTAINER here.
176 NSView* sidebarView = sidebarContents->GetNativeView();
177 view_id_util::SetID(sidebarView, VIEW_ID_SIDE_BAR_CONTAINER);
178 if ([subviews count] == 1) {
179 [contentsContainer_ addSubview:sidebarView];
180 } else {
181 DCHECK_EQ([subviews count], 2u);
182 sidebarWidth = NSWidth([[subviews objectAtIndex:1] frame]);
183 [contentsContainer_ replaceSubview:[subviews objectAtIndex:1]
184 with:sidebarContents->GetNativeView()];
185 }
186
187 // Make sure |sidebarWidth| isn't too large or too small.
188 sidebarWidth = MIN(sidebarWidth,
189 NSWidth([contentsContainer_ frame]) - kMinWebWidth);
190 DCHECK_GE(sidebarWidth, 0) << "kMinWebWidth needs to be smaller than "
191 << "smallest available tab contents space.";
192 sidebarWidth = MAX(0, sidebarWidth);
193
194 // It seems as if |-setPosition:ofDividerAtIndex:| should do what's needed,
195 // but I can't figure out how to use it. Manually resize web and sidebar.
196 NSRect sidebarFrame = [sidebarView frame];
197 sidebarFrame.size.width = sidebarWidth;
198 [sidebarView setFrame:sidebarFrame];
199
200 NSRect webFrame = [[subviews objectAtIndex:0] frame];
201 webFrame.size.width = NSWidth([contentsContainer_ frame]) -
202 ([contentsContainer_ dividerThickness] + sidebarWidth);
203 [[subviews objectAtIndex:0] setFrame:webFrame];
204
205 [contentsContainer_ adjustSubviews];
206 } else {
207 if ([subviews count] > 1) {
208 NSView* oldSidebarContentsView = [subviews objectAtIndex:1];
209 // Store split offset when hiding sidebar window only.
210 int sidebarWidth = NSWidth([oldSidebarContentsView frame]);
211 g_browser_process->local_state()->SetInteger(
212 prefs::kExtensionSidebarWidth, sidebarWidth);
213 [oldSidebarContentsView removeFromSuperview];
214 }
215 }
216 }
217
218 - (TabContents*)sidebarContents {
219 return sidebarContents_;
152 } 220 }
153 221
154 @end 222 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/tab_contents_controller.h ('k') | chrome/browser/cocoa/tab_strip_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698