quick_action_item.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2020 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/phonehub/quick_action_item.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/strings/grit/ash_strings.h"
  8. #include "ash/style/ash_color_provider.h"
  9. #include "ash/system/tray/tray_constants.h"
  10. #include "base/bind.h"
  11. #include "ui/base/l10n/l10n_util.h"
  12. #include "ui/compositor/layer.h"
  13. #include "ui/gfx/geometry/insets.h"
  14. #include "ui/views/border.h"
  15. #include "ui/views/controls/label.h"
  16. #include "ui/views/layout/box_layout.h"
  17. namespace ash {
  18. namespace {
  19. void ConfigureLabel(views::Label* label, int line_height, int font_size) {
  20. label->SetAutoColorReadabilityEnabled(false);
  21. label->SetSubpixelRenderingEnabled(false);
  22. label->SetCanProcessEventsWithinSubtree(false);
  23. label->SetLineHeight(line_height);
  24. gfx::Font default_font;
  25. gfx::Font label_font =
  26. default_font.Derive(font_size - default_font.GetFontSize(),
  27. gfx::Font::NORMAL, gfx::Font::Weight::NORMAL);
  28. gfx::FontList font_list(label_font);
  29. label->SetFontList(font_list);
  30. }
  31. } // namespace
  32. QuickActionItem::QuickActionItem(Delegate* delegate,
  33. int label_id,
  34. const gfx::VectorIcon& icon) {
  35. SetPreferredSize(kUnifiedFeaturePodSize);
  36. auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
  37. views::BoxLayout::Orientation::kVertical, gfx::Insets(),
  38. kUnifiedFeaturePodSpacing));
  39. layout->set_cross_axis_alignment(
  40. views::BoxLayout::CrossAxisAlignment::kCenter);
  41. icon_button_ = AddChildView(std::make_unique<FeaturePodIconButton>(
  42. base::BindRepeating(
  43. [](Delegate* delegate, QuickActionItem* item) {
  44. delegate->OnButtonPressed(item->IsToggled());
  45. },
  46. delegate, this),
  47. true /* is_togglable */));
  48. icon_button_->SetVectorIcon(icon);
  49. auto* label_view = AddChildView(std::make_unique<views::View>());
  50. label_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
  51. views::BoxLayout::Orientation::kVertical, gfx::Insets()));
  52. label_ = label_view->AddChildView(
  53. std::make_unique<views::Label>(l10n_util::GetStringUTF16(label_id)));
  54. label_->SetBorder(views::CreateEmptyBorder(
  55. gfx::Insets::TLBR(0, 0, kUnifiedFeaturePodInterLabelPadding, 0)));
  56. sub_label_ = label_view->AddChildView(std::make_unique<views::Label>());
  57. ConfigureLabel(label_, kUnifiedFeaturePodLabelLineHeight,
  58. kUnifiedFeaturePodLabelFontSize);
  59. ConfigureLabel(sub_label_, kUnifiedFeaturePodSubLabelLineHeight,
  60. kUnifiedFeaturePodSubLabelFontSize);
  61. sub_label_color_ = AshColorProvider::Get()->GetContentLayerColor(
  62. AshColorProvider::ContentLayerType::kTextColorSecondary);
  63. SetEnabled(true /* enabled */);
  64. // In dark light mode, we switch TrayBubbleView to use a textured layer
  65. // instead of solid color layer, so no need to create an extra layer here.
  66. if (!features::IsDarkLightModeEnabled()) {
  67. SetPaintToLayer();
  68. layer()->SetFillsBoundsOpaquely(false);
  69. }
  70. }
  71. QuickActionItem::~QuickActionItem() = default;
  72. void QuickActionItem::SetSubLabel(const std::u16string& sub_label) {
  73. sub_label_->SetText(sub_label);
  74. }
  75. void QuickActionItem::SetSubLabelColor(SkColor color) {
  76. if (sub_label_color_ == color)
  77. return;
  78. sub_label_color_ = color;
  79. sub_label_->SetEnabledColor(sub_label_color_);
  80. }
  81. void QuickActionItem::SetTooltip(const std::u16string& text) {
  82. icon_button_->SetTooltipText(text);
  83. }
  84. void QuickActionItem::SetToggled(bool toggled) {
  85. icon_button_->SetToggled(toggled);
  86. }
  87. bool QuickActionItem::IsToggled() const {
  88. return icon_button_->toggled();
  89. }
  90. const std::u16string& QuickActionItem::GetItemLabel() const {
  91. return label_->GetText();
  92. }
  93. void QuickActionItem::SetEnabled(bool enabled) {
  94. View::SetEnabled(enabled);
  95. icon_button_->SetEnabled(enabled);
  96. if (!enabled) {
  97. label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  98. AshColorProvider::ContentLayerType::kTextColorSecondary));
  99. sub_label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  100. AshColorProvider::ContentLayerType::kTextColorSecondary));
  101. sub_label_->SetText(l10n_util::GetStringUTF16(
  102. IDS_ASH_PHONE_HUB_QUICK_ACTIONS_NOT_AVAILABLE_STATE));
  103. icon_button_->SetTooltipText(l10n_util::GetStringFUTF16(
  104. IDS_ASH_PHONE_HUB_QUICK_ACTIONS_NOT_AVAILABLE_STATE_TOOLTIP,
  105. GetItemLabel()));
  106. } else {
  107. label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  108. AshColorProvider::ContentLayerType::kTextColorPrimary));
  109. sub_label_->SetEnabledColor(sub_label_color_);
  110. }
  111. }
  112. bool QuickActionItem::HasFocus() const {
  113. return icon_button_->HasFocus() || label_->HasFocus() ||
  114. sub_label_->HasFocus();
  115. }
  116. void QuickActionItem::RequestFocus() {
  117. icon_button_->RequestFocus();
  118. }
  119. const char* QuickActionItem::GetClassName() const {
  120. return "QuickActionItem";
  121. }
  122. } // namespace ash