[go: nahoru, domu]

blob: 8a3a836d2bd1e15f39555965fb3e0382a3879a39 [file] [log] [blame]
Andre Lebcad17f2020-09-30 21:13:551// 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 Wang269f0e42020-10-28 22:56:2712#include "ui/gfx/geometry/insets.h"
Andre Lebcad17f2020-09-30 21:13:5513#include "ui/views/controls/label.h"
14#include "ui/views/layout/box_layout.h"
15
16namespace ash {
17
18namespace {
19
Meilin Wang269f0e42020-10-28 22:56:2720void ConfigureLabel(views::Label* label, int line_height, int font_size) {
Andre Lebcad17f2020-09-30 21:13:5521 label->SetAutoColorReadabilityEnabled(false);
22 label->SetSubpixelRenderingEnabled(false);
23 label->SetCanProcessEventsWithinSubtree(false);
24
Meilin Wang269f0e42020-10-28 22:56:2725 label->SetLineHeight(line_height);
Andre Lebcad17f2020-09-30 21:13:5526
27 gfx::Font default_font;
Meilin Wang269f0e42020-10-28 22:56:2728 gfx::Font label_font =
29 default_font.Derive(font_size - default_font.GetFontSize(),
30 gfx::Font::NORMAL, gfx::Font::Weight::NORMAL);
Andre Lebcad17f2020-09-30 21:13:5531 gfx::FontList font_list(label_font);
32 label->SetFontList(font_list);
33}
34
35} // namespace
36
37QuickActionItem::QuickActionItem(Delegate* delegate,
38 int label_id,
39 const gfx::VectorIcon& icon_on,
40 const gfx::VectorIcon& icon_off)
Peter Kasting014a0582020-11-05 02:02:4041 : icon_on_(icon_on), icon_off_(icon_off) {
Meilin Wang269f0e42020-10-28 22:56:2742 SetPreferredSize(kUnifiedFeaturePodSize);
Andre Lebcad17f2020-09-30 21:13:5543 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 Kasting014a0582020-11-05 02:02:4049 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 Lebcad17f2020-09-30 21:13:5556
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 Wang269f0e42020-10-28 22:56:2763 label_->SetBorder(views::CreateEmptyBorder(
64 gfx::Insets(0, 0, /*bottom=*/kUnifiedFeaturePodInterLabelPadding, 0)));
Andre Lebcad17f2020-09-30 21:13:5565 sub_label_ = label_view->AddChildView(std::make_unique<views::Label>());
Meilin Wang269f0e42020-10-28 22:56:2766 ConfigureLabel(label_, kUnifiedFeaturePodLabelLineHeight,
67 kUnifiedFeaturePodLabelFontSize);
68 ConfigureLabel(sub_label_, kUnifiedFeaturePodSubLabelLineHeight,
69 kUnifiedFeaturePodSubLabelFontSize);
Andre Le086a9cd2020-10-02 18:35:3170
Andre Lec439739b2020-11-02 23:05:0271 sub_label_color_ = AshColorProvider::Get()->GetContentLayerColor(
72 AshColorProvider::ContentLayerType::kTextColorSecondary);
73
Andre Le086a9cd2020-10-02 18:35:3174 SetEnabled(true /* enabled */);
Andre Lebcad17f2020-09-30 21:13:5575
76 SetPaintToLayer();
77 layer()->SetFillsBoundsOpaquely(false);
78}
79
80QuickActionItem::QuickActionItem(Delegate* delegate,
81 int label_id,
82 const gfx::VectorIcon& icon)
83 : QuickActionItem(delegate, label_id, icon, icon) {}
84
85QuickActionItem::~QuickActionItem() = default;
86
Jan Wilken Dörrie85285b02021-03-11 23:38:4787void QuickActionItem::SetSubLabel(const std::u16string& sub_label) {
Andre Lebcad17f2020-09-30 21:13:5588 sub_label_->SetText(sub_label);
89}
90
Andre Lec439739b2020-11-02 23:05:0291void 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 Le086a9cd2020-10-02 18:35:3198void QuickActionItem::SetIcon(bool is_on) {
99 icon_button_->SetVectorIcon(is_on ? icon_on_ : icon_off_);
100}
101
Jan Wilken Dörrie85285b02021-03-11 23:38:47102void QuickActionItem::SetIconTooltip(const std::u16string& text) {
Andre Lebcad17f2020-09-30 21:13:55103 icon_button_->SetTooltipText(text);
104}
105
106void QuickActionItem::SetToggled(bool toggled) {
107 icon_button_->SetToggled(toggled);
Andre Le086a9cd2020-10-02 18:35:31108 SetIcon(toggled /* is_on */);
Andre Lebcad17f2020-09-30 21:13:55109}
110
111bool QuickActionItem::IsToggled() const {
112 return icon_button_->toggled();
113}
114
Jan Wilken Dörrie85285b02021-03-11 23:38:47115const std::u16string& QuickActionItem::GetItemLabel() const {
Andre Lebcad17f2020-09-30 21:13:55116 return label_->GetText();
117}
118
119void QuickActionItem::SetEnabled(bool enabled) {
120 View::SetEnabled(enabled);
121 icon_button_->SetEnabled(enabled);
122
123 if (!enabled) {
Jimmy Gonge0b3c1e2020-12-17 23:54:47124 label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
125 AshColorProvider::ContentLayerType::kTextColorSecondary));
126 sub_label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
127 AshColorProvider::ContentLayerType::kTextColorSecondary));
Andre Lebcad17f2020-09-30 21:13:55128
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 Le086a9cd2020-10-02 18:35:31134 SetIcon(false /* is_on */);
Andre Lebcad17f2020-09-30 21:13:55135 } else {
Meilin Wang269f0e42020-10-28 22:56:27136 label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
137 AshColorProvider::ContentLayerType::kTextColorPrimary));
Andre Lec439739b2020-11-02 23:05:02138 sub_label_->SetEnabledColor(sub_label_color_);
Andre Lebcad17f2020-09-30 21:13:55139 }
140}
141
Andre Lebcad17f2020-09-30 21:13:55142bool QuickActionItem::HasFocus() const {
143 return icon_button_->HasFocus() || label_->HasFocus() ||
144 sub_label_->HasFocus();
145}
146
147void QuickActionItem::RequestFocus() {
148 icon_button_->RequestFocus();
149}
150
151const char* QuickActionItem::GetClassName() const {
152 return "QuickActionItem";
153}
154
155} // namespace ash