Chromium Code Reviews| 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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 paint.setColor(SkColorSetA(view->GetNativeTheme()->GetSystemColor( | 129 paint.setColor(SkColorSetA(view->GetNativeTheme()->GetSystemColor( |
| 130 ui::NativeTheme::kColorId_CallToActionColor), | 130 ui::NativeTheme::kColorId_CallToActionColor), |
| 131 alpha)); | 131 alpha)); |
| 132 paint.setStyle(SkPaint::kStroke_Style); | 132 paint.setStyle(SkPaint::kStroke_Style); |
| 133 paint.setStrokeWidth(thickness); | 133 paint.setStrokeWidth(thickness); |
| 134 gfx::RectF rect(view->GetLocalBounds()); | 134 gfx::RectF rect(view->GetLocalBounds()); |
| 135 rect.Inset(gfx::InsetsF(thickness / 2.f)); | 135 rect.Inset(gfx::InsetsF(thickness / 2.f)); |
| 136 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); | 136 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void MdTextButton::AdjustFontSize(int size_delta) { | |
| 140 SetFontList(GetFontList().DeriveWithSizeDelta(size_delta)); | |
| 141 UpdatePaddingForFont(); | |
| 142 } | |
| 143 | |
| 139 void MdTextButton::SetCallToAction(bool cta) { | 144 void MdTextButton::SetCallToAction(bool cta) { |
| 140 if (is_cta_ == cta) | 145 if (is_cta_ == cta) |
| 141 return; | 146 return; |
| 142 | 147 |
| 143 is_cta_ = cta; | 148 is_cta_ = cta; |
| 144 focus_ring_->set_thickness(cta ? kFocusBorderThicknessCta | 149 focus_ring_->set_thickness(cta ? kFocusBorderThicknessCta |
| 145 : kFocusBorderThickness); | 150 : kFocusBorderThickness); |
| 146 UpdateColors(); | 151 UpdateColors(); |
| 147 } | 152 } |
| 148 | 153 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 : LabelButton(listener, base::string16()), | 219 : LabelButton(listener, base::string16()), |
| 215 focus_ring_(new internal::MdFocusRing()), | 220 focus_ring_(new internal::MdFocusRing()), |
| 216 is_cta_(false) { | 221 is_cta_(false) { |
| 217 SetInkDropMode(InkDropMode::ON); | 222 SetInkDropMode(InkDropMode::ON); |
| 218 set_has_ink_drop_action_on_click(true); | 223 set_has_ink_drop_action_on_click(true); |
| 219 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 224 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 220 SetFocusForPlatform(); | 225 SetFocusForPlatform(); |
| 221 SetMinSize(gfx::Size(kMinWidth, 0)); | 226 SetMinSize(gfx::Size(kMinWidth, 0)); |
| 222 SetFocusPainter(nullptr); | 227 SetFocusPainter(nullptr); |
| 223 label()->SetAutoColorReadabilityEnabled(false); | 228 label()->SetAutoColorReadabilityEnabled(false); |
| 224 SetFontList(GetMdFontList()); | |
| 225 | |
| 226 AddChildView(focus_ring_); | 229 AddChildView(focus_ring_); |
| 227 focus_ring_->SetVisible(false); | 230 focus_ring_->SetVisible(false); |
| 228 set_request_focus_on_press(false); | 231 set_request_focus_on_press(false); |
| 232 SetFontList(GetMdFontList()); | |
|
sky
2016/07/27 19:57:07
As anyone can call SetFontList, shouldn't you make
Evan Stade
2016/07/27 20:09:47
are you suggesting making SetFontList virtual? I'm
| |
| 233 UpdatePaddingForFont(); | |
| 234 } | |
| 229 | 235 |
| 236 MdTextButton::~MdTextButton() {} | |
| 237 | |
| 238 void MdTextButton::UpdatePaddingForFont() { | |
| 230 // Top and bottom padding depend on the font. Example: if font cap height is | 239 // Top and bottom padding depend on the font. Example: if font cap height is |
| 231 // 9dp, use 8dp bottom padding and 7dp top padding to total 24dp. | 240 // 9dp, use 8dp bottom padding and 7dp top padding to total 24dp. |
| 232 const gfx::FontList& font = label()->font_list(); | 241 const gfx::FontList& font = label()->font_list(); |
| 233 int text_height = font.GetCapHeight(); | 242 int text_height = font.GetCapHeight(); |
| 234 int even_text_height = text_height - (text_height % 2); | 243 int even_text_height = text_height - (text_height % 2); |
| 235 const int top_padding = even_text_height - (text_height - even_text_height); | 244 const int top_padding = even_text_height - (text_height - even_text_height); |
| 236 const int bottom_padding = even_text_height; | 245 const int bottom_padding = even_text_height; |
| 237 DCHECK_EQ(3 * even_text_height, top_padding + text_height + bottom_padding); | 246 DCHECK_EQ(3 * even_text_height, top_padding + text_height + bottom_padding); |
| 238 | 247 |
| 239 const int inbuilt_top_padding = font.GetBaseline() - font.GetCapHeight(); | 248 const int inbuilt_top_padding = font.GetBaseline() - font.GetCapHeight(); |
| 240 const int inbuilt_bottom_padding = | 249 const int inbuilt_bottom_padding = |
| 241 font.GetHeight() - label()->font_list().GetBaseline(); | 250 font.GetHeight() - label()->font_list().GetBaseline(); |
| 242 | 251 |
| 243 // TODO(estade): can we get rid of the platform style border hoopla if | 252 // TODO(estade): can we get rid of the platform style border hoopla if |
| 244 // we apply the MD treatment to all buttons, even GTK buttons? | 253 // we apply the MD treatment to all buttons, even GTK buttons? |
| 245 SetBorder(Border::CreateEmptyBorder( | 254 SetBorder(Border::CreateEmptyBorder( |
| 246 top_padding - inbuilt_top_padding, kHorizontalPadding, | 255 top_padding - inbuilt_top_padding, kHorizontalPadding, |
| 247 bottom_padding - inbuilt_bottom_padding, kHorizontalPadding)); | 256 bottom_padding - inbuilt_bottom_padding, kHorizontalPadding)); |
| 248 } | 257 } |
| 249 | 258 |
| 250 MdTextButton::~MdTextButton() {} | |
| 251 | |
| 252 void MdTextButton::UpdateColors() { | 259 void MdTextButton::UpdateColors() { |
| 253 ui::NativeTheme::ColorId fg_color_id = | 260 ui::NativeTheme::ColorId fg_color_id = |
| 254 is_cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor | 261 is_cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor |
| 255 : ui::NativeTheme::kColorId_ButtonEnabledColor; | 262 : ui::NativeTheme::kColorId_ButtonEnabledColor; |
| 256 | 263 |
| 257 ui::NativeTheme* theme = GetNativeTheme(); | 264 ui::NativeTheme* theme = GetNativeTheme(); |
| 258 if (!explicitly_set_normal_color()) | 265 if (!explicitly_set_normal_color()) |
| 259 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); | 266 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); |
| 260 | 267 |
| 261 SkColor text_color = label()->enabled_color(); | 268 SkColor text_color = label()->enabled_color(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 273 SkColor stroke_color = (is_cta_ || color_utils::IsDark(text_color)) | 280 SkColor stroke_color = (is_cta_ || color_utils::IsDark(text_color)) |
| 274 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) | 281 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) |
| 275 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); | 282 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); |
| 276 | 283 |
| 277 set_background(Background::CreateBackgroundPainter( | 284 set_background(Background::CreateBackgroundPainter( |
| 278 true, Painter::CreateRoundRectWith1PxBorderPainter( | 285 true, Painter::CreateRoundRectWith1PxBorderPainter( |
| 279 bg_color, stroke_color, kInkDropSmallCornerRadius))); | 286 bg_color, stroke_color, kInkDropSmallCornerRadius))); |
| 280 } | 287 } |
| 281 | 288 |
| 282 } // namespace views | 289 } // namespace views |
| OLD | NEW |