power_button_menu_item_view.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2018 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. #include "ash/system/power/power_button_menu_item_view.h"
  5. #include "ash/public/cpp/style/scoped_light_mode_as_default.h"
  6. #include "ash/style/ash_color_provider.h"
  7. #include "ui/base/resource/resource_bundle.h"
  8. #include "ui/gfx/canvas.h"
  9. #include "ui/gfx/font.h"
  10. #include "ui/gfx/paint_vector_icon.h"
  11. #include "ui/views/accessibility/view_accessibility.h"
  12. #include "ui/views/border.h"
  13. #include "ui/views/controls/image_view.h"
  14. #include "ui/views/controls/label.h"
  15. namespace ash {
  16. namespace {
  17. // Size of the image icon in pixels.
  18. constexpr int kIconSize = 24;
  19. // Top padding of the image icon to the top of the item view.
  20. constexpr int kIconTopPadding = 17;
  21. // The distance from one line title's bottom to the top of the item view.
  22. constexpr int kTitleTopPaddingIncludesOneLineHeight =
  23. kIconTopPadding + kIconSize + 22;
  24. // The amount of rounding applied to the corners of the focused menu item.
  25. constexpr int kFocusedItemRoundRectRadiusDp = 8;
  26. // Line height of the label.
  27. constexpr int kLineHeight = 20;
  28. } // namespace
  29. PowerButtonMenuItemView::PowerButtonMenuItemView(
  30. PressedCallback callback,
  31. const gfx::VectorIcon& icon,
  32. const std::u16string& title_text)
  33. : views::ImageButton(std::move(callback)), icon_(icon) {
  34. SetFocusBehavior(FocusBehavior::ALWAYS);
  35. set_suppress_default_focus_handling();
  36. SetFocusPainter(nullptr);
  37. icon_view_ = AddChildView(std::make_unique<views::ImageView>());
  38. title_ = AddChildView(std::make_unique<views::Label>());
  39. title_->SetBackgroundColor(SK_ColorTRANSPARENT);
  40. title_->SetText(title_text);
  41. title_->SetVerticalAlignment(gfx::ALIGN_TOP);
  42. title_->SetLineHeight(kLineHeight);
  43. title_->SetMultiLine(true);
  44. title_->SetMaxLines(2);
  45. GetViewAccessibility().OverrideRole(ax::mojom::Role::kMenuItem);
  46. GetViewAccessibility().OverrideName(title_->GetText());
  47. SetBorder(views::CreateEmptyBorder(
  48. gfx::Insets::TLBR(kItemBorderThickness, kItemBorderThickness,
  49. kItemBorderThickness, kItemBorderThickness)));
  50. }
  51. PowerButtonMenuItemView::~PowerButtonMenuItemView() = default;
  52. const char* PowerButtonMenuItemView::GetClassName() const {
  53. return "PowerButtonMenuItemView";
  54. }
  55. void PowerButtonMenuItemView::Layout() {
  56. const gfx::Rect rect(GetContentsBounds());
  57. gfx::Rect icon_rect(rect);
  58. icon_rect.ClampToCenteredSize(gfx::Size(kIconSize, kIconSize));
  59. icon_rect.set_y(kIconTopPadding);
  60. icon_view_->SetBoundsRect(icon_rect);
  61. const int kTitleTopPadding =
  62. kTitleTopPaddingIncludesOneLineHeight - title_->font_list().GetHeight();
  63. title_->SetBoundsRect(gfx::Rect(0, kTitleTopPadding, kMenuItemWidth,
  64. kMenuItemHeight - kTitleTopPadding));
  65. }
  66. gfx::Size PowerButtonMenuItemView::CalculatePreferredSize() const {
  67. return gfx::Size(kMenuItemWidth + 2 * kItemBorderThickness,
  68. kMenuItemHeight + 2 * kItemBorderThickness);
  69. }
  70. void PowerButtonMenuItemView::OnFocus() {
  71. parent()->SetFocusBehavior(FocusBehavior::NEVER);
  72. NotifyAccessibilityEvent(ax::mojom::Event::kSelection, true);
  73. SchedulePaint();
  74. }
  75. void PowerButtonMenuItemView::OnBlur() {
  76. SchedulePaint();
  77. }
  78. void PowerButtonMenuItemView::OnThemeChanged() {
  79. views::ImageButton::OnThemeChanged();
  80. ScopedLightModeAsDefault scoped_light_mode_as_default;
  81. const auto* color_provider = AshColorProvider::Get();
  82. icon_view_->SetImage(gfx::CreateVectorIcon(
  83. icon_, color_provider->GetContentLayerColor(
  84. AshColorProvider::ContentLayerType::kIconColorPrimary)));
  85. title_->SetEnabledColor(color_provider->GetContentLayerColor(
  86. AshColorProvider::ContentLayerType::kTextColorPrimary));
  87. }
  88. void PowerButtonMenuItemView::PaintButtonContents(gfx::Canvas* canvas) {
  89. cc::PaintFlags flags;
  90. flags.setAntiAlias(true);
  91. // Set the background color to SK_ColorTRANSPARENT here since the parent view
  92. // PowerButtonMenuView has already set the background color.
  93. flags.setColor(SK_ColorTRANSPARENT);
  94. const gfx::Rect content_bounds = GetContentsBounds();
  95. canvas->DrawRoundRect(content_bounds, kFocusedItemRoundRectRadiusDp, flags);
  96. if (!HasFocus() || content_bounds.IsEmpty())
  97. return;
  98. // Border.
  99. gfx::Rect bounds = GetLocalBounds();
  100. bounds.Inset(gfx::Insets(kItemBorderThickness));
  101. // Stroke.
  102. ScopedLightModeAsDefault scoped_light_mode_as_default;
  103. flags.setColor(AshColorProvider::Get()->GetControlsLayerColor(
  104. AshColorProvider::ControlsLayerType::kFocusRingColor));
  105. flags.setStrokeWidth(kItemBorderThickness);
  106. flags.setStyle(cc::PaintFlags::Style::kStroke_Style);
  107. canvas->DrawRoundRect(bounds, kFocusedItemRoundRectRadiusDp, flags);
  108. }
  109. } // namespace ash