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

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

Issue 240163006: Linux Aura Task Manager Frame Buttons Misaligned (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
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_aura.h" 24 #include "ui/views/widget/native_widget_aura.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 NativeWidgetAura::GetWindowTitleFontList(); 69 NativeWidgetAura::GetWindowTitleFontList();
67 return title_font_list; 70 return title_font_list;
68 } 71 }
69 72
73 void LayoutButton(ImageButton* button, int x, int y, int extra_width) {
flackr 2014/05/07 00:37:58 It'd probably be better to pass in the target size
jonross 2014/05/07 17:27:08 Done.
74 button->SetVisible(true);
75 button->SetImageAlignment(ImageButton::ALIGN_LEFT,
76 ImageButton::ALIGN_BOTTOM);
77 gfx::Size button_size = button->GetPreferredSize();
78 button->SetBounds(x, y, button_size.width() + extra_width,
79 button_size.height());
80 }
81
70 } // namespace 82 } // namespace
71 83
72 /////////////////////////////////////////////////////////////////////////////// 84 ///////////////////////////////////////////////////////////////////////////////
73 // CustomFrameView, public: 85 // CustomFrameView, public:
74 86
75 CustomFrameView::CustomFrameView() 87 CustomFrameView::CustomFrameView()
76 : frame_(NULL), 88 : frame_(NULL),
77 window_icon_(NULL), 89 window_icon_(NULL),
78 minimize_button_(NULL), 90 minimize_button_(NULL),
79 maximize_button_(NULL), 91 maximize_button_(NULL),
80 restore_button_(NULL), 92 restore_button_(NULL),
81 close_button_(NULL), 93 close_button_(NULL),
82 should_show_maximize_button_(false), 94 should_show_maximize_button_(false),
83 frame_background_(new FrameBackground()) { 95 frame_background_(new FrameBackground()),
96 minimum_title_bar_x_(0),
97 maximum_title_bar_x_(-1) {
84 } 98 }
85 99
86 CustomFrameView::~CustomFrameView() { 100 CustomFrameView::~CustomFrameView() {
87 } 101 }
88 102
89 void CustomFrameView::Init(Widget* frame) { 103 void CustomFrameView::Init(Widget* frame) {
90 frame_ = frame; 104 frame_ = frame;
91 105
92 close_button_ = new ImageButton(this); 106 close_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_CLOSE,
93 close_button_->SetAccessibleName( 107 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, 108 minimize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MINIMIZE,
100 IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P); 109 IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);
101
102 maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE, 110 maximize_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_MAXIMIZE,
103 IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P); 111 IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);
104
105 restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE, 112 restore_button_ = InitWindowCaptionButton(IDS_APP_ACCNAME_RESTORE,
106 IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P); 113 IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);
107 114
108 should_show_maximize_button_ = frame_->widget_delegate()->CanMaximize(); 115 should_show_maximize_button_ = frame_->widget_delegate()->CanMaximize();
109 116
110 if (frame_->widget_delegate()->ShouldShowWindowIcon()) { 117 if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
111 window_icon_ = new ImageButton(this); 118 window_icon_ = new ImageButton(this);
112 AddChildView(window_icon_); 119 AddChildView(window_icon_);
113 } 120 }
114 } 121 }
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 275
269 int CustomFrameView::NonClientTopBorderHeight() const { 276 int CustomFrameView::NonClientTopBorderHeight() const {
270 return std::max(FrameBorderThickness() + IconSize(), 277 return std::max(FrameBorderThickness() + IconSize(),
271 CaptionButtonY() + kCaptionButtonHeightWithPadding) + 278 CaptionButtonY() + kCaptionButtonHeightWithPadding) +
272 TitlebarBottomThickness(); 279 TitlebarBottomThickness();
273 } 280 }
274 281
275 int CustomFrameView::CaptionButtonY() const { 282 int CustomFrameView::CaptionButtonY() const {
276 // Maximized buttons start at window top so that even if their images aren't 283 // Maximized buttons start at window top so that even if their images aren't
277 // drawn flush with the screen edge, they still obey Fitts' Law. 284 // drawn flush with the screen edge, they still obey Fitts' Law.
278 return frame_->IsMaximized() ? FrameBorderThickness() : kFrameShadowThickness; 285 return frame_->IsMaximized() ? FrameBorderThickness() : kFrameBorderThickness;
279 } 286 }
280 287
281 int CustomFrameView::TitlebarBottomThickness() const { 288 int CustomFrameView::TitlebarBottomThickness() const {
282 return kTitlebarTopAndBottomEdgeThickness + 289 return kTitlebarTopAndBottomEdgeThickness +
283 (ShouldShowClientEdge() ? kClientEdgeThickness : 0); 290 (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
284 } 291 }
285 292
286 int CustomFrameView::IconSize() const { 293 int CustomFrameView::IconSize() const {
287 #if defined(OS_WIN) 294 #if defined(OS_WIN)
288 // This metric scales up if either the titlebar height or the titlebar font 295 // This metric scales up if either the titlebar height or the titlebar font
(...skipping 17 matching lines...) Expand all
306 // from below the 3D edge. 313 // from below the 3D edge.
307 int unavailable_px_at_top = frame_->IsMaximized() ? 314 int unavailable_px_at_top = frame_->IsMaximized() ?
308 frame_thickness : kTitlebarTopAndBottomEdgeThickness; 315 frame_thickness : kTitlebarTopAndBottomEdgeThickness;
309 // When the icon is shorter than the minimum space we reserve for the caption 316 // When the icon is shorter than the minimum space we reserve for the caption
310 // button, we vertically center it. We want to bias rounding to put extra 317 // button, we vertically center it. We want to bias rounding to put extra
311 // space above the icon, since the 3D edge (+ client edge, for restored 318 // space above the icon, since the 3D edge (+ client edge, for restored
312 // windows) below looks (to the eye) more like additional space than does the 319 // windows) below looks (to the eye) more like additional space than does the
313 // 3D edge (or nothing at all, for maximized windows) above; hence the +1. 320 // 3D edge (or nothing at all, for maximized windows) above; hence the +1.
314 int y = unavailable_px_at_top + (NonClientTopBorderHeight() - 321 int y = unavailable_px_at_top + (NonClientTopBorderHeight() -
315 unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2; 322 unavailable_px_at_top - size - TitlebarBottomThickness() + 1) / 2;
316 return gfx::Rect(frame_thickness + kIconLeftSpacing, y, size, size); 323 return gfx::Rect(frame_thickness + kIconLeftSpacing + minimum_title_bar_x_,
324 y, size, size);
317 } 325 }
318 326
319 bool CustomFrameView::ShouldShowTitleBarAndBorder() const { 327 bool CustomFrameView::ShouldShowTitleBarAndBorder() const {
320 if (frame_->IsFullscreen()) 328 if (frame_->IsFullscreen())
321 return false; 329 return false;
322 330
323 if (ViewsDelegate::views_delegate) { 331 if (ViewsDelegate::views_delegate) {
324 return !ViewsDelegate::views_delegate->WindowManagerProvidesTitleBar( 332 return !ViewsDelegate::views_delegate->WindowManagerProvidesTitleBar(
325 frame_->IsMaximized()); 333 frame_->IsMaximized());
326 } 334 }
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 SkColor CustomFrameView::GetFrameColor() const { 468 SkColor CustomFrameView::GetFrameColor() const {
461 return frame_->IsActive() ? kDefaultColorFrame : kDefaultColorFrameInactive; 469 return frame_->IsActive() ? kDefaultColorFrame : kDefaultColorFrameInactive;
462 } 470 }
463 471
464 const gfx::ImageSkia* CustomFrameView::GetFrameImage() const { 472 const gfx::ImageSkia* CustomFrameView::GetFrameImage() const {
465 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( 473 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
466 frame_->IsActive() ? IDR_FRAME : IDR_FRAME_INACTIVE).ToImageSkia(); 474 frame_->IsActive() ? IDR_FRAME : IDR_FRAME_INACTIVE).ToImageSkia();
467 } 475 }
468 476
469 void CustomFrameView::LayoutWindowControls() { 477 void CustomFrameView::LayoutWindowControls() {
470 close_button_->SetImageAlignment(ImageButton::ALIGN_LEFT, 478 minimum_title_bar_x_ = 0;
471 ImageButton::ALIGN_BOTTOM); 479 maximum_title_bar_x_ = width();
480
481 if (bounds().IsEmpty())
482 return;
483
472 int caption_y = CaptionButtonY(); 484 int caption_y = CaptionButtonY();
473 bool is_maximized = frame_->IsMaximized(); 485 bool is_maximized = frame_->IsMaximized();
474 // There should always be the same number of non-shadow pixels visible to the 486 // There should always be the same number of non-shadow pixels visible to the
475 // side of the caption buttons. In maximized mode we extend the rightmost 487 // side of the caption buttons. In maximized mode we extend the edge button
476 // button to the screen corner to obey Fitts' Law. 488 // to the screen corner to obey Fitts' Law.
477 int right_extra_width = is_maximized ? 489 int extra_width = is_maximized ?
478 (kFrameBorderThickness - kFrameShadowThickness) : 0; 490 (kFrameBorderThickness - kFrameShadowThickness) : 0;
479 gfx::Size close_button_size = close_button_->GetPreferredSize(); 491 int next_button_x = FrameBorderThickness();
480 close_button_->SetBounds(width() - FrameBorderThickness() -
481 right_extra_width - close_button_size.width(), caption_y,
482 close_button_size.width() + right_extra_width,
483 close_button_size.height());
484 492
485 // When the window is restored, we show a maximized button; otherwise, we show
486 // a restore button.
487 bool is_restored = !is_maximized && !frame_->IsMinimized(); 493 bool is_restored = !is_maximized && !frame_->IsMinimized();
488 ImageButton* invisible_button = is_restored ? restore_button_ 494 ImageButton* invisible_button = is_restored ? restore_button_
489 : maximize_button_; 495 : maximize_button_;
490 invisible_button->SetVisible(false); 496 invisible_button->SetVisible(false);
491 497
492 ImageButton* visible_button = is_restored ? maximize_button_ 498 WindowButtonOrderProvider* button_order =
493 : restore_button_; 499 WindowButtonOrderProvider::GetInstance();
494 FramePartImage normal_part, hot_part, pushed_part; 500 std::vector<views::FrameButton> leading_buttons =
495 int next_button_x; 501 button_order->GetLeadingButtons();
496 if (should_show_maximize_button_) { 502 std::vector<views::FrameButton> trailing_buttons =
497 visible_button->SetVisible(true); 503 button_order->GetTrailingButtons();
498 visible_button->SetImageAlignment(ImageButton::ALIGN_LEFT, 504
499 ImageButton::ALIGN_BOTTOM); 505 ImageButton* button = NULL;
500 gfx::Size visible_button_size = visible_button->GetPreferredSize(); 506 for (std::vector<views::FrameButton>::const_iterator it =
501 visible_button->SetBounds(close_button_->x() - visible_button_size.width(), 507 leading_buttons.begin(); it != leading_buttons.end(); ++it) {
502 caption_y, visible_button_size.width(), 508 button = GetImageButton(*it);
503 visible_button_size.height()); 509 if (!button)
504 next_button_x = visible_button->x(); 510 continue;
505 } else { 511 LayoutButton(button, next_button_x, caption_y,
506 visible_button->SetVisible(false); 512 (it == leading_buttons.begin())? extra_width : 0);
flackr 2014/05/07 00:37:58 nit: space before ?, no brackets around condition.
jonross 2014/05/07 17:27:08 Done.
507 next_button_x = close_button_->x(); 513 next_button_x += button->width();
514 minimum_title_bar_x_ = std::min(width(), next_button_x);
508 } 515 }
509 516
510 minimize_button_->SetVisible(true); 517 // Trailing buttions are laid out in a RTL fashion
511 minimize_button_->SetImageAlignment(ImageButton::ALIGN_LEFT, 518 next_button_x = width() - FrameBorderThickness() - extra_width;
512 ImageButton::ALIGN_BOTTOM); 519 for (std::vector<views::FrameButton>::const_reverse_iterator it =
513 gfx::Size minimize_button_size = minimize_button_->GetPreferredSize(); 520 trailing_buttons.rbegin(); it != trailing_buttons.rend(); ++it) {
514 minimize_button_->SetBounds( 521 button = GetImageButton(*it);
515 next_button_x - minimize_button_size.width(), caption_y, 522 if (!button)
516 minimize_button_size.width(), 523 continue;
517 minimize_button_size.height()); 524 gfx::Size button_size = button->GetPreferredSize();
518 525 next_button_x -= button_size.width();
519 normal_part = IDR_CLOSE; 526 LayoutButton(button, next_button_x, caption_y,
520 hot_part = IDR_CLOSE_H; 527 (it == trailing_buttons.rend())? extra_width : 0);
flackr 2014/05/07 00:37:58 ditto
jonross 2014/05/07 17:27:08 Done.
521 pushed_part = IDR_CLOSE_P; 528 next_button_x = button->x();
522 529 maximum_title_bar_x_ = std::max(minimum_title_bar_x_, next_button_x);
523 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 530 }
524
525 close_button_->SetImage(CustomButton::STATE_NORMAL,
526 rb.GetImageNamed(normal_part).ToImageSkia());
527 close_button_->SetImage(CustomButton::STATE_HOVERED,
528 rb.GetImageNamed(hot_part).ToImageSkia());
529 close_button_->SetImage(CustomButton::STATE_PRESSED,
530 rb.GetImageNamed(pushed_part).ToImageSkia());
531 } 531 }
532 532
533 void CustomFrameView::LayoutTitleBar() { 533 void CustomFrameView::LayoutTitleBar() {
534 DCHECK_GE(maximum_title_bar_x_, 0);
534 // The window title position is calculated based on the icon position, even 535 // The window title position is calculated based on the icon position, even
535 // when there is no icon. 536 // when there is no icon.
536 gfx::Rect icon_bounds(IconBounds()); 537 gfx::Rect icon_bounds(IconBounds());
537 bool show_window_icon = window_icon_ != NULL; 538 bool show_window_icon = window_icon_ != NULL;
538 if (show_window_icon) 539 if (show_window_icon)
539 window_icon_->SetBoundsRect(icon_bounds); 540 window_icon_->SetBoundsRect(icon_bounds);
540 541
541 // The offset between the window left edge and the title text. 542 // The offset between the window left edge and the title text.
542 int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX 543 int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
543 : icon_bounds.x(); 544 : icon_bounds.x();
544 int title_height = GetTitleFontList().GetHeight(); 545 int title_height = GetTitleFontList().GetHeight();
545 // We bias the title position so that when the difference between the icon and 546 // We bias the title position so that when the difference between the icon and
546 // title heights is odd, the extra pixel of the title is above the vertical 547 // title heights is odd, the extra pixel of the title is above the vertical
547 // midline rather than below. This compensates for how the icon is already 548 // midline rather than below. This compensates for how the icon is already
548 // biased downwards (see IconBounds()) and helps prevent descenders on the 549 // biased downwards (see IconBounds()) and helps prevent descenders on the
549 // title from overlapping the 3D edge at the bottom of the titlebar. 550 // title from overlapping the 3D edge at the bottom of the titlebar.
550 title_bounds_.SetRect(title_x, 551 title_bounds_.SetRect(title_x,
551 icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2), 552 icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
552 std::max(0, minimize_button_->x() - kTitleCaptionSpacing - 553 std::max(0, maximum_title_bar_x_ - kTitleCaptionSpacing -
553 title_x), title_height); 554 title_x), title_height);
554 } 555 }
555 556
556 void CustomFrameView::LayoutClientView() { 557 void CustomFrameView::LayoutClientView() {
557 if (!ShouldShowTitleBarAndBorder()) { 558 if (!ShouldShowTitleBarAndBorder()) {
558 client_view_bounds_ = bounds(); 559 client_view_bounds_ = bounds();
559 return; 560 return;
560 } 561 }
561 562
562 int top_height = NonClientTopBorderHeight(); 563 int top_height = NonClientTopBorderHeight();
(...skipping 14 matching lines...) Expand all
577 button->SetImage(CustomButton::STATE_NORMAL, 578 button->SetImage(CustomButton::STATE_NORMAL,
578 rb.GetImageNamed(normal_image_id).ToImageSkia()); 579 rb.GetImageNamed(normal_image_id).ToImageSkia());
579 button->SetImage(CustomButton::STATE_HOVERED, 580 button->SetImage(CustomButton::STATE_HOVERED,
580 rb.GetImageNamed(hot_image_id).ToImageSkia()); 581 rb.GetImageNamed(hot_image_id).ToImageSkia());
581 button->SetImage(CustomButton::STATE_PRESSED, 582 button->SetImage(CustomButton::STATE_PRESSED,
582 rb.GetImageNamed(pushed_image_id).ToImageSkia()); 583 rb.GetImageNamed(pushed_image_id).ToImageSkia());
583 AddChildView(button); 584 AddChildView(button);
584 return button; 585 return button;
585 } 586 }
586 587
588 ImageButton* CustomFrameView::GetImageButton(views::FrameButton frame_button) {
589 ImageButton* button = NULL;
590 switch (frame_button) {
591 case views::FRAME_BUTTON_MINIMIZE: {
592 button = minimize_button_;
593 break;
594 }
595 case views::FRAME_BUTTON_MAXIMIZE: {
596 bool is_restored = !frame_->IsMaximized() && !frame_->IsMinimized();
597 button = is_restored ? maximize_button_ : restore_button_;
598 if (!should_show_maximize_button_) {
599 // If we should not show the maximize/restore button, then we return
600 // NULL as we don't want this button to become visible and to be laid
601 // out.
602 button->SetVisible(false);
603 return NULL;
604 }
605 break;
606 }
607 case views::FRAME_BUTTON_CLOSE: {
608 button = close_button_;
609 break;
610 }
611 }
612 return button;
613 }
614
587 } // namespace views 615 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698