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
ash / style / tab_slider.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/style/tab_slider.h"
#include <cstddef>
#include "ash/style/style_util.h"
#include "ash/style/tab_slider_button.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/view_class_properties.h"
namespace ash {
namespace {
constexpr ui::ColorId kSliderBackgroundColorId =
cros_tokens::kCrosSysSystemOnBase;
constexpr ui::ColorId kSelectorBackgroundColorId =
cros_tokens::kCrosSysSystemPrimaryContainer;
constexpr base::TimeDelta kSelectorAnimationDuration = base::Milliseconds(150);
} // namespace
//------------------------------------------------------------------------------
// TabSlider::SelectorView:
// The selector shows behind the selected slider button. When a button is
// selected, it moves from the previously selected button to the currently
// selected button.
class TabSlider::SelectorView : public views::View {
METADATA_HEADER(SelectorView, views::View)
public:
explicit SelectorView(bool has_animation) : has_animation_(has_animation) {
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
SetBackground(StyleUtil::CreateThemedFullyRoundedRectBackground(
kSelectorBackgroundColorId));
}
SelectorView(const SelectorView&) = delete;
SelectorView& operator=(const SelectorView&) = delete;
~SelectorView() override = default;
// Moves the selector to the selected button. Performs animation if
// `has_animation_` is true.
void MoveToSelectedButton(TabSliderButton* button) {
DCHECK(button);
DCHECK(button->selected());
if (button_ == button) {
return;
}
TabSliderButton* previous_button = button_;
button_ = button;
// Update selector's bounds with the selected button's bounds.
SetBoundsRect(button_->bounds());
// Performs an animation of the selector moving from the position of last
// selected button to the position of currently selected button, if needed.
if (!previous_button || !has_animation_) {
return;
}
auto* view_layer = layer();
gfx::Transform reverse_transform = gfx::TransformBetweenRects(
gfx::RectF(button_->GetMirroredBounds()),
gfx::RectF(previous_button->GetMirroredBounds()));
view_layer->SetTransform(reverse_transform);
ui::ScopedLayerAnimationSettings settings(view_layer->GetAnimator());
settings.SetTransitionDuration(kSelectorAnimationDuration);
view_layer->SetTransform(gfx::Transform());
}
private:
// Indicates if there is a movement animation.
const bool has_animation_;
// Now owned.
raw_ptr<TabSliderButton> button_ = nullptr;
};
BEGIN_METADATA(TabSlider, SelectorView)
END_METADATA
//------------------------------------------------------------------------------
// TabSlider:
TabSlider::TabSlider(size_t max_tab_num, const InitParams& params)
: max_tab_num_(max_tab_num),
params_(params),
selector_view_(AddChildView(
std::make_unique<SelectorView>(params.has_selector_animation))) {
// Add a fully rounded rect background if needed.
if (params_.has_background) {
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
SetBackground(StyleUtil::CreateThemedFullyRoundedRectBackground(
kSliderBackgroundColorId));
}
Init();
selector_view_->SetProperty(views::kViewIgnoredByLayoutKey, true);
enabled_changed_subscription_ = AddEnabledChangedCallback(base::BindRepeating(
&TabSlider::OnEnabledStateChanged, base::Unretained(this)));
}
TabSlider::~TabSlider() = default;
views::View* TabSlider::GetSelectorView() {
return selector_view_;
}
TabSliderButton* TabSlider::GetButtonAtIndex(size_t index) {
CHECK(index < buttons_.size());
return buttons_[index];
}
void TabSlider::OnButtonSelected(TabSliderButton* button) {
DCHECK(button);
DCHECK(base::Contains(buttons_, button));
DCHECK(button->selected());
// Deselect all the other buttons and check if the tab slider has focus.
bool has_focus = false;
for (ash::TabSliderButton* b : buttons_) {
b->SetSelected(b == button);
has_focus |= b->HasFocus();
}
// Move the selector to the selected button.
selector_view_->MoveToSelectedButton(button);
// Move the focus to the selected button.
if (has_focus) {
GetFocusManager()->SetFocusedView(button);
}
}
void TabSlider::Layout(PassKey) {
LayoutSuperclass<views::View>(this);
// Synchronize the selector bounds with selected button's bounds.
auto it =
std::find_if(buttons_.begin(), buttons_.end(),
[](TabSliderButton* button) { return button->selected(); });
if (it == buttons_.end()) {
return;
}
selector_view_->SetBoundsRect((*it)->bounds());
}
void TabSlider::Init() {
const int internal_border_padding = params_.internal_border_padding;
// Create rows:
// Add top border padding row.
AddPaddingRow(views::TableLayout::kFixedSize, internal_border_padding);
// Add middle buttons row.
AddRows(1, views::TableLayout::kFixedSize);
// Add bottom border padding row.
AddPaddingRow(views::TableLayout::kFixedSize, internal_border_padding);
// Create columns:
// Add left border padding column.
AddPaddingColumn(views::TableLayout::kFixedSize, internal_border_padding);
// Alternatively add button column and padding column.
std::vector<size_t> columns_containing_buttons;
for (size_t i = 0; i < max_tab_num_; ++i) {
AddColumn(views::LayoutAlignment::kStretch, views::LayoutAlignment::kCenter,
1.0f, views::TableLayout::ColumnSize::kUsePreferred, 0, 0);
columns_containing_buttons.push_back(2 * i + 1);
if (i != max_tab_num_ - 1) {
AddPaddingColumn(views::TableLayout::kFixedSize,
params_.between_buttons_spacing);
}
}
// Add right border padding column.
AddPaddingColumn(views::TableLayout::kFixedSize, internal_border_padding);
if (params_.distribute_space_evenly) {
// Ensure extra space is spread evenly between the button containing
// columns.
LinkColumnSizes(columns_containing_buttons);
}
}
void TabSlider::AddButtonInternal(TabSliderButton* button) {
CHECK(button);
CHECK_LT(buttons_.size(), max_tab_num_)
<< "Number of buttons reaches the limit";
// Add the button as a child of the tab slider and insert it in the
// `buttons_` list.
AddChildView(button);
buttons_.emplace_back(button);
button->AddedToSlider(this);
}
void TabSlider::OnEnabledStateChanged() {
// Propagate the enabled state to all slider buttons and the selector view.
const bool enabled = GetEnabled();
for (ash::TabSliderButton* b : buttons_) {
b->SetEnabled(enabled);
}
selector_view_->SetEnabled(enabled);
SchedulePaint();
}
BEGIN_METADATA(TabSlider)
END_METADATA
} // namespace ash