Chromium Code Reviews| 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/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 |
| 27 // static | |
| 28 bool Checkbox::UseMd() { | |
| 29 return ui::MaterialDesignController::IsSecondaryUiMaterial(); | |
| 30 } | |
| 31 | |
| 23 Checkbox::Checkbox(const base::string16& label) | 32 Checkbox::Checkbox(const base::string16& label) |
| 24 : LabelButton(NULL, label), | 33 : LabelButton(NULL, label), |
| 25 checked_(false) { | 34 checked_(false) { |
| 26 SetHorizontalAlignment(gfx::ALIGN_LEFT); | 35 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(); | 36 SetFocusForPlatform(); |
| 32 set_request_focus_on_press(true); | |
| 33 | 37 |
| 34 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 38 if (UseMd()) { |
| 39 set_request_focus_on_press(false); | |
|
sky
2016/06/01 03:28:23
I didn't realize we were changing behavior too. In
Evan Stade
2016/06/01 21:01:11
I'll double check with Sebastien but AFAIU we don'
| |
| 40 } else { | |
| 41 std::unique_ptr<LabelButtonBorder> button_border(new LabelButtonBorder()); | |
| 42 // Inset the trailing side by a couple pixels for the focus border. | |
| 43 button_border->set_insets(gfx::Insets(0, 0, 0, 2)); | |
| 44 SetBorder(std::move(button_border)); | |
| 45 set_request_focus_on_press(true); | |
| 35 | 46 |
| 36 // Unchecked/Unfocused images. | 47 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 | 48 |
| 46 // Checked/Unfocused images. | 49 // Unchecked/Unfocused images. |
| 47 SetCustomImage(true, false, STATE_NORMAL, | 50 SetCustomImage(false, false, STATE_NORMAL, |
| 48 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED)); | 51 *rb.GetImageSkiaNamed(IDR_CHECKBOX)); |
| 49 SetCustomImage(true, false, STATE_HOVERED, | 52 SetCustomImage(false, false, STATE_HOVERED, |
| 50 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER)); | 53 *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER)); |
| 51 SetCustomImage(true, false, STATE_PRESSED, | 54 SetCustomImage(false, false, STATE_PRESSED, |
| 52 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED)); | 55 *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED)); |
| 53 SetCustomImage(true, false, STATE_DISABLED, | 56 SetCustomImage(false, false, STATE_DISABLED, |
| 54 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_DISABLED)); | 57 *rb.GetImageSkiaNamed(IDR_CHECKBOX_DISABLED)); |
| 55 | 58 |
| 56 // Unchecked/Focused images. | 59 // Checked/Unfocused images. |
| 57 SetCustomImage(false, true, STATE_NORMAL, | 60 SetCustomImage(true, false, STATE_NORMAL, |
| 58 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED)); | 61 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED)); |
| 59 SetCustomImage(false, true, STATE_HOVERED, | 62 SetCustomImage(true, false, STATE_HOVERED, |
| 60 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER)); | 63 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER)); |
| 61 SetCustomImage(false, true, STATE_PRESSED, | 64 SetCustomImage(true, false, STATE_PRESSED, |
| 62 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED)); | 65 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED)); |
| 66 SetCustomImage(true, false, STATE_DISABLED, | |
| 67 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_DISABLED)); | |
| 63 | 68 |
| 64 // Checked/Focused images. | 69 // Unchecked/Focused images. |
| 65 SetCustomImage(true, true, STATE_NORMAL, | 70 SetCustomImage(false, true, STATE_NORMAL, |
| 66 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED)); | 71 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED)); |
| 67 SetCustomImage(true, true, STATE_HOVERED, | 72 SetCustomImage(false, true, STATE_HOVERED, |
| 68 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER)); | 73 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER)); |
| 69 SetCustomImage(true, true, STATE_PRESSED, | 74 SetCustomImage(false, true, STATE_PRESSED, |
| 70 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED)); | 75 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED)); |
| 76 | |
| 77 // Checked/Focused images. | |
| 78 SetCustomImage(true, true, STATE_NORMAL, | |
| 79 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED)); | |
| 80 SetCustomImage(true, true, STATE_HOVERED, | |
| 81 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER)); | |
| 82 SetCustomImage(true, true, STATE_PRESSED, | |
| 83 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED)); | |
| 84 } | |
| 71 | 85 |
| 72 // Limit the checkbox height to match the legacy appearance. | 86 // Limit the checkbox height to match the legacy appearance. |
| 73 const gfx::Size preferred_size(LabelButton::GetPreferredSize()); | 87 const gfx::Size preferred_size(LabelButton::GetPreferredSize()); |
| 74 SetMinSize(gfx::Size(0, preferred_size.height() + 4)); | 88 SetMinSize(gfx::Size(0, preferred_size.height() + 4)); |
| 75 } | 89 } |
| 76 | 90 |
| 77 Checkbox::~Checkbox() { | 91 Checkbox::~Checkbox() { |
| 78 } | 92 } |
| 79 | 93 |
| 80 void Checkbox::SetChecked(bool checked) { | 94 void Checkbox::SetChecked(bool checked) { |
| 81 checked_ = checked; | 95 checked_ = checked; |
| 82 UpdateImage(); | 96 UpdateImage(); |
| 83 } | 97 } |
| 84 | 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 |
| OLD | NEW |