Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Side by Side Diff: ui/views/controls/button/checkbox.cc

Issue 2026833003: MD - update checkbox and radio buttons (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sky review Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/button/checkbox.h ('k') | ui/views/controls/button/label_button.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/button/checkbox.h" 5 #include "ui/views/controls/button/checkbox.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "ui/accessibility/ax_view_state.h" 11 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/material_design/material_design_controller.h"
12 #include "ui/base/resource/resource_bundle.h" 13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/paint_vector_icon.h"
16 #include "ui/gfx/vector_icons_public.h"
13 #include "ui/resources/grit/ui_resources.h" 17 #include "ui/resources/grit/ui_resources.h"
14 #include "ui/views/controls/button/label_button_border.h" 18 #include "ui/views/controls/button/label_button_border.h"
15 #include "ui/views/painter.h" 19 #include "ui/views/painter.h"
16 #include "ui/views/resources/grit/views_resources.h" 20 #include "ui/views/resources/grit/views_resources.h"
17 21
18 namespace views { 22 namespace views {
19 23
20 // static 24 // static
21 const char Checkbox::kViewClassName[] = "Checkbox"; 25 const char Checkbox::kViewClassName[] = "Checkbox";
22 26
23 Checkbox::Checkbox(const base::string16& label) 27 Checkbox::Checkbox(const base::string16& label)
24 : LabelButton(NULL, label), 28 : LabelButton(NULL, label),
25 checked_(false) { 29 checked_(false) {
26 SetHorizontalAlignment(gfx::ALIGN_LEFT); 30 SetHorizontalAlignment(gfx::ALIGN_LEFT);
27 std::unique_ptr<LabelButtonBorder> button_border(new LabelButtonBorder());
28 // Inset the trailing side by a couple pixels for the focus border.
29 button_border->set_insets(gfx::Insets(0, 0, 0, 2));
30 SetBorder(std::move(button_border));
31 SetFocusForPlatform(); 31 SetFocusForPlatform();
32 set_request_focus_on_press(true);
33 32
34 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 33 if (UseMd()) {
34 set_request_focus_on_press(false);
35 } else {
36 std::unique_ptr<LabelButtonBorder> button_border(new LabelButtonBorder());
37 // Inset the trailing side by a couple pixels for the focus border.
38 button_border->set_insets(gfx::Insets(0, 0, 0, 2));
39 SetBorder(std::move(button_border));
40 set_request_focus_on_press(true);
35 41
36 // Unchecked/Unfocused images. 42 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
37 SetCustomImage(false, false, STATE_NORMAL,
38 *rb.GetImageSkiaNamed(IDR_CHECKBOX));
39 SetCustomImage(false, false, STATE_HOVERED,
40 *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER));
41 SetCustomImage(false, false, STATE_PRESSED,
42 *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED));
43 SetCustomImage(false, false, STATE_DISABLED,
44 *rb.GetImageSkiaNamed(IDR_CHECKBOX_DISABLED));
45 43
46 // Checked/Unfocused images. 44 // Unchecked/Unfocused images.
47 SetCustomImage(true, false, STATE_NORMAL, 45 SetCustomImage(false, false, STATE_NORMAL,
48 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED)); 46 *rb.GetImageSkiaNamed(IDR_CHECKBOX));
49 SetCustomImage(true, false, STATE_HOVERED, 47 SetCustomImage(false, false, STATE_HOVERED,
50 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER)); 48 *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER));
51 SetCustomImage(true, false, STATE_PRESSED, 49 SetCustomImage(false, false, STATE_PRESSED,
52 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED)); 50 *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED));
53 SetCustomImage(true, false, STATE_DISABLED, 51 SetCustomImage(false, false, STATE_DISABLED,
54 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_DISABLED)); 52 *rb.GetImageSkiaNamed(IDR_CHECKBOX_DISABLED));
55 53
56 // Unchecked/Focused images. 54 // Checked/Unfocused images.
57 SetCustomImage(false, true, STATE_NORMAL, 55 SetCustomImage(true, false, STATE_NORMAL,
58 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED)); 56 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED));
59 SetCustomImage(false, true, STATE_HOVERED, 57 SetCustomImage(true, false, STATE_HOVERED,
60 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER)); 58 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER));
61 SetCustomImage(false, true, STATE_PRESSED, 59 SetCustomImage(true, false, STATE_PRESSED,
62 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED)); 60 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED));
61 SetCustomImage(true, false, STATE_DISABLED,
62 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_DISABLED));
63 63
64 // Checked/Focused images. 64 // Unchecked/Focused images.
65 SetCustomImage(true, true, STATE_NORMAL, 65 SetCustomImage(false, true, STATE_NORMAL,
66 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED)); 66 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED));
67 SetCustomImage(true, true, STATE_HOVERED, 67 SetCustomImage(false, true, STATE_HOVERED,
68 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER)); 68 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER));
69 SetCustomImage(true, true, STATE_PRESSED, 69 SetCustomImage(false, true, STATE_PRESSED,
70 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED)); 70 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED));
71
72 // Checked/Focused images.
73 SetCustomImage(true, true, STATE_NORMAL,
74 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED));
75 SetCustomImage(true, true, STATE_HOVERED,
76 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER));
77 SetCustomImage(true, true, STATE_PRESSED,
78 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED));
79 }
71 80
72 // Limit the checkbox height to match the legacy appearance. 81 // Limit the checkbox height to match the legacy appearance.
73 const gfx::Size preferred_size(LabelButton::GetPreferredSize()); 82 const gfx::Size preferred_size(LabelButton::GetPreferredSize());
74 SetMinSize(gfx::Size(0, preferred_size.height() + 4)); 83 SetMinSize(gfx::Size(0, preferred_size.height() + 4));
75 } 84 }
76 85
77 Checkbox::~Checkbox() { 86 Checkbox::~Checkbox() {
78 } 87 }
79 88
80 void Checkbox::SetChecked(bool checked) { 89 void Checkbox::SetChecked(bool checked) {
81 checked_ = checked; 90 checked_ = checked;
82 UpdateImage(); 91 UpdateImage();
83 } 92 }
84 93
94 // static
95 bool Checkbox::UseMd() {
96 return ui::MaterialDesignController::IsSecondaryUiMaterial();
97 }
98
85 void Checkbox::Layout() { 99 void Checkbox::Layout() {
86 LabelButton::Layout(); 100 LabelButton::Layout();
87 101
88 // Construct a focus painter that only surrounds the label area. 102 if (!UseMd()) {
89 gfx::Rect rect = label()->GetMirroredBounds(); 103 // Construct a focus painter that only surrounds the label area.
90 rect.Inset(-2, 3); 104 gfx::Rect rect = label()->GetMirroredBounds();
91 SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets( 105 rect.Inset(-2, 3);
92 gfx::Insets(rect.y(), rect.x(), 106 SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets(gfx::Insets(
93 height() - rect.bottom(), 107 rect.y(), rect.x(), height() - rect.bottom(), width() - rect.right())));
94 width() - rect.right()))); 108 }
95 } 109 }
96 110
97 const char* Checkbox::GetClassName() const { 111 const char* Checkbox::GetClassName() const {
98 return kViewClassName; 112 return kViewClassName;
99 } 113 }
100 114
101 void Checkbox::GetAccessibleState(ui::AXViewState* state) { 115 void Checkbox::GetAccessibleState(ui::AXViewState* state) {
102 LabelButton::GetAccessibleState(state); 116 LabelButton::GetAccessibleState(state);
103 state->role = ui::AX_ROLE_CHECK_BOX; 117 state->role = ui::AX_ROLE_CHECK_BOX;
104 if (checked()) 118 if (checked())
105 state->AddStateFlag(ui::AX_STATE_CHECKED); 119 state->AddStateFlag(ui::AX_STATE_CHECKED);
106 } 120 }
107 121
122 void Checkbox::OnPaint(gfx::Canvas* canvas) {
123 LabelButton::OnPaint(canvas);
124
125 if (!UseMd() || !HasFocus())
126 return;
127
128 SkPaint focus_paint;
129 focus_paint.setAntiAlias(true);
130 focus_paint.setColor(GetNativeTheme()->GetSystemColor(
131 ui::NativeTheme::kColorId_FocusedBorderColor));
132 focus_paint.setStyle(SkPaint::kStroke_Style);
133 focus_paint.setStrokeWidth(1);
134 PaintFocusRing(canvas, focus_paint);
135 }
136
108 void Checkbox::OnFocus() { 137 void Checkbox::OnFocus() {
109 LabelButton::OnFocus(); 138 LabelButton::OnFocus();
110 UpdateImage(); 139 if (!UseMd())
140 UpdateImage();
111 } 141 }
112 142
113 void Checkbox::OnBlur() { 143 void Checkbox::OnBlur() {
114 LabelButton::OnBlur(); 144 LabelButton::OnBlur();
115 UpdateImage(); 145 if (!UseMd())
146 UpdateImage();
116 } 147 }
117 148
118 const gfx::ImageSkia& Checkbox::GetImage(ButtonState for_state) { 149 void Checkbox::OnNativeThemeChanged(const ui::NativeTheme* theme) {
150 LabelButton::OnNativeThemeChanged(theme);
151 if (UseMd())
152 UpdateImage();
153 }
154
155 gfx::ImageSkia Checkbox::GetImage(ButtonState for_state) const {
156 if (UseMd()) {
157 return gfx::CreateVectorIcon(
158 checked_ ? gfx::VectorIconId::CHECKBOX_ACTIVE
159 : gfx::VectorIconId::CHECKBOX_NORMAL,
160 16, GetNativeTheme()->GetSystemColor(
161 checked_ ? ui::NativeTheme::kColorId_FocusedBorderColor
162 : ui::NativeTheme::kColorId_UnfocusedBorderColor));
163 }
164
119 const size_t checked_index = checked_ ? 1 : 0; 165 const size_t checked_index = checked_ ? 1 : 0;
120 const size_t focused_index = HasFocus() ? 1 : 0; 166 const size_t focused_index = HasFocus() ? 1 : 0;
121 if (for_state != STATE_NORMAL && 167 if (for_state != STATE_NORMAL &&
122 images_[checked_index][focused_index][for_state].isNull()) 168 images_[checked_index][focused_index][for_state].isNull())
123 return images_[checked_index][focused_index][STATE_NORMAL]; 169 return images_[checked_index][focused_index][STATE_NORMAL];
124 return images_[checked_index][focused_index][for_state]; 170 return images_[checked_index][focused_index][for_state];
125 } 171 }
126 172
127 void Checkbox::SetCustomImage(bool checked, 173 void Checkbox::SetCustomImage(bool checked,
128 bool focused, 174 bool focused,
129 ButtonState for_state, 175 ButtonState for_state,
130 const gfx::ImageSkia& image) { 176 const gfx::ImageSkia& image) {
131 const size_t checked_index = checked ? 1 : 0; 177 const size_t checked_index = checked ? 1 : 0;
132 const size_t focused_index = focused ? 1 : 0; 178 const size_t focused_index = focused ? 1 : 0;
133 images_[checked_index][focused_index][for_state] = image; 179 images_[checked_index][focused_index][for_state] = image;
134 UpdateImage(); 180 UpdateImage();
135 } 181 }
136 182
183 void Checkbox::PaintFocusRing(gfx::Canvas* canvas, const SkPaint& paint) {
184 gfx::RectF focus_rect(image()->bounds());
185 focus_rect.Inset(gfx::InsetsF(-.5f));
186 canvas->DrawRoundRect(focus_rect, 2.f, paint);
187 }
188
137 void Checkbox::NotifyClick(const ui::Event& event) { 189 void Checkbox::NotifyClick(const ui::Event& event) {
138 SetChecked(!checked()); 190 SetChecked(!checked());
139 LabelButton::NotifyClick(event); 191 LabelButton::NotifyClick(event);
140 } 192 }
141 193
142 ui::NativeTheme::Part Checkbox::GetThemePart() const { 194 ui::NativeTheme::Part Checkbox::GetThemePart() const {
143 return ui::NativeTheme::kCheckbox; 195 return ui::NativeTheme::kCheckbox;
144 } 196 }
145 197
146 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { 198 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
147 LabelButton::GetExtraParams(params); 199 LabelButton::GetExtraParams(params);
148 params->button.checked = checked_; 200 params->button.checked = checked_;
149 } 201 }
150 202
151 } // namespace views 203 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/checkbox.h ('k') | ui/views/controls/button/label_button.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698