| Index: ui/views/controls/button/checkbox.cc
|
| diff --git a/ui/views/controls/button/checkbox.cc b/ui/views/controls/button/checkbox.cc
|
| index 8ee9dc18baa71ee65a2851b10ad1e8138cd2dc3b..c1db4715f33b532cdc2a7204ff02568972166a93 100644
|
| --- a/ui/views/controls/button/checkbox.cc
|
| +++ b/ui/views/controls/button/checkbox.cc
|
| @@ -9,7 +9,11 @@
|
| #include <utility>
|
|
|
| #include "ui/accessibility/ax_view_state.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/paint_vector_icon.h"
|
| +#include "ui/gfx/vector_icons_public.h"
|
| #include "ui/resources/grit/ui_resources.h"
|
| #include "ui/views/controls/button/label_button_border.h"
|
| #include "ui/views/painter.h"
|
| @@ -24,50 +28,55 @@ Checkbox::Checkbox(const base::string16& label)
|
| : LabelButton(NULL, label),
|
| checked_(false) {
|
| SetHorizontalAlignment(gfx::ALIGN_LEFT);
|
| - 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));
|
| SetFocusForPlatform();
|
| - 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));
|
| +
|
| + if (UseMd()) {
|
| + set_request_focus_on_press(false);
|
| + } 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::GetPreferredSize());
|
| @@ -82,16 +91,21 @@ void Checkbox::SetChecked(bool checked) {
|
| UpdateImage();
|
| }
|
|
|
| +// static
|
| +bool Checkbox::UseMd() {
|
| + return ui::MaterialDesignController::IsSecondaryUiMaterial();
|
| +}
|
| +
|
| void Checkbox::Layout() {
|
| LabelButton::Layout();
|
|
|
| - // Construct a focus painter that only surrounds the label area.
|
| - gfx::Rect rect = label()->GetMirroredBounds();
|
| - rect.Inset(-2, 3);
|
| - SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets(
|
| - gfx::Insets(rect.y(), rect.x(),
|
| - height() - rect.bottom(),
|
| - width() - rect.right())));
|
| + if (!UseMd()) {
|
| + // Construct a focus painter that only surrounds the label area.
|
| + gfx::Rect rect = label()->GetMirroredBounds();
|
| + rect.Inset(-2, 3);
|
| + SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets(gfx::Insets(
|
| + rect.y(), rect.x(), height() - rect.bottom(), width() - rect.right())));
|
| + }
|
| }
|
|
|
| const char* Checkbox::GetClassName() const {
|
| @@ -105,17 +119,49 @@ void Checkbox::GetAccessibleState(ui::AXViewState* state) {
|
| state->AddStateFlag(ui::AX_STATE_CHECKED);
|
| }
|
|
|
| +void Checkbox::OnPaint(gfx::Canvas* canvas) {
|
| + LabelButton::OnPaint(canvas);
|
| +
|
| + if (!UseMd() || !HasFocus())
|
| + return;
|
| +
|
| + SkPaint focus_paint;
|
| + focus_paint.setAntiAlias(true);
|
| + focus_paint.setColor(GetNativeTheme()->GetSystemColor(
|
| + ui::NativeTheme::kColorId_FocusedBorderColor));
|
| + focus_paint.setStyle(SkPaint::kStroke_Style);
|
| + focus_paint.setStrokeWidth(1);
|
| + PaintFocusRing(canvas, focus_paint);
|
| +}
|
| +
|
| void Checkbox::OnFocus() {
|
| LabelButton::OnFocus();
|
| - UpdateImage();
|
| + if (!UseMd())
|
| + UpdateImage();
|
| }
|
|
|
| void Checkbox::OnBlur() {
|
| LabelButton::OnBlur();
|
| - UpdateImage();
|
| + if (!UseMd())
|
| + UpdateImage();
|
| +}
|
| +
|
| +void Checkbox::OnNativeThemeChanged(const ui::NativeTheme* theme) {
|
| + LabelButton::OnNativeThemeChanged(theme);
|
| + if (UseMd())
|
| + UpdateImage();
|
| }
|
|
|
| -const gfx::ImageSkia& Checkbox::GetImage(ButtonState for_state) {
|
| +gfx::ImageSkia Checkbox::GetImage(ButtonState for_state) const {
|
| + if (UseMd()) {
|
| + return gfx::CreateVectorIcon(
|
| + checked_ ? gfx::VectorIconId::CHECKBOX_ACTIVE
|
| + : gfx::VectorIconId::CHECKBOX_NORMAL,
|
| + 16, GetNativeTheme()->GetSystemColor(
|
| + checked_ ? ui::NativeTheme::kColorId_FocusedBorderColor
|
| + : ui::NativeTheme::kColorId_UnfocusedBorderColor));
|
| + }
|
| +
|
| const size_t checked_index = checked_ ? 1 : 0;
|
| const size_t focused_index = HasFocus() ? 1 : 0;
|
| if (for_state != STATE_NORMAL &&
|
| @@ -134,6 +180,12 @@ void Checkbox::SetCustomImage(bool checked,
|
| UpdateImage();
|
| }
|
|
|
| +void Checkbox::PaintFocusRing(gfx::Canvas* canvas, const SkPaint& paint) {
|
| + gfx::RectF focus_rect(image()->bounds());
|
| + focus_rect.Inset(gfx::InsetsF(-.5f));
|
| + canvas->DrawRoundRect(focus_rect, 2.f, paint);
|
| +}
|
| +
|
| void Checkbox::NotifyClick(const ui::Event& event) {
|
| SetChecked(!checked());
|
| LabelButton::NotifyClick(event);
|
|
|