Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/styled_label.h" | 5 #include "ui/views/controls/styled_label.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "ui/gfx/font_list.h" | 10 #include "ui/gfx/font_list.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 } | 135 } |
| 136 | 136 |
| 137 void StyledLabel::SetLineHeight(int line_height) { | 137 void StyledLabel::SetLineHeight(int line_height) { |
| 138 specified_line_height_ = line_height; | 138 specified_line_height_ = line_height; |
| 139 PreferredSizeChanged(); | 139 PreferredSizeChanged(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { | 142 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { |
| 143 displayed_on_background_color_ = color; | 143 displayed_on_background_color_ = color; |
| 144 displayed_on_background_color_set_ = true; | 144 displayed_on_background_color_set_ = true; |
| 145 calculated_size_ = gfx::Size(); // force layout recalculation | |
|
sky
2014/11/18 18:30:03
This comment is a bit wrong. It's not a layout cal
edjomin
2014/11/19 17:04:00
Yes, you are right. I reset cached size here to fo
| |
| 145 } | 146 } |
| 146 | 147 |
| 147 gfx::Insets StyledLabel::GetInsets() const { | 148 gfx::Insets StyledLabel::GetInsets() const { |
| 148 gfx::Insets insets = View::GetInsets(); | 149 gfx::Insets insets = View::GetInsets(); |
| 149 | 150 |
| 150 // We need a focus border iff we contain a link that will have a focus border. | 151 // We need a focus border iff we contain a link that will have a focus border. |
| 151 // That in turn will be true only if the link is non-empty. | 152 // That in turn will be true only if the link is non-empty. |
| 152 for (StyleRanges::const_iterator i(style_ranges_.begin()); | 153 for (StyleRanges::const_iterator i(style_ranges_.begin()); |
| 153 i != style_ranges_.end(); ++i) { | 154 i != style_ranges_.end(); ++i) { |
| 154 if (i->style_info.is_link && !i->range.is_empty()) { | 155 if (i->style_info.is_link && !i->range.is_empty()) { |
| 155 const gfx::Insets focus_border_padding( | 156 const gfx::Insets focus_border_padding( |
| 156 Label::kFocusBorderPadding, Label::kFocusBorderPadding, | 157 Label::kFocusBorderPadding, Label::kFocusBorderPadding, |
| 157 Label::kFocusBorderPadding, Label::kFocusBorderPadding); | 158 Label::kFocusBorderPadding, Label::kFocusBorderPadding); |
| 158 insets += focus_border_padding; | 159 insets += focus_border_padding; |
| 159 break; | 160 break; |
| 160 } | 161 } |
| 161 } | 162 } |
| 162 | 163 |
| 163 return insets; | 164 return insets; |
| 164 } | 165 } |
| 165 | 166 |
| 166 int StyledLabel::GetHeightForWidth(int w) const { | 167 int StyledLabel::GetHeightForWidth(int w) const { |
| 167 if (w != calculated_size_.width()) { | 168 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout |
| 168 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout | 169 // doesn't actually make any changes to member variables when |dry_run| is |
| 169 // doesn't actually make any changes to member variables when |dry_run| is | 170 // set to true. In general, the mutating and non-mutating parts shouldn't |
| 170 // set to true. In general, the mutating and non-mutating parts shouldn't | 171 // be in the same codepath. |
| 171 // be in the same codepath. | 172 |
| 172 calculated_size_ = | 173 // do not store calculated_size_ for dry_run call. |
| 173 const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true); | 174 // calculated_size_ should be cached for non-dry calls only, |
| 174 } | 175 // because they create controls alongside with size calculation |
| 175 return calculated_size_.height(); | 176 // otherwise all caching is useless |
| 177 return const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true).height(); | |
|
sky
2014/11/18 18:30:03
I think we should continue caching here, but make
edjomin
2014/11/19 17:04:00
This was my first idea but it doesn't work this wa
sky
2014/11/19 19:05:51
Layout and sizing for this class is expensive, so
| |
| 176 } | 178 } |
| 177 | 179 |
| 178 void StyledLabel::Layout() { | 180 void StyledLabel::Layout() { |
| 179 calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); | 181 calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); |
| 180 } | 182 } |
| 181 | 183 |
| 182 void StyledLabel::PreferredSizeChanged() { | 184 void StyledLabel::PreferredSizeChanged() { |
| 183 calculated_size_ = gfx::Size(); | 185 calculated_size_ = gfx::Size(); |
| 184 View::PreferredSizeChanged(); | 186 View::PreferredSizeChanged(); |
| 185 } | 187 } |
| 186 | 188 |
| 187 void StyledLabel::LinkClicked(Link* source, int event_flags) { | 189 void StyledLabel::LinkClicked(Link* source, int event_flags) { |
| 188 if (listener_) | 190 if (listener_) |
| 189 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags); | 191 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags); |
| 190 } | 192 } |
| 191 | 193 |
| 192 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { | 194 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { |
| 195 width -= GetInsets().width(); | |
| 196 if (width == calculated_size_.width()) | |
| 197 return calculated_size_; | |
| 198 | |
| 193 if (!dry_run) { | 199 if (!dry_run) { |
| 194 RemoveAllChildViews(true); | 200 RemoveAllChildViews(true); |
| 195 link_targets_.clear(); | 201 link_targets_.clear(); |
| 196 } | 202 } |
| 197 | 203 |
| 198 width -= GetInsets().width(); | |
| 199 if (width <= 0 || text_.empty()) | 204 if (width <= 0 || text_.empty()) |
| 200 return gfx::Size(); | 205 return gfx::Size(); |
| 201 | 206 |
| 202 const int line_height = specified_line_height_ > 0 ? specified_line_height_ | 207 const int line_height = specified_line_height_ > 0 ? specified_line_height_ |
| 203 : CalculateLineHeight(font_list_); | 208 : CalculateLineHeight(font_list_); |
| 204 // The index of the line we're on. | 209 // The index of the line we're on. |
| 205 int line = 0; | 210 int line = 0; |
| 206 // The x position (in pixels) of the line we're on, relative to content | 211 // The x position (in pixels) of the line we're on, relative to content |
| 207 // bounds. | 212 // bounds. |
| 208 int x = 0; | 213 int x = 0; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 } | 321 } |
| 317 | 322 |
| 318 // The user-specified line height only applies to interline spacing, so the | 323 // The user-specified line height only applies to interline spacing, so the |
| 319 // final line's height is unaffected. | 324 // final line's height is unaffected. |
| 320 int total_height = line * line_height + | 325 int total_height = line * line_height + |
| 321 CalculateLineHeight(font_list_) + GetInsets().height(); | 326 CalculateLineHeight(font_list_) + GetInsets().height(); |
| 322 return gfx::Size(width, total_height); | 327 return gfx::Size(width, total_height); |
| 323 } | 328 } |
| 324 | 329 |
| 325 } // namespace views | 330 } // namespace views |
| OLD | NEW |