| 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" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/color_utils.h" | 10 #include "ui/gfx/color_utils.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 gfx::RectF rect(view->GetLocalBounds()); | 118 gfx::RectF rect(view->GetLocalBounds()); |
| 119 rect.Inset(gfx::InsetsF(0.5)); | 119 rect.Inset(gfx::InsetsF(0.5)); |
| 120 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); | 120 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void MdTextButton::SetCallToAction(bool cta) { | 123 void MdTextButton::SetCallToAction(bool cta) { |
| 124 if (cta_ == cta) | 124 if (cta_ == cta) |
| 125 return; | 125 return; |
| 126 | 126 |
| 127 cta_ = cta; | 127 cta_ = cta; |
| 128 UpdateColorsFromNativeTheme(); | 128 UpdateColors(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void MdTextButton::Layout() { | 131 void MdTextButton::Layout() { |
| 132 LabelButton::Layout(); | 132 LabelButton::Layout(); |
| 133 gfx::Rect focus_bounds = GetLocalBounds(); | 133 gfx::Rect focus_bounds = GetLocalBounds(); |
| 134 focus_bounds.Inset(gfx::Insets(kFocusBorderOutset)); | 134 focus_bounds.Inset(gfx::Insets(kFocusBorderOutset)); |
| 135 focus_ring_->SetBoundsRect(focus_bounds); | 135 focus_ring_->SetBoundsRect(focus_bounds); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void MdTextButton::OnFocus() { | 138 void MdTextButton::OnFocus() { |
| 139 LabelButton::OnFocus(); | 139 LabelButton::OnFocus(); |
| 140 focus_ring_->SetVisible(true); | 140 focus_ring_->SetVisible(true); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void MdTextButton::OnBlur() { | 143 void MdTextButton::OnBlur() { |
| 144 LabelButton::OnBlur(); | 144 LabelButton::OnBlur(); |
| 145 focus_ring_->SetVisible(false); | 145 focus_ring_->SetVisible(false); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 148 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 149 LabelButton::OnNativeThemeChanged(theme); | 149 LabelButton::OnNativeThemeChanged(theme); |
| 150 UpdateColorsFromNativeTheme(); | 150 UpdateColors(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 SkColor MdTextButton::GetInkDropBaseColor() const { | 153 SkColor MdTextButton::GetInkDropBaseColor() const { |
| 154 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); | 154 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); |
| 155 } | 155 } |
| 156 | 156 |
| 157 bool MdTextButton::ShouldShowInkDropForFocus() const { | 157 bool MdTextButton::ShouldShowInkDropForFocus() const { |
| 158 // These types of button use |focus_ring_|. | 158 // These types of button use |focus_ring_|. |
| 159 return false; | 159 return false; |
| 160 } | 160 } |
| 161 | 161 |
| 162 void MdTextButton::SetEnabledTextColors(SkColor color) { |
| 163 LabelButton::SetEnabledTextColors(color); |
| 164 UpdateColors(); |
| 165 } |
| 166 |
| 162 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { | 167 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { |
| 163 UpdateColorsFromNativeTheme(); | 168 UpdateColors(); |
| 164 } | 169 } |
| 165 | 170 |
| 166 MdTextButton::MdTextButton(ButtonListener* listener) | 171 MdTextButton::MdTextButton(ButtonListener* listener) |
| 167 : LabelButton(listener, base::string16()), | 172 : LabelButton(listener, base::string16()), |
| 168 focus_ring_(new MdFocusRing()), | 173 focus_ring_(new MdFocusRing()), |
| 169 cta_(false) { | 174 cta_(false) { |
| 170 SetHasInkDrop(true); | 175 SetHasInkDrop(true); |
| 171 set_has_ink_drop_action_on_click(true); | 176 set_has_ink_drop_action_on_click(true); |
| 172 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 177 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 173 SetFocusForPlatform(); | 178 SetFocusForPlatform(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 195 | 200 |
| 196 // TODO(estade): can we get rid of the platform style border hoopla if | 201 // TODO(estade): can we get rid of the platform style border hoopla if |
| 197 // we apply the MD treatment to all buttons, even GTK buttons? | 202 // we apply the MD treatment to all buttons, even GTK buttons? |
| 198 SetBorder(Border::CreateEmptyBorder( | 203 SetBorder(Border::CreateEmptyBorder( |
| 199 top_padding - inbuilt_top_padding, kHorizontalPadding, | 204 top_padding - inbuilt_top_padding, kHorizontalPadding, |
| 200 bottom_padding - inbuilt_bottom_padding, kHorizontalPadding)); | 205 bottom_padding - inbuilt_bottom_padding, kHorizontalPadding)); |
| 201 } | 206 } |
| 202 | 207 |
| 203 MdTextButton::~MdTextButton() {} | 208 MdTextButton::~MdTextButton() {} |
| 204 | 209 |
| 205 void MdTextButton::UpdateColorsFromNativeTheme() { | 210 void MdTextButton::UpdateColors() { |
| 206 ui::NativeTheme::ColorId fg_color_id = | 211 ui::NativeTheme::ColorId fg_color_id = |
| 207 cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor | 212 cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor |
| 208 : ui::NativeTheme::kColorId_ButtonEnabledColor; | 213 : ui::NativeTheme::kColorId_ButtonEnabledColor; |
| 209 | 214 |
| 210 // When there's no call to action, respect a color override if one has | 215 // When there's no call to action, respect a color override if one has |
| 211 // been set. For call to action styling, don't let individual buttons | 216 // been set. For call to action styling, don't let individual buttons |
| 212 // specify a color. | 217 // specify a color. |
| 213 ui::NativeTheme* theme = GetNativeTheme(); | 218 ui::NativeTheme* theme = GetNativeTheme(); |
| 214 if (cta_ || !explicitly_set_normal_color()) | 219 if (cta_ || !explicitly_set_normal_color()) |
| 215 SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); | 220 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); |
| 216 | 221 |
| 217 SkColor text_color = label()->enabled_color(); | 222 SkColor text_color = label()->enabled_color(); |
| 218 SkColor bg_color = | 223 SkColor bg_color = |
| 219 cta_ ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor) | 224 cta_ ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor) |
| 220 : is_default() | 225 : is_default() |
| 221 ? color_utils::BlendTowardOppositeLuma(text_color, 0xD8) | 226 ? color_utils::BlendTowardOppositeLuma(text_color, 0xD8) |
| 222 : SK_ColorTRANSPARENT; | 227 : SK_ColorTRANSPARENT; |
| 223 SkColor stroke_color = SkColorSetA(SK_ColorBLACK, 0x1A); | 228 |
| 229 const SkAlpha kStrokeOpacity = 0x1A; |
| 230 SkColor stroke_color = (cta_ || color_utils::IsDark(text_color)) |
| 231 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) |
| 232 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); |
| 233 |
| 224 set_background(Background::CreateBackgroundPainter( | 234 set_background(Background::CreateBackgroundPainter( |
| 225 true, Painter::CreateRoundRectWith1PxBorderPainter( | 235 true, Painter::CreateRoundRectWith1PxBorderPainter( |
| 226 bg_color, stroke_color, kInkDropSmallCornerRadius))); | 236 bg_color, stroke_color, kInkDropSmallCornerRadius))); |
| 227 } | 237 } |
| 228 | 238 |
| 229 } // namespace views | 239 } // namespace views |
| OLD | NEW |