Chromium Code Reviews| Index: ui/views/controls/styled_label.cc |
| diff --git a/ui/views/controls/styled_label.cc b/ui/views/controls/styled_label.cc |
| index 700e62a392115963ae77adb8abb4a2fb60aa0205..9b65f81163fa67c263c211548a058e0f1e19acd3 100644 |
| --- a/ui/views/controls/styled_label.cc |
| +++ b/ui/views/controls/styled_label.cc |
| @@ -97,7 +97,8 @@ StyledLabel::StyledLabel(const base::string16& text, |
| : specified_line_height_(0), |
| listener_(listener), |
| displayed_on_background_color_set_(false), |
| - auto_color_readability_enabled_(true) { |
| + auto_color_readability_enabled_(true), |
| + layout_built_for_width_(0) { |
| base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); |
| } |
| @@ -142,6 +143,11 @@ void StyledLabel::SetLineHeight(int line_height) { |
| void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { |
| displayed_on_background_color_ = color; |
|
sky
2014/12/10 17:02:54
Early out if color == displayed_on_background_colo
edjomin
2014/12/11 10:50:54
Done.
|
| displayed_on_background_color_set_ = true; |
| + |
| + // when background is changed all controls should be recreated |
|
sky
2014/12/10 17:02:54
Can't we instead reset the colors on the child lab
edjomin
2014/12/11 10:50:53
Done.
|
| + // reset cached values to force CalculateAndDoLayout do its work |
| + calculated_size_ = gfx::Size(); |
| + layout_built_for_width_ = 0; |
| } |
| gfx::Insets StyledLabel::GetInsets() const { |
| @@ -164,19 +170,23 @@ gfx::Insets StyledLabel::GetInsets() const { |
| } |
| int StyledLabel::GetHeightForWidth(int w) const { |
| - if (w != calculated_size_.width()) { |
| - // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout |
| - // doesn't actually make any changes to member variables when |dry_run| is |
| - // set to true. In general, the mutating and non-mutating parts shouldn't |
| - // be in the same codepath. |
| - calculated_size_ = |
| - const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true); |
| - } |
| + // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout |
| + // doesn't actually make any changes to member variables when |dry_run| is |
| + // set to true. In general, the mutating and non-mutating parts shouldn't |
| + // be in the same codepath. |
| + |
| + // do not store calculated_size_ for dry_run call. |
| + // calculated_size_ should be cached for non-dry calls only, |
| + // because they create controls alongside with size calculation |
| + // otherwise all caching is useless |
| + calculated_size_ = |
| + const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true); |
| return calculated_size_.height(); |
| } |
| void StyledLabel::Layout() { |
| calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); |
| + layout_built_for_width_ = calculated_size_.width(); |
| } |
| void StyledLabel::PreferredSizeChanged() { |
| @@ -190,12 +200,16 @@ void StyledLabel::LinkClicked(Link* source, int event_flags) { |
| } |
| gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { |
| + width -= GetInsets().width(); |
| + if (width == calculated_size_.width() && |
| + (dry_run || layout_built_for_width_ == width)) |
| + return calculated_size_; |
| + |
| if (!dry_run) { |
| RemoveAllChildViews(true); |
| link_targets_.clear(); |
| } |
| - width -= GetInsets().width(); |
| if (width <= 0 || text_.empty()) |
| return gfx::Size(); |