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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
ash / system / phonehub / app_stream_launcher_view.cc [blame]
// Copyright 2022 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/app_stream_launcher_view.h"
#include <cmath>
#include <memory>
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/controls/rounded_scroll_bar.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "ash/style/style_util.h"
#include "ash/style/typography.h"
#include "ash/system/phonehub/app_stream_launcher_item.h"
#include "ash/system/phonehub/app_stream_launcher_list_item.h"
#include "ash/system/phonehub/app_stream_launcher_view.h"
#include "ash/system/phonehub/phone_hub_view_ids.h"
#include "ash/system/phonehub/ui_constants.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/webui/eche_app_ui/mojom/eche_app.mojom.h"
#include "chromeos/ash/components/phonehub/notification.h"
#include "chromeos/ash/components/phonehub/phone_hub_manager.h"
#include "chromeos/ash/components/phonehub/user_action_recorder.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/text_constants.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/separator.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/view.h"
namespace ash {
namespace {
// Insets for the vertical scroll bar.
constexpr auto kVerticalScrollInsets = gfx::Insets::TLBR(1, 0, 1, 1);
constexpr auto kHeaderDefaultSpacing = gfx::Insets::VH(0, 0);
constexpr gfx::Size kDefaultAppListScrollViewSize = gfx::Size(400, 400);
// The horizontal interior margin for the apps page container - i.e. the margin
// between the apps page bounds and the page content.
constexpr int kHorizontalInteriorMargin = 25;
// Number of columns of apps in the grid
constexpr int kColumns = 4;
constexpr int kRowHeight = 70;
constexpr auto kHeaderViewInsets = gfx::Insets::TLBR(25, 15, 15, 15);
constexpr int kAppViewWidth = 50;
constexpr int kHeaderChildrenSpacing = 20;
// The padding between different sections within the apps page. Also used for
// interior apps page container margin.
constexpr int kVerticalPaddingBetweenSections = 16;
constexpr int kAppListItemHorizontalMargin = 16;
constexpr int kAppListItemSpacing = 8;
} // namespace
AppStreamLauncherView::AppStreamLauncherView(
phonehub::PhoneHubManager* phone_hub_manager)
: phone_hub_manager_(phone_hub_manager) {
SetID(PhoneHubViewID::kAppStreamLauncherView);
auto* layout_manager =
SetLayoutManager(std::make_unique<views::FlexLayout>());
layout_manager->SetInteriorMargin(gfx::Insets::VH(0, 0))
.SetOrientation(views::LayoutOrientation::kVertical)
.SetCollapseMargins(false)
.SetDefault(views::kMarginsKey, kHeaderDefaultSpacing)
.SetCrossAxisAlignment(views::LayoutAlignment::kStretch);
AddChildView(CreateHeaderView());
auto* app_list_view = AddChildView(CreateAppListView());
gfx::Size launcher_size;
if (phone_hub_manager->GetAppStreamLauncherDataModel() &&
phone_hub_manager->GetAppStreamLauncherDataModel()->launcher_height() >
kDefaultAppListScrollViewSize.height()) {
launcher_size = gfx::Size(
phone_hub_manager->GetAppStreamLauncherDataModel()->launcher_width(),
phone_hub_manager->GetAppStreamLauncherDataModel()->launcher_height());
} else {
launcher_size = kDefaultAppListScrollViewSize;
}
app_list_view->SetPreferredSize(launcher_size);
app_list_view->SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred,
views::MaximumFlexSizeRule::kPreferred,
/*adjust_height_for_width =*/false)
.WithWeight(1));
phone_hub_manager->GetUserActionRecorder()->RecordUiOpened();
UpdateFromDataModel();
if (phone_hub_manager->GetAppStreamLauncherDataModel())
phone_hub_manager->GetAppStreamLauncherDataModel()->AddObserver(this);
}
AppStreamLauncherView::~AppStreamLauncherView() {
if (phone_hub_manager_->GetAppStreamLauncherDataModel())
phone_hub_manager_->GetAppStreamLauncherDataModel()->RemoveObserver(this);
}
// The behavior is inspired from ash/app_list/views/app_list_bubble_apps_page.cc
std::unique_ptr<views::View> AppStreamLauncherView::CreateAppListView() {
// The entire page scrolls.
auto scroll_view = std::make_unique<views::ScrollView>(
views::ScrollView::ScrollWithLayers::kEnabled);
scroll_view->ClipHeightTo(0, std::numeric_limits<int>::max());
scroll_view->SetDrawOverflowIndicator(false);
// Don't paint a background. The bubble already has one.
scroll_view->SetBackgroundColor(std::nullopt);
// Arrow keys are used to select app icons.
scroll_view->SetAllowKeyboardScrolling(false);
// Scroll view will have a gradient mask layer.
scroll_view->SetPaintToLayer(ui::LAYER_NOT_DRAWN);
// Set up scroll bars.
scroll_view->SetHorizontalScrollBarMode(
views::ScrollView::ScrollBarMode::kDisabled);
auto vertical_scroll = std::make_unique<RoundedScrollBar>(
views::ScrollBar::Orientation::kVertical);
vertical_scroll->SetInsets(kVerticalScrollInsets);
vertical_scroll->SetSnapBackOnDragOutside(false);
scroll_view->SetVerticalScrollBar(std::move(vertical_scroll));
auto scroll_contents = std::make_unique<views::View>();
scroll_contents->SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred,
views::MaximumFlexSizeRule::kUnbounded,
/*adjust_height_for_width =*/false)
.WithWeight(1));
auto* layout =
scroll_contents->SetLayoutManager(std::make_unique<views::FlexLayout>());
layout->SetOrientation(views::LayoutOrientation::kVertical)
.SetCrossAxisAlignment(views::LayoutAlignment::kStretch);
if (features::IsEcheLauncherListViewEnabled()) {
layout->SetInteriorMargin(gfx::Insets::VH(kVerticalPaddingBetweenSections,
kAppListItemHorizontalMargin));
} else {
layout->SetInteriorMargin(gfx::Insets::VH(kVerticalPaddingBetweenSections,
kHorizontalInteriorMargin));
}
// All apps section.
items_container_ =
scroll_contents->AddChildView(std::make_unique<views::View>());
items_container_->SetPaintToLayer();
items_container_->layer()->SetFillsBoundsOpaquely(false);
scroll_view->SetContents(std::move(scroll_contents));
return scroll_view;
}
void AppStreamLauncherView::AppIconActivated(
phonehub::Notification::AppMetadata app) {
auto* interaction_handler_ =
phone_hub_manager_->GetRecentAppsInteractionHandler();
if (!interaction_handler_)
return;
interaction_handler_->NotifyRecentAppClicked(
app, eche_app::mojom::AppStreamLaunchEntryPoint::APPS_LIST);
}
void AppStreamLauncherView::UpdateFromDataModel() {
if (!items_container_)
return;
items_container_->RemoveAllChildViews();
if (!phone_hub_manager_->GetAppStreamLauncherDataModel())
return;
const std::vector<phonehub::Notification::AppMetadata>* apps_list =
phone_hub_manager_->GetAppStreamLauncherDataModel()
->GetAppsListSortedByName();
if (features::IsEcheLauncherListViewEnabled()) {
CreateListView(apps_list);
} else {
CreateGridView(apps_list);
}
}
std::unique_ptr<views::View> AppStreamLauncherView::CreateItemView(
const phonehub::Notification::AppMetadata& app) {
return std::make_unique<AppStreamLauncherItem>(
base::BindRepeating(&AppStreamLauncherView::AppIconActivated,
base::Unretained(this), app),
app);
}
std::unique_ptr<views::View> AppStreamLauncherView::CreateListItemView(
const phonehub::Notification::AppMetadata& app) {
return std::make_unique<AppStreamLauncherListItem>(
base::BindRepeating(&AppStreamLauncherView::AppIconActivated,
base::Unretained(this), app),
app);
}
std::unique_ptr<views::View> AppStreamLauncherView::CreateHeaderView() {
auto header = std::make_unique<views::View>();
header->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal, kHeaderViewInsets,
kHeaderChildrenSpacing));
header->SetBackground(views::CreateThemedSolidBackground(
kColorAshControlBackgroundColorInactive));
// Add arrowback button
arrow_back_button_ = header->AddChildView(CreateButton(
base::BindRepeating(&AppStreamLauncherView::OnArrowBackActivated,
weak_factory_.GetWeakPtr()),
kEcheArrowBackIcon, IDS_APP_ACCNAME_BACK));
views::Label* title = header->AddChildView(std::make_unique<views::Label>(
std::u16string(), views::style::CONTEXT_DIALOG_TITLE,
views::style::STYLE_PRIMARY,
gfx::DirectionalityMode::DIRECTIONALITY_AS_URL));
title->SetMultiLine(true);
title->SetAllowCharacterBreak(true);
title->SetProperty(views::kBoxLayoutFlexKey,
views::BoxLayoutFlexSpecification());
title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
title->SetText(
l10n_util::GetStringUTF16(IDS_ASH_PHONE_HUB_APP_STREAM_LAUNCHER_TITLE));
TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosHeadline1,
*title);
return header;
}
// Creates a button with the given callback, icon, and tooltip text.
// `message_id` is the resource id of the tooltip text of the icon.
std::unique_ptr<views::Button> AppStreamLauncherView::CreateButton(
views::Button::PressedCallback callback,
const gfx::VectorIcon& icon,
int message_id) {
SkColor color = AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kIconColorPrimary);
SkColor disabled_color = SkColorSetA(color, gfx::kDisabledControlAlpha);
auto button = views::CreateVectorImageButton(std::move(callback));
views::SetImageFromVectorIconWithColor(button.get(), icon, color,
disabled_color);
ash::StyleUtil::SetUpInkDropForButton(button.get(), gfx::Insets(),
/*highlight_on_hover=*/false,
/*highlight_on_focus=*/true);
views::FocusRing::Get(button.get())
->SetColorId(static_cast<ui::ColorId>(cros_tokens::kCrosSysFocusRing));
button->SetTooltipText(l10n_util::GetStringUTF16(message_id));
button->SizeToPreferredSize();
views::InstallCircleHighlightPathGenerator(button.get());
return button;
}
void AppStreamLauncherView::OnArrowBackActivated() {
phone_hub_manager_->GetAppStreamLauncherDataModel()
->SetShouldShowMiniLauncher(false);
}
void AppStreamLauncherView::ChildPreferredSizeChanged(View* child) {
// Resize the bubble when the child change its size.
PreferredSizeChanged();
}
void AppStreamLauncherView::ChildVisibilityChanged(View* child) {
// Resize the bubble when the child change its visibility.
PreferredSizeChanged();
}
phone_hub_metrics::Screen AppStreamLauncherView::GetScreenForMetrics() const {
return phone_hub_metrics::Screen::kMiniLauncher;
}
void AppStreamLauncherView::OnBubbleClose() {
RemoveAllChildViews();
}
void AppStreamLauncherView::OnAppListChanged() {
if (!features::IsEcheSWAEnabled() || !features::IsEcheLauncherEnabled())
return;
UpdateFromDataModel();
}
void AppStreamLauncherView::CreateListView(
const std::vector<phonehub::Notification::AppMetadata>* apps_list) {
items_container_->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical, gfx::Insets::VH(0, 0),
kAppListItemSpacing));
for (auto& app : *apps_list) {
items_container_->AddChildView(CreateListItemView(app));
}
}
void AppStreamLauncherView::CreateGridView(
const std::vector<phonehub::Notification::AppMetadata>* apps_list) {
auto* table_layout = items_container_->SetLayoutManager(
std::make_unique<views::TableLayout>());
int spacing = (kTrayMenuWidth - kHorizontalInteriorMargin * 2 -
kAppViewWidth * kColumns) /
(kColumns - 1);
for (int i = 0; i < kColumns; i++) {
table_layout->AddColumn(
views::LayoutAlignment::kStretch, views::LayoutAlignment::kStretch, 1.0,
views::TableLayout::ColumnSize::kUsePreferred, 0, 0);
if (i != kColumns - 1) {
table_layout->AddPaddingColumn(1.0, spacing);
}
}
table_layout->AddRows(ceil((double)apps_list->size() / kColumns),
views::TableLayout::kFixedSize, kRowHeight);
for (auto& app : *apps_list) {
items_container_->AddChildView(CreateItemView(app));
}
}
BEGIN_METADATA(AppStreamLauncherView)
END_METADATA
} // namespace ash