1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
ash / system / phonehub / phone_hub_more_apps_button.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/phonehub/phone_hub_more_apps_button.h"
#include <memory>
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/phonehub/phone_hub_app_count_icon.h"
#include "ash/system/phonehub/phone_hub_app_icon.h"
#include "ash/system/phonehub/phone_hub_app_loading_icon.h"
#include "ash/system/phonehub/phone_hub_metrics.h"
#include "base/metrics/histogram_functions.h"
#include "chromeos/ash/components/phonehub/app_stream_launcher_data_model.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/layout/table_layout.h"
namespace ash {
// Appearance constants in DIPs
constexpr int kMoreAppsButtonRowPadding = 20;
constexpr int kMoreAppsButtonColumnPadding = 2;
constexpr int kMoreAppsButtonBackgroundRadius = 18;
// The app icons in the LoadingView stagger the start of the loading animation
// to make the appearance of a ripple.
constexpr int kAnimationLoadingIconStaggerDelayInMs = 100;
class MoreAppsButtonBackground : public views::Background {
public:
MoreAppsButtonBackground() = default;
MoreAppsButtonBackground(const MoreAppsButtonBackground&) = delete;
MoreAppsButtonBackground& operator=(const MoreAppsButtonBackground&) = delete;
void Paint(gfx::Canvas* canvas, views::View* view) const override {
cc::PaintFlags flags;
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setAntiAlias(true);
flags.setColor(AshColorProvider::Get()->GetControlsLayerColor(
AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive));
canvas->DrawCircle(view->GetContentsBounds().CenterPoint(),
kMoreAppsButtonBackgroundRadius, flags);
}
};
PhoneHubMoreAppsButton::PhoneHubMoreAppsButton() {
InitLayout();
}
PhoneHubMoreAppsButton::PhoneHubMoreAppsButton(
phonehub::AppStreamLauncherDataModel* app_stream_launcher_data_model,
views::Button::PressedCallback callback)
: views::Button(std::move(callback)),
app_stream_launcher_data_model_(app_stream_launcher_data_model) {
CHECK(app_stream_launcher_data_model_);
SetFocusBehavior(FocusBehavior::ALWAYS);
GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_FULL_APPS_LIST_BUTTON_TITLE));
SetTooltipText(
l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_FULL_APPS_LIST_BUTTON_TITLE));
InitLayout();
app_stream_launcher_data_model_->AddObserver(this);
}
PhoneHubMoreAppsButton::~PhoneHubMoreAppsButton() {
if (app_stream_launcher_data_model_) {
app_stream_launcher_data_model_->RemoveObserver(this);
}
}
void PhoneHubMoreAppsButton::InitLayout() {
table_layout_ = SetLayoutManager(std::make_unique<views::TableLayout>());
table_layout_->AddPaddingColumn(/*horizontal_resize=*/3, /*width=*/0);
table_layout_->AddColumn(views::LayoutAlignment::kStretch,
views::LayoutAlignment::kStretch,
views::TableLayout::kFixedSize,
views::TableLayout::ColumnSize::kUsePreferred,
/*fixed_width=*/0, /*min_width=*/0);
table_layout_->AddPaddingColumn(views::TableLayout::kFixedSize,
kMoreAppsButtonColumnPadding);
table_layout_->AddColumn(views::LayoutAlignment::kStretch,
views::LayoutAlignment::kStretch,
views::TableLayout::kFixedSize,
views::TableLayout::ColumnSize::kUsePreferred,
/*fixed_width=*/0, /*min_width=*/0);
table_layout_->AddPaddingColumn(/*horizontal_resize=*/3, /*width=*/0);
table_layout_->AddRows(/*n=*/1, views::TableLayout::kFixedSize,
kMoreAppsButtonRowPadding);
table_layout_->AddPaddingRow(views::TableLayout::kFixedSize,
kMoreAppsButtonColumnPadding);
table_layout_->AddRows(/*n=*/1, views::TableLayout::kFixedSize,
kMoreAppsButtonRowPadding);
SetEnabled(false);
SetBackground(std::make_unique<MoreAppsButtonBackground>());
if (!app_stream_launcher_data_model_) {
return;
}
if (app_stream_launcher_data_model_->GetAppsListSortedByName()->empty()) {
load_app_list_latency_ = base::TimeTicks::Now();
StartLoadingAnimation(/*initial_delay=*/std::nullopt);
SetEnabled(false);
phone_hub_metrics::LogMoreAppsButtonAnimationOnShow(
phone_hub_metrics::MoreAppsButtonLoadingState::kAnimationShown);
} else {
LoadAppList();
SetEnabled(true);
// Reset the latency variable to a default state when there is already an
// apps list from the app stream launcher data model. This ensures we don't
// log the latency metric since the loaded list should already be shown.
load_app_list_latency_ = base::TimeTicks();
phone_hub_metrics::LogMoreAppsButtonAnimationOnShow(
phone_hub_metrics::MoreAppsButtonLoadingState::kMoreAppsButtonLoaded);
}
}
void PhoneHubMoreAppsButton::StartLoadingAnimation(
std::optional<base::TimeDelta> initial_delay) {
app_loading_icons_.clear();
RemoveAllChildViews();
for (size_t i = 0; i < 4; i++) {
AppLoadingIcon* app_loading_icon =
AddChildView(new AppLoadingIcon(AppIcon::kSizeSmall));
app_loading_icons_.push_back(app_loading_icon);
size_t x = i % 2;
size_t y = i / 2;
base::TimeDelta stagger_delay =
(x + y) * base::Milliseconds(kAnimationLoadingIconStaggerDelayInMs);
if (initial_delay) {
stagger_delay += *initial_delay;
}
app_loading_icon->StartLoadingAnimation(stagger_delay);
}
}
void PhoneHubMoreAppsButton::StopLoadingAnimation() {
for (AppLoadingIcon* app_loading_icon : app_loading_icons_) {
app_loading_icon->StopLoadingAnimation();
}
}
void PhoneHubMoreAppsButton::OnShouldShowMiniLauncherChanged() {}
void PhoneHubMoreAppsButton::OnAppListChanged() {
LoadAppList();
if (load_app_list_latency_ != base::TimeTicks()) {
phone_hub_metrics::LogMoreAppsButtonFullAppsLatency(base::TimeTicks::Now() -
load_app_list_latency_);
load_app_list_latency_ = base::TimeTicks();
}
}
void PhoneHubMoreAppsButton::LoadAppList() {
CHECK(app_stream_launcher_data_model_);
app_loading_icons_.clear();
RemoveAllChildViews();
const std::vector<phonehub::Notification::AppMetadata>* app_list =
app_stream_launcher_data_model_->GetAppsListSortedByName();
if (!app_list->empty()) {
auto app_count = std::min(app_list->size(), size_t{3});
for (size_t i = 0; i < app_count; i++) {
AddChildView(std::make_unique<AppIcon>(app_list->at(i).color_icon,
AppIcon::kSizeSmall));
}
}
AddChildView(std::make_unique<AppCountIcon>(app_list->size()));
SetEnabled(true);
}
BEGIN_METADATA(PhoneHubMoreAppsButton)
END_METADATA
} // namespace ash