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
ash / wm / desks / desk_button / desk_button_container.cc [blame]
// Copyright 2024 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_button/desk_button_container.h"
#include <vector>
#include "ash/public/cpp/desk_profiles_delegate.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/desk_button_widget.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/wm/desks/desk.h"
#include "ash/wm/desks/desk_button/desk_button.h"
#include "ash/wm/desks/desk_button/desk_switch_button.h"
#include "ash/wm/desks/desks_constants.h"
#include "ash/wm/desks/desks_controller.h"
#include "base/i18n/rtl.h"
#include "base/notreached.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/background.h"
#include "ui/views/view.h"
namespace ash {
DeskButtonContainer::DeskButtonContainer() = default;
DeskButtonContainer::~DeskButtonContainer() = default;
// static
bool DeskButtonContainer::ShouldShowDeskProfilesUi() {
if (!chromeos::features::IsDeskProfilesEnabled()) {
return false;
}
auto* desk_profiles_delegate = Shell::Get()->GetDeskProfilesDelegate();
if (!desk_profiles_delegate ||
desk_profiles_delegate->GetProfilesSnapshot().size() < 2u) {
return false;
}
return true;
}
// static
int DeskButtonContainer::GetMaxLength(bool zero_state) {
if (zero_state) {
return kDeskButtonContainerHeightVertical;
}
if (ShouldShowDeskProfilesUi()) {
return kDeskButtonContainerWidthHorizontalExpandedWithAvatar;
}
return kDeskButtonContainerWidthHorizontalExpandedNoAvatar;
}
void DeskButtonContainer::OnProfileUpsert(const LacrosProfileSummary& summary) {
UpdateUiAndLayoutIfNeeded(DesksController::Get()->active_desk());
}
void DeskButtonContainer::OnProfileRemoved(uint64_t profile_id) {
UpdateUiAndLayoutIfNeeded(DesksController::Get()->active_desk());
}
void DeskButtonContainer::OnFirstSessionStarted() {
// The desk profiles delegate will be available if lacros and desk profiles
// are both enabled.
desk_profiles_observer_.Reset();
if (auto* delegate = Shell::Get()->GetDeskProfilesDelegate()) {
desk_profiles_observer_.Observe(delegate);
}
}
gfx::Size DeskButtonContainer::CalculatePreferredSize(
const views::SizeBounds& available_size) const {
if (zero_state_) {
return {kDeskButtonContainerWidthVertical, GetPreferredLength()};
}
return {GetPreferredLength(), kDeskButtonContainerHeightHorizontal};
}
void DeskButtonContainer::Layout(PassKey) {
if (!desk_button_widget_) {
return;
}
if (zero_state_) {
desk_button_->SetBoundsRect(
gfx::Rect({kDeskButtonContainerInsetsVertical.left(),
kDeskButtonContainerInsetsVertical.top()},
desk_button_->GetPreferredSize()));
} else {
auto get_spacing = [&](views::View* view1, views::View* view2) {
if ((view1 == prev_desk_button_ && view2 == next_desk_button_) ||
(view1 == next_desk_button_ && view2 == prev_desk_button_)) {
return kDeskButtonSwitchButtonSpacing;
}
return kDeskButtonContainerChildSpacingHorizontal;
};
std::vector<views::View*> views_to_layout;
for (auto child : children()) {
if (child->GetVisible()) {
views_to_layout.emplace_back(child);
}
}
if (base::i18n::IsRTL()) {
std::reverse(views_to_layout.begin(), views_to_layout.end());
}
int x = kDeskButtonContainerInsetsHorizontal.left();
const int y = kDeskButtonContainerInsetsHorizontal.top();
for (size_t i = 0; i < views_to_layout.size(); i++) {
if (i) {
x += get_spacing(views_to_layout[i - 1], views_to_layout[i]);
}
views_to_layout[i]->SetBoundsRect(
gfx::Rect({x, y}, views_to_layout[i]->GetPreferredSize()));
x += views_to_layout[i]->GetPreferredSize().width();
}
}
}
void DeskButtonContainer::OnDeskAdded(const Desk* desk, bool from_undo) {
UpdateUiAndLayoutIfNeeded(DesksController::Get()->active_desk());
}
void DeskButtonContainer::OnDeskRemoved(const Desk* desk) {
UpdateUiAndLayoutIfNeeded(DesksController::Get()->active_desk());
}
void DeskButtonContainer::OnDeskReordered(int old_index, int new_index) {
UpdateUiAndLayoutIfNeeded(DesksController::Get()->active_desk());
}
void DeskButtonContainer::OnDeskActivationChanged(const Desk* activated,
const Desk* deactivated) {
UpdateUiAndLayoutIfNeeded(activated);
}
void DeskButtonContainer::OnDeskNameChanged(const Desk* desk,
const std::u16string& new_name) {
if (!desk->is_active()) {
return;
}
UpdateUi(desk);
}
void DeskButtonContainer::PrepareForAlignmentChange() {
UpdateUiAndLayoutIfNeeded(DesksController::Get()->active_desk());
}
int DeskButtonContainer::GetPreferredLength() const {
int len = 0;
if (zero_state_) {
len += kDeskButtonContainerInsetsVertical.height() +
desk_button_->GetPreferredSize().height();
} else {
len += kDeskButtonContainerInsetsHorizontal.left() +
desk_button_->GetPreferredSize().width();
if (prev_desk_button_->GetVisible() && next_desk_button_->GetVisible()) {
len += kDeskButtonContainerChildSpacingHorizontal +
prev_desk_button_->GetPreferredSize().width() +
kDeskButtonSwitchButtonSpacing +
prev_desk_button_->GetPreferredSize().width();
} else if (prev_desk_button_->GetVisible()) {
len += kDeskButtonContainerChildSpacingHorizontal +
prev_desk_button_->GetPreferredSize().width();
} else if (next_desk_button_->GetVisible()) {
len += kDeskButtonContainerChildSpacingHorizontal +
next_desk_button_->GetPreferredSize().width();
}
len += kDeskButtonContainerInsetsHorizontal.right();
}
return len;
}
bool DeskButtonContainer::IntersectsWithDeskButtonUi(
const gfx::Point& screen_location) const {
if (auto* widget = GetWidget(); widget && widget->IsVisible()) {
for (const auto view : children()) {
if (view->GetVisible() &&
view->GetBoundsInScreen().Contains(screen_location)) {
return true;
}
}
}
return false;
}
std::u16string DeskButtonContainer::GetTitleForView(
const views::View* view) const {
if (view == desk_button_) {
return desk_button_->GetTitle();
} else if (view == prev_desk_button_) {
return prev_desk_button_->GetTitle();
} else if (view == next_desk_button_) {
return next_desk_button_->GetTitle();
}
NOTREACHED();
}
void DeskButtonContainer::Init(DeskButtonWidget* desk_button_widget) {
CHECK(desk_button_widget);
desk_button_widget_ = desk_button_widget;
shelf_ = desk_button_widget_->shelf();
CHECK(shelf_);
zero_state_ = !shelf_->IsHorizontalAlignment();
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
SetFlipCanvasOnPaintForRTLUI(false);
AddChildView(views::Builder<DeskButton>()
.CopyAddressTo(&desk_button_)
.Init(/*desk_button_container=*/this)
.Build());
AddChildView(
views::Builder<DeskSwitchButton>()
.CopyAddressTo(&prev_desk_button_)
.Init(/*desk_button_container=*/this, DeskSwitchButton::Type::kPrev)
.SetVisible(!zero_state_)
.Build());
AddChildView(
views::Builder<DeskSwitchButton>()
.CopyAddressTo(&next_desk_button_)
.Init(/*desk_button_container=*/this, DeskSwitchButton::Type::kNext)
.SetVisible(!zero_state_)
.Build());
desks_observation_.Observe(DesksController::Get());
session_observer_.Observe(SessionController::Get());
}
void DeskButtonContainer::UpdateUi(const Desk* active_desk) {
SetBackground(zero_state_ ? nullptr
: views::CreateThemedRoundedRectBackground(
cros_tokens::kCrosSysSystemOnBase,
kDeskButtonContainerCornerRadius));
desk_button_->SetZeroState(zero_state_);
desk_button_->UpdateUi(active_desk);
prev_desk_button_->UpdateUi(active_desk);
next_desk_button_->UpdateUi(active_desk);
}
void DeskButtonContainer::UpdateUiAndLayoutIfNeeded(const Desk* active_desk) {
gfx::Size old_preferred_size = GetPreferredSize();
UpdateUi(active_desk);
if (GetPreferredSize() != old_preferred_size) {
desk_button_widget_->delegate_view()->InvalidateLayout();
}
}
void DeskButtonContainer::HandleLocaleChange() {
desk_button_->UpdateLocaleSpecificSettings();
prev_desk_button_->UpdateLocaleSpecificSettings();
next_desk_button_->UpdateLocaleSpecificSettings();
}
void DeskButtonContainer::MaybeShowContextMenu(views::View* source,
ui::LocatedEvent* event) {
if (!desk_button_->is_activated()) {
ui::mojom::MenuSourceType source_type = ui::mojom::MenuSourceType::kMouse;
if (event->type() == ui::EventType::kGestureLongPress) {
source_type = ui::mojom::MenuSourceType::kLongPress;
} else if (event->type() == ui::EventType::kGestureLongTap) {
source_type = ui::mojom::MenuSourceType::kLongTap;
}
gfx::Point location_in_screen(event->location());
View::ConvertPointToScreen(source, &location_in_screen);
source->ShowContextMenu(location_in_screen, source_type);
}
event->SetHandled();
event->StopPropagation();
}
void DeskButtonContainer::InitializeAccessibleProperties() {
desk_button()->UpdateAccessiblePreviousAndNextFocus();
}
BEGIN_METADATA(DeskButtonContainer)
END_METADATA
} // namespace ash