| 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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); | 136 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void MdTextButton::SetCallToAction(bool cta) { | 139 void MdTextButton::SetCallToAction(bool cta) { |
| 140 if (is_cta_ == cta) | 140 if (is_cta_ == cta) |
| 141 return; | 141 return; |
| 142 | 142 |
| 143 is_cta_ = cta; | 143 is_cta_ = cta; |
| 144 focus_ring_->set_thickness(cta ? kFocusBorderThicknessCta | 144 focus_ring_->set_thickness(cta ? kFocusBorderThicknessCta |
| 145 : kFocusBorderThickness); | 145 : kFocusBorderThickness); |
| 146 UpdateColorsFromNativeTheme(); | 146 UpdateColors(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void MdTextButton::Layout() { | 149 void MdTextButton::Layout() { |
| 150 LabelButton::Layout(); | 150 LabelButton::Layout(); |
| 151 gfx::Rect focus_bounds = GetLocalBounds(); | 151 gfx::Rect focus_bounds = GetLocalBounds(); |
| 152 focus_bounds.Inset(gfx::Insets(-focus_ring_->thickness())); | 152 focus_bounds.Inset(gfx::Insets(-focus_ring_->thickness())); |
| 153 focus_ring_->SetBoundsRect(focus_bounds); | 153 focus_ring_->SetBoundsRect(focus_bounds); |
| 154 } | 154 } |
| 155 | 155 |
| 156 void MdTextButton::OnFocus() { | 156 void MdTextButton::OnFocus() { |
| 157 LabelButton::OnFocus(); | 157 LabelButton::OnFocus(); |
| 158 focus_ring_->SetVisible(true); | 158 focus_ring_->SetVisible(true); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void MdTextButton::OnBlur() { | 161 void MdTextButton::OnBlur() { |
| 162 LabelButton::OnBlur(); | 162 LabelButton::OnBlur(); |
| 163 focus_ring_->SetVisible(false); | 163 focus_ring_->SetVisible(false); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 166 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 167 LabelButton::OnNativeThemeChanged(theme); | 167 LabelButton::OnNativeThemeChanged(theme); |
| 168 UpdateColorsFromNativeTheme(); | 168 UpdateColors(); |
| 169 } | 169 } |
| 170 | 170 |
| 171 SkColor MdTextButton::GetInkDropBaseColor() const { | 171 SkColor MdTextButton::GetInkDropBaseColor() const { |
| 172 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); | 172 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); |
| 173 } | 173 } |
| 174 | 174 |
| 175 std::unique_ptr<views::InkDropHighlight> MdTextButton::CreateInkDropHighlight() | 175 std::unique_ptr<views::InkDropHighlight> MdTextButton::CreateInkDropHighlight() |
| 176 const { | 176 const { |
| 177 if (!ShouldShowInkDropHighlight()) | 177 if (!ShouldShowInkDropHighlight()) |
| 178 return nullptr; | 178 return nullptr; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 192 GetLocalBounds().CenterPoint(), | 192 GetLocalBounds().CenterPoint(), |
| 193 base::WrapUnique(new BorderShadowLayerDelegate( | 193 base::WrapUnique(new BorderShadowLayerDelegate( |
| 194 shadows, GetLocalBounds(), kInkDropSmallCornerRadius)))); | 194 shadows, GetLocalBounds(), kInkDropSmallCornerRadius)))); |
| 195 } | 195 } |
| 196 | 196 |
| 197 bool MdTextButton::ShouldShowInkDropForFocus() const { | 197 bool MdTextButton::ShouldShowInkDropForFocus() const { |
| 198 // These types of button use |focus_ring_|. | 198 // These types of button use |focus_ring_|. |
| 199 return false; | 199 return false; |
| 200 } | 200 } |
| 201 | 201 |
| 202 void MdTextButton::SetEnabledTextColors(SkColor color) { |
| 203 LabelButton::SetEnabledTextColors(color); |
| 204 UpdateColors(); |
| 205 } |
| 206 |
| 202 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { | 207 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { |
| 203 UpdateColorsFromNativeTheme(); | 208 UpdateColors(); |
| 204 } | 209 } |
| 205 | 210 |
| 206 MdTextButton::MdTextButton(ButtonListener* listener) | 211 MdTextButton::MdTextButton(ButtonListener* listener) |
| 207 : LabelButton(listener, base::string16()), | 212 : LabelButton(listener, base::string16()), |
| 208 focus_ring_(new internal::MdFocusRing()), | 213 focus_ring_(new internal::MdFocusRing()), |
| 209 is_cta_(false) { | 214 is_cta_(false) { |
| 210 SetHasInkDrop(true); | 215 SetHasInkDrop(true); |
| 211 set_has_ink_drop_action_on_click(true); | 216 set_has_ink_drop_action_on_click(true); |
| 212 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 217 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 213 SetFocusForPlatform(); | 218 SetFocusForPlatform(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 235 | 240 |
| 236 // TODO(estade): can we get rid of the platform style border hoopla if | 241 // TODO(estade): can we get rid of the platform style border hoopla if |
| 237 // we apply the MD treatment to all buttons, even GTK buttons? | 242 // we apply the MD treatment to all buttons, even GTK buttons? |
| 238 SetBorder(Border::CreateEmptyBorder( | 243 SetBorder(Border::CreateEmptyBorder( |
| 239 top_padding - inbuilt_top_padding, kHorizontalPadding, | 244 top_padding - inbuilt_top_padding, kHorizontalPadding, |
| 240 bottom_padding - inbuilt_bottom_padding, kHorizontalPadding)); | 245 bottom_padding - inbuilt_bottom_padding, kHorizontalPadding)); |
| 241 } | 246 } |
| 242 | 247 |
| 243 MdTextButton::~MdTextButton() {} | 248 MdTextButton::~MdTextButton() {} |
| 244 | 249 |
| 245 void MdTextButton::UpdateColorsFromNativeTheme() { | 250 void MdTextButton::UpdateColors() { |
| 246 ui::NativeTheme::ColorId fg_color_id = | 251 ui::NativeTheme::ColorId fg_color_id = |
| 247 is_cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor | 252 is_cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor |
| 248 : ui::NativeTheme::kColorId_ButtonEnabledColor; | 253 : ui::NativeTheme::kColorId_ButtonEnabledColor; |
| 249 | 254 |
| 250 // When there's no call to action, respect a color override if one has | 255 // When there's no call to action, respect a color override if one has |
| 251 // been set. For call to action styling, don't let individual buttons | 256 // been set. For call to action styling, don't let individual buttons |
| 252 // specify a color. | 257 // specify a color. |
| 253 ui::NativeTheme* theme = GetNativeTheme(); | 258 ui::NativeTheme* theme = GetNativeTheme(); |
| 254 if (is_cta_ || !explicitly_set_normal_color()) | 259 if (is_cta_ || !explicitly_set_normal_color()) |
| 255 SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); | 260 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); |
| 256 | 261 |
| 257 SkColor text_color = label()->enabled_color(); | 262 SkColor text_color = label()->enabled_color(); |
| 258 SkColor bg_color = | 263 SkColor bg_color = |
| 259 is_cta_ | 264 is_cta_ |
| 260 ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor) | 265 ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor) |
| 261 : is_default() | 266 : is_default() |
| 262 ? color_utils::BlendTowardOppositeLuma(text_color, 0xD8) | 267 ? color_utils::BlendTowardOppositeLuma(text_color, 0xD8) |
| 263 : SK_ColorTRANSPARENT; | 268 : SK_ColorTRANSPARENT; |
| 264 SkColor stroke_color = SkColorSetA(SK_ColorBLACK, 0x1A); | 269 |
| 270 const SkAlpha kStrokeOpacity = 0x1A; |
| 271 SkColor stroke_color = (is_cta_ || color_utils::IsDark(text_color)) |
| 272 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) |
| 273 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); |
| 274 |
| 265 set_background(Background::CreateBackgroundPainter( | 275 set_background(Background::CreateBackgroundPainter( |
| 266 true, Painter::CreateRoundRectWith1PxBorderPainter( | 276 true, Painter::CreateRoundRectWith1PxBorderPainter( |
| 267 bg_color, stroke_color, kInkDropSmallCornerRadius))); | 277 bg_color, stroke_color, kInkDropSmallCornerRadius))); |
| 268 } | 278 } |
| 269 | 279 |
| 270 } // namespace views | 280 } // namespace views |
| OLD | NEW |