| OLD | NEW |
| 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/material_design/material_design_controller.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/color_utils.h" |
| 15 #include "ui/gfx/paint_vector_icon.h" | 16 #include "ui/gfx/paint_vector_icon.h" |
| 16 #include "ui/gfx/vector_icons_public.h" | 17 #include "ui/gfx/vector_icons_public.h" |
| 17 #include "ui/resources/grit/ui_resources.h" | 18 #include "ui/resources/grit/ui_resources.h" |
| 18 #include "ui/views/animation/ink_drop_highlight.h" | 19 #include "ui/views/animation/ink_drop_highlight.h" |
| 19 #include "ui/views/animation/ink_drop_ripple.h" | 20 #include "ui/views/animation/ink_drop_ripple.h" |
| 20 #include "ui/views/controls/button/label_button_border.h" | 21 #include "ui/views/controls/button/label_button_border.h" |
| 21 #include "ui/views/painter.h" | 22 #include "ui/views/painter.h" |
| 22 #include "ui/views/resources/grit/views_resources.h" | 23 #include "ui/views/resources/grit/views_resources.h" |
| 23 | 24 |
| 24 namespace views { | 25 namespace views { |
| 25 | 26 |
| 26 // static | 27 // static |
| 27 const char Checkbox::kViewClassName[] = "Checkbox"; | 28 const char Checkbox::kViewClassName[] = "Checkbox"; |
| 28 | 29 |
| 29 Checkbox::Checkbox(const base::string16& label) | 30 Checkbox::Checkbox(const base::string16& label) |
| 30 : LabelButton(NULL, label), | 31 : LabelButton(NULL, label), |
| 31 checked_(false) { | 32 checked_(false) { |
| 32 SetHorizontalAlignment(gfx::ALIGN_LEFT); | 33 SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 33 SetFocusForPlatform(); | 34 SetFocusForPlatform(); |
| 34 | 35 |
| 35 if (UseMd()) { | 36 if (UseMd()) { |
| 36 set_request_focus_on_press(false); | 37 set_request_focus_on_press(false); |
| 37 SetInkDropMode(InkDropMode::ON); | 38 SetInkDropMode(InkDropMode::ON); |
| 38 set_has_ink_drop_action_on_click(true); | 39 set_has_ink_drop_action_on_click(true); |
| 39 // The "small" size is 21dp, the large size is 1.33 * 21dp = 28dp. | 40 // The "small" size is 21dp, the large size is 1.33 * 21dp = 28dp. |
| 40 set_ink_drop_size(gfx::Size(21, 21)); | 41 set_ink_drop_size(gfx::Size(21, 21)); |
| 42 SetFocusPainter(nullptr); |
| 41 } else { | 43 } else { |
| 42 std::unique_ptr<LabelButtonBorder> button_border(new LabelButtonBorder()); | 44 std::unique_ptr<LabelButtonBorder> button_border(new LabelButtonBorder()); |
| 43 // Inset the trailing side by a couple pixels for the focus border. | 45 // Inset the trailing side by a couple pixels for the focus border. |
| 44 button_border->set_insets(gfx::Insets(0, 0, 0, 2)); | 46 button_border->set_insets(gfx::Insets(0, 0, 0, 2)); |
| 45 SetBorder(std::move(button_border)); | 47 SetBorder(std::move(button_border)); |
| 46 set_request_focus_on_press(true); | 48 set_request_focus_on_press(true); |
| 47 | 49 |
| 48 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 50 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 49 | 51 |
| 50 // Unchecked/Unfocused images. | 52 // Unchecked/Unfocused images. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 } | 128 } |
| 127 | 129 |
| 128 void Checkbox::OnPaint(gfx::Canvas* canvas) { | 130 void Checkbox::OnPaint(gfx::Canvas* canvas) { |
| 129 LabelButton::OnPaint(canvas); | 131 LabelButton::OnPaint(canvas); |
| 130 | 132 |
| 131 if (!UseMd() || !HasFocus()) | 133 if (!UseMd() || !HasFocus()) |
| 132 return; | 134 return; |
| 133 | 135 |
| 134 SkPaint focus_paint; | 136 SkPaint focus_paint; |
| 135 focus_paint.setAntiAlias(true); | 137 focus_paint.setAntiAlias(true); |
| 136 focus_paint.setColor(GetNativeTheme()->GetSystemColor( | 138 focus_paint.setColor( |
| 137 ui::NativeTheme::kColorId_FocusedBorderColor)); | 139 SkColorSetA(GetNativeTheme()->GetSystemColor( |
| 140 ui::NativeTheme::kColorId_FocusedBorderColor), |
| 141 0x66)); |
| 138 focus_paint.setStyle(SkPaint::kStroke_Style); | 142 focus_paint.setStyle(SkPaint::kStroke_Style); |
| 139 focus_paint.setStrokeWidth(1); | 143 focus_paint.setStrokeWidth(2); |
| 140 PaintFocusRing(canvas, focus_paint); | 144 PaintFocusRing(canvas, focus_paint); |
| 141 } | 145 } |
| 142 | 146 |
| 143 void Checkbox::OnFocus() { | 147 void Checkbox::OnFocus() { |
| 144 LabelButton::OnFocus(); | 148 LabelButton::OnFocus(); |
| 145 if (!UseMd()) | 149 if (!UseMd()) |
| 146 UpdateImage(); | 150 UpdateImage(); |
| 147 } | 151 } |
| 148 | 152 |
| 149 void Checkbox::OnBlur() { | 153 void Checkbox::OnBlur() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 167 } | 171 } |
| 168 | 172 |
| 169 SkColor Checkbox::GetInkDropBaseColor() const { | 173 SkColor Checkbox::GetInkDropBaseColor() const { |
| 170 return GetNativeTheme()->GetSystemColor( | 174 return GetNativeTheme()->GetSystemColor( |
| 171 ui::NativeTheme::kColorId_UnfocusedBorderColor); | 175 ui::NativeTheme::kColorId_UnfocusedBorderColor); |
| 172 } | 176 } |
| 173 | 177 |
| 174 gfx::ImageSkia Checkbox::GetImage(ButtonState for_state) const { | 178 gfx::ImageSkia Checkbox::GetImage(ButtonState for_state) const { |
| 175 if (UseMd()) { | 179 if (UseMd()) { |
| 176 return gfx::CreateVectorIcon( | 180 return gfx::CreateVectorIcon( |
| 177 checked_ ? gfx::VectorIconId::CHECKBOX_ACTIVE | 181 GetVectorIconId(), 16, |
| 178 : gfx::VectorIconId::CHECKBOX_NORMAL, | 182 // When not checked, the icon color matches the button text color. |
| 179 16, GetNativeTheme()->GetSystemColor( | 183 GetNativeTheme()->GetSystemColor( |
| 180 checked_ ? ui::NativeTheme::kColorId_FocusedBorderColor | 184 checked_ ? ui::NativeTheme::kColorId_CallToActionColor |
| 181 : ui::NativeTheme::kColorId_UnfocusedBorderColor)); | 185 : ui::NativeTheme::kColorId_ButtonEnabledColor)); |
| 182 } | 186 } |
| 183 | 187 |
| 184 const size_t checked_index = checked_ ? 1 : 0; | 188 const size_t checked_index = checked_ ? 1 : 0; |
| 185 const size_t focused_index = HasFocus() ? 1 : 0; | 189 const size_t focused_index = HasFocus() ? 1 : 0; |
| 186 if (for_state != STATE_NORMAL && | 190 if (for_state != STATE_NORMAL && |
| 187 images_[checked_index][focused_index][for_state].isNull()) | 191 images_[checked_index][focused_index][for_state].isNull()) |
| 188 return images_[checked_index][focused_index][STATE_NORMAL]; | 192 return images_[checked_index][focused_index][STATE_NORMAL]; |
| 189 return images_[checked_index][focused_index][for_state]; | 193 return images_[checked_index][focused_index][for_state]; |
| 190 } | 194 } |
| 191 | 195 |
| 192 void Checkbox::SetCustomImage(bool checked, | 196 void Checkbox::SetCustomImage(bool checked, |
| 193 bool focused, | 197 bool focused, |
| 194 ButtonState for_state, | 198 ButtonState for_state, |
| 195 const gfx::ImageSkia& image) { | 199 const gfx::ImageSkia& image) { |
| 196 const size_t checked_index = checked ? 1 : 0; | 200 const size_t checked_index = checked ? 1 : 0; |
| 197 const size_t focused_index = focused ? 1 : 0; | 201 const size_t focused_index = focused ? 1 : 0; |
| 198 images_[checked_index][focused_index][for_state] = image; | 202 images_[checked_index][focused_index][for_state] = image; |
| 199 UpdateImage(); | 203 UpdateImage(); |
| 200 } | 204 } |
| 201 | 205 |
| 202 void Checkbox::PaintFocusRing(gfx::Canvas* canvas, const SkPaint& paint) { | 206 void Checkbox::PaintFocusRing(gfx::Canvas* canvas, const SkPaint& paint) { |
| 203 gfx::RectF focus_rect(image()->bounds()); | 207 gfx::RectF focus_rect(image()->bounds()); |
| 204 focus_rect.Inset(gfx::InsetsF(-.5f)); | |
| 205 canvas->DrawRoundRect(focus_rect, 2.f, paint); | 208 canvas->DrawRoundRect(focus_rect, 2.f, paint); |
| 206 } | 209 } |
| 207 | 210 |
| 211 gfx::VectorIconId Checkbox::GetVectorIconId() const { |
| 212 return checked() ? gfx::VectorIconId::CHECKBOX_ACTIVE |
| 213 : gfx::VectorIconId::CHECKBOX_NORMAL; |
| 214 } |
| 215 |
| 208 void Checkbox::NotifyClick(const ui::Event& event) { | 216 void Checkbox::NotifyClick(const ui::Event& event) { |
| 209 SetChecked(!checked()); | 217 SetChecked(!checked()); |
| 210 LabelButton::NotifyClick(event); | 218 LabelButton::NotifyClick(event); |
| 211 } | 219 } |
| 212 | 220 |
| 213 ui::NativeTheme::Part Checkbox::GetThemePart() const { | 221 ui::NativeTheme::Part Checkbox::GetThemePart() const { |
| 214 return ui::NativeTheme::kCheckbox; | 222 return ui::NativeTheme::kCheckbox; |
| 215 } | 223 } |
| 216 | 224 |
| 217 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { | 225 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 218 LabelButton::GetExtraParams(params); | 226 LabelButton::GetExtraParams(params); |
| 219 params->button.checked = checked_; | 227 params->button.checked = checked_; |
| 220 } | 228 } |
| 221 | 229 |
| 222 } // namespace views | 230 } // namespace views |
| OLD | NEW |