Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: ui/views/controls/button/label_button.cc

Issue 2801583002: Use views::style for buttons, bootstrap ash_typography to do so. (Closed)
Patch Set: selfnits Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/label_button.h" 5 #include "ui/views/controls/button/label_button.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 13 matching lines...) Expand all
24 #include "ui/views/animation/ink_drop_impl.h" 24 #include "ui/views/animation/ink_drop_impl.h"
25 #include "ui/views/animation/square_ink_drop_ripple.h" 25 #include "ui/views/animation/square_ink_drop_ripple.h"
26 #include "ui/views/background.h" 26 #include "ui/views/background.h"
27 #include "ui/views/controls/button/label_button_border.h" 27 #include "ui/views/controls/button/label_button_border.h"
28 #include "ui/views/layout/layout_constants.h" 28 #include "ui/views/layout/layout_constants.h"
29 #include "ui/views/layout/layout_provider.h" 29 #include "ui/views/layout/layout_provider.h"
30 #include "ui/views/painter.h" 30 #include "ui/views/painter.h"
31 #include "ui/views/style/platform_style.h" 31 #include "ui/views/style/platform_style.h"
32 #include "ui/views/window/dialog_delegate.h" 32 #include "ui/views/window/dialog_delegate.h"
33 33
34 namespace {
35
36 gfx::Font::Weight GetValueBolderThan(gfx::Font::Weight weight) {
37 if (weight < gfx::Font::Weight::BOLD)
38 return gfx::Font::Weight::BOLD;
39 switch (weight) {
40 case gfx::Font::Weight::BOLD:
41 return gfx::Font::Weight::EXTRA_BOLD;
42 case gfx::Font::Weight::EXTRA_BOLD:
43 case gfx::Font::Weight::BLACK:
44 return gfx::Font::Weight::BLACK;
45 default:
46 NOTREACHED();
47 }
48 return gfx::Font::Weight::INVALID;
49 }
50
51 const gfx::FontList& GetDefaultNormalFontList() {
52 static base::LazyInstance<gfx::FontList>::Leaky font_list =
53 LAZY_INSTANCE_INITIALIZER;
54 return font_list.Get();
55 }
56
57 const gfx::FontList& GetDefaultBoldFontList() {
58 if (!views::PlatformStyle::kDefaultLabelButtonHasBoldFont)
59 return GetDefaultNormalFontList();
60
61 static base::LazyInstance<gfx::FontList>::Leaky font_list =
62 LAZY_INSTANCE_INITIALIZER;
63
64 static const gfx::Font::Weight default_bold_weight =
65 font_list.Get().GetFontWeight();
66
67 font_list.Get() = font_list.Get().DeriveWithWeight(
68 GetValueBolderThan(default_bold_weight));
69 DCHECK_GE(font_list.Get().GetFontWeight(), gfx::Font::Weight::BOLD);
70
71 return font_list.Get();
72 }
73
74 } // namespace
75
76 namespace views { 34 namespace views {
77 35
78 // static 36 // static
79 const int LabelButton::kHoverAnimationDurationMs = 170; 37 const int LabelButton::kHoverAnimationDurationMs = 170;
80 const char LabelButton::kViewClassName[] = "LabelButton"; 38 const char LabelButton::kViewClassName[] = "LabelButton";
81 39
82 LabelButton::LabelButton(ButtonListener* listener, const base::string16& text) 40 LabelButton::LabelButton(ButtonListener* listener,
41 const base::string16& text,
42 int button_context)
83 : CustomButton(listener), 43 : CustomButton(listener),
84 image_(new ImageView()), 44 image_(new ImageView()),
85 label_(new Label()), 45 label_(new Label(text, button_context, style::STYLE_PRIMARY)),
86 ink_drop_container_(new InkDropContainerView()), 46 ink_drop_container_(new InkDropContainerView()),
87 cached_normal_font_list_(GetDefaultNormalFontList()), 47 cached_normal_font_list_(
88 cached_bold_font_list_(GetDefaultBoldFontList()), 48 style::GetFont(button_context, style::STYLE_PRIMARY)),
49 cached_default_button_font_list_(
50 style::GetFont(button_context, style::STYLE_DIALOG_BUTTON_DEFAULT)),
89 button_state_images_(), 51 button_state_images_(),
90 button_state_colors_(), 52 button_state_colors_(),
91 explicitly_set_colors_(), 53 explicitly_set_colors_(),
92 is_default_(false), 54 is_default_(false),
93 style_(STYLE_TEXTBUTTON), 55 style_(STYLE_TEXTBUTTON),
94 border_is_themed_border_(true), 56 border_is_themed_border_(true),
95 image_label_spacing_(LayoutProvider::Get()->GetDistanceMetric( 57 image_label_spacing_(LayoutProvider::Get()->GetDistanceMetric(
96 DISTANCE_RELATED_CONTROL_HORIZONTAL)), 58 DISTANCE_RELATED_CONTROL_HORIZONTAL)),
97 horizontal_alignment_(gfx::ALIGN_LEFT) { 59 horizontal_alignment_(gfx::ALIGN_LEFT) {
98 SetAnimationDuration(kHoverAnimationDurationMs); 60 SetAnimationDuration(kHoverAnimationDurationMs);
99 SetTextInternal(text); 61 SetAccessibleName(text);
sky 2017/05/12 17:00:11 It seems less error prone to call SetTextInternal
tapted 2017/05/15 23:02:32 Went back to SetTextInternal. This means the text
100 62
101 AddChildView(ink_drop_container_); 63 AddChildView(ink_drop_container_);
102 ink_drop_container_->SetPaintToLayer(); 64 ink_drop_container_->SetPaintToLayer();
103 ink_drop_container_->layer()->SetFillsBoundsOpaquely(false); 65 ink_drop_container_->layer()->SetFillsBoundsOpaquely(false);
104 ink_drop_container_->SetVisible(false); 66 ink_drop_container_->SetVisible(false);
105 67
106 AddChildView(image_); 68 AddChildView(image_);
107 image_->set_can_process_events_within_subtree(false); 69 image_->set_can_process_events_within_subtree(false);
108 70
109 AddChildView(label_); 71 AddChildView(label_);
110 label_->SetFontList(cached_normal_font_list_);
111 label_->SetAutoColorReadabilityEnabled(false); 72 label_->SetAutoColorReadabilityEnabled(false);
112 label_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); 73 label_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
113 74
114 // Inset the button focus rect from the actual border; roughly match Windows. 75 // Inset the button focus rect from the actual border; roughly match Windows.
115 SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets(gfx::Insets(3))); 76 SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets(gfx::Insets(3)));
116 } 77 }
117 78
118 LabelButton::~LabelButton() {} 79 LabelButton::~LabelButton() {}
119 80
120 gfx::ImageSkia LabelButton::GetImage(ButtonState for_state) const { 81 gfx::ImageSkia LabelButton::GetImage(ButtonState for_state) const {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 } 113 }
153 114
154 void LabelButton::SetTextShadows(const gfx::ShadowValues& shadows) { 115 void LabelButton::SetTextShadows(const gfx::ShadowValues& shadows) {
155 label_->SetShadows(shadows); 116 label_->SetShadows(shadows);
156 } 117 }
157 118
158 void LabelButton::SetTextSubpixelRenderingEnabled(bool enabled) { 119 void LabelButton::SetTextSubpixelRenderingEnabled(bool enabled) {
159 label_->SetSubpixelRenderingEnabled(enabled); 120 label_->SetSubpixelRenderingEnabled(enabled);
160 } 121 }
161 122
162 void LabelButton::AdjustFontSize(int font_size_delta) {
163 LabelButton::SetFontList(
164 label()->font_list().DeriveWithSizeDelta(font_size_delta));
165 }
166
167 void LabelButton::SetElideBehavior(gfx::ElideBehavior elide_behavior) { 123 void LabelButton::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
168 label_->SetElideBehavior(elide_behavior); 124 label_->SetElideBehavior(elide_behavior);
169 } 125 }
170 126
171 void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { 127 void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
172 DCHECK_NE(gfx::ALIGN_TO_HEAD, alignment); 128 DCHECK_NE(gfx::ALIGN_TO_HEAD, alignment);
173 horizontal_alignment_ = alignment; 129 horizontal_alignment_ = alignment;
174 InvalidateLayout(); 130 InvalidateLayout();
175 } 131 }
176 132
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 190
235 // Use a temporary label copy for sizing to avoid calculation side-effects. 191 // Use a temporary label copy for sizing to avoid calculation side-effects.
236 Label label(GetText(), {label_->font_list()}); 192 Label label(GetText(), {label_->font_list()});
237 label.SetLineHeight(label_->line_height()); 193 label.SetLineHeight(label_->line_height());
238 label.SetShadows(label_->shadows()); 194 label.SetShadows(label_->shadows());
239 195
240 if (style_ == STYLE_BUTTON && PlatformStyle::kDefaultLabelButtonHasBoldFont) { 196 if (style_ == STYLE_BUTTON && PlatformStyle::kDefaultLabelButtonHasBoldFont) {
241 // Some text appears wider when rendered normally than when rendered bold. 197 // Some text appears wider when rendered normally than when rendered bold.
242 // Accommodate the widest, as buttons may show bold and shouldn't resize. 198 // Accommodate the widest, as buttons may show bold and shouldn't resize.
243 const int current_width = label.GetPreferredSize().width(); 199 const int current_width = label.GetPreferredSize().width();
244 label.SetFontList(cached_bold_font_list_); 200 label.SetFontList(cached_default_button_font_list_);
245 if (label.GetPreferredSize().width() < current_width) 201 if (label.GetPreferredSize().width() < current_width)
246 label.SetFontList(label_->font_list()); 202 label.SetFontList(label_->font_list());
247 } 203 }
248 204
249 // Calculate the required size. 205 // Calculate the required size.
250 const gfx::Size image_size(image_->GetPreferredSize()); 206 const gfx::Size image_size(image_->GetPreferredSize());
251 gfx::Size size(label.GetPreferredSize()); 207 gfx::Size size(label.GetPreferredSize());
252 if (image_size.width() > 0 && size.width() > 0) 208 if (image_size.width() > 0 && size.width() > 0)
253 size.Enlarge(image_label_spacing_, 0); 209 size.Enlarge(image_label_spacing_, 0);
254 size.SetToMax(gfx::Size(0, image_size.height())); 210 size.SetToMax(gfx::Size(0, image_size.height()));
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 void LabelButton::SetBorder(std::unique_ptr<Border> border) { 327 void LabelButton::SetBorder(std::unique_ptr<Border> border) {
372 border_is_themed_border_ = false; 328 border_is_themed_border_ = false;
373 View::SetBorder(std::move(border)); 329 View::SetBorder(std::move(border));
374 ResetCachedPreferredSize(); 330 ResetCachedPreferredSize();
375 } 331 }
376 332
377 gfx::Rect LabelButton::GetChildAreaBounds() { 333 gfx::Rect LabelButton::GetChildAreaBounds() {
378 return GetLocalBounds(); 334 return GetLocalBounds();
379 } 335 }
380 336
381 void LabelButton::SetFontList(const gfx::FontList& font_list) {
382 cached_normal_font_list_ = font_list;
383 if (PlatformStyle::kDefaultLabelButtonHasBoldFont) {
384 cached_bold_font_list_ = font_list.DeriveWithWeight(
385 GetValueBolderThan(font_list.GetFontWeight()));
386 if (is_default_) {
387 label_->SetFontList(cached_bold_font_list_);
388 return;
389 }
390 }
391 label_->SetFontList(cached_normal_font_list_);
392 }
393
394 void LabelButton::OnPaint(gfx::Canvas* canvas) { 337 void LabelButton::OnPaint(gfx::Canvas* canvas) {
395 View::OnPaint(canvas); 338 View::OnPaint(canvas);
396 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); 339 Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
397 } 340 }
398 341
399 void LabelButton::OnFocus() { 342 void LabelButton::OnFocus() {
400 CustomButton::OnFocus(); 343 CustomButton::OnFocus();
401 // Typically the border renders differently when focused. 344 // Typically the border renders differently when focused.
402 SchedulePaint(); 345 SchedulePaint();
403 } 346 }
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 458
516 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { 459 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) {
517 if (!explicitly_set_colors_[state]) { 460 if (!explicitly_set_colors_[state]) {
518 SetTextColor(static_cast<ButtonState>(state), colors[state]); 461 SetTextColor(static_cast<ButtonState>(state), colors[state]);
519 explicitly_set_colors_[state] = false; 462 explicitly_set_colors_[state] = false;
520 } 463 }
521 } 464 }
522 } 465 }
523 466
524 void LabelButton::UpdateStyleToIndicateDefaultStatus() { 467 void LabelButton::UpdateStyleToIndicateDefaultStatus() {
468 // Check that a subclass hasn't replaced the Label font. These buttons may
469 // never be given default status.
470 DCHECK_EQ(cached_normal_font_list_.GetFontSize(),
471 label()->font_list().GetFontSize());
525 const bool bold = 472 const bool bold =
526 PlatformStyle::kDefaultLabelButtonHasBoldFont && is_default_; 473 PlatformStyle::kDefaultLabelButtonHasBoldFont && is_default_;
527 label_->SetFontList(bold ? cached_bold_font_list_ : cached_normal_font_list_); 474 // TODO(tapted): This should use style::GetFont(), but this part can just be
475 // deleted when default buttons no longer go bold. Colors will need updating
476 // still.
477 label_->SetFontList(bold ? cached_default_button_font_list_
478 : cached_normal_font_list_);
528 InvalidateLayout(); 479 InvalidateLayout();
529 ResetLabelEnabledColor(); 480 ResetLabelEnabledColor();
530 } 481 }
531 482
532 void LabelButton::UpdateImage() { 483 void LabelButton::UpdateImage() {
533 image_->SetImage(GetImage(state())); 484 image_->SetImage(GetImage(state()));
534 ResetCachedPreferredSize(); 485 ResetCachedPreferredSize();
535 } 486 }
536 487
537 void LabelButton::UpdateThemedBorder() { 488 void LabelButton::UpdateThemedBorder() {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 : PlatformStyle::TextColorForButton(button_state_colors_, *this); 554 : PlatformStyle::TextColorForButton(button_state_colors_, *this);
604 if (state() != STATE_DISABLED && label_->enabled_color() != color) 555 if (state() != STATE_DISABLED && label_->enabled_color() != color)
605 label_->SetEnabledColor(color); 556 label_->SetEnabledColor(color);
606 } 557 }
607 558
608 bool LabelButton::UseFloodFillInkDrop() const { 559 bool LabelButton::UseFloodFillInkDrop() const {
609 return !GetText().empty(); 560 return !GetText().empty();
610 } 561 }
611 562
612 } // namespace views 563 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698