| 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_palette.h" | 10 #include "ui/gfx/color_palette.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 return CreateButton(listener, text, | 100 return CreateButton(listener, text, |
| 101 ui::MaterialDesignController::IsSecondaryUiMaterial()); | 101 ui::MaterialDesignController::IsSecondaryUiMaterial()); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // static | 104 // static |
| 105 LabelButton* MdTextButton::CreateSecondaryUiBlueButton( | 105 LabelButton* MdTextButton::CreateSecondaryUiBlueButton( |
| 106 ButtonListener* listener, | 106 ButtonListener* listener, |
| 107 const base::string16& text) { | 107 const base::string16& text) { |
| 108 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { | 108 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { |
| 109 MdTextButton* md_button = MdTextButton::Create(listener, text); | 109 MdTextButton* md_button = MdTextButton::Create(listener, text); |
| 110 md_button->SetCallToAction(true); | 110 md_button->SetProminent(true); |
| 111 return md_button; | 111 return md_button; |
| 112 } | 112 } |
| 113 | 113 |
| 114 return new BlueButton(listener, text); | 114 return new BlueButton(listener, text); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // static | 117 // static |
| 118 MdTextButton* MdTextButton::Create(ButtonListener* listener, | 118 MdTextButton* MdTextButton::Create(ButtonListener* listener, |
| 119 const base::string16& text) { | 119 const base::string16& text) { |
| 120 MdTextButton* button = new MdTextButton(listener); | 120 MdTextButton* button = new MdTextButton(listener); |
| 121 button->SetText(text); | 121 button->SetText(text); |
| 122 button->SetFocusForPlatform(); | 122 button->SetFocusForPlatform(); |
| 123 return button; | 123 return button; |
| 124 } | 124 } |
| 125 | 125 |
| 126 void MdTextButton::SetCallToAction(bool cta) { | 126 void MdTextButton::SetProminent(bool is_prominent) { |
| 127 if (is_cta_ == cta) | 127 if (is_prominent_ == is_prominent) |
| 128 return; | 128 return; |
| 129 | 129 |
| 130 is_cta_ = cta; | 130 is_prominent_ = is_prominent; |
| 131 UpdateColors(); | 131 UpdateColors(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void MdTextButton::Layout() { | 134 void MdTextButton::Layout() { |
| 135 LabelButton::Layout(); | 135 LabelButton::Layout(); |
| 136 gfx::Rect focus_bounds = GetLocalBounds(); | 136 gfx::Rect focus_bounds = GetLocalBounds(); |
| 137 focus_bounds.Inset(gfx::Insets(-kFocusBorderThickness)); | 137 focus_bounds.Inset(gfx::Insets(-kFocusBorderThickness)); |
| 138 focus_ring_->SetBoundsRect(focus_bounds); | 138 focus_ring_->SetBoundsRect(focus_bounds); |
| 139 } | 139 } |
| 140 | 140 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 167 | 167 |
| 168 void MdTextButton::StateChanged() { | 168 void MdTextButton::StateChanged() { |
| 169 LabelButton::StateChanged(); | 169 LabelButton::StateChanged(); |
| 170 UpdateColors(); | 170 UpdateColors(); |
| 171 } | 171 } |
| 172 | 172 |
| 173 std::unique_ptr<views::InkDropHighlight> MdTextButton::CreateInkDropHighlight() | 173 std::unique_ptr<views::InkDropHighlight> MdTextButton::CreateInkDropHighlight() |
| 174 const { | 174 const { |
| 175 if (!ShouldShowInkDropHighlight()) | 175 if (!ShouldShowInkDropHighlight()) |
| 176 return nullptr; | 176 return nullptr; |
| 177 if (!is_cta_) | 177 if (!is_prominent_) |
| 178 return LabelButton::CreateInkDropHighlight(); | 178 return LabelButton::CreateInkDropHighlight(); |
| 179 | 179 |
| 180 // The call to action hover effect is a shadow. | 180 // The prominent button hover effect is a shadow. |
| 181 const int kYOffset = 1; | 181 const int kYOffset = 1; |
| 182 const int kSkiaBlurRadius = 1; | 182 const int kSkiaBlurRadius = 1; |
| 183 std::vector<gfx::ShadowValue> shadows; | 183 std::vector<gfx::ShadowValue> shadows; |
| 184 // The notion of blur that gfx::ShadowValue uses is twice the Skia/CSS value. | 184 // The notion of blur that gfx::ShadowValue uses is twice the Skia/CSS value. |
| 185 // Skia counts the number of pixels outside the mask area whereas | 185 // Skia counts the number of pixels outside the mask area whereas |
| 186 // gfx::ShadowValue counts together the number of pixels inside and outside | 186 // gfx::ShadowValue counts together the number of pixels inside and outside |
| 187 // the mask bounds. | 187 // the mask bounds. |
| 188 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(0, kYOffset), | 188 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(0, kYOffset), |
| 189 2 * kSkiaBlurRadius, | 189 2 * kSkiaBlurRadius, |
| 190 SkColorSetA(SK_ColorBLACK, 0x3D))); | 190 SkColorSetA(SK_ColorBLACK, 0x3D))); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 208 LabelButton::SetText(text); | 208 LabelButton::SetText(text); |
| 209 UpdatePadding(); | 209 UpdatePadding(); |
| 210 } | 210 } |
| 211 | 211 |
| 212 void MdTextButton::AdjustFontSize(int size_delta) { | 212 void MdTextButton::AdjustFontSize(int size_delta) { |
| 213 LabelButton::AdjustFontSize(size_delta); | 213 LabelButton::AdjustFontSize(size_delta); |
| 214 UpdatePadding(); | 214 UpdatePadding(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { | 217 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { |
| 218 is_cta_ = is_cta_ || is_default(); | 218 is_prominent_ = is_prominent_ || is_default(); |
| 219 UpdateColors(); | 219 UpdateColors(); |
| 220 } | 220 } |
| 221 | 221 |
| 222 void MdTextButton::SetFontList(const gfx::FontList& font_list) { | 222 void MdTextButton::SetFontList(const gfx::FontList& font_list) { |
| 223 NOTREACHED() | 223 NOTREACHED() |
| 224 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; | 224 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; |
| 225 } | 225 } |
| 226 | 226 |
| 227 MdTextButton::MdTextButton(ButtonListener* listener) | 227 MdTextButton::MdTextButton(ButtonListener* listener) |
| 228 : LabelButton(listener, base::string16()), | 228 : LabelButton(listener, base::string16()), |
| 229 focus_ring_(new internal::MdFocusRing()), | 229 focus_ring_(new internal::MdFocusRing()), |
| 230 is_cta_(false) { | 230 is_prominent_(false) { |
| 231 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON | 231 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON |
| 232 : InkDropMode::OFF); | 232 : InkDropMode::OFF); |
| 233 set_has_ink_drop_action_on_click(true); | 233 set_has_ink_drop_action_on_click(true); |
| 234 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 234 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 235 SetFocusForPlatform(); | 235 SetFocusForPlatform(); |
| 236 SetMinSize(gfx::Size(kMinWidth, 0)); | 236 SetMinSize(gfx::Size(kMinWidth, 0)); |
| 237 SetFocusPainter(nullptr); | 237 SetFocusPainter(nullptr); |
| 238 label()->SetAutoColorReadabilityEnabled(false); | 238 label()->SetAutoColorReadabilityEnabled(false); |
| 239 AddChildView(focus_ring_); | 239 AddChildView(focus_ring_); |
| 240 focus_ring_->SetVisible(false); | 240 focus_ring_->SetVisible(false); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 280 |
| 281 // TODO(estade): can we get rid of the platform style border hoopla if | 281 // TODO(estade): can we get rid of the platform style border hoopla if |
| 282 // we apply the MD treatment to all buttons, even GTK buttons? | 282 // we apply the MD treatment to all buttons, even GTK buttons? |
| 283 const int kHorizontalPadding = 16; | 283 const int kHorizontalPadding = 16; |
| 284 SetBorder(Border::CreateEmptyBorder(top_padding, kHorizontalPadding, | 284 SetBorder(Border::CreateEmptyBorder(top_padding, kHorizontalPadding, |
| 285 bottom_padding, kHorizontalPadding)); | 285 bottom_padding, kHorizontalPadding)); |
| 286 } | 286 } |
| 287 | 287 |
| 288 void MdTextButton::UpdateColors() { | 288 void MdTextButton::UpdateColors() { |
| 289 ui::NativeTheme::ColorId fg_color_id = | 289 ui::NativeTheme::ColorId fg_color_id = |
| 290 is_cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor | 290 is_prominent_ ? ui::NativeTheme::kColorId_TextOnProminentButtonColor |
| 291 : ui::NativeTheme::kColorId_ButtonEnabledColor; | 291 : ui::NativeTheme::kColorId_ButtonEnabledColor; |
| 292 | 292 |
| 293 ui::NativeTheme* theme = GetNativeTheme(); | 293 ui::NativeTheme* theme = GetNativeTheme(); |
| 294 if (!explicitly_set_normal_color()) | 294 if (!explicitly_set_normal_color()) |
| 295 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); | 295 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); |
| 296 | 296 |
| 297 // CTA buttons keep their enabled text color; disabled state is conveyed by | 297 // Prominent buttons keep their enabled text color; disabled state is conveyed |
| 298 // shading the background instead. | 298 // by shading the background instead. |
| 299 if (is_cta_) | 299 if (is_prominent_) |
| 300 SetTextColor(STATE_DISABLED, theme->GetSystemColor(fg_color_id)); | 300 SetTextColor(STATE_DISABLED, theme->GetSystemColor(fg_color_id)); |
| 301 | 301 |
| 302 SkColor text_color = label()->enabled_color(); | 302 SkColor text_color = label()->enabled_color(); |
| 303 SkColor bg_color = | 303 SkColor bg_color = |
| 304 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground); | 304 theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground); |
| 305 | 305 |
| 306 if (bg_color_override_) { | 306 if (bg_color_override_) { |
| 307 bg_color = *bg_color_override_; | 307 bg_color = *bg_color_override_; |
| 308 } else if (is_cta_) { | 308 } else if (is_prominent_) { |
| 309 bg_color = theme->GetSystemColor( | 309 bg_color = theme->GetSystemColor( |
| 310 ui::NativeTheme::kColorId_CallToActionColor); | 310 ui::NativeTheme::kColorId_ProminentButtonColor); |
| 311 if (state() == STATE_DISABLED) | 311 if (state() == STATE_DISABLED) |
| 312 bg_color = color_utils::BlendTowardOppositeLuma( | 312 bg_color = color_utils::BlendTowardOppositeLuma( |
| 313 bg_color, gfx::kDisabledControlAlpha); | 313 bg_color, gfx::kDisabledControlAlpha); |
| 314 } | 314 } |
| 315 | 315 |
| 316 if (state() == STATE_PRESSED) { | 316 if (state() == STATE_PRESSED) { |
| 317 SkColor shade = | 317 SkColor shade = |
| 318 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonPressedShade); | 318 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonPressedShade); |
| 319 bg_color = color_utils::GetResultingPaintColor(shade, bg_color); | 319 bg_color = color_utils::GetResultingPaintColor(shade, bg_color); |
| 320 } | 320 } |
| 321 | 321 |
| 322 const SkAlpha kStrokeOpacity = 0x1A; | 322 const SkAlpha kStrokeOpacity = 0x1A; |
| 323 SkColor stroke_color = (is_cta_ || color_utils::IsDark(text_color)) | 323 SkColor stroke_color = (is_prominent_ || color_utils::IsDark(text_color)) |
| 324 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) | 324 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) |
| 325 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); | 325 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); |
| 326 | 326 |
| 327 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); | 327 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); |
| 328 set_background(Background::CreateBackgroundPainter( | 328 set_background(Background::CreateBackgroundPainter( |
| 329 true, Painter::CreateRoundRectWith1PxBorderPainter( | 329 true, Painter::CreateRoundRectWith1PxBorderPainter( |
| 330 bg_color, stroke_color, kInkDropSmallCornerRadius))); | 330 bg_color, stroke_color, kInkDropSmallCornerRadius))); |
| 331 } | 331 } |
| 332 | 332 |
| 333 } // namespace views | 333 } // namespace views |
| OLD | NEW |