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" |
11 #include "ui/native_theme/native_theme.h" | 11 #include "ui/native_theme/native_theme.h" |
12 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" | 12 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" |
13 #include "ui/views/animation/ink_drop_highlight.h" | 13 #include "ui/views/animation/ink_drop_highlight.h" |
14 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" | 14 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" |
15 #include "ui/views/background.h" | 15 #include "ui/views/background.h" |
16 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
17 #include "ui/views/controls/button/blue_button.h" | 17 #include "ui/views/controls/button/blue_button.h" |
18 #include "ui/views/painter.h" | 18 #include "ui/views/painter.h" |
19 | 19 |
20 namespace views { | 20 namespace views { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Inset between clickable region border and button contents (text). | |
25 const int kHorizontalPadding = 16; | |
26 | |
27 // Minimum size to reserve for the button contents. | 24 // Minimum size to reserve for the button contents. |
28 const int kMinWidth = 48; | 25 const int kMinWidth = 48; |
29 | 26 |
30 // The stroke width of the focus border in normal and call to action mode. | 27 // The stroke width of the focus border in normal and call to action mode. |
31 const int kFocusBorderThickness = 1; | 28 const int kFocusBorderThickness = 1; |
32 const int kFocusBorderThicknessCta = 2; | 29 const int kFocusBorderThicknessCta = 2; |
33 | 30 |
34 // The corner radius of the focus border roundrect. | 31 // The corner radius of the focus border roundrect. |
35 const int kFocusBorderCornerRadius = 3; | 32 const int kFocusBorderCornerRadius = 3; |
36 | 33 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 return false; | 207 return false; |
211 } | 208 } |
212 | 209 |
213 void MdTextButton::SetEnabledTextColors(SkColor color) { | 210 void MdTextButton::SetEnabledTextColors(SkColor color) { |
214 LabelButton::SetEnabledTextColors(color); | 211 LabelButton::SetEnabledTextColors(color); |
215 UpdateColors(); | 212 UpdateColors(); |
216 } | 213 } |
217 | 214 |
218 void MdTextButton::SetText(const base::string16& text) { | 215 void MdTextButton::SetText(const base::string16& text) { |
219 LabelButton::SetText(text); | 216 LabelButton::SetText(text); |
220 UpdatePaddingForFont(); | 217 UpdatePadding(); |
221 } | 218 } |
222 | 219 |
223 void MdTextButton::AdjustFontSize(int size_delta) { | 220 void MdTextButton::AdjustFontSize(int size_delta) { |
224 LabelButton::AdjustFontSize(size_delta); | 221 LabelButton::AdjustFontSize(size_delta); |
225 UpdatePaddingForFont(); | 222 UpdatePadding(); |
226 } | 223 } |
227 | 224 |
228 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { | 225 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { |
229 UpdateColors(); | 226 UpdateColors(); |
230 } | 227 } |
231 | 228 |
232 void MdTextButton::SetFontList(const gfx::FontList& font_list) { | 229 void MdTextButton::SetFontList(const gfx::FontList& font_list) { |
233 NOTREACHED() | 230 NOTREACHED() |
234 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; | 231 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; |
235 } | 232 } |
(...skipping 10 matching lines...) Expand all Loading... |
246 SetFocusPainter(nullptr); | 243 SetFocusPainter(nullptr); |
247 label()->SetAutoColorReadabilityEnabled(false); | 244 label()->SetAutoColorReadabilityEnabled(false); |
248 AddChildView(focus_ring_); | 245 AddChildView(focus_ring_); |
249 focus_ring_->SetVisible(false); | 246 focus_ring_->SetVisible(false); |
250 set_request_focus_on_press(false); | 247 set_request_focus_on_press(false); |
251 LabelButton::SetFontList(GetMdFontList()); | 248 LabelButton::SetFontList(GetMdFontList()); |
252 } | 249 } |
253 | 250 |
254 MdTextButton::~MdTextButton() {} | 251 MdTextButton::~MdTextButton() {} |
255 | 252 |
256 void MdTextButton::UpdatePaddingForFont() { | 253 void MdTextButton::UpdatePadding() { |
257 // Don't use font-based padding when there's no text visible. | 254 // Don't use font-based padding when there's no text visible. |
258 if (GetText().empty()) { | 255 if (GetText().empty()) { |
259 SetBorder(Border::NullBorder()); | 256 SetBorder(Border::NullBorder()); |
260 return; | 257 return; |
261 } | 258 } |
262 | 259 |
263 // Top and bottom padding depend on the font. Example: if font cap height is | 260 // Text buttons default to 28dp in height on all platforms when the base font |
264 // 9dp, use 8dp bottom padding and 7dp top padding to total 24dp. | 261 // is in use, but should grow or shrink if the font size is adjusted up or |
265 const gfx::FontList& font = label()->font_list(); | 262 // down. When the system font size has been adjusted, the base font will be |
266 int text_height = font.GetCapHeight(); | 263 // larger than normal such that 28dp might not be enough, so also enforce a |
267 int even_text_height = text_height - (text_height % 2); | 264 // minimum height of twice the font size. |
268 const int top_padding = even_text_height - (text_height - even_text_height); | 265 // Example 1: |
269 const int bottom_padding = even_text_height; | 266 // * Normal button on ChromeOS, 12pt Roboto. Button height of 28dp. |
270 DCHECK_EQ(3 * even_text_height, top_padding + text_height + bottom_padding); | 267 // * Button on ChromeOS that has been adjusted to 14pt Roboto. Button height |
| 268 // of 28 + 2 * 2 = 32dp. |
| 269 // * Linux user sets base system font size to 17dp. For a normal button, the |
| 270 // |size_delta| will be zero, so to adjust upwards we double 17 to get 34. |
| 271 int size_delta = |
| 272 label()->font_list().GetFontSize() - GetMdFontList().GetFontSize(); |
| 273 const int kBaseHeight = 28; |
| 274 int target_height = std::max(kBaseHeight + size_delta * 2, |
| 275 label()->font_list().GetFontSize() * 2); |
271 | 276 |
272 const int inbuilt_top_padding = font.GetBaseline() - font.GetCapHeight(); | 277 int label_height = label()->GetPreferredSize().height(); |
273 const int inbuilt_bottom_padding = | 278 int top_padding = (target_height - label_height) / 2; |
274 font.GetHeight() - label()->font_list().GetBaseline(); | 279 int bottom_padding = (target_height - label_height + 1) / 2; |
| 280 DCHECK_EQ(target_height, label_height + top_padding + bottom_padding); |
275 | 281 |
276 // TODO(estade): can we get rid of the platform style border hoopla if | 282 // TODO(estade): can we get rid of the platform style border hoopla if |
277 // we apply the MD treatment to all buttons, even GTK buttons? | 283 // we apply the MD treatment to all buttons, even GTK buttons? |
278 SetBorder(Border::CreateEmptyBorder( | 284 const int kHorizontalPadding = 16; |
279 top_padding - inbuilt_top_padding, kHorizontalPadding, | 285 SetBorder(Border::CreateEmptyBorder(top_padding, kHorizontalPadding, |
280 bottom_padding - inbuilt_bottom_padding, kHorizontalPadding)); | 286 bottom_padding, kHorizontalPadding)); |
281 } | 287 } |
282 | 288 |
283 void MdTextButton::UpdateColors() { | 289 void MdTextButton::UpdateColors() { |
284 ui::NativeTheme::ColorId fg_color_id = | 290 ui::NativeTheme::ColorId fg_color_id = |
285 is_cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor | 291 is_cta_ ? ui::NativeTheme::kColorId_TextOnCallToActionColor |
286 : ui::NativeTheme::kColorId_ButtonEnabledColor; | 292 : ui::NativeTheme::kColorId_ButtonEnabledColor; |
287 | 293 |
288 ui::NativeTheme* theme = GetNativeTheme(); | 294 ui::NativeTheme* theme = GetNativeTheme(); |
289 if (!explicitly_set_normal_color()) | 295 if (!explicitly_set_normal_color()) |
290 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); | 296 LabelButton::SetEnabledTextColors(theme->GetSystemColor(fg_color_id)); |
(...skipping 13 matching lines...) Expand all Loading... |
304 SkColor stroke_color = (is_cta_ || color_utils::IsDark(text_color)) | 310 SkColor stroke_color = (is_cta_ || color_utils::IsDark(text_color)) |
305 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) | 311 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) |
306 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); | 312 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); |
307 | 313 |
308 set_background(Background::CreateBackgroundPainter( | 314 set_background(Background::CreateBackgroundPainter( |
309 true, Painter::CreateRoundRectWith1PxBorderPainter( | 315 true, Painter::CreateRoundRectWith1PxBorderPainter( |
310 bg_color, stroke_color, kInkDropSmallCornerRadius))); | 316 bg_color, stroke_color, kInkDropSmallCornerRadius))); |
311 } | 317 } |
312 | 318 |
313 } // namespace views | 319 } // namespace views |
OLD | NEW |