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

Side by Side Diff: ui/views/window/custom_frame_view.cc

Issue 296283003: Re-Reland Linux Window Control Alignment (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Build Error Created 6 years, 7 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
« no previous file with comments | « ui/views/window/custom_frame_view.h ('k') | ui/views/window/custom_frame_view_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #include "ui/views/window/custom_frame_view.h" 5 #include "ui/views/window/custom_frame_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector>
8 9
9 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
10 #include "grit/ui_resources.h" 11 #include "grit/ui_resources.h"
11 #include "grit/ui_strings.h" 12 #include "grit/ui_strings.h"
12 #include "ui/base/hit_test.h" 13 #include "ui/base/hit_test.h"
13 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/canvas.h" 16 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/font.h" 17 #include "ui/gfx/font.h"
17 #include "ui/gfx/image/image.h" 18 #include "ui/gfx/image/image.h"
18 #include "ui/gfx/path.h" 19 #include "ui/gfx/path.h"
20 #include "ui/gfx/rect.h"
19 #include "ui/views/color_constants.h" 21 #include "ui/views/color_constants.h"
20 #include "ui/views/controls/button/image_button.h" 22 #include "ui/views/controls/button/image_button.h"
21 #include "ui/views/views_delegate.h" 23 #include "ui/views/views_delegate.h"
22 #include "ui/views/widget/native_widget_private.h" 24 #include "ui/views/widget/native_widget_private.h"
23 #include "ui/views/widget/widget.h" 25 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h" 26 #include "ui/views/widget/widget_delegate.h"
25 #include "ui/views/window/client_view.h" 27 #include "ui/views/window/client_view.h"
26 #include "ui/views/window/frame_background.h" 28 #include "ui/views/window/frame_background.h"
29 #include "ui/views/window/window_button_order_provider.h"
27 #include "ui/views/window/window_resources.h" 30 #include "ui/views/window/window_resources.h"
28 #include "ui/views/window/window_shape.h" 31 #include "ui/views/window/window_shape.h"
29 32
30 namespace views { 33 namespace views {
31 34
32 namespace { 35 namespace {
33 36
34 // The frame border is only visible in restored mode and is hardcoded to 4 px on 37 // The frame border is only visible in restored mode and is hardcoded to 4 px on
35 // each side regardless of the system window border size. 38 // each side regardless of the system window border size.
36 const int kFrameBorderThickness = 4; 39 const int kFrameBorderThickness = 4;
(...skipping 23 matching lines...) Expand all
60 const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201); 63 const SkColor kDefaultColorFrame = SkColorSetRGB(66, 116, 201);
61 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228); 64 const SkColor kDefaultColorFrameInactive = SkColorSetRGB(161, 182, 228);
62 #endif 65 #endif
63 66
64 const gfx::FontList& GetTitleFontList() { 67 const gfx::FontList& GetTitleFontList() {
65 static const gfx::FontList title_font_list = 68 static const gfx::FontList title_font_list =
66 internal::NativeWidgetPrivate::GetWindowTitleFontList(); 69 internal::NativeWidgetPrivate::GetWindowTitleFontList();
67 return title_font_list; 70 return title_font_list;
68 } 71 }
69 72
73 void LayoutButton(ImageButton* button, const gfx::Rect& bounds) {
74 button->SetVisible(true);
75 button->SetImageAlignment(ImageButton::ALIGN_LEFT,
76 ImageButton::ALIGN_BOTTOM);
77 button->SetBoundsRect(bounds);
78 }
79
70 } // namespace 80 } // namespace
71 81
72 /////////////////////////////////////////////////////////////////////////////// 82 ///////////////////////////////////////////////////////////////////////////////
73 // CustomFrameView, public: 83 // CustomFrameView, public:
74 84
75 CustomFrameView::CustomFrameView() 85 CustomFrameView::CustomFrameView()
76 : frame_(NULL), 86 : frame_(NULL),
77 window_icon_(NULL), 87 window_icon_(NULL),
78 minimize_button_(NULL), 88 minimize_button_(NULL),
79 maximize_button_(NULL), 89 maximize_button_(NULL),
80 restore_button_(NULL), 90 restore_button_(NULL),
81 close_button_(NULL), 91 close_button_(NULL),
82 should_show_maximize_button_(false), 92 should_show_maximize_button_(false),
83 frame_background_(new FrameBackground()) { 93 frame_background_(new FrameBackground()),
94 minimum_title_bar_x_(0),
95 maximum_title_bar_x_(-1) {
84 } 96 }
85 97
86 CustomFrameView::~CustomFrameView() { 98 CustomFrameView::~CustomFrameView() {
87 } 99 }
88 100
89 void CustomFrameView::Init(Widget* frame) { 101 void CustomFrameView::Init(Widget* frame) {
90 frame_ = frame; 102 frame_ = frame;
91 103
92 close_button_ = new ImageButton(this); 104 close_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_CLOSE,
93 close_button_->SetAccessibleName( 105 IDR_CLOSE, IDR_CLOSE_H, IDR_CLOSE_P);
94 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_CLOSE));
95
96 // Close button images will be set in LayoutWindowControls().
97 AddChildView(close_button_);
98
99 minimize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MINIMIZE, 106 minimize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MINIMIZE,
100 IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P); 107 IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);
101
102 maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE, 108 maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE,
103 IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P); 109 IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);
104
105 restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE, 110 restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE,
106 IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P); 111 IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);
107 112
108 should_show_maximize_button_ = frame_->widget_delegate()->CanMaximize(); 113 should_show_maximize_button_ = frame_->widget_delegate()->CanMaximize();
109 114
110 if (frame_->widget_delegate()->ShouldShowWindowIcon()) { 115 if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
111 window_icon_ = new ImageButton(this); 116 window_icon_ = new ImageButton(this);
112 AddChildView(window_icon_); 117 AddChildView(window_icon_);
113 } 118 }
114 } 119 }
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 274
270 int CustomFrameView::NonClientTopBorderHeight() const { 275 int CustomFrameView::NonClientTopBorderHeight() const {
271 return std::max(FrameBorderThickness() + IconSize(), 276 return std::max(FrameBorderThickness() + IconSize(),
272 CaptionButtonY() + kCaptionButtonHeightWithPadding) + 277 CaptionButtonY() + kCaptionButtonHeightWithPadding) +
273 TitlebarBottomThickness(); 278 TitlebarBottomThickness();
274 } 279 }
275 280
276 int CustomFrameView::CaptionButtonY() const { 281 int CustomFrameView::CaptionButtonY() const {
277 // Maximized buttons start at window top so that even if their images aren't 282 // Maximized buttons start at window top so that even if their images aren't
278 // drawn flush with the screen edge, they still obey Fitts' Law. 283 // drawn flush with the screen edge, they still obey Fitts' Law.
279 return frame_->IsMaximized() ? FrameBorderThickness() : kFrameShadowThickness; 284 return frame_->IsMaximized() ? FrameBorderThickness() : kFrameBorderThickness;
280 } 285 }
281 286
282 int CustomFrameView::TitlebarBottomThickness() const { 287 int CustomFrameView::TitlebarBottomThickness() const {
283 return kTitlebarTopAndBottomEdgeThickness + 288 return kTitlebarTopAndBottomEdgeThickness +
284 (ShouldShowClientEdge() ? kClientEdgeThickness : 0); 289 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
285 } 290 }
286 291
287 int CustomFrameView::IconSize() const { 292 int CustomFrameView::IconSize() const {
288 #if defined(OS_WIN) 293 #if defined(OS_WIN)
289 // This metric scales up if either the titlebar height or the titlebar font 294 // This metric scales up if either the titlebar height or the titlebar font
(...skipping 17 matching lines...) Expand all
307 // from below the 3D edge. 312 // from below the 3D edge.
308 int unavailable_px_at_top = frame_->IsMaximized() ? 313 int unavailable_px_at_top = frame_->IsMaximized() ?
309 frame_thickness : kTitlebarTopAndBottomEdgeThickness; 314 frame_thickness : kTitlebarTopAndBottomEdgeThickness;
310 // When the icon is shorter than the minimum space we reserve for the caption 315 // When the icon is shorter than the minimum space we reserve for the caption
311 // button, we vertically center it. We want to bias rounding to put extra 316 // button, we vertically center it. We want to bias rounding to put extra
312 // space above the icon, since the 3D edge (+ client edge, for restored 317 // space above the icon, since the 3D edge (+ client edge, for restored
313 // windows) below looks (to the eye) more like additional space than does the 318 // windows) below looks (to the eye) more like additional space than does the
314 // 3D edge (or nothing at all, for maximized windows) above; hence the +1. 319 // 3D edge (or nothing at all, for maximized windows) above; hence the +1.
315 int y = unavailable_px_at_top + (NonClientTopBorderHeight() - 320 int y = unavailable_px_at_top + (NonClientTopBorderHeight() -
316 unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2; 321 unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2;
317 return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size); 322 return gfx::Rect(frame_thickness + kIconLeftSpacing + minimum_title_bar_x_,
323 y, size, size);
318 } 324 }
319 325
320 bool CustomFrameView::ShouldShowTitleBarAndBorder() const { 326 bool CustomFrameView::ShouldShowTitleBarAndBorder() const {
321 if (frame_->IsFullscreen()) 327 if (frame_->IsFullscreen())
322 return false; 328 return false;
323 329
324 if (ViewsDelegate::views_delegate) { 330 if (ViewsDelegate::views_delegate) {
325 return !ViewsDelegate::views_delegate->WindowManagerProvidesTitleBar( 331 return !ViewsDelegate::views_delegate->WindowManagerProvidesTitleBar(
326 frame_->IsMaximized()); 332 frame_->IsMaximized());
327 } 333 }
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 SkColor CustomFrameView::GetFrameColor() const { 467 SkColor CustomFrameView::GetFrameColor() const {
462 return frame_->IsActive() ? kDefaultColorFrame : kDefaultColorFrameInactive; 468 return frame_->IsActive() ? kDefaultColorFrame : kDefaultColorFrameInactive;
463 } 469 }
464 470
465 const gfx::ImageSkia* CustomFrameView::GetFrameImage() const { 471 const gfx::ImageSkia* CustomFrameView::GetFrameImage() const {
466 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( 472 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
467 frame_->IsActive() ? IDR_FRAME : IDR_FRAME_INACTIVE).ToImageSkia(); 473 frame_->IsActive() ? IDR_FRAME : IDR_FRAME_INACTIVE).ToImageSkia();
468 } 474 }
469 475
470 void CustomFrameView::LayoutWindowControls() { 476 void CustomFrameView::LayoutWindowControls() {
471 close_button_->SetImageAlignment(ImageButton::ALIGN_LEFT, 477 minimum_title_bar_x_ = 0;
472 ImageButton::ALIGN_BOTTOM); 478 maximum_title_bar_x_ = width();
479
480 if (bounds().IsEmpty())
481 return;
482
473 int caption_y = CaptionButtonY(); 483 int caption_y = CaptionButtonY();
474 bool is_maximized = frame_->IsMaximized(); 484 bool is_maximized = frame_->IsMaximized();
475 // There should always be the same number of non-shadow pixels visible to the 485 // There should always be the same number of non-shadow pixels visible to the
476 // side of the caption buttons. In maximized mode we extend the rightmost 486 // side of the caption buttons. In maximized mode we extend the edge button
477 // button to the screen corner to obey Fitts' Law. 487 // to the screen corner to obey Fitts' Law.
478 int right_extra_width = is_maximized ? 488 int extra_width = is_maximized ?
479 (kFrameBorderThickness - kFrameShadowThickness) : 0; 489 (kFrameBorderThickness - kFrameShadowThickness) : 0;
480 gfx::Size close_button_size = close_button_->GetPreferredSize(); 490 int next_button_x = FrameBorderThickness();
481 close_button_->SetBounds(width() - FrameBorderThickness() -
482 right_extra_width - close_button_size.width(), caption_y,
483 close_button_size.width() + right_extra_width,
484 close_button_size.height());
485 491
486 // When the window is restored, we show a maximized button; otherwise, we show
487 // a restore button.
488 bool is_restored = !is_maximized && !frame_->IsMinimized(); 492 bool is_restored = !is_maximized && !frame_->IsMinimized();
489 ImageButton* invisible_button = is_restored ? restore_button_ 493 ImageButton* invisible_button = is_restored ? restore_button_
490 : maximize_button_; 494 : maximize_button_;
491 invisible_button->SetVisible(false); 495 invisible_button->SetVisible(false);
492 496
493 ImageButton* visible_button = is_restored ? maximize_button_ 497 WindowButtonOrderProvider* button_order =
494 : restore_button_; 498 WindowButtonOrderProvider::GetInstance();
495 FramePartImage normal_part, hot_part, pushed_part; 499 const std::vector<views::FrameButton>& leading_buttons =
496 int next_button_x; 500 button_order->leading_buttons();
497 if (should_show_maximize_button_) { 501 const std::vector<views::FrameButton>& trailing_buttons =
498 visible_button->SetVisible(true); 502 button_order->trailing_buttons();
499 visible_button->SetImageAlignment(ImageButton::ALIGN_LEFT, 503
500 ImageButton::ALIGN_BOTTOM); 504 ImageButton* button = NULL;
501 gfx::Size visible_button_size = visible_button->GetPreferredSize(); 505 for (std::vector<views::FrameButton>::const_iterator it =
502 visible_button->SetBounds(close_button_->x() - visible_button_size.width(), 506 leading_buttons.begin(); it != leading_buttons.end(); ++it) {
503 caption_y, visible_button_size.width(), 507 button = GetImageButton(*it);
504 visible_button_size.height()); 508 if (!button)
505 next_button_x = visible_button->x(); 509 continue;
506 } else { 510 gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
507 visible_button->SetVisible(false); 511 button->GetPreferredSize());
508 next_button_x = close_button_->x(); 512 if (it == leading_buttons.begin())
513 target_bounds.set_width(target_bounds.width() + extra_width);
514 LayoutButton(button, target_bounds);
515 next_button_x += button->width();
516 minimum_title_bar_x_ = std::min(width(), next_button_x);
509 } 517 }
510 518
511 minimize_button_->SetVisible(true); 519 // Trailing buttions are laid out in a RTL fashion
512 minimize_button_->SetImageAlignment(ImageButton::ALIGN_LEFT, 520 next_button_x = width() - FrameBorderThickness();
513 ImageButton::ALIGN_BOTTOM); 521 for (std::vector<views::FrameButton>::const_reverse_iterator it =
514 gfx::Size minimize_button_size = minimize_button_->GetPreferredSize(); 522 trailing_buttons.rbegin(); it != trailing_buttons.rend(); ++it) {
515 minimize_button_->SetBounds( 523 button = GetImageButton(*it);
516 next_button_x - minimize_button_size.width(), caption_y, 524 if (!button)
517 minimize_button_size.width(), 525 continue;
518 minimize_button_size.height()); 526 gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
519 527 button->GetPreferredSize());
520 normal_part = IDR_CLOSE; 528 if (it == trailing_buttons.rbegin())
521 hot_part = IDR_CLOSE_H; 529 target_bounds.set_width(target_bounds.width() + extra_width);
522 pushed_part = IDR_CLOSE_P; 530 target_bounds.Offset(-target_bounds.width(), 0);
523 531 LayoutButton(button, target_bounds);
524 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 532 next_button_x = button->x();
525 533 maximum_title_bar_x_ = std::max(minimum_title_bar_x_, next_button_x);
526 close_button_->SetImage(CustomButton::STATE_NORMAL, 534 }
527 rb.GetImageNamed(normal_part).ToImageSkia());
528 close_button_->SetImage(CustomButton::STATE_HOVERED,
529 rb.GetImageNamed(hot_part).ToImageSkia());
530 close_button_->SetImage(CustomButton::STATE_PRESSED,
531 rb.GetImageNamed(pushed_part).ToImageSkia());
532 } 535 }
533 536
534 void CustomFrameView::LayoutTitleBar() { 537 void CustomFrameView::LayoutTitleBar() {
538 DCHECK_GE(maximum_title_bar_x_, 0);
535 // The window title position is calculated based on the icon position, even 539 // The window title position is calculated based on the icon position, even
536 // when there is no icon. 540 // when there is no icon.
537 gfx::Rect icon_bounds(IconBounds()); 541 gfx::Rect icon_bounds(IconBounds());
538 bool show_window_icon = window_icon_ != NULL; 542 bool show_window_icon = window_icon_ != NULL;
539 if (show_window_icon) 543 if (show_window_icon)
540 window_icon_->SetBoundsRect(icon_bounds); 544 window_icon_->SetBoundsRect(icon_bounds);
541 545
542 if (!frame_->widget_delegate()->ShouldShowWindowTitle()) 546 if (!frame_->widget_delegate()->ShouldShowWindowTitle())
543 return; 547 return;
544 548
545 // The offset between the window left edge and the title text. 549 // The offset between the window left edge and the title text.
546 int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX 550 int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
547 : icon_bounds.x(); 551 : icon_bounds.x();
548 int title_height = GetTitleFontList().GetHeight(); 552 int title_height = GetTitleFontList().GetHeight();
549 // We bias the title position so that when the difference between the icon and 553 // We bias the title position so that when the difference between the icon and
550 // title heights is odd, the extra pixel of the title is above the vertical 554 // title heights is odd, the extra pixel of the title is above the vertical
551 // midline rather than below. This compensates for how the icon is already 555 // midline rather than below. This compensates for how the icon is already
552 // biased downwards (see IconBounds()) and helps prevent descenders on the 556 // biased downwards (see IconBounds()) and helps prevent descenders on the
553 // title from overlapping the 3D edge at the bottom of the titlebar. 557 // title from overlapping the 3D edge at the bottom of the titlebar.
554 title_bounds_.SetRect(title_x, 558 title_bounds_.SetRect(title_x,
555 icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2), 559 icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
556 std::max(0, minimize_button_->x() - kTitleCaptionSpacing - 560 std::max(0, maximum_title_bar_x_ - kTitleCaptionSpacing -
557 title_x), title_height); 561 title_x), title_height);
558 } 562 }
559 563
560 void CustomFrameView::LayoutClientView() { 564 void CustomFrameView::LayoutClientView() {
561 if (!ShouldShowTitleBarAndBorder()) { 565 if (!ShouldShowTitleBarAndBorder()) {
562 client_view_bounds_ = bounds(); 566 client_view_bounds_ = bounds();
563 return; 567 return;
564 } 568 }
565 569
566 int top_height = NonClientTopBorderHeight(); 570 int top_height = NonClientTopBorderHeight();
(...skipping 14 matching lines...) Expand all
581 button->SetImage(CustomButton::STATE_NORMAL, 585 button->SetImage(CustomButton::STATE_NORMAL,
582 rb.GetImageNamed(normal_image_id).ToImageSkia()); 586 rb.GetImageNamed(normal_image_id).ToImageSkia());
583 button->SetImage(CustomButton::STATE_HOVERED, 587 button->SetImage(CustomButton::STATE_HOVERED,
584 rb.GetImageNamed(hot_image_id).ToImageSkia()); 588 rb.GetImageNamed(hot_image_id).ToImageSkia());
585 button->SetImage(CustomButton::STATE_PRESSED, 589 button->SetImage(CustomButton::STATE_PRESSED,
586 rb.GetImageNamed(pushed_image_id).ToImageSkia()); 590 rb.GetImageNamed(pushed_image_id).ToImageSkia());
587 AddChildView(button); 591 AddChildView(button);
588 return button; 592 return button;
589 } 593 }
590 594
595 ImageButton* CustomFrameView::GetImageButton(views::FrameButton frame_button) {
596 ImageButton* button = NULL;
597 switch (frame_button) {
598 case views::FRAME_BUTTON_MINIMIZE: {
599 button = minimize_button_;
600 break;
601 }
602 case views::FRAME_BUTTON_MAXIMIZE: {
603 bool is_restored = !frame_->IsMaximized() && !frame_->IsMinimized();
604 button = is_restored ? maximize_button_ : restore_button_;
605 if (!should_show_maximize_button_) {
606 // If we should not show the maximize/restore button, then we return
607 // NULL as we don't want this button to become visible and to be laid
608 // out.
609 button->SetVisible(false);
610 return NULL;
611 }
612 break;
613 }
614 case views::FRAME_BUTTON_CLOSE: {
615 button = close_button_;
616 break;
617 }
618 }
619 return button;
620 }
621
591 } // namespace views 622 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/window/custom_frame_view.h ('k') | ui/views/window/custom_frame_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698