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

Side by Side Diff: chrome/browser/ui/views/frame/opaque_browser_frame_view_layout.cc

Issue 23531006: Factor out the layout code from OpaqueBrowserFrameView for testing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More clean, hopefully fix that final chromeos test Created 7 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
(Empty)
1 // Copyright (c) 2013 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.
4
5 #include "chrome/browser/ui/views/frame/opaque_browser_frame_view_layout.h"
6
7 #include "ui/gfx/font.h"
8 #include "ui/views/controls/button/image_button.h"
9 #include "ui/views/controls/label.h"
10
11 #if defined(OS_WIN)
12 #include "win8/util/win8_util.h"
13 #endif // OS_WIN
14
15 namespace {
16
17 // Besides the frame border, there's another 9 px of empty space atop the
18 // window in restored mode, to use to drag the window around.
19 const int kNonClientRestoredExtraThickness = 9;
20
21 // The titlebar never shrinks too short to show the caption button plus some
22 // padding below it.
23 const int kCaptionButtonHeightWithPadding = 19;
24
25 // There is a 5 px gap between the title text and the caption buttons.
26 const int kTitleLogoSpacing = 5;
27
28 // The frame border is only visible in restored mode and is hardcoded to 4 px on
29 // each side regardless of the system window border size.
30 const int kFrameBorderThickness = 4;
31
32 // The titlebar has a 2 px 3D edge along the top and bottom.
33 const int kTitlebarTopAndBottomEdgeThickness = 2;
34
35 // The icon is inset 2 px from the left frame border.
36 const int kIconLeftSpacing = 2;
37
38 // There is a 4 px gap between the icon and the title text.
39 const int kIconTitleSpacing = 4;
40
41 // The avatar ends 2 px above the bottom of the tabstrip (which, given the
42 // way the tabstrip draws its bottom edge, will appear like a 1 px gap to the
43 // user).
44 const int kAvatarBottomSpacing = 2;
45
46 // Space between the frame border and the left edge of the avatar.
47 const int kAvatarLeftSpacing = 2;
48
49 // Space between the right edge of the avatar and the tabstrip.
50 const int kAvatarRightSpacing = -2;
51
52 // In restored mode, the New Tab button isn't at the same height as the caption
53 // buttons, but the space will look cluttered if it actually slides under them,
54 // so we stop it when the gap between the two is down to 5 px.
55 const int kNewTabCaptionRestoredSpacing = 5;
56
57 // In maximized mode, where the New Tab button and the caption buttons are at
58 // similar vertical coordinates, we need to reserve a larger, 16 px gap to avoid
59 // looking too cluttered.
60 const int kNewTabCaptionMaximizedSpacing = 16;
61
62 // The top 3 px of the tabstrip is shadow; in maximized mode we push this off
63 // the top of the screen so the tabs appear flush against the screen edge.
64 const int kTabstripTopShadowThickness = 3;
65
66 // How far to indent the tabstrip from the left side of the screen when there
67 // is no avatar icon.
68 const int kTabStripIndent = -6;
69
70 } // namespace
71
72 ///////////////////////////////////////////////////////////////////////////////
73 // OpaqueBrowserFrameView, public:
74
75 OpaqueBrowserFrameViewLayout::OpaqueBrowserFrameViewLayout(
76 OpaqueBrowserFrameViewLayoutDelegate* delegate)
77 : delegate_(delegate),
78 minimize_button_(NULL),
79 maximize_button_(NULL),
80 restore_button_(NULL),
81 close_button_(NULL),
82 window_icon_(NULL),
83 window_title_(NULL),
84 avatar_label_(NULL),
85 avatar_button_(NULL) {
86 }
87
88 OpaqueBrowserFrameViewLayout::~OpaqueBrowserFrameViewLayout() {}
89
90 // static
91 bool OpaqueBrowserFrameViewLayout::ShouldAddDefaultCaptionButtons() {
92 #if defined(OS_WIN)
93 return !win8::IsSingleWindowMetroMode();
94 #endif // OS_WIN
95 return true;
96 }
97
98 gfx::Rect OpaqueBrowserFrameViewLayout::GetBoundsForTabStrip(
99 const gfx::Size& tabstrip_preferred_size, int available_width) const {
sky 2013/08/30 16:35:18 nit: wrap each param to its own line (here and eve
100 gfx::Rect bounds = GetBoundsForTabStripAndAvatarArea(
101 tabstrip_preferred_size, available_width);
102 int space_left_of_tabstrip = kTabStripIndent;
103 if (delegate_->ShouldShowAvatar()) {
104 if (avatar_label_ && avatar_label_->bounds().width()) {
105 // Space between the right edge of the avatar label and the tabstrip.
106 const int kAvatarLabelRightSpacing = -10;
107 space_left_of_tabstrip =
108 avatar_label_->bounds().right() + kAvatarLabelRightSpacing;
109 } else {
110 space_left_of_tabstrip =
111 kAvatarLeftSpacing + avatar_bounds_.width() +
112 kAvatarRightSpacing;
113 }
114 }
115 bounds.Inset(space_left_of_tabstrip, 0, 0, 0);
116 return bounds;
117 }
118
119 gfx::Rect OpaqueBrowserFrameViewLayout::GetBoundsForTabStripAndAvatarArea(
120 const gfx::Size& tabstrip_preferred_size, int available_width) const {
121 if (minimize_button_) {
122 available_width = minimize_button_->x();
123 } else if (delegate_->GetAdditionalReservedSpaceInTabStrip()) {
124 available_width -= delegate_->GetAdditionalReservedSpaceInTabStrip();
125 }
126 const int caption_spacing = delegate_->IsMaximized() ?
127 kNewTabCaptionMaximizedSpacing : kNewTabCaptionRestoredSpacing;
128 const int tabstrip_x = NonClientBorderThickness();
129 const int tabstrip_width = available_width - tabstrip_x - caption_spacing;
130 return gfx::Rect(tabstrip_x, GetTabStripInsetsTop(false),
131 std::max(0, tabstrip_width),
132 tabstrip_preferred_size.height());
133 }
134
135 gfx::Size OpaqueBrowserFrameViewLayout::GetMinimumSize(
136 int available_width) const {
137 gfx::Size min_size = delegate_->GetBrowserViewMinimumSize();
138 int border_thickness = NonClientBorderThickness();
139 min_size.Enlarge(2 * border_thickness,
140 NonClientTopBorderHeight(false) + border_thickness);
141
142 int min_titlebar_width = (2 * FrameBorderThickness(false)) +
143 kIconLeftSpacing +
144 (delegate_->ShouldShowWindowIcon() ?
145 (delegate_->GetIconSize() + kTitleLogoSpacing) : 0);
146 if (OpaqueBrowserFrameViewLayout::ShouldAddDefaultCaptionButtons()) {
sky 2013/08/30 16:35:18 nit: don't need OpaqueBrowserFrameViewLayout::
147 min_titlebar_width +=
148 minimize_button_->GetMinimumSize().width() +
149 restore_button_->GetMinimumSize().width() +
150 close_button_->GetMinimumSize().width();
151 }
152 min_size.set_width(std::max(min_size.width(), min_titlebar_width));
153
154 // Ensure that the minimum width is enough to hold a minimum width tab strip
155 // and avatar icon at their usual insets.
156 if (delegate_->IsTabStripVisible()) {
157 gfx::Size preferred_size = delegate_->GetTabstripPreferredSize();
158 const int min_tabstrip_width = preferred_size.width();
159 const int min_tabstrip_area_width =
160 available_width -
161 GetBoundsForTabStripAndAvatarArea(
162 preferred_size, available_width).width() +
163 min_tabstrip_width + delegate_->GetOTRAvatarIcon().width() +
164 kAvatarLeftSpacing + kAvatarRightSpacing;
165 min_size.set_width(std::max(min_size.width(), min_tabstrip_area_width));
166 }
167
168 return min_size;
169 }
170
171 gfx::Rect OpaqueBrowserFrameViewLayout::GetWindowBoundsForClientBounds(
172 const gfx::Rect& client_bounds) const {
173 int top_height = NonClientTopBorderHeight(false);
174 int border_thickness = NonClientBorderThickness();
175 return gfx::Rect(std::max(0, client_bounds.x() - border_thickness),
176 std::max(0, client_bounds.y() - top_height),
177 client_bounds.width() + (2 * border_thickness),
178 client_bounds.height() + top_height + border_thickness);
179 }
180
181 int OpaqueBrowserFrameViewLayout::FrameBorderThickness(bool restored) const {
182 return (!restored && (delegate_->IsMaximized() ||
183 delegate_->IsFullscreen())) ?
184 0 : kFrameBorderThickness;
185 }
186
187 int OpaqueBrowserFrameViewLayout::NonClientBorderThickness() const {
188 // When we fill the screen, we don't show a client edge.
189 return FrameBorderThickness(false) +
190 ((delegate_->IsMaximized() || delegate_->IsFullscreen()) ?
191 0 : views::NonClientFrameView::kClientEdgeThickness);
192 }
193
194 int OpaqueBrowserFrameViewLayout::NonClientTopBorderHeight(
195 bool restored) const {
196 if (delegate_->ShouldShowWindowTitle()) {
197 return std::max(FrameBorderThickness(restored) + delegate_->GetIconSize(),
198 CaptionButtonY(restored) + kCaptionButtonHeightWithPadding) +
199 TitlebarBottomThickness(restored);
200 }
201
202 return FrameBorderThickness(restored) -
203 ((delegate_->IsTabStripVisible() &&
204 !restored && !delegate_->ShouldLeaveOffsetNearTopBorder())
205 ? kTabstripTopShadowThickness : 0);
206 }
207
208 int OpaqueBrowserFrameViewLayout::GetTabStripInsetsTop(bool restored) const {
209 return NonClientTopBorderHeight(restored) + ((!restored &&
210 (!delegate_->ShouldLeaveOffsetNearTopBorder() ||
211 delegate_->IsFullscreen())) ?
212 0 : kNonClientRestoredExtraThickness);
213 }
214
215 int OpaqueBrowserFrameViewLayout::TitlebarBottomThickness(bool restored) const {
216 return kTitlebarTopAndBottomEdgeThickness +
217 ((!restored && delegate_->IsMaximized()) ? 0 :
218 views::NonClientFrameView::kClientEdgeThickness);
219 }
220
221 int OpaqueBrowserFrameViewLayout::CaptionButtonY(bool restored) const {
222 // Maximized buttons start at window top so that even if their images aren't
223 // drawn flush with the screen edge, they still obey Fitts' Law.
224 return (!restored && delegate_->IsMaximized()) ?
225 FrameBorderThickness(false) :
226 views::NonClientFrameView::kFrameShadowThickness;
227 }
228
229 gfx::Rect OpaqueBrowserFrameViewLayout::IconBounds() const {
230 int size = delegate_->GetIconSize();
231 int frame_thickness = FrameBorderThickness(false);
232 int y;
233 if (delegate_->ShouldShowWindowIcon() ||
234 delegate_->ShouldShowWindowTitle()) {
235 // Our frame border has a different "3D look" than Windows'. Theirs has a
236 // more complex gradient on the top that they push their icon/title below;
237 // then the maximized window cuts this off and the icon/title are centered
238 // in the remaining space. Because the apparent shape of our border is
239 // simpler, using the same positioning makes things look slightly uncentered
240 // with restored windows, so when the window is restored, instead of
241 // calculating the remaining space from below the frame border, we calculate
242 // from below the 3D edge.
243 int unavailable_px_at_top = delegate_->IsMaximized() ?
244 frame_thickness : kTitlebarTopAndBottomEdgeThickness;
245 // When the icon is shorter than the minimum space we reserve for the
246 // caption button, we vertically center it. We want to bias rounding to put
247 // extra space above the icon, since the 3D edge (+ client edge, for
248 // restored windows) below looks (to the eye) more like additional space
249 // than does the 3D edge (or nothing at all, for maximized windows) above;
250 // hence the +1.
251 y = unavailable_px_at_top + (NonClientTopBorderHeight(false) -
252 unavailable_px_at_top - size - TitlebarBottomThickness(false) + 1) / 2;
253 } else {
254 // For "browser mode" windows, we use the native positioning, which is just
255 // below the top frame border.
256 y = frame_thickness;
257 }
258 return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size);
259 }
260
261 gfx::Rect OpaqueBrowserFrameViewLayout::CalculateClientAreaBounds(
262 int width, int height) const {
263 int top_height = NonClientTopBorderHeight(false);
264 int border_thickness = NonClientBorderThickness();
265 return gfx::Rect(border_thickness, top_height,
266 std::max(0, width - (2 * border_thickness)),
267 std::max(0, height - top_height - border_thickness));
268 }
269
270 ///////////////////////////////////////////////////////////////////////////////
271 // OpaqueBrowserFrameView, private:
272
273 void OpaqueBrowserFrameViewLayout::LayoutWindowControls(views::View* host) {
274 if (!ShouldAddDefaultCaptionButtons())
275 return;
276 bool is_maximized = delegate_->IsMaximized();
277 close_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
278 views::ImageButton::ALIGN_BOTTOM);
279 int caption_y = CaptionButtonY(false);
280 // There should always be the same number of non-shadow pixels visible to the
281 // side of the caption buttons. In maximized mode we extend the rightmost
282 // button to the screen corner to obey Fitts' Law.
283 int right_extra_width = is_maximized ?
284 (kFrameBorderThickness -
285 views::NonClientFrameView::kFrameShadowThickness) : 0;
286 gfx::Size close_button_size = close_button_->GetPreferredSize();
287 close_button_->SetBounds(host->width() - FrameBorderThickness(false) -
288 right_extra_width - close_button_size.width(), caption_y,
289 close_button_size.width() + right_extra_width,
290 close_button_size.height());
291
292 // When the window is restored, we show a maximized button; otherwise, we show
293 // a restore button.
294 bool is_restored = !is_maximized && !delegate_->IsMinimized();
295 views::ImageButton* invisible_button = is_restored ?
296 restore_button_ : maximize_button_;
297 invisible_button->SetVisible(false);
298
299 views::ImageButton* visible_button = is_restored ?
300 maximize_button_ : restore_button_;
301 visible_button->SetVisible(true);
302 visible_button->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
303 views::ImageButton::ALIGN_BOTTOM);
304 gfx::Size visible_button_size = visible_button->GetPreferredSize();
305 visible_button->SetBounds(close_button_->x() - visible_button_size.width(),
306 caption_y, visible_button_size.width(),
307 visible_button_size.height());
308
309 minimize_button_->SetVisible(true);
310 minimize_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
311 views::ImageButton::ALIGN_BOTTOM);
312 gfx::Size minimize_button_size = minimize_button_->GetPreferredSize();
313 minimize_button_->SetBounds(
314 visible_button->x() - minimize_button_size.width(), caption_y,
315 minimize_button_size.width(),
316 minimize_button_size.height());
317 }
318
319 void OpaqueBrowserFrameViewLayout::LayoutTitleBar() {
320 gfx::Rect icon_bounds(IconBounds());
321 if (delegate_->ShouldShowWindowIcon() && window_icon_)
322 window_icon_->SetBoundsRect(icon_bounds);
323
324 if (window_title_) {
325 bool should_show = delegate_->ShouldShowWindowTitle();
326 window_title_->SetVisible(should_show);
327
328 if (should_show) {
329 window_title_->SetText(delegate_->GetWindowTitle());
330 const int title_x = delegate_->ShouldShowWindowIcon() ?
331 icon_bounds.right() + kIconTitleSpacing : icon_bounds.x();
332 window_title_->SetBounds(title_x, icon_bounds.y(),
333 std::max(0, minimize_button_->x() - kTitleLogoSpacing - title_x),
334 icon_bounds.height());
335 }
336 }
337 }
338
339 void OpaqueBrowserFrameViewLayout::LayoutAvatar() {
340 // Even though the avatar is used for both incognito and profiles we always
341 // use the incognito icon to layout the avatar button. The profile icon
342 // can be customized so we can't depend on its size to perform layout.
343 gfx::ImageSkia incognito_icon = delegate_->GetOTRAvatarIcon();
344
345 int avatar_bottom = GetTabStripInsetsTop(false) +
346 delegate_->GetTabStripHeight() - kAvatarBottomSpacing;
347 int avatar_restored_y = avatar_bottom - incognito_icon.height();
348 int avatar_y = delegate_->IsMaximized() ?
349 (NonClientTopBorderHeight(false) + kTabstripTopShadowThickness) :
350 avatar_restored_y;
351 avatar_bounds_.SetRect(NonClientBorderThickness() + kAvatarLeftSpacing,
352 avatar_y, incognito_icon.width(),
353 delegate_->ShouldShowAvatar() ? (avatar_bottom - avatar_y) : 0);
354 if (avatar_button_)
355 avatar_button_->SetBoundsRect(avatar_bounds_);
356
357 if (avatar_label_) {
358 // Space between the bottom of the avatar and the bottom of the avatar
359 // label.
360 const int kAvatarLabelBottomSpacing = 3;
361 // Space between the frame border and the left edge of the avatar label.
362 const int kAvatarLabelLeftSpacing = -1;
363 gfx::Size label_size = avatar_label_->GetPreferredSize();
364 gfx::Rect label_bounds(
365 FrameBorderThickness(false) + kAvatarLabelLeftSpacing,
366 avatar_bottom - kAvatarLabelBottomSpacing - label_size.height(),
367 label_size.width(),
368 delegate_->ShouldShowAvatar() ? label_size.height() : 0);
369 avatar_label_->SetBoundsRect(label_bounds);
370 }
371 }
372
373 void OpaqueBrowserFrameViewLayout::SetView(int id, views::View* view) {
374 // Why do things this way instead of having an Init() method, where we're
375 // passed the views we'll handle? Because OpaqueBrowserFrameView doesn't own
376 // all the views which are part of it. The avatar stuff, for example, will be
377 // added and removed by the base class of OpaqueBrowserFrameView.
378 switch (id) {
379 case VIEW_ID_MINIMIZE_BUTTON:
380 minimize_button_ = static_cast<views::ImageButton*>(view);
381 break;
382 case VIEW_ID_MAXIMIZE_BUTTON:
383 maximize_button_ = static_cast<views::ImageButton*>(view);
384 break;
385 case VIEW_ID_RESTORE_BUTTON:
386 restore_button_ = static_cast<views::ImageButton*>(view);
387 break;
388 case VIEW_ID_CLOSE_BUTTON:
389 close_button_ = static_cast<views::ImageButton*>(view);
sky 2013/08/30 16:35:18 Can you DCHECK on the view class names of the ones
390 break;
391 case VIEW_ID_WINDOW_ICON:
392 window_icon_ = view;
393 break;
394 case VIEW_ID_WINDOW_TITLE:
395 window_title_ = static_cast<views::Label*>(view);
396 break;
397 case VIEW_ID_AVATAR_LABEL:
398 avatar_label_ = view;
399 break;
400 case VIEW_ID_AVATAR_BUTTON:
401 avatar_button_ = view;
402 break;
403 default:
404 NOTIMPLEMENTED() << "Unknown view id " << id;
405 break;
406 }
407 }
408
409 ///////////////////////////////////////////////////////////////////////////////
410 // OpaqueBrowserFrameView, views::LayoutManager:
411
412 void OpaqueBrowserFrameViewLayout::Layout(views::View* host) {
413 LayoutWindowControls(host);
414 LayoutTitleBar();
415 LayoutAvatar();
416
417 client_view_bounds_ = CalculateClientAreaBounds(
418 host->width(), host->height());
419 }
420
421 gfx::Size OpaqueBrowserFrameViewLayout::GetPreferredSize(views::View* host) {
422 // This is never used; NonClientView::GetPreferredSize() will be called
423 // instead.
424 NOTREACHED();
425 return gfx::Size();
426 }
427
428 void OpaqueBrowserFrameViewLayout::ViewAdded(views::View* host,
429 views::View* view) {
430 SetView(view->id(), view);
431 }
432
433 void OpaqueBrowserFrameViewLayout::ViewRemoved(views::View* host,
434 views::View* view) {
435 SetView(view->id(), NULL);
436 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698