OLD | NEW |
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 Loading... |
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 SetTextInternal(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 Loading... |
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 Loading... |
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 Loading... |
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 bool LabelButton::ShouldUseFloodFillInkDrop() const { | 337 bool LabelButton::ShouldUseFloodFillInkDrop() const { |
395 return !GetText().empty(); | 338 return !GetText().empty(); |
396 } | 339 } |
397 | 340 |
398 void LabelButton::OnPaint(gfx::Canvas* canvas) { | 341 void LabelButton::OnPaint(gfx::Canvas* canvas) { |
399 View::OnPaint(canvas); | 342 View::OnPaint(canvas); |
400 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | 343 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
401 } | 344 } |
402 | 345 |
403 void LabelButton::OnFocus() { | 346 void LabelButton::OnFocus() { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 | 462 |
520 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { | 463 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { |
521 if (!explicitly_set_colors_[state]) { | 464 if (!explicitly_set_colors_[state]) { |
522 SetTextColor(static_cast<ButtonState>(state), colors[state]); | 465 SetTextColor(static_cast<ButtonState>(state), colors[state]); |
523 explicitly_set_colors_[state] = false; | 466 explicitly_set_colors_[state] = false; |
524 } | 467 } |
525 } | 468 } |
526 } | 469 } |
527 | 470 |
528 void LabelButton::UpdateStyleToIndicateDefaultStatus() { | 471 void LabelButton::UpdateStyleToIndicateDefaultStatus() { |
| 472 // Check that a subclass hasn't replaced the Label font. These buttons may |
| 473 // never be given default status. |
| 474 DCHECK_EQ(cached_normal_font_list_.GetFontSize(), |
| 475 label()->font_list().GetFontSize()); |
529 const bool bold = | 476 const bool bold = |
530 PlatformStyle::kDefaultLabelButtonHasBoldFont && is_default_; | 477 PlatformStyle::kDefaultLabelButtonHasBoldFont && is_default_; |
531 label_->SetFontList(bold ? cached_bold_font_list_ : cached_normal_font_list_); | 478 // TODO(tapted): This should use style::GetFont(), but this part can just be |
| 479 // deleted when default buttons no longer go bold. Colors will need updating |
| 480 // still. |
| 481 label_->SetFontList(bold ? cached_default_button_font_list_ |
| 482 : cached_normal_font_list_); |
532 InvalidateLayout(); | 483 InvalidateLayout(); |
533 ResetLabelEnabledColor(); | 484 ResetLabelEnabledColor(); |
534 } | 485 } |
535 | 486 |
536 void LabelButton::UpdateImage() { | 487 void LabelButton::UpdateImage() { |
537 image_->SetImage(GetImage(state())); | 488 image_->SetImage(GetImage(state())); |
538 ResetCachedPreferredSize(); | 489 ResetCachedPreferredSize(); |
539 } | 490 } |
540 | 491 |
541 void LabelButton::UpdateThemedBorder() { | 492 void LabelButton::UpdateThemedBorder() { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 void LabelButton::ResetLabelEnabledColor() { | 554 void LabelButton::ResetLabelEnabledColor() { |
604 const SkColor color = | 555 const SkColor color = |
605 explicitly_set_colors_[state()] | 556 explicitly_set_colors_[state()] |
606 ? button_state_colors_[state()] | 557 ? button_state_colors_[state()] |
607 : PlatformStyle::TextColorForButton(button_state_colors_, *this); | 558 : PlatformStyle::TextColorForButton(button_state_colors_, *this); |
608 if (state() != STATE_DISABLED && label_->enabled_color() != color) | 559 if (state() != STATE_DISABLED && label_->enabled_color() != color) |
609 label_->SetEnabledColor(color); | 560 label_->SetEnabledColor(color); |
610 } | 561 } |
611 | 562 |
612 } // namespace views | 563 } // namespace views |
OLD | NEW |