Chromium Code Reviews| Index: ui/views/controls/button/label_button.cc |
| diff --git a/ui/views/controls/button/label_button.cc b/ui/views/controls/button/label_button.cc |
| index 89ae8c351142345a5dce0cca5adea8244b4b3b7f..a11c35d7965ca0e2dcca399b9ca04ceee7ed34d5 100644 |
| --- a/ui/views/controls/button/label_button.cc |
| +++ b/ui/views/controls/button/label_button.cc |
| @@ -67,6 +67,7 @@ const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
| } |
| void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { |
| + cached_size_.SetSize(0, 0); |
| button_state_images_[for_state] = image; |
| image_->SetImage(GetImage(state())); |
| } |
| @@ -76,9 +77,9 @@ const string16& LabelButton::GetText() const { |
| } |
| void LabelButton::SetText(const string16& text) { |
| + cached_size_.SetSize(0, 0); |
| SetAccessibleName(text); |
| label_->SetText(text); |
| - SetAccessibleName(text); |
| } |
| void LabelButton::SetTextColor(ButtonState for_state, SkColor color) { |
| @@ -95,6 +96,7 @@ bool LabelButton::GetTextMultiLine() const { |
| } |
| void LabelButton::SetTextMultiLine(bool text_multi_line) { |
| + cached_size_.SetSize(0, 0); |
| label_->SetMultiLine(text_multi_line); |
| } |
| @@ -103,6 +105,7 @@ const gfx::Font& LabelButton::GetFont() const { |
| } |
| void LabelButton::SetFont(const gfx::Font& font) { |
| + cached_size_.SetSize(0, 0); |
| label_->SetFont(font); |
| } |
| @@ -111,6 +114,7 @@ gfx::HorizontalAlignment LabelButton::GetHorizontalAlignment() const { |
| } |
| void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { |
| + cached_size_.SetSize(0, 0); |
| label_->SetHorizontalAlignment(alignment); |
| InvalidateLayout(); |
| } |
| @@ -131,6 +135,8 @@ void LabelButton::SetIsDefault(bool is_default) { |
| } |
| void LabelButton::SetStyle(ButtonStyle style) { |
| + cached_size_.SetSize(0, 0); |
| + |
| // Use the new button style instead of the native button style. |
| // TODO(msw): Officialy deprecate and remove STYLE_NATIVE_TEXTBUTTON. |
| if (DialogDelegate::UseNewStyle() && style == STYLE_NATIVE_TEXTBUTTON) |
| @@ -158,32 +164,33 @@ void LabelButton::SetStyle(ButtonStyle style) { |
| } |
| gfx::Size LabelButton::GetPreferredSize() { |
| - // Accommodate bold text in case this STYLE_BUTTON button is made default. |
| - const gfx::Font font = label_->font(); |
| - if (style_ == STYLE_BUTTON) |
| - label_->SetFont(font.DeriveFont(0, font.GetStyle() | gfx::Font::BOLD)); |
| - |
| - // Resize multi-line labels paired with images to use their available width. |
| - const gfx::Size image_size(image_->GetPreferredSize()); |
| - if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && |
| - GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
| - label_->SizeToFit(GetLocalBounds().width() - image_size.width() - kSpacing); |
| - } |
| - |
| - // Calculate the required size. |
| - gfx::Size size(label_->GetPreferredSize()); |
| - if (image_size.width() > 0 && size.width() > 0) |
| - size.Enlarge(kSpacing, 0); |
| - size.set_height(std::max(size.height(), image_size.height())); |
| - size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height()); |
| + if (cached_size_.IsEmpty()) { |
| + gfx::Font font = label_->font(); |
| + // Accommodate bold text in case this STYLE_BUTTON button is made default. |
| + if (style_ == STYLE_BUTTON) |
| + font = font.DeriveFont(0, font.GetStyle() | gfx::Font::BOLD); |
| + // Use a temporary label copy for sizing to avoid calculation side-effects. |
| + Label label(label_->text(), font); |
| + |
| + // Resize multi-line labels paired with images to use their available width. |
| + const gfx::Size image_size(image_->GetPreferredSize()); |
| + if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && |
| + GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
| + label.SizeToFit(GetLocalBounds().width() - image_size.width() - kSpacing); |
| + } |
| - // Restore the label's original font without the temporary bold style. |
| - if (style_ == STYLE_BUTTON) |
| - label_->SetFont(font); |
| + // Calculate the required size. |
| + cached_size_ = label.GetPreferredSize(); |
| + if (image_size.width() > 0 && cached_size_.width() > 0) |
| + cached_size_.Enlarge(kSpacing, 0); |
| + cached_size_.ClampToMin(gfx::Size(0, image_size.height())); |
| + cached_size_.Enlarge(image_size.width(), 0); |
| + cached_size_.Enlarge(GetInsets().width(), GetInsets().height()); |
| + } |
| // Increase the minimum size monotonically with the preferred size. |
| - size.SetSize(std::max(min_size_.width(), size.width()), |
| - std::max(min_size_.height(), size.height())); |
| + gfx::Size size(cached_size_); |
| + size.ClampToMin(min_size_); |
|
Mike Wittman
2013/05/11 04:35:58
Should this be ClampToMax?
msw
2013/05/11 04:40:26
No, I think it's correct as-is (this will ensure t
|
| min_size_ = size; |
| // Return the largest known size clamped to the maximum size (if valid). |
| @@ -278,6 +285,7 @@ void LabelButton::Layout() { |
| } |
| void LabelButton::ChildPreferredSizeChanged(View* child) { |
| + cached_size_.SetSize(0, 0); |
| PreferredSizeChanged(); |
| } |