OLD | NEW |
(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, |
| 100 int available_width) const { |
| 101 gfx::Rect bounds = GetBoundsForTabStripAndAvatarArea( |
| 102 tabstrip_preferred_size, available_width); |
| 103 int space_left_of_tabstrip = kTabStripIndent; |
| 104 if (delegate_->ShouldShowAvatar()) { |
| 105 if (avatar_label_ && avatar_label_->bounds().width()) { |
| 106 // Space between the right edge of the avatar label and the tabstrip. |
| 107 const int kAvatarLabelRightSpacing = -10; |
| 108 space_left_of_tabstrip = |
| 109 avatar_label_->bounds().right() + kAvatarLabelRightSpacing; |
| 110 } else { |
| 111 space_left_of_tabstrip = |
| 112 kAvatarLeftSpacing + avatar_bounds_.width() + |
| 113 kAvatarRightSpacing; |
| 114 } |
| 115 } |
| 116 bounds.Inset(space_left_of_tabstrip, 0, 0, 0); |
| 117 return bounds; |
| 118 } |
| 119 |
| 120 gfx::Rect OpaqueBrowserFrameViewLayout::GetBoundsForTabStripAndAvatarArea( |
| 121 const gfx::Size& tabstrip_preferred_size, |
| 122 int available_width) const { |
| 123 if (minimize_button_) { |
| 124 available_width = minimize_button_->x(); |
| 125 } else if (delegate_->GetAdditionalReservedSpaceInTabStrip()) { |
| 126 available_width -= delegate_->GetAdditionalReservedSpaceInTabStrip(); |
| 127 } |
| 128 const int caption_spacing = delegate_->IsMaximized() ? |
| 129 kNewTabCaptionMaximizedSpacing : kNewTabCaptionRestoredSpacing; |
| 130 const int tabstrip_x = NonClientBorderThickness(); |
| 131 const int tabstrip_width = available_width - tabstrip_x - caption_spacing; |
| 132 return gfx::Rect(tabstrip_x, GetTabStripInsetsTop(false), |
| 133 std::max(0, tabstrip_width), |
| 134 tabstrip_preferred_size.height()); |
| 135 } |
| 136 |
| 137 gfx::Size OpaqueBrowserFrameViewLayout::GetMinimumSize( |
| 138 int available_width) const { |
| 139 gfx::Size min_size = delegate_->GetBrowserViewMinimumSize(); |
| 140 int border_thickness = NonClientBorderThickness(); |
| 141 min_size.Enlarge(2 * border_thickness, |
| 142 NonClientTopBorderHeight(false) + border_thickness); |
| 143 |
| 144 int min_titlebar_width = (2 * FrameBorderThickness(false)) + |
| 145 kIconLeftSpacing + |
| 146 (delegate_->ShouldShowWindowIcon() ? |
| 147 (delegate_->GetIconSize() + kTitleLogoSpacing) : 0); |
| 148 if (ShouldAddDefaultCaptionButtons()) { |
| 149 min_titlebar_width += |
| 150 minimize_button_->GetMinimumSize().width() + |
| 151 restore_button_->GetMinimumSize().width() + |
| 152 close_button_->GetMinimumSize().width(); |
| 153 } |
| 154 min_size.set_width(std::max(min_size.width(), min_titlebar_width)); |
| 155 |
| 156 // Ensure that the minimum width is enough to hold a minimum width tab strip |
| 157 // and avatar icon at their usual insets. |
| 158 if (delegate_->IsTabStripVisible()) { |
| 159 gfx::Size preferred_size = delegate_->GetTabstripPreferredSize(); |
| 160 const int min_tabstrip_width = preferred_size.width(); |
| 161 const int min_tabstrip_area_width = |
| 162 available_width - |
| 163 GetBoundsForTabStripAndAvatarArea( |
| 164 preferred_size, available_width).width() + |
| 165 min_tabstrip_width + delegate_->GetOTRAvatarIcon().width() + |
| 166 kAvatarLeftSpacing + kAvatarRightSpacing; |
| 167 min_size.set_width(std::max(min_size.width(), min_tabstrip_area_width)); |
| 168 } |
| 169 |
| 170 return min_size; |
| 171 } |
| 172 |
| 173 gfx::Rect OpaqueBrowserFrameViewLayout::GetWindowBoundsForClientBounds( |
| 174 const gfx::Rect& client_bounds) const { |
| 175 int top_height = NonClientTopBorderHeight(false); |
| 176 int border_thickness = NonClientBorderThickness(); |
| 177 return gfx::Rect(std::max(0, client_bounds.x() - border_thickness), |
| 178 std::max(0, client_bounds.y() - top_height), |
| 179 client_bounds.width() + (2 * border_thickness), |
| 180 client_bounds.height() + top_height + border_thickness); |
| 181 } |
| 182 |
| 183 int OpaqueBrowserFrameViewLayout::FrameBorderThickness(bool restored) const { |
| 184 return (!restored && (delegate_->IsMaximized() || |
| 185 delegate_->IsFullscreen())) ? |
| 186 0 : kFrameBorderThickness; |
| 187 } |
| 188 |
| 189 int OpaqueBrowserFrameViewLayout::NonClientBorderThickness() const { |
| 190 // When we fill the screen, we don't show a client edge. |
| 191 return FrameBorderThickness(false) + |
| 192 ((delegate_->IsMaximized() || delegate_->IsFullscreen()) ? |
| 193 0 : views::NonClientFrameView::kClientEdgeThickness); |
| 194 } |
| 195 |
| 196 int OpaqueBrowserFrameViewLayout::NonClientTopBorderHeight( |
| 197 bool restored) const { |
| 198 if (delegate_->ShouldShowWindowTitle()) { |
| 199 return std::max(FrameBorderThickness(restored) + delegate_->GetIconSize(), |
| 200 CaptionButtonY(restored) + kCaptionButtonHeightWithPadding) + |
| 201 TitlebarBottomThickness(restored); |
| 202 } |
| 203 |
| 204 return FrameBorderThickness(restored) - |
| 205 ((delegate_->IsTabStripVisible() && |
| 206 !restored && !delegate_->ShouldLeaveOffsetNearTopBorder()) |
| 207 ? kTabstripTopShadowThickness : 0); |
| 208 } |
| 209 |
| 210 int OpaqueBrowserFrameViewLayout::GetTabStripInsetsTop(bool restored) const { |
| 211 return NonClientTopBorderHeight(restored) + ((!restored && |
| 212 (!delegate_->ShouldLeaveOffsetNearTopBorder() || |
| 213 delegate_->IsFullscreen())) ? |
| 214 0 : kNonClientRestoredExtraThickness); |
| 215 } |
| 216 |
| 217 int OpaqueBrowserFrameViewLayout::TitlebarBottomThickness(bool restored) const { |
| 218 return kTitlebarTopAndBottomEdgeThickness + |
| 219 ((!restored && delegate_->IsMaximized()) ? 0 : |
| 220 views::NonClientFrameView::kClientEdgeThickness); |
| 221 } |
| 222 |
| 223 int OpaqueBrowserFrameViewLayout::CaptionButtonY(bool restored) const { |
| 224 // Maximized buttons start at window top so that even if their images aren't |
| 225 // drawn flush with the screen edge, they still obey Fitts' Law. |
| 226 return (!restored && delegate_->IsMaximized()) ? |
| 227 FrameBorderThickness(false) : |
| 228 views::NonClientFrameView::kFrameShadowThickness; |
| 229 } |
| 230 |
| 231 gfx::Rect OpaqueBrowserFrameViewLayout::IconBounds() const { |
| 232 int size = delegate_->GetIconSize(); |
| 233 int frame_thickness = FrameBorderThickness(false); |
| 234 int y; |
| 235 if (delegate_->ShouldShowWindowIcon() || |
| 236 delegate_->ShouldShowWindowTitle()) { |
| 237 // Our frame border has a different "3D look" than Windows'. Theirs has a |
| 238 // more complex gradient on the top that they push their icon/title below; |
| 239 // then the maximized window cuts this off and the icon/title are centered |
| 240 // in the remaining space. Because the apparent shape of our border is |
| 241 // simpler, using the same positioning makes things look slightly uncentered |
| 242 // with restored windows, so when the window is restored, instead of |
| 243 // calculating the remaining space from below the frame border, we calculate |
| 244 // from below the 3D edge. |
| 245 int unavailable_px_at_top = delegate_->IsMaximized() ? |
| 246 frame_thickness : kTitlebarTopAndBottomEdgeThickness; |
| 247 // When the icon is shorter than the minimum space we reserve for the |
| 248 // caption button, we vertically center it. We want to bias rounding to put |
| 249 // extra space above the icon, since the 3D edge (+ client edge, for |
| 250 // restored windows) below looks (to the eye) more like additional space |
| 251 // than does the 3D edge (or nothing at all, for maximized windows) above; |
| 252 // hence the +1. |
| 253 y = unavailable_px_at_top + (NonClientTopBorderHeight(false) - |
| 254 unavailable_px_at_top - size - TitlebarBottomThickness(false) + 1) / 2; |
| 255 } else { |
| 256 // For "browser mode" windows, we use the native positioning, which is just |
| 257 // below the top frame border. |
| 258 y = frame_thickness; |
| 259 } |
| 260 return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size); |
| 261 } |
| 262 |
| 263 gfx::Rect OpaqueBrowserFrameViewLayout::CalculateClientAreaBounds( |
| 264 int width, |
| 265 int height) const { |
| 266 int top_height = NonClientTopBorderHeight(false); |
| 267 int border_thickness = NonClientBorderThickness(); |
| 268 return gfx::Rect(border_thickness, top_height, |
| 269 std::max(0, width - (2 * border_thickness)), |
| 270 std::max(0, height - top_height - border_thickness)); |
| 271 } |
| 272 |
| 273 /////////////////////////////////////////////////////////////////////////////// |
| 274 // OpaqueBrowserFrameView, private: |
| 275 |
| 276 void OpaqueBrowserFrameViewLayout::LayoutWindowControls(views::View* host) { |
| 277 if (!ShouldAddDefaultCaptionButtons()) |
| 278 return; |
| 279 bool is_maximized = delegate_->IsMaximized(); |
| 280 close_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT, |
| 281 views::ImageButton::ALIGN_BOTTOM); |
| 282 int caption_y = CaptionButtonY(false); |
| 283 // There should always be the same number of non-shadow pixels visible to the |
| 284 // side of the caption buttons. In maximized mode we extend the rightmost |
| 285 // button to the screen corner to obey Fitts' Law. |
| 286 int right_extra_width = is_maximized ? |
| 287 (kFrameBorderThickness - |
| 288 views::NonClientFrameView::kFrameShadowThickness) : 0; |
| 289 gfx::Size close_button_size = close_button_->GetPreferredSize(); |
| 290 close_button_->SetBounds(host->width() - FrameBorderThickness(false) - |
| 291 right_extra_width - close_button_size.width(), caption_y, |
| 292 close_button_size.width() + right_extra_width, |
| 293 close_button_size.height()); |
| 294 |
| 295 // When the window is restored, we show a maximized button; otherwise, we show |
| 296 // a restore button. |
| 297 bool is_restored = !is_maximized && !delegate_->IsMinimized(); |
| 298 views::ImageButton* invisible_button = is_restored ? |
| 299 restore_button_ : maximize_button_; |
| 300 invisible_button->SetVisible(false); |
| 301 |
| 302 views::ImageButton* visible_button = is_restored ? |
| 303 maximize_button_ : restore_button_; |
| 304 visible_button->SetVisible(true); |
| 305 visible_button->SetImageAlignment(views::ImageButton::ALIGN_LEFT, |
| 306 views::ImageButton::ALIGN_BOTTOM); |
| 307 gfx::Size visible_button_size = visible_button->GetPreferredSize(); |
| 308 visible_button->SetBounds(close_button_->x() - visible_button_size.width(), |
| 309 caption_y, visible_button_size.width(), |
| 310 visible_button_size.height()); |
| 311 |
| 312 minimize_button_->SetVisible(true); |
| 313 minimize_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT, |
| 314 views::ImageButton::ALIGN_BOTTOM); |
| 315 gfx::Size minimize_button_size = minimize_button_->GetPreferredSize(); |
| 316 minimize_button_->SetBounds( |
| 317 visible_button->x() - minimize_button_size.width(), caption_y, |
| 318 minimize_button_size.width(), |
| 319 minimize_button_size.height()); |
| 320 } |
| 321 |
| 322 void OpaqueBrowserFrameViewLayout::LayoutTitleBar() { |
| 323 gfx::Rect icon_bounds(IconBounds()); |
| 324 if (delegate_->ShouldShowWindowIcon() && window_icon_) |
| 325 window_icon_->SetBoundsRect(icon_bounds); |
| 326 |
| 327 if (window_title_) { |
| 328 bool should_show = delegate_->ShouldShowWindowTitle(); |
| 329 window_title_->SetVisible(should_show); |
| 330 |
| 331 if (should_show) { |
| 332 window_title_->SetText(delegate_->GetWindowTitle()); |
| 333 const int title_x = delegate_->ShouldShowWindowIcon() ? |
| 334 icon_bounds.right() + kIconTitleSpacing : icon_bounds.x(); |
| 335 window_title_->SetBounds(title_x, icon_bounds.y(), |
| 336 std::max(0, minimize_button_->x() - kTitleLogoSpacing - title_x), |
| 337 icon_bounds.height()); |
| 338 } |
| 339 } |
| 340 } |
| 341 |
| 342 void OpaqueBrowserFrameViewLayout::LayoutAvatar() { |
| 343 // Even though the avatar is used for both incognito and profiles we always |
| 344 // use the incognito icon to layout the avatar button. The profile icon |
| 345 // can be customized so we can't depend on its size to perform layout. |
| 346 gfx::ImageSkia incognito_icon = delegate_->GetOTRAvatarIcon(); |
| 347 |
| 348 int avatar_bottom = GetTabStripInsetsTop(false) + |
| 349 delegate_->GetTabStripHeight() - kAvatarBottomSpacing; |
| 350 int avatar_restored_y = avatar_bottom - incognito_icon.height(); |
| 351 int avatar_y = delegate_->IsMaximized() ? |
| 352 (NonClientTopBorderHeight(false) + kTabstripTopShadowThickness) : |
| 353 avatar_restored_y; |
| 354 avatar_bounds_.SetRect(NonClientBorderThickness() + kAvatarLeftSpacing, |
| 355 avatar_y, incognito_icon.width(), |
| 356 delegate_->ShouldShowAvatar() ? (avatar_bottom - avatar_y) : 0); |
| 357 if (avatar_button_) |
| 358 avatar_button_->SetBoundsRect(avatar_bounds_); |
| 359 |
| 360 if (avatar_label_) { |
| 361 // Space between the bottom of the avatar and the bottom of the avatar |
| 362 // label. |
| 363 const int kAvatarLabelBottomSpacing = 3; |
| 364 // Space between the frame border and the left edge of the avatar label. |
| 365 const int kAvatarLabelLeftSpacing = -1; |
| 366 gfx::Size label_size = avatar_label_->GetPreferredSize(); |
| 367 gfx::Rect label_bounds( |
| 368 FrameBorderThickness(false) + kAvatarLabelLeftSpacing, |
| 369 avatar_bottom - kAvatarLabelBottomSpacing - label_size.height(), |
| 370 label_size.width(), |
| 371 delegate_->ShouldShowAvatar() ? label_size.height() : 0); |
| 372 avatar_label_->SetBoundsRect(label_bounds); |
| 373 } |
| 374 } |
| 375 |
| 376 void OpaqueBrowserFrameViewLayout::SetView(int id, views::View* view) { |
| 377 // Why do things this way instead of having an Init() method, where we're |
| 378 // passed the views we'll handle? Because OpaqueBrowserFrameView doesn't own |
| 379 // all the views which are part of it. The avatar stuff, for example, will be |
| 380 // added and removed by the base class of OpaqueBrowserFrameView. |
| 381 switch (id) { |
| 382 case VIEW_ID_MINIMIZE_BUTTON: |
| 383 if (view) { |
| 384 DCHECK_EQ(std::string(views::ImageButton::kViewClassName), |
| 385 view->GetClassName()); |
| 386 } |
| 387 minimize_button_ = static_cast<views::ImageButton*>(view); |
| 388 break; |
| 389 case VIEW_ID_MAXIMIZE_BUTTON: |
| 390 if (view) { |
| 391 DCHECK_EQ(std::string(views::ImageButton::kViewClassName), |
| 392 view->GetClassName()); |
| 393 } |
| 394 maximize_button_ = static_cast<views::ImageButton*>(view); |
| 395 break; |
| 396 case VIEW_ID_RESTORE_BUTTON: |
| 397 if (view) { |
| 398 DCHECK_EQ(std::string(views::ImageButton::kViewClassName), |
| 399 view->GetClassName()); |
| 400 } |
| 401 restore_button_ = static_cast<views::ImageButton*>(view); |
| 402 break; |
| 403 case VIEW_ID_CLOSE_BUTTON: |
| 404 if (view) { |
| 405 DCHECK_EQ(std::string(views::ImageButton::kViewClassName), |
| 406 view->GetClassName()); |
| 407 } |
| 408 close_button_ = static_cast<views::ImageButton*>(view); |
| 409 break; |
| 410 case VIEW_ID_WINDOW_ICON: |
| 411 window_icon_ = view; |
| 412 break; |
| 413 case VIEW_ID_WINDOW_TITLE: |
| 414 if (view) { |
| 415 DCHECK_EQ(std::string(views::Label::kViewClassName), |
| 416 view->GetClassName()); |
| 417 } |
| 418 window_title_ = static_cast<views::Label*>(view); |
| 419 break; |
| 420 case VIEW_ID_AVATAR_LABEL: |
| 421 avatar_label_ = view; |
| 422 break; |
| 423 case VIEW_ID_AVATAR_BUTTON: |
| 424 avatar_button_ = view; |
| 425 break; |
| 426 default: |
| 427 NOTIMPLEMENTED() << "Unknown view id " << id; |
| 428 break; |
| 429 } |
| 430 } |
| 431 |
| 432 /////////////////////////////////////////////////////////////////////////////// |
| 433 // OpaqueBrowserFrameView, views::LayoutManager: |
| 434 |
| 435 void OpaqueBrowserFrameViewLayout::Layout(views::View* host) { |
| 436 LayoutWindowControls(host); |
| 437 LayoutTitleBar(); |
| 438 LayoutAvatar(); |
| 439 |
| 440 client_view_bounds_ = CalculateClientAreaBounds( |
| 441 host->width(), host->height()); |
| 442 } |
| 443 |
| 444 gfx::Size OpaqueBrowserFrameViewLayout::GetPreferredSize(views::View* host) { |
| 445 // This is never used; NonClientView::GetPreferredSize() will be called |
| 446 // instead. |
| 447 NOTREACHED(); |
| 448 return gfx::Size(); |
| 449 } |
| 450 |
| 451 void OpaqueBrowserFrameViewLayout::ViewAdded(views::View* host, |
| 452 views::View* view) { |
| 453 SetView(view->id(), view); |
| 454 } |
| 455 |
| 456 void OpaqueBrowserFrameViewLayout::ViewRemoved(views::View* host, |
| 457 views::View* view) { |
| 458 SetView(view->id(), NULL); |
| 459 } |
OLD | NEW |