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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/button/checkbox.h"
#include <stddef.h>
#include <utility>
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/animation/ink_drop_ripple.h"
#include "ui/views/controls/button/label_button_border.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/painter.h"
#include "ui/views/resources/grit/views_resources.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/style/typography.h"
#include "ui/views/vector_icons.h"
namespace views {
constexpr int kFocusRingThicknessDip = 2;
// View used to paint the focus ring around the Checkbox icon.
// The icon is painted separately.
class IconFocusRing : public View {
public:
explicit IconFocusRing(Checkbox* checkbox);
~IconFocusRing() override = default;
private:
// View:
void Layout() override;
void OnPaint(gfx::Canvas* canvas) override;
Checkbox* checkbox_;
DISALLOW_COPY_AND_ASSIGN(IconFocusRing);
};
IconFocusRing::IconFocusRing(Checkbox* checkbox) : checkbox_(checkbox) {
FocusRing::InitFocusRing(this);
}
void IconFocusRing::Layout() {
gfx::Rect focus_bounds = checkbox_->image()->bounds();
focus_bounds.Inset(-kFocusRingThicknessDip, -kFocusRingThicknessDip);
SetBoundsRect(focus_bounds);
}
void IconFocusRing::OnPaint(gfx::Canvas* canvas) {
cc::PaintFlags focus_flags;
focus_flags.setAntiAlias(true);
focus_flags.setColor(
SkColorSetA(GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_FocusedBorderColor),
0x66));
focus_flags.setStyle(cc::PaintFlags::kStroke_Style);
focus_flags.setStrokeWidth(2);
checkbox_->PaintFocusRing(this, canvas, focus_flags);
}
// static
const char Checkbox::kViewClassName[] = "Checkbox";
Checkbox::Checkbox(const base::string16& label, bool force_md)
: LabelButton(NULL, label),
checked_(false),
label_ax_id_(0),
use_md_(force_md ||
ui::MaterialDesignController::IsSecondaryUiMaterial()) {
SetHorizontalAlignment(gfx::ALIGN_LEFT);
SetFocusForPlatform();
SetFocusPainter(nullptr);
if (UseMd()) {
set_request_focus_on_press(false);
SetInkDropMode(InkDropMode::ON);
set_has_ink_drop_action_on_click(true);
focus_ring_ = new IconFocusRing(this);
focus_ring_->SetVisible(false);
AddChildView(focus_ring_);
} else {
std::unique_ptr<LabelButtonBorder> button_border(new LabelButtonBorder());
// Inset the trailing side by a couple pixels for the focus border.
button_border->set_insets(gfx::Insets(0, 0, 0, 2));
SetBorder(std::move(button_border));
set_request_focus_on_press(true);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
// Unchecked/Unfocused images.
SetCustomImage(false, false, STATE_NORMAL,
*rb.GetImageSkiaNamed(IDR_CHECKBOX));
SetCustomImage(false, false, STATE_HOVERED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER));
SetCustomImage(false, false, STATE_PRESSED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED));
SetCustomImage(false, false, STATE_DISABLED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_DISABLED));
// Checked/Unfocused images.
SetCustomImage(true, false, STATE_NORMAL,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED));
SetCustomImage(true, false, STATE_HOVERED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER));
SetCustomImage(true, false, STATE_PRESSED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED));
SetCustomImage(true, false, STATE_DISABLED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_DISABLED));
// Unchecked/Focused images.
SetCustomImage(false, true, STATE_NORMAL,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED));
SetCustomImage(false, true, STATE_HOVERED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER));
SetCustomImage(false, true, STATE_PRESSED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED));
// Checked/Focused images.
SetCustomImage(true, true, STATE_NORMAL,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED));
SetCustomImage(true, true, STATE_HOVERED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER));
SetCustomImage(true, true, STATE_PRESSED,
*rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED));
}
// Limit the checkbox height to match the legacy appearance.
const gfx::Size preferred_size(LabelButton::CalculatePreferredSize());
SetMinSize(gfx::Size(0, preferred_size.height() + 4));
}
Checkbox::~Checkbox() {
}
void Checkbox::SetChecked(bool checked) {
checked_ = checked;
UpdateImage();
}
void Checkbox::SetMultiLine(bool multi_line) {
label()->SetMultiLine(multi_line);
}
void Checkbox::SetAssociatedLabel(View* labelling_view) {
DCHECK(labelling_view);
label_ax_id_ = labelling_view->GetViewAccessibility().GetUniqueId().Get();
ui::AXNodeData node_data;
labelling_view->GetAccessibleNodeData(&node_data);
// TODO(aleventhal) automatically handle setting the name from the related
// label in view_accessibility and have it update the name if the text of the
// associated label changes.
SetAccessibleName(
node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
}
// TODO(tetsui): Remove this method and |use_md_| when MD for secondary UI
// becomes default and IsSecondaryUiMaterial() is tautology.
bool Checkbox::UseMd() const {
return use_md_;
}
const char* Checkbox::GetClassName() const {
return kViewClassName;
}
void Checkbox::GetAccessibleNodeData(ui::AXNodeData* node_data) {
LabelButton::GetAccessibleNodeData(node_data);
node_data->role = ax::mojom::Role::kCheckBox;
const ax::mojom::CheckedState checked_state =
checked() ? ax::mojom::CheckedState::kTrue
: ax::mojom::CheckedState::kFalse;
node_data->SetCheckedState(checked_state);
if (enabled()) {
if (checked()) {
node_data->SetDefaultActionVerb(ax::mojom::DefaultActionVerb::kUncheck);
} else {
node_data->SetDefaultActionVerb(ax::mojom::DefaultActionVerb::kCheck);
}
}
if (label_ax_id_) {
node_data->AddIntListAttribute(ax::mojom::IntListAttribute::kLabelledbyIds,
{label_ax_id_});
}
}
void Checkbox::OnFocus() {
LabelButton::OnFocus();
if (!UseMd())
UpdateImage();
else
focus_ring_->SetVisible(true);
}
void Checkbox::OnBlur() {
LabelButton::OnBlur();
if (!UseMd())
UpdateImage();
else
focus_ring_->SetVisible(false);
}
void Checkbox::OnNativeThemeChanged(const ui::NativeTheme* theme) {
LabelButton::OnNativeThemeChanged(theme);
if (UseMd())
UpdateImage();
}
std::unique_ptr<InkDrop> Checkbox::CreateInkDrop() {
// Completely removes the highlight.
std::unique_ptr<InkDropImpl> ink_drop = CreateDefaultInkDropImpl();
ink_drop->SetShowHighlightOnHover(false);
ink_drop->SetAutoHighlightMode(InkDropImpl::AutoHighlightMode::NONE);
return ink_drop;
}
std::unique_ptr<InkDropRipple> Checkbox::CreateInkDropRipple() const {
// The "small" size is 21dp, the large size is 1.33 * 21dp = 28dp.
return CreateDefaultInkDropRipple(image()->GetMirroredBounds().CenterPoint(),
gfx::Size(21, 21));
}
SkColor Checkbox::GetInkDropBaseColor() const {
// Usually ink drop ripples match the text color. Checkboxes use the color of
// the unchecked, enabled icon.
return GetIconImageColor(IconState::ENABLED);
}
gfx::ImageSkia Checkbox::GetImage(ButtonState for_state) const {
if (UseMd()) {
const int checked = checked_ ? IconState::CHECKED : 0;
const int enabled = for_state != STATE_DISABLED ? IconState::ENABLED : 0;
return gfx::CreateVectorIcon(GetVectorIcon(), 16,
GetIconImageColor(checked | enabled));
}
const size_t checked_index = checked_ ? 1 : 0;
const size_t focused_index = HasFocus() ? 1 : 0;
if (for_state != STATE_NORMAL &&
images_[checked_index][focused_index][for_state].isNull())
return images_[checked_index][focused_index][STATE_NORMAL];
return images_[checked_index][focused_index][for_state];
}
std::unique_ptr<LabelButtonBorder> Checkbox::CreateDefaultBorder() const {
std::unique_ptr<LabelButtonBorder> border =
LabelButton::CreateDefaultBorder();
border->set_insets(
LayoutProvider::Get()->GetInsetsMetric(INSETS_CHECKBOX_RADIO_BUTTON));
return border;
}
void Checkbox::SetCustomImage(bool checked,
bool focused,
ButtonState for_state,
const gfx::ImageSkia& image) {
const size_t checked_index = checked ? 1 : 0;
const size_t focused_index = focused ? 1 : 0;
images_[checked_index][focused_index][for_state] = image;
UpdateImage();
}
void Checkbox::PaintFocusRing(View* view,
gfx::Canvas* canvas,
const cc::PaintFlags& flags) {
gfx::RectF bounds(view->GetLocalBounds());
bounds.Inset(kFocusRingThicknessDip, kFocusRingThicknessDip);
canvas->DrawRoundRect(bounds, kFocusRingThicknessDip, flags);
}
const gfx::VectorIcon& Checkbox::GetVectorIcon() const {
return checked() ? kCheckboxActiveIcon : kCheckboxNormalIcon;
}
SkColor Checkbox::GetIconImageColor(int icon_state) const {
DCHECK(UseMd());
const SkColor active_color =
(icon_state & IconState::CHECKED)
? GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_FocusedBorderColor)
// When unchecked, the icon color matches push button text color.
: style::GetColor(*this, style::CONTEXT_BUTTON_MD,
style::STYLE_PRIMARY);
return (icon_state & IconState::ENABLED)
? active_color
: color_utils::BlendTowardOppositeLuma(active_color,
gfx::kDisabledControlAlpha);
}
void Checkbox::NotifyClick(const ui::Event& event) {
SetChecked(!checked());
LabelButton::NotifyClick(event);
}
ui::NativeTheme::Part Checkbox::GetThemePart() const {
return ui::NativeTheme::kCheckbox;
}
void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
LabelButton::GetExtraParams(params);
params->button.checked = checked_;
}
} // namespace views
|