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
ash / style / style_viewer / system_ui_components_style_viewer_view.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/style_viewer/system_ui_components_style_viewer_view.h"
#include <memory>
#include <string>
#include <utility>
#include "ash/shell.h"
#include "ash/style/style_viewer/system_ui_components_grid_view.h"
#include "ash/style/style_viewer/system_ui_components_grid_view_factories.h"
#include "ash/wm/desks/desks_util.h"
#include "base/containers/contains.h"
#include "base/functional/bind.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/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/scrollbar/overlay_scroll_bar.h"
#include "ui/views/highlight_border.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
// The width and height of viewer contents.
constexpr int kContentWidth = 960;
constexpr int kContentHeight = 496;
constexpr int kBottomSpacing = 16;
// The width of components menu.
constexpr int kMenuWidth = 160;
// The height of component button.
constexpr int kDefaultButtonHeight = 32;
// The background color id of active component button.
constexpr ui::ColorId kActiveButtonBackgroundColorId =
cros_tokens::kCrosSysPrimary;
// The text color id of active component button.
constexpr ui::ColorId kActiveButtonTextColorId = cros_tokens::kCrosSysOnPrimary;
// The background color id of inactive component button.
constexpr ui::ColorId kInactiveButtonBackgroundColorId =
cros_tokens::kCrosSysSystemOnBase;
// The text color id of inactive component button.
constexpr ui::ColorId kInactiveButtonTextColorId =
cros_tokens::kCrosSysOnSurface;
class SystemUIComponentsStyleViewerClientView : public views::ClientView {
public:
SystemUIComponentsStyleViewerClientView(views::Widget* widget,
views::View* contents_view)
: views::ClientView(widget, contents_view) {}
SystemUIComponentsStyleViewerClientView(
const SystemUIComponentsStyleViewerClientView&) = delete;
SystemUIComponentsStyleViewerClientView& operator=(
const SystemUIComponentsStyleViewerClientView&) = delete;
~SystemUIComponentsStyleViewerClientView() override = default;
// ClientView:
void UpdateWindowRoundedCorners(int corner_radius) override {
// The top corners will be rounded by NonClientFrameViewAsh. The
// client-view is responsible for rounding the bottom corners.
const gfx::RoundedCornersF radii(0, 0, corner_radius, corner_radius);
contents_view()->SetBackground(views::CreateThemedRoundedRectBackground(
ui::kColorDialogBackground, radii));
}
};
} // namespace
// The global singleton of the viewer widget.
static views::Widget* g_instance = nullptr;
// -----------------------------------------------------------------------------
// SystemUIComponentsStyleViewerView::ComponentButton:
class SystemUIComponentsStyleViewerView::ComponentButton
: public views::LabelButton {
METADATA_HEADER(ComponentButton, views::LabelButton)
public:
ComponentButton(views::LabelButton::PressedCallback pressed_callback,
const std::u16string& name)
: views::LabelButton(std::move(pressed_callback), name) {
SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_CENTER);
SetBorder(std::make_unique<views::HighlightBorder>(
0, views::HighlightBorder::Type::kHighlightBorderNoShadow));
label()->SetSubpixelRenderingEnabled(false);
label()->SetFontList(views::Label::GetDefaultFontList().Derive(
1, gfx::Font::NORMAL, gfx::Font::Weight::MEDIUM));
SetFocusBehavior(views::View::FocusBehavior::NEVER);
}
ComponentButton(const ComponentButton&) = delete;
ComponentButton& operator=(const ComponentButton&) = delete;
~ComponentButton() override = default;
void SetActive(bool active) {
background_color_id_ = active ? kActiveButtonBackgroundColorId
: kInactiveButtonBackgroundColorId;
text_color_id_ =
active ? kActiveButtonTextColorId : kInactiveButtonTextColorId;
OnThemeChanged();
}
// views::LabelButton:
void AddedToWidget() override {
SetBackground(views::CreateSolidBackground(
GetColorProvider()->GetColor(background_color_id_)));
}
gfx::Size CalculatePreferredSize(
const views::SizeBounds& available_size) const override {
return gfx::Size(kMenuWidth, kDefaultButtonHeight);
}
void OnThemeChanged() override {
views::LabelButton::OnThemeChanged();
if (!GetWidget())
return;
ui::ColorProvider* color_provider = GetColorProvider();
SetEnabledTextColors(color_provider->GetColor(text_color_id_));
if (auto* bg = background())
bg->SetNativeControlColor(color_provider->GetColor(background_color_id_));
}
private:
ui::ColorId background_color_id_ = kInactiveButtonBackgroundColorId;
ui::ColorId text_color_id_ = kInactiveButtonTextColorId;
};
BEGIN_METADATA(SystemUIComponentsStyleViewerView, ComponentButton)
END_METADATA
// -----------------------------------------------------------------------------
// SystemUIComponentsStyleViewerView:
SystemUIComponentsStyleViewerView::SystemUIComponentsStyleViewerView()
: menu_scroll_view_(
AddChildView(views::ScrollView::CreateScrollViewWithBorder())),
component_instances_scroll_view_(
AddChildView(std::make_unique<views::ScrollView>())) {
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal));
SetBackground(views::CreateThemedSolidBackground(ui::kColorDialogBackground));
SetBorder(
views::CreateEmptyBorder(gfx::Insets::TLBR(0, 0, kBottomSpacing, 0)));
// Set menu scroll view.
menu_scroll_view_->SetPreferredSize(gfx::Size(kMenuWidth, kContentHeight));
menu_contents_view_ =
menu_scroll_view_->SetContents(std::make_unique<views::View>());
menu_contents_view_->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
component_instances_scroll_view_->SetPreferredSize(
gfx::Size(kContentWidth - kMenuWidth, kContentHeight));
components_grid_view_ = component_instances_scroll_view_->SetContents(
std::make_unique<views::View>());
}
SystemUIComponentsStyleViewerView::~SystemUIComponentsStyleViewerView() =
default;
// static.
void SystemUIComponentsStyleViewerView::CreateAndShowWidget() {
// Only create widget when there is no running instance.
if (g_instance)
return;
// Owned by widget.
SystemUIComponentsStyleViewerView* viewer_view =
new SystemUIComponentsStyleViewerView();
viewer_view->SetOwnedByWidget(true);
viewer_view->AddComponent(
u"PillButton", base::BindRepeating(&CreatePillButtonInstancesGirdView));
viewer_view->AddComponent(
u"IconButton", base::BindRepeating(&CreateIconButtonInstancesGridView));
viewer_view->AddComponent(
u"Checkbox", base::BindRepeating(&CreateCheckboxInstancesGridView));
viewer_view->AddComponent(
u"CheckboxGroup",
base::BindRepeating(&CreateCheckboxGroupInstancesGridView));
viewer_view->AddComponent(
u"RadioButton", base::BindRepeating(&CreateRadioButtonInstancesGridView));
viewer_view->AddComponent(
u"RadioButtonGroup",
base::BindRepeating(&CreateRadioButtonGroupInstancesGridView));
viewer_view->AddComponent(
u"Switch", base::BindRepeating(&CreateSwitchInstancesGridView));
viewer_view->AddComponent(
u"TabSlider", base::BindRepeating(&CreateTabSliderInstancesGridView));
viewer_view->AddComponent(
u"System Textfield",
base::BindRepeating(&CreateSystemTextfieldInstancesGridView));
viewer_view->AddComponent(
u"Pagination", base::BindRepeating(&CreatePaginationInstancesGridView));
viewer_view->AddComponent(
u"Combobox", base::BindRepeating(&CreateComboboxInstancesGridView));
viewer_view->AddComponent(
u"Typography", base::BindRepeating(&CreateTypographyInstancesGridView));
viewer_view->AddComponent(u"Cutouts",
base::BindRepeating(&CreateCutoutsGridView));
// Show PillButton on start.
viewer_view->ShowComponentInstances(u"PillButton");
views::Widget::InitParams params(
views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
views::Widget::InitParams::TYPE_WINDOW);
params.parent =
desks_util::GetActiveDeskContainerForRoot(Shell::GetPrimaryRootWindow());
params.delegate = viewer_view;
// The widget is owned by the native widget.
g_instance = new views::Widget(std::move(params));
g_instance->AddObserver(viewer_view);
g_instance->Show();
}
void SystemUIComponentsStyleViewerView::AddComponent(
const std::u16string& name,
SystemUIComponentsStyleViewerView::ComponentsGridViewFactory
grid_view_factory) {
DCHECK(!base::Contains(components_grid_view_factories_, name));
// Add a new component button and components grid view factory.
auto* button =
menu_contents_view_->AddChildView(std::make_unique<ComponentButton>(
base::BindRepeating(
&SystemUIComponentsStyleViewerView::ShowComponentInstances,
base::Unretained(this), name),
name));
buttons_.push_back(button);
components_grid_view_factories_[name] = grid_view_factory;
}
void SystemUIComponentsStyleViewerView::ShowComponentInstances(
const std::u16string& name) {
DCHECK(base::Contains(components_grid_view_factories_, name));
// Set the button corresponding to the component indicated by the name active.
// Set other buttons inactive.
for (ash::SystemUIComponentsStyleViewerView::ComponentButton* button :
buttons_) {
button->SetActive(button->GetText() == name);
}
// Toggle corresponding components grid view.
components_grid_view_ = nullptr;
components_grid_view_ = component_instances_scroll_view_->SetContents(
components_grid_view_factories_[name].Run());
}
void SystemUIComponentsStyleViewerView::Layout(PassKey) {
menu_contents_view_->SetSize(
gfx::Size(kMenuWidth, menu_contents_view_->GetPreferredSize().height()));
components_grid_view_->SizeToPreferredSize();
LayoutSuperclass<views::View>(this);
}
std::u16string SystemUIComponentsStyleViewerView::GetWindowTitle() const {
return u"System Components Style Viewer";
}
views::ClientView* SystemUIComponentsStyleViewerView::CreateClientView(
views::Widget* widget) {
return new SystemUIComponentsStyleViewerClientView(widget, this);
}
void SystemUIComponentsStyleViewerView::OnWidgetDestroyed(
views::Widget* widget) {
g_instance = nullptr;
}
BEGIN_METADATA(SystemUIComponentsStyleViewerView)
END_METADATA
} // namespace ash