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
ash / style / style_viewer / pagination_instances_grid_view_factory.cc [blame]
// Copyright 2023 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/style_viewer/system_ui_components_grid_view_factories.h"
#include "ash/public/cpp/pagination/pagination_controller.h"
#include "ash/public/cpp/pagination/pagination_model.h"
#include "ash/public/cpp/pagination/pagination_model_observer.h"
#include "ash/style/pagination_view.h"
#include "ash/style/style_viewer/system_ui_components_grid_view.h"
#include "base/functional/bind.h"
#include "base/scoped_observation.h"
#include "ui/events/event.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/layout/box_layout_view.h"
namespace ash {
namespace {
// Configurations of grid view for `Pagination` instances.
constexpr size_t kGridViewRowNum = 3;
constexpr size_t kGridViewColNum = 2;
constexpr size_t kGridViewRowGroupSize = 1;
constexpr size_t kGridViewColGroupSize = 1;
// The size of a test page.
constexpr size_t kPageWidth = 100;
constexpr size_t kPageHeight = 30;
//------------------------------------------------------------------------------
// PaginationTestContents:
// A view binded with a pagination controller. It dispatches the dragging event
// to the pagination controller.
class PaginationTestContents : public views::BoxLayoutView {
public:
PaginationTestContents(PaginationController* pagination_controller,
views::BoxLayout::Orientation orientation)
: pagination_controller_(pagination_controller) {
SetOrientation(orientation);
}
PaginationTestContents(const PaginationTestContents&) = delete;
PaginationTestContents& operator=(const PaginationTestContents&) = delete;
~PaginationTestContents() override = default;
// views::BoxLayoutView:
bool OnMousePressed(const ui::MouseEvent& event) override {
// Record potential dragging origin.
dragging_origin_ = event.target()->GetScreenLocationF(event);
return true;
}
bool OnMouseDragged(const ui::MouseEvent& event) override {
gfx::PointF dragging_pos = event.target()->GetScreenLocationF(event);
gfx::Vector2dF offset = dragging_pos - dragging_origin_;
if (!is_dragging_) {
pagination_controller_->StartMouseDrag(offset);
is_dragging_ = true;
} else {
pagination_controller_->UpdateMouseDrag(
offset, gfx::Rect(0, 0, kPageWidth, kPageHeight));
}
dragging_origin_ = dragging_pos;
return true;
}
void OnMouseReleased(const ui::MouseEvent& event) override {
if (!is_dragging_) {
return;
}
pagination_controller_->EndMouseDrag(event);
is_dragging_ = false;
}
private:
// The current dragging location.
gfx::PointF dragging_origin_;
// True if the content is being dragged.
bool is_dragging_ = false;
raw_ptr<PaginationController> const pagination_controller_;
};
//------------------------------------------------------------------------------
// PaginationTestScrollView:
// A scroll view with as many labels as the number of pages in a pagination
// model. Each label corresponds to a page. Eveytime a page is selected, the
// view will scroll to show corresponding label.
class PaginationTestScrollView : public views::ScrollView,
public PaginationModelObserver {
public:
PaginationTestScrollView(PaginationModel* model,
PaginationView::Orientation orientation)
: model_(model),
orientation_(orientation),
pagination_controller_(std::make_unique<PaginationController>(
model_,
(orientation == PaginationView::Orientation::kHorizontal)
? PaginationController::SCROLL_AXIS_HORIZONTAL
: PaginationController::SCROLL_AXIS_VERTICAL,
base::BindRepeating([](ui::EventType) {}))),
page_container_(SetContents(std::make_unique<PaginationTestContents>(
pagination_controller_.get(),
(orientation == PaginationView::Orientation::kHorizontal)
? views::BoxLayout::Orientation::kHorizontal
: views::BoxLayout::Orientation::kVertical))) {
model_observer_.Observe(model_);
SetHorizontalScrollBarMode(views::ScrollView::ScrollBarMode::kDisabled);
SetVerticalScrollBarMode(views::ScrollView::ScrollBarMode::kDisabled);
if (model_->total_pages() > 0) {
TotalPagesChanged(0, model_->total_pages());
}
}
PaginationTestScrollView(const PaginationTestScrollView&) = delete;
PaginationTestScrollView& operator=(const PaginationTestScrollView&) = delete;
~PaginationTestScrollView() override = default;
// views::ScrollView:
gfx::Size CalculatePreferredSize(
const views::SizeBounds& available_size) const override {
return gfx::Size(kPageWidth, kPageHeight);
}
void Layout(PassKey) override {
page_container_->SizeToPreferredSize();
LayoutSuperclass<views::ScrollView>(this);
}
// PaginationModelObserver:
void TotalPagesChanged(int previous_page_count, int new_page_count) override {
previous_page_count = std::max(0, previous_page_count);
new_page_count = std::max(0, new_page_count);
if (previous_page_count == new_page_count) {
return;
}
// Synchronize the number of labels with total pages.
if (previous_page_count < new_page_count) {
for (int i = previous_page_count; i < new_page_count; i++) {
auto* page =
page_container_->AddChildView(std::make_unique<views::Label>(
u"Page " + base::NumberToString16(i + 1)));
page->SetPreferredSize(gfx::Size(kPageWidth, kPageHeight));
page->SetLineHeight(kPageHeight);
}
} else {
for (int i = previous_page_count; i > new_page_count; i--) {
page_container_->RemoveChildView(page_container_->children().back());
}
}
}
void SelectedPageChanged(int old_selected, int new_selected) override {
// Scroll to show the label corresponding to the selected page.
if (model_->is_valid_page(new_selected)) {
if (orientation_ == PaginationView::Orientation::kHorizontal) {
page_container_->SetX(-new_selected * kPageWidth);
} else {
page_container_->SetY(-new_selected * kPageHeight);
}
}
}
void TransitionChanged() override {
// Update scrolling during the page transition.
const bool horizontal =
(orientation_ == PaginationView::Orientation::kHorizontal);
const int page_size = horizontal ? kPageWidth : kPageHeight;
const int origin_offset = -model_->selected_page() * page_size;
const int target_offset = -model_->transition().target_page * page_size;
const double progress = model_->transition().progress;
const int offset =
(1 - progress) * origin_offset + progress * target_offset;
if (orientation_ == PaginationView::Orientation::kHorizontal) {
page_container_->SetX(offset);
} else {
page_container_->SetY(offset);
}
}
private:
raw_ptr<PaginationModel> const model_;
PaginationView::Orientation orientation_;
std::unique_ptr<PaginationController> pagination_controller_;
raw_ptr<PaginationTestContents> page_container_;
base::ScopedObservation<PaginationModel, PaginationModelObserver>
model_observer_{this};
};
//------------------------------------------------------------------------------
// PaginationGridView:
class PaginationGridView : public SystemUIComponentsGridView {
public:
PaginationGridView(size_t num_row, size_t num_col)
: SystemUIComponentsGridView(num_row, num_col, num_row, num_col) {}
PaginationGridView(const PaginationGridView&) = delete;
PaginationGridView& operator=(const PaginationGridView&) = delete;
~PaginationGridView() override = default;
// Add a pagination instance and a test view binded with the given pagination
// model.
void AddPaginationWithModel(
const std::u16string& name,
PaginationView::Orientation orientation,
std::unique_ptr<PaginationModel> pagination_model) {
AddInstance(name, std::make_unique<PaginationView>(pagination_model.get(),
orientation));
AddInstance(u"", std::make_unique<PaginationTestScrollView>(
pagination_model.get(), orientation));
model_ = std::move(pagination_model);
}
private:
std::unique_ptr<PaginationModel> model_;
};
} // namespace
std::unique_ptr<SystemUIComponentsGridView>
CreatePaginationInstancesGridView() {
auto grid_view = std::make_unique<SystemUIComponentsGridView>(
kGridViewRowNum, kGridViewColNum, kGridViewRowGroupSize,
kGridViewColGroupSize);
// Add a horizontal pagination view with 3 pages.
auto* horizontal_instance_grid_view_1 =
grid_view->AddInstance(u"", std::make_unique<PaginationGridView>(2, 1));
auto model_three_horizontal = std::make_unique<PaginationModel>(nullptr);
model_three_horizontal->SetTotalPages(3);
horizontal_instance_grid_view_1->AddPaginationWithModel(
u"Horizontal pagenation with 3 pages",
PaginationView::Orientation::kHorizontal,
std::move(model_three_horizontal));
// Add a vertical pagination view with 3 pages.
auto* vertical_instance_grid_view_1 =
grid_view->AddInstance(u"", std::make_unique<PaginationGridView>(1, 2));
auto model_three_vertical = std::make_unique<PaginationModel>(nullptr);
model_three_vertical->SetTotalPages(3);
vertical_instance_grid_view_1->AddPaginationWithModel(
u"Vertical pagenation with 3 pages",
PaginationView::Orientation::kVertical, std::move(model_three_vertical));
// Add a Horizontal pagination view with 5 pages.
auto* horizontal_instance_grid_view_2 =
grid_view->AddInstance(u"", std::make_unique<PaginationGridView>(2, 1));
auto model_five_horizontal = std::make_unique<PaginationModel>(nullptr);
model_five_horizontal->SetTotalPages(5);
horizontal_instance_grid_view_2->AddPaginationWithModel(
u"Pagenation with 5 pages", PaginationView::Orientation::kHorizontal,
std::move(model_five_horizontal));
// Add a vertical pagination view with 5 pages.
auto* vertical_instance_grid_view_2 =
grid_view->AddInstance(u"", std::make_unique<PaginationGridView>(1, 2));
auto model_five_vertical = std::make_unique<PaginationModel>(nullptr);
model_five_vertical->SetTotalPages(5);
vertical_instance_grid_view_2->AddPaginationWithModel(
u"Vertical pagenation with 5 pages",
PaginationView::Orientation::kVertical, std::move(model_five_vertical));
// Add a horizontal pagination view with 10 pages.
auto* horizontal_instance_grid_view_3 =
grid_view->AddInstance(u"", std::make_unique<PaginationGridView>(2, 1));
auto model_ten_horizontal = std::make_unique<PaginationModel>(nullptr);
model_ten_horizontal->SetTotalPages(10);
horizontal_instance_grid_view_3->AddPaginationWithModel(
u"Pagenation with 10 pages", PaginationView::Orientation::kHorizontal,
std::move(model_ten_horizontal));
// Add a vertical pagination view with 10 pages.
auto* vertical_instance_grid_view_3 =
grid_view->AddInstance(u"", std::make_unique<PaginationGridView>(1, 2));
auto model_ten_vertical = std::make_unique<PaginationModel>(nullptr);
model_ten_vertical->SetTotalPages(10);
vertical_instance_grid_view_3->AddPaginationWithModel(
u"Vertical pagenation with 10 pages",
PaginationView::Orientation::kVertical, std::move(model_ten_vertical));
return grid_view;
}
} // namespace ash