Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 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 | |
| 5 | #include "ash/system/phonehub/quick_action_item.h" |
| 6 | |
| 7 | #include "ash/resources/vector_icons/vector_icons.h" |
| 8 | #include "ash/strings/grit/ash_strings.h" |
| 9 | #include "ash/style/ash_color_provider.h" |
| 10 | #include "ash/system/tray/tray_constants.h" |
| 11 | #include "ui/base/l10n/l10n_util.h" |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 12 | #include "ui/gfx/geometry/insets.h" |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 13 | #include "ui/views/controls/label.h" |
| 14 | #include "ui/views/layout/box_layout.h" |
| 15 | |
| 16 | namespace ash { |
| 17 | |
| 18 | namespace { |
| 19 | |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 20 | void ConfigureLabel(views::Label* label, int line_height, int font_size) { |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 21 | label->SetAutoColorReadabilityEnabled(false); |
| 22 | label->SetSubpixelRenderingEnabled(false); |
| 23 | label->SetCanProcessEventsWithinSubtree(false); |
| 24 | |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 25 | label->SetLineHeight(line_height); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 26 | |
| 27 | gfx::Font default_font; |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 28 | gfx::Font label_font = |
| 29 | default_font.Derive(font_size - default_font.GetFontSize(), |
| 30 | gfx::Font::NORMAL, gfx::Font::Weight::NORMAL); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 31 | gfx::FontList font_list(label_font); |
| 32 | label->SetFontList(font_list); |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | QuickActionItem::QuickActionItem(Delegate* delegate, |
| 38 | int label_id, |
| 39 | const gfx::VectorIcon& icon_on, |
| 40 | const gfx::VectorIcon& icon_off) |
Peter Kasting | 014a058 | 2020-11-05 02:02:40 | [diff] [blame] | 41 | : icon_on_(icon_on), icon_off_(icon_off) { |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 42 | SetPreferredSize(kUnifiedFeaturePodSize); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 43 | auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>( |
| 44 | views::BoxLayout::Orientation::kVertical, gfx::Insets(), |
| 45 | kUnifiedFeaturePodSpacing)); |
| 46 | layout->set_cross_axis_alignment( |
| 47 | views::BoxLayout::CrossAxisAlignment::kCenter); |
| 48 | |
Peter Kasting | 014a058 | 2020-11-05 02:02:40 | [diff] [blame] | 49 | icon_button_ = AddChildView(std::make_unique<FeaturePodIconButton>( |
| 50 | base::BindRepeating( |
| 51 | [](Delegate* delegate, QuickActionItem* item) { |
| 52 | delegate->OnButtonPressed(item->IsToggled()); |
| 53 | }, |
| 54 | delegate, this), |
| 55 | true /* is_togglable */)); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 56 | |
| 57 | auto* label_view = AddChildView(std::make_unique<views::View>()); |
| 58 | label_view->SetLayoutManager(std::make_unique<views::BoxLayout>( |
| 59 | views::BoxLayout::Orientation::kVertical, gfx::Insets())); |
| 60 | |
| 61 | label_ = label_view->AddChildView( |
| 62 | std::make_unique<views::Label>(l10n_util::GetStringUTF16(label_id))); |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 63 | label_->SetBorder(views::CreateEmptyBorder( |
| 64 | gfx::Insets(0, 0, /*bottom=*/kUnifiedFeaturePodInterLabelPadding, 0))); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 65 | sub_label_ = label_view->AddChildView(std::make_unique<views::Label>()); |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 66 | ConfigureLabel(label_, kUnifiedFeaturePodLabelLineHeight, |
| 67 | kUnifiedFeaturePodLabelFontSize); |
| 68 | ConfigureLabel(sub_label_, kUnifiedFeaturePodSubLabelLineHeight, |
| 69 | kUnifiedFeaturePodSubLabelFontSize); |
Andre Le | 086a9cd | 2020-10-02 18:35:31 | [diff] [blame] | 70 | |
Andre Le | c439739b | 2020-11-02 23:05:02 | [diff] [blame] | 71 | sub_label_color_ = AshColorProvider::Get()->GetContentLayerColor( |
| 72 | AshColorProvider::ContentLayerType::kTextColorSecondary); |
| 73 | |
Andre Le | 086a9cd | 2020-10-02 18:35:31 | [diff] [blame] | 74 | SetEnabled(true /* enabled */); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 75 | |
| 76 | SetPaintToLayer(); |
| 77 | layer()->SetFillsBoundsOpaquely(false); |
| 78 | } |
| 79 | |
| 80 | QuickActionItem::QuickActionItem(Delegate* delegate, |
| 81 | int label_id, |
| 82 | const gfx::VectorIcon& icon) |
| 83 | : QuickActionItem(delegate, label_id, icon, icon) {} |
| 84 | |
| 85 | QuickActionItem::~QuickActionItem() = default; |
| 86 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 87 | void QuickActionItem::SetSubLabel(const std::u16string& sub_label) { |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 88 | sub_label_->SetText(sub_label); |
| 89 | } |
| 90 | |
Andre Le | c439739b | 2020-11-02 23:05:02 | [diff] [blame] | 91 | void QuickActionItem::SetSubLabelColor(SkColor color) { |
| 92 | if (sub_label_color_ == color) |
| 93 | return; |
| 94 | sub_label_color_ = color; |
| 95 | sub_label_->SetEnabledColor(sub_label_color_); |
| 96 | } |
| 97 | |
Andre Le | 086a9cd | 2020-10-02 18:35:31 | [diff] [blame] | 98 | void QuickActionItem::SetIcon(bool is_on) { |
| 99 | icon_button_->SetVectorIcon(is_on ? icon_on_ : icon_off_); |
| 100 | } |
| 101 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 102 | void QuickActionItem::SetIconTooltip(const std::u16string& text) { |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 103 | icon_button_->SetTooltipText(text); |
| 104 | } |
| 105 | |
| 106 | void QuickActionItem::SetToggled(bool toggled) { |
| 107 | icon_button_->SetToggled(toggled); |
Andre Le | 086a9cd | 2020-10-02 18:35:31 | [diff] [blame] | 108 | SetIcon(toggled /* is_on */); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool QuickActionItem::IsToggled() const { |
| 112 | return icon_button_->toggled(); |
| 113 | } |
| 114 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 115 | const std::u16string& QuickActionItem::GetItemLabel() const { |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 116 | return label_->GetText(); |
| 117 | } |
| 118 | |
| 119 | void QuickActionItem::SetEnabled(bool enabled) { |
| 120 | View::SetEnabled(enabled); |
| 121 | icon_button_->SetEnabled(enabled); |
| 122 | |
| 123 | if (!enabled) { |
Jimmy Gong | e0b3c1e | 2020-12-17 23:54:47 | [diff] [blame] | 124 | label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor( |
| 125 | AshColorProvider::ContentLayerType::kTextColorSecondary)); |
| 126 | sub_label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor( |
| 127 | AshColorProvider::ContentLayerType::kTextColorSecondary)); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 128 | |
| 129 | sub_label_->SetText(l10n_util::GetStringUTF16( |
| 130 | IDS_ASH_PHONE_HUB_QUICK_ACTIONS_NOT_AVAILABLE_STATE)); |
| 131 | icon_button_->SetTooltipText(l10n_util::GetStringFUTF16( |
| 132 | IDS_ASH_PHONE_HUB_QUICK_ACTIONS_NOT_AVAILABLE_STATE_TOOLTIP, |
| 133 | GetItemLabel())); |
Andre Le | 086a9cd | 2020-10-02 18:35:31 | [diff] [blame] | 134 | SetIcon(false /* is_on */); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 135 | } else { |
Meilin Wang | 269f0e4 | 2020-10-28 22:56:27 | [diff] [blame] | 136 | label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor( |
| 137 | AshColorProvider::ContentLayerType::kTextColorPrimary)); |
Andre Le | c439739b | 2020-11-02 23:05:02 | [diff] [blame] | 138 | sub_label_->SetEnabledColor(sub_label_color_); |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Andre Le | bcad17f | 2020-09-30 21:13:55 | [diff] [blame] | 142 | bool QuickActionItem::HasFocus() const { |
| 143 | return icon_button_->HasFocus() || label_->HasFocus() || |
| 144 | sub_label_->HasFocus(); |
| 145 | } |
| 146 | |
| 147 | void QuickActionItem::RequestFocus() { |
| 148 | icon_button_->RequestFocus(); |
| 149 | } |
| 150 | |
| 151 | const char* QuickActionItem::GetClassName() const { |
| 152 | return "QuickActionItem"; |
| 153 | } |
| 154 | |
| 155 | } // namespace ash |