| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/md_text_button.h" | 5 #include "ui/views/controls/button/md_text_button.h" |
| 6 | 6 |
| 7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
| 8 #include "ui/base/material_design/material_design_controller.h" | 8 #include "ui/base/material_design/material_design_controller.h" |
| 9 #include "ui/gfx/canvas.h" | |
| 10 #include "ui/gfx/color_utils.h" | 9 #include "ui/gfx/color_utils.h" |
| 11 #include "ui/native_theme/native_theme.h" | 10 #include "ui/native_theme/native_theme.h" |
| 12 #include "ui/views/background.h" | 11 #include "ui/views/background.h" |
| 13 #include "ui/views/border.h" | 12 #include "ui/views/border.h" |
| 14 #include "ui/views/painter.h" | 13 #include "ui/views/painter.h" |
| 15 | 14 |
| 16 namespace views { | 15 namespace views { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 // Inset between clickable region border and button contents (text). | 19 // Inset between clickable region border and button contents (text). |
| 21 const int kHorizontalPadding = 12; | 20 const int kHorizontalPadding = 12; |
| 22 const int kVerticalPadding = 6; | 21 const int kVerticalPadding = 6; |
| 23 | 22 |
| 24 // Minimum size to reserve for the button contents. | 23 // Minimum size to reserve for the button contents. |
| 25 const int kMinWidth = 48; | 24 const int kMinWidth = 48; |
| 26 | 25 |
| 27 // The amount to enlarge the focus border in all directions relative to the | |
| 28 // button. | |
| 29 const int kFocusBorderOutset = -2; | |
| 30 | |
| 31 // The corner radius of the focus border roundrect. | |
| 32 const int kFocusBorderCornerRadius = 3; | |
| 33 | |
| 34 class MdFocusRing : public views::View { | |
| 35 public: | |
| 36 MdFocusRing() { | |
| 37 SetPaintToLayer(true); | |
| 38 layer()->SetFillsBoundsOpaquely(false); | |
| 39 } | |
| 40 ~MdFocusRing() override {} | |
| 41 | |
| 42 void OnPaint(gfx::Canvas* canvas) override { | |
| 43 SkPaint paint; | |
| 44 paint.setAntiAlias(true); | |
| 45 paint.setColor(GetNativeTheme()->GetSystemColor( | |
| 46 ui::NativeTheme::kColorId_CallToActionColor)); | |
| 47 paint.setStyle(SkPaint::kStroke_Style); | |
| 48 paint.setStrokeWidth(1); | |
| 49 gfx::RectF rect(GetLocalBounds()); | |
| 50 rect.Inset(gfx::InsetsF(0.5)); | |
| 51 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 DISALLOW_COPY_AND_ASSIGN(MdFocusRing); | |
| 56 }; | |
| 57 | |
| 58 } // namespace | 26 } // namespace |
| 59 | 27 |
| 60 // static | 28 // static |
| 61 LabelButton* MdTextButton::CreateStandardButton(ButtonListener* listener, | 29 LabelButton* MdTextButton::CreateStandardButton(ButtonListener* listener, |
| 62 const base::string16& text) { | 30 const base::string16& text) { |
| 63 if (ui::MaterialDesignController::IsModeMaterial()) | 31 if (ui::MaterialDesignController::IsModeMaterial()) |
| 64 return CreateMdButton(listener, text); | 32 return CreateMdButton(listener, text); |
| 65 | 33 |
| 66 LabelButton* button = new LabelButton(listener, text); | 34 LabelButton* button = new LabelButton(listener, text); |
| 67 button->SetStyle(STYLE_BUTTON); | 35 button->SetStyle(STYLE_BUTTON); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 81 } | 49 } |
| 82 | 50 |
| 83 void MdTextButton::SetCallToAction(CallToAction cta) { | 51 void MdTextButton::SetCallToAction(CallToAction cta) { |
| 84 if (cta_ == cta) | 52 if (cta_ == cta) |
| 85 return; | 53 return; |
| 86 | 54 |
| 87 cta_ = cta; | 55 cta_ = cta; |
| 88 UpdateColorsFromNativeTheme(); | 56 UpdateColorsFromNativeTheme(); |
| 89 } | 57 } |
| 90 | 58 |
| 91 void MdTextButton::Layout() { | |
| 92 LabelButton::Layout(); | |
| 93 | |
| 94 gfx::Rect focus_bounds = GetLocalBounds(); | |
| 95 focus_bounds.Inset(gfx::Insets(kFocusBorderOutset)); | |
| 96 focus_ring_->SetBoundsRect(focus_bounds); | |
| 97 } | |
| 98 | |
| 99 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 59 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 100 LabelButton::OnNativeThemeChanged(theme); | 60 LabelButton::OnNativeThemeChanged(theme); |
| 101 UpdateColorsFromNativeTheme(); | 61 UpdateColorsFromNativeTheme(); |
| 102 } | 62 } |
| 103 | 63 |
| 104 SkColor MdTextButton::GetInkDropBaseColor() const { | 64 SkColor MdTextButton::GetInkDropBaseColor() const { |
| 105 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); | 65 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); |
| 106 } | 66 } |
| 107 | 67 |
| 108 void MdTextButton::SetText(const base::string16& text) { | 68 void MdTextButton::SetText(const base::string16& text) { |
| 109 LabelButton::SetText(base::i18n::ToUpper(text)); | 69 LabelButton::SetText(base::i18n::ToUpper(text)); |
| 110 } | 70 } |
| 111 | 71 |
| 112 void MdTextButton::OnFocus() { | |
| 113 View::OnFocus(); | |
| 114 focus_ring_->SetVisible(true); | |
| 115 } | |
| 116 | |
| 117 void MdTextButton::OnBlur() { | |
| 118 View::OnBlur(); | |
| 119 focus_ring_->SetVisible(false); | |
| 120 } | |
| 121 | |
| 122 MdTextButton::MdTextButton(ButtonListener* listener) | 72 MdTextButton::MdTextButton(ButtonListener* listener) |
| 123 : LabelButton(listener, base::string16()), | 73 : LabelButton(listener, base::string16()), |
| 124 ink_drop_delegate_(this, this), | 74 ink_drop_delegate_(this, this), |
| 125 focus_ring_(new MdFocusRing()), | |
| 126 cta_(NO_CALL_TO_ACTION) { | 75 cta_(NO_CALL_TO_ACTION) { |
| 127 set_ink_drop_delegate(&ink_drop_delegate_); | 76 set_ink_drop_delegate(&ink_drop_delegate_); |
| 128 set_has_ink_drop_action_on_click(true); | 77 set_has_ink_drop_action_on_click(true); |
| 129 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 78 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 130 SetFocusable(true); | 79 SetFocusable(true); |
| 131 SetMinSize(gfx::Size(kMinWidth, 0)); | 80 SetMinSize(gfx::Size(kMinWidth, 0)); |
| 132 | |
| 133 AddChildView(focus_ring_); | |
| 134 focus_ring_->SetVisible(false); | |
| 135 SetFocusPainter(nullptr); | 81 SetFocusPainter(nullptr); |
| 136 set_request_focus_on_press(false); | 82 UseMdFocusRing(); |
| 137 | |
| 138 label()->SetAutoColorReadabilityEnabled(false); | 83 label()->SetAutoColorReadabilityEnabled(false); |
| 139 } | 84 } |
| 140 | 85 |
| 141 MdTextButton::~MdTextButton() {} | 86 MdTextButton::~MdTextButton() {} |
| 142 | 87 |
| 143 void MdTextButton::UpdateColorsFromNativeTheme() { | 88 void MdTextButton::UpdateColorsFromNativeTheme() { |
| 144 ui::NativeTheme::ColorId fg_color_id = ui::NativeTheme::kColorId_NumColors; | 89 ui::NativeTheme::ColorId fg_color_id = ui::NativeTheme::kColorId_NumColors; |
| 145 switch (cta_) { | 90 switch (cta_) { |
| 146 case NO_CALL_TO_ACTION: | 91 case NO_CALL_TO_ACTION: |
| 147 fg_color_id = ui::NativeTheme::kColorId_ButtonEnabledColor; | 92 fg_color_id = ui::NativeTheme::kColorId_ButtonEnabledColor; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 160 cta_ == STRONG_CALL_TO_ACTION | 105 cta_ == STRONG_CALL_TO_ACTION |
| 161 ? Background::CreateBackgroundPainter( | 106 ? Background::CreateBackgroundPainter( |
| 162 true, Painter::CreateSolidRoundRectPainter( | 107 true, Painter::CreateSolidRoundRectPainter( |
| 163 theme->GetSystemColor( | 108 theme->GetSystemColor( |
| 164 ui::NativeTheme::kColorId_CallToActionColor), | 109 ui::NativeTheme::kColorId_CallToActionColor), |
| 165 kInkDropSmallCornerRadius)) | 110 kInkDropSmallCornerRadius)) |
| 166 : nullptr); | 111 : nullptr); |
| 167 } | 112 } |
| 168 | 113 |
| 169 } // namespace views | 114 } // namespace views |
| OLD | NEW |