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
ash / wm / desks / desk_action_context_menu.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/wm/desks/desk_action_context_menu.h"
#include <string>
#include "ash/public/cpp/desk_profiles_delegate.h"
#include "ash/public/cpp/desk_template.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/wm/desks/desks_histogram_enums.h"
#include "ash/wm/desks/templates/saved_desk_save_desk_button.h"
#include "ash/wm/overview/overview_grid.h"
#include "ash/wm/overview/overview_session.h"
#include "ash/wm/overview/overview_utils.h"
#include "base/metrics/histogram_functions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/menu_source_type.mojom-forward.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/view.h"
namespace ash {
namespace {
// The size of desk profile icon.
constexpr int kIconProfileSize = 24;
// The size for selected profile checker icon.
constexpr int kCheckButtonSize = 20;
class MenuModelAdapter : public views::MenuModelAdapter {
public:
MenuModelAdapter(ui::SimpleMenuModel* model, base::WeakPtr<OverviewGrid> grid)
: views::MenuModelAdapter(model), grid_(grid) {}
views::MenuItemView* AppendMenuItem(views::MenuItemView* menu,
ui::MenuModel* model,
size_t index) override {
if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR) {
menu->AppendSeparator();
return nullptr;
}
const int command_id = model->GetCommandIdAt(index);
views::MenuItemView* item_view = menu->AppendMenuItem(command_id);
if (command_id >= DeskActionContextMenu::kDynamicProfileStart) {
item_view->SetSecondaryTitle(model->GetMinorTextAt(index));
}
item_view->SetIcon(model->GetIconAt(index));
// The save desk option may be disabled if there are unsupported windows.
if (command_id == DeskActionContextMenu::CommandId::kSaveAsTemplate ||
command_id == DeskActionContextMenu::CommandId::kSaveForLater) {
CHECK(grid_);
SaveDeskOptionStatus status =
grid_->GetEnableStateAndTooltipIDForTemplateType(
command_id == DeskActionContextMenu::CommandId::kSaveAsTemplate
? DeskTemplateType::kTemplate
: DeskTemplateType::kSaveAndRecall);
menu->SetTooltip(l10n_util::GetStringUTF16(status.tooltip_id),
command_id);
}
// If the minor icon is set, then it's expected to be the checkmark used to
// identify the currently selected desk profile. Note that simply doing
// `item_view->SetMinorIcon` does not render the icon where we want it.
if (auto minor_icon = model->GetMinorIconAt(index); !minor_icon.IsEmpty()) {
item_view->AddChildView(
views::Builder<views::BoxLayoutView>()
.SetOrientation(views::BoxLayout::Orientation::kHorizontal)
.SetCrossAxisAlignment(
views::BoxLayout::CrossAxisAlignment::kCenter)
.AddChild(views::Builder<views::ImageView>().SetImage(
std::move(minor_icon)))
.Build());
}
return item_view;
}
private:
// Used to get the enabled/disabled status and tooltip.
base::WeakPtr<OverviewGrid> grid_;
};
} // namespace
DeskActionContextMenu::Config::Config() = default;
DeskActionContextMenu::Config::Config(Config&&) = default;
DeskActionContextMenu::Config::~Config() = default;
DeskActionContextMenu::Config& DeskActionContextMenu::Config::operator=(
Config&&) = default;
DeskActionContextMenu::DeskActionContextMenu(Config config,
DeskMiniView* mini_view)
: config_(std::move(config)),
mini_view_(mini_view),
context_menu_model_(this) {
bool separator_needed = false;
auto maybe_add_separator = [&] {
if (separator_needed) {
context_menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
separator_needed = false;
}
};
// Set the accessible name for menu items here and then pipe the information
// to the view instances during `ShowContextMenuForViewImpl()`.
if (config_.profiles.size() > 1) {
for (size_t i = 0; i != config_.profiles.size(); ++i) {
const auto& summary = config_.profiles[i];
gfx::ImageSkia icon = gfx::ImageSkiaOperations::CreateResizedImage(
summary.icon, skia::ImageOperations::RESIZE_BEST,
gfx::Size(kIconProfileSize, kIconProfileSize));
context_menu_model_.AddItemWithIcon(
static_cast<int>(kDynamicProfileStart + i), summary.name,
ui::ImageModel::FromImageSkia(
gfx::ImageSkiaOperations::CreateImageWithRoundRectClip(
kIconProfileSize, icon)));
auto entry_index = context_menu_model_.GetItemCount() - 1;
context_menu_model_.SetMinorText(entry_index, summary.email);
int profile_a11y_id = IDS_ASH_DESKS_MENU_ITEM_PROFILE_NOT_CHECKED;
if (summary.profile_id == config_.current_lacros_profile_id) {
context_menu_model_.SetMinorIcon(
entry_index, ui::ImageModel::FromVectorIcon(
kHollowCheckCircleIcon,
cros_tokens::kCrosSysPrimary, kCheckButtonSize));
profile_a11y_id = IDS_ASH_DESKS_MENU_ITEM_PROFILE_CHECKED;
}
context_menu_model_.SetAccessibleNameAt(
entry_index, l10n_util::GetStringFUTF16(profile_a11y_id, summary.name,
summary.email));
}
context_menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
const std::u16string profile_manager_a11y =
l10n_util::GetStringUTF16(IDS_ASH_DESKS_OPEN_PROFILE_MANAGER);
context_menu_model_.AddItemWithIcon(
CommandId::kShowProfileManager, profile_manager_a11y,
ui::ImageModel::FromVectorIcon(
kSettingsIcon, cros_tokens::kCrosSysOnSurface, kCheckButtonSize));
context_menu_model_.SetAccessibleNameAt(
context_menu_model_.GetItemCount() - 1, profile_manager_a11y);
separator_needed = true;
}
// Accessible names should be set on each of the following menu items to
// ensure the labels are read properly by screen readers.
if (config_.save_template_target_name) {
maybe_add_separator();
const std::u16string save_template_a11y = l10n_util::GetStringUTF16(
IDS_ASH_DESKS_TEMPLATES_SAVE_DESK_AS_TEMPLATE_BUTTON);
context_menu_model_.AddItemWithIcon(
CommandId::kSaveAsTemplate, save_template_a11y,
ui::ImageModel::FromVectorIcon(kSaveDeskAsTemplateIcon,
ui::kColorAshSystemUIMenuIcon));
context_menu_model_.SetAccessibleNameAt(
context_menu_model_.GetItemCount() - 1, save_template_a11y);
// The save desk options may be disabled if there are unsupported windows.
OverviewSession* session = GetOverviewSession();
CHECK(session);
OverviewGrid* grid =
session->GetGridWithRootWindow(mini_view_->root_window());
CHECK(grid);
context_menu_model_.SetEnabledAt(
context_menu_model_.GetItemCount() - 1,
grid->GetEnableStateAndTooltipIDForTemplateType(
DeskTemplateType::kTemplate)
.enabled);
}
if (config_.save_later_target_name) {
maybe_add_separator();
const std::u16string save_later_a11y = l10n_util::GetStringUTF16(
IDS_ASH_DESKS_TEMPLATES_SAVE_DESK_FOR_LATER_BUTTON);
context_menu_model_.AddItemWithIcon(
CommandId::kSaveForLater, save_later_a11y,
ui::ImageModel::FromVectorIcon(kSaveDeskForLaterIcon,
ui::kColorAshSystemUIMenuIcon));
context_menu_model_.SetAccessibleNameAt(
context_menu_model_.GetItemCount() - 1, save_later_a11y);
// The save desk options may be disabled if there are unsupported windows.
OverviewSession* session = GetOverviewSession();
CHECK(session);
OverviewGrid* grid =
session->GetGridWithRootWindow(mini_view_->root_window());
CHECK(grid);
context_menu_model_.SetEnabledAt(
context_menu_model_.GetItemCount() - 1,
grid->GetEnableStateAndTooltipIDForTemplateType(
DeskTemplateType::kSaveAndRecall)
.enabled);
}
if (config_.combine_desks_target_name) {
maybe_add_separator();
const std::u16string combine_desks_a11y =
l10n_util::GetStringFUTF16(IDS_ASH_DESKS_COMBINE_DESKS_DESCRIPTION,
*config_.combine_desks_target_name);
context_menu_model_.AddItemWithIcon(
CommandId::kCombineDesks, combine_desks_a11y,
ui::ImageModel::FromVectorIcon(kCombineDesksIcon,
ui::kColorAshSystemUIMenuIcon));
context_menu_model_.SetAccessibleNameAt(
context_menu_model_.GetItemCount() - 1, combine_desks_a11y);
}
if (config_.close_all_target_name) {
maybe_add_separator();
const std::u16string close_all_a11y = l10n_util::GetStringFUTF16(
IDS_ASH_DESKS_CLOSE_ALL_DESCRIPTION, *config_.close_all_target_name);
context_menu_model_.AddItemWithIcon(
CommandId::kCloseAll, close_all_a11y,
ui::ImageModel::FromVectorIcon(kMediumOrLargeCloseButtonIcon,
ui::kColorAshSystemUIMenuIcon));
context_menu_model_.SetAccessibleNameAt(
context_menu_model_.GetItemCount() - 1, close_all_a11y);
}
OverviewGrid* grid = mini_view_->owner_bar()->overview_grid();
menu_model_adapter_ = std::make_unique<MenuModelAdapter>(
&context_menu_model_, grid ? grid->GetWeakPtr() : nullptr);
}
DeskActionContextMenu::~DeskActionContextMenu() = default;
void DeskActionContextMenu::MaybeCloseMenu() {
if (context_menu_runner_)
context_menu_runner_->Cancel();
}
void DeskActionContextMenu::ExecuteCommand(int command_id, int event_flags) {
switch (command_id) {
case CommandId::kSaveAsTemplate:
config_.save_template_callback.Run();
break;
case CommandId::kSaveForLater:
config_.save_later_callback.Run();
break;
case CommandId::kCombineDesks:
config_.combine_desks_callback.Run();
break;
case CommandId::kCloseAll:
config_.close_all_callback.Run();
break;
case CommandId::kShowProfileManager:
Shell::Get()->shell_delegate()->OpenProfileManager();
break;
default:
MaybeSetLacrosProfileId(command_id);
break;
}
}
void DeskActionContextMenu::MenuClosed(ui::SimpleMenuModel* menu) {
if (config_.on_context_menu_closed_callback) {
config_.on_context_menu_closed_callback.Run();
}
}
void DeskActionContextMenu::ShowContextMenuForViewImpl(
views::View* source,
const gfx::Point& point,
ui::mojom::MenuSourceType source_type) {
const int run_types = views::MenuRunner::USE_ASH_SYS_UI_LAYOUT |
views::MenuRunner::CONTEXT_MENU |
views::MenuRunner::FIXED_ANCHOR |
views::MenuRunner::SEND_GESTURE_EVENTS_TO_OWNER;
auto menu_root = menu_model_adapter_->CreateMenu();
root_menu_item_view_ = menu_root.get();
context_menu_runner_ =
std::make_unique<views::MenuRunner>(std::move(menu_root), run_types);
context_menu_runner_->RunMenuAt(source->GetWidget(),
/*button_controller=*/nullptr,
/*bounds=*/gfx::Rect(point, gfx::Size()),
config_.anchor_position, source_type);
// Pipe accessible names from the menu model to the view instances. Please
// note, a separator in menu model does *not* end up being represented by a
// view instance.
auto item_views = root_menu_item_view_->GetSubmenu()->GetMenuItems();
const size_t model_count = context_menu_model_.GetItemCount();
CHECK_LE(item_views.size(), model_count);
for (size_t view_index = 0, model_index = 0; model_index < model_count;
model_index++) {
if (auto a11y_name = context_menu_model_.GetAccessibleNameAt(model_index);
!a11y_name.empty()) {
item_views[view_index++]->GetViewAccessibility().SetName(a11y_name);
}
}
}
void DeskActionContextMenu::MaybeSetLacrosProfileId(int command_id) {
size_t profile_index = static_cast<size_t>(command_id - kDynamicProfileStart);
if (profile_index < config_.profiles.size()) {
config_.set_lacros_profile_id.Run(
config_.profiles[profile_index].profile_id);
}
}
} // namespace ash