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 / templates / saved_desk_grid_view.cc [blame]
// Copyright 2021 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/templates/saved_desk_grid_view.h"
#include <memory>
#include "ash/public/cpp/desk_template.h"
#include "ash/shell.h"
#include "ash/wm/desks/templates/saved_desk_constants.h"
#include "ash/wm/desks/templates/saved_desk_item_view.h"
#include "ash/wm/desks/templates/saved_desk_name_view.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_session.h"
#include "base/i18n/string_compare.h"
#include "base/memory/raw_ptr.h"
#include "base/ranges/algorithm.h"
#include "third_party/icu/source/i18n/unicode/coll.h"
#include "ui/accessibility/ax_enums.mojom-shared.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
constexpr int kLandscapeMaxColumns = 3;
constexpr int kPortraitMaxColumns = 2;
// This is the maximum number of saved desks we will show in the grid. This
// constant is used instead of the Desk model `GetMaxEntryCount()` because that
// takes into consideration the number of `policy_entries_`, which can cause it
// to exceed 6 items.
// Note: Because we are only showing a maximum number of saved desks, there are
// cases that not all existing saved desks will be displayed, such as when a
// user has more than the maximum count. Since we also don't update the grid
// whenever there is a change, deleting a saved desk may result in existing
// saved desks not being shown as well, if the user originally exceeded the max
// saved desk item count when the grid was first shown.
constexpr std::size_t kMaxItemCount = 6u;
constexpr gfx::Transform kEndTransform;
// Scale for adding/deleting grid items.
constexpr float kAddOrDeleteItemScale = 0.75f;
constexpr base::TimeDelta kBoundsChangeAnimationDuration =
base::Milliseconds(300);
constexpr base::TimeDelta kItemViewsScaleAndFadeDuration =
base::Milliseconds(50);
// Gets the scale transform for `view`. It returns a transform with a scale of
// `kAddOrDeleteItemScale`. The pivot of the scale animation will be the center
// point of the view.
gfx::Transform GetScaleTransformForView(views::View* view) {
gfx::Transform scale_transform;
scale_transform.Scale(kAddOrDeleteItemScale, kAddOrDeleteItemScale);
return gfx::TransformAboutPivot(
gfx::RectF(view->GetLocalBounds()).CenterPoint(), scale_transform);
}
} // namespace
SavedDeskGridView::SavedDeskGridView()
: bounds_animator_(this, /*use_transforms=*/true) {
// Bounds animator is unaffected by debug tools such as "--ui-slow-animations"
// flag, so manually multiply the duration here.
bounds_animator_.SetAnimationDuration(
ui::ScopedAnimationDurationScaleMode::duration_multiplier() *
kBoundsChangeAnimationDuration);
bounds_animator_.set_tween_type(gfx::Tween::LINEAR);
}
SavedDeskGridView::~SavedDeskGridView() = default;
void SavedDeskGridView::SortEntries(const base::Uuid& order_first_uuid) {
// Sort the `grid_items_` into alphabetical order based on saved desk name.
// Note that this doesn't update the order of the child views, but just sorts
// the vector. `Layout` is responsible for placing the views in the correct
// locations in the grid.
UErrorCode error_code = U_ZERO_ERROR;
std::unique_ptr<icu::Collator> collator(
icu::Collator::createInstance(error_code)); // Use current ICU locale.
DCHECK(U_SUCCESS(error_code));
// If there is a uuid that is to be placed first, move that saved desk to the
// front of the grid, and sort the rest of the entries after it.
auto rest = base::ranges::partition(
grid_items_,
[&order_first_uuid](const base::Uuid& uuid) {
return uuid == order_first_uuid;
},
&SavedDeskItemView::uuid);
std::sort(
rest, grid_items_.end(),
[&collator](const SavedDeskItemView* a, const SavedDeskItemView* b) {
return base::i18n::CompareString16WithCollator(
*collator, a->name_view()->GetText(),
b->name_view()->GetText()) < 0;
});
// A11y traverses views based on the order of the children, so we need to
// manually reorder the child views to match the order that they are
// displayed, which is the alphabetically sorted `grid_items_` order. If
// there was a newly saved desk item, the first item in the grid will
// be the new item, while the rest will be sorted alphabetically.
for (size_t i = 0; i < grid_items_.size(); i++)
ReorderChildView(grid_items_[i], i);
NotifyAccessibilityEvent(ax::mojom::Event::kTreeChanged, true);
if (bounds_animator_.IsAnimating())
bounds_animator_.Cancel();
DeprecatedLayoutImmediately();
}
void SavedDeskGridView::AddOrUpdateEntries(
const std::vector<raw_ptr<const DeskTemplate, VectorExperimental>>& entries,
const base::Uuid& order_first_uuid,
bool animate) {
std::vector<SavedDeskItemView*> new_grid_items;
for (const DeskTemplate* entry : entries) {
auto iter = base::ranges::find(grid_items_, entry->uuid(),
&SavedDeskItemView::uuid);
if (iter != grid_items_.end()) {
(*iter)->UpdateSavedDesk(*entry);
} else if (grid_items_.size() < kMaxItemCount) {
SavedDeskItemView* grid_item =
AddChildView(std::make_unique<SavedDeskItemView>(entry->Clone()));
grid_items_.push_back(grid_item);
if (animate)
new_grid_items.push_back(grid_item);
}
}
SortEntries(order_first_uuid);
// The preferred size of `SavedDeskGridView` is related to the number of
// items. Here our quantities may have changed which means the preferred size
// has period.
PreferredSizeChanged();
if (animate)
AnimateGridItems(new_grid_items);
}
void SavedDeskGridView::DeleteEntries(const std::vector<base::Uuid>& uuids,
bool delete_animation) {
for (const base::Uuid& uuid : uuids) {
auto iter = base::ranges::find(grid_items_, uuid, &SavedDeskItemView::uuid);
if (iter == grid_items_.end())
continue;
SavedDeskItemView* grid_item = *iter;
// Performs an animation of changing the deleted grid item opacity
// from 1 to 0 and scales down to `kAddOrDeleteItemScale`. `old_layer_tree`
// will be deleted when the animation is complete.
if (delete_animation) {
auto old_grid_item_layer_tree = wm::RecreateLayers(grid_item);
auto* old_grid_item_layer_tree_root = old_grid_item_layer_tree->root();
GetWidget()->GetLayer()->Add(old_grid_item_layer_tree_root);
views::AnimationBuilder()
.OnEnded(base::BindOnce(
[](std::unique_ptr<ui::LayerTreeOwner> layer_tree_owner) {},
std::move(old_grid_item_layer_tree)))
.Once()
.SetTransform(old_grid_item_layer_tree_root,
GetScaleTransformForView(grid_item))
.SetOpacity(old_grid_item_layer_tree_root, 1.f)
.SetDuration(kItemViewsScaleAndFadeDuration);
}
RemoveChildViewT(grid_item);
grid_items_.erase(iter);
}
AnimateGridItems(/*new_grid_items=*/{});
NotifyAccessibilityEvent(ax::mojom::Event::kTreeChanged, true);
}
bool SavedDeskGridView::IsSavedDeskNameBeingModified() const {
if (!GetWidget()->IsActive())
return false;
for (ash::SavedDeskItemView* grid_item : grid_items_) {
if (grid_item->IsNameBeingModified())
return true;
}
return false;
}
gfx::Size SavedDeskGridView::CalculatePreferredSize(
const views::SizeBounds& available_size) const {
if (grid_items_.empty())
return gfx::Size();
const size_t cols = GetMaxColumns();
const size_t rows = (grid_items_.size() + cols - 1) / cols;
DCHECK_GT(cols, 0u);
DCHECK_GT(rows, 0u);
const int item_width = SavedDeskItemView::kPreferredSize.width();
const int item_height = SavedDeskItemView::kPreferredSize.height();
return gfx::Size(cols * item_width + (cols - 1) * kSaveDeskPaddingDp,
rows * item_height + (rows - 1) * kSaveDeskPaddingDp);
}
void SavedDeskGridView::Layout(PassKey) {
if (grid_items_.empty())
return;
if (bounds_animator_.IsAnimating())
return;
const std::vector<gfx::Rect> positions = CalculateGridItemPositions();
for (size_t i = 0; i < grid_items_.size(); i++)
grid_items_[i]->SetBoundsRect(positions[i]);
}
void SavedDeskGridView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
// In the event where the bounds change while an animation is in progress
// (i.e. screen rotation), we need to ensure that we stop the current
// animation. This is because we block layouts while an animation is in
// progress.
if (bounds_animator_.IsAnimating())
bounds_animator_.Cancel();
}
bool SavedDeskGridView::IsAnimating() const {
return bounds_animator_.IsAnimating();
}
SavedDeskItemView* SavedDeskGridView::GetItemForUUID(const base::Uuid& uuid) {
if (!uuid.is_valid())
return nullptr;
auto it = base::ranges::find(grid_items_, uuid, &SavedDeskItemView::uuid);
return it == grid_items_.end() ? nullptr : *it;
}
size_t SavedDeskGridView::GetMaxColumns() const {
return layout_mode_ == LayoutMode::LANDSCAPE ? kLandscapeMaxColumns
: kPortraitMaxColumns;
}
std::vector<gfx::Rect> SavedDeskGridView::CalculateGridItemPositions() const {
std::vector<gfx::Rect> positions;
if (grid_items_.empty())
return positions;
const size_t count = grid_items_.size();
const gfx::Size grid_item_size = grid_items_[0]->GetPreferredSize();
const size_t max_column_count = GetMaxColumns();
const size_t column_count = std::min(count, max_column_count);
int x = 0;
int y = 0;
for (size_t i = 0; i < count; i++) {
if (i != 0 && i % column_count == 0) {
// Move the position to the start of the next row.
x = 0;
y += grid_item_size.height() + kSaveDeskPaddingDp;
}
positions.emplace_back(gfx::Point(x, y), grid_item_size);
x += grid_item_size.width() + kSaveDeskPaddingDp;
}
DCHECK_EQ(positions.size(), grid_items_.size());
return positions;
}
void SavedDeskGridView::AnimateGridItems(
const std::vector<SavedDeskItemView*>& new_grid_items) {
const std::vector<gfx::Rect> positions = CalculateGridItemPositions();
for (size_t i = 0; i < grid_items_.size(); i++) {
SavedDeskItemView* grid_item = grid_items_[i];
const gfx::Rect target_bounds = positions[i];
if (bounds_animator_.GetTargetBounds(grid_item) == target_bounds)
continue;
// This is a new grid_item, so do the scale up to identity and fade in
// animation. The animation is delayed to sync up with the
// `bounds_animator_` animation.
if (base::Contains(new_grid_items, grid_item)) {
grid_item->SetBoundsRect(target_bounds);
ui::Layer* layer = grid_item->layer();
layer->SetTransform(GetScaleTransformForView(grid_item));
layer->SetOpacity(0.f);
views::AnimationBuilder()
.Once()
.Offset(kBoundsChangeAnimationDuration -
kItemViewsScaleAndFadeDuration)
.SetTransform(layer, kEndTransform)
.SetOpacity(layer, 1.f)
.SetDuration(kItemViewsScaleAndFadeDuration);
continue;
}
bounds_animator_.AnimateViewTo(grid_item, target_bounds);
}
}
BEGIN_METADATA(SavedDeskGridView)
END_METADATA
} // namespace ash