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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 } | 90 } |
91 | 91 |
92 | 92 |
93 // StyledLabel ---------------------------------------------------------------- | 93 // StyledLabel ---------------------------------------------------------------- |
94 | 94 |
95 StyledLabel::StyledLabel(const base::string16& text, | 95 StyledLabel::StyledLabel(const base::string16& text, |
96 StyledLabelListener* listener) | 96 StyledLabelListener* listener) |
97 : specified_line_height_(0), | 97 : specified_line_height_(0), |
98 listener_(listener), | 98 listener_(listener), |
99 displayed_on_background_color_set_(false), | 99 displayed_on_background_color_set_(false), |
100 auto_color_readability_enabled_(true) { | 100 auto_color_readability_enabled_(true), |
101 layout_built_for_width_(0) { | |
101 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); | 102 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); |
102 } | 103 } |
103 | 104 |
104 StyledLabel::~StyledLabel() {} | 105 StyledLabel::~StyledLabel() {} |
105 | 106 |
106 void StyledLabel::SetText(const base::string16& text) { | 107 void StyledLabel::SetText(const base::string16& text) { |
107 text_ = text; | 108 text_ = text; |
108 style_ranges_.clear(); | 109 style_ranges_.clear(); |
109 RemoveAllChildViews(true); | 110 RemoveAllChildViews(true); |
110 PreferredSizeChanged(); | 111 PreferredSizeChanged(); |
(...skipping 22 matching lines...) Expand all Loading... | |
133 default_style_info_ = style_info; | 134 default_style_info_ = style_info; |
134 PreferredSizeChanged(); | 135 PreferredSizeChanged(); |
135 } | 136 } |
136 | 137 |
137 void StyledLabel::SetLineHeight(int line_height) { | 138 void StyledLabel::SetLineHeight(int line_height) { |
138 specified_line_height_ = line_height; | 139 specified_line_height_ = line_height; |
139 PreferredSizeChanged(); | 140 PreferredSizeChanged(); |
140 } | 141 } |
141 | 142 |
142 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { | 143 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { |
143 displayed_on_background_color_ = color; | 144 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.
| |
144 displayed_on_background_color_set_ = true; | 145 displayed_on_background_color_set_ = true; |
146 | |
147 // 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.
| |
148 // reset cached values to force CalculateAndDoLayout do its work | |
149 calculated_size_ = gfx::Size(); | |
150 layout_built_for_width_ = 0; | |
145 } | 151 } |
146 | 152 |
147 gfx::Insets StyledLabel::GetInsets() const { | 153 gfx::Insets StyledLabel::GetInsets() const { |
148 gfx::Insets insets = View::GetInsets(); | 154 gfx::Insets insets = View::GetInsets(); |
149 | 155 |
150 // We need a focus border iff we contain a link that will have a focus border. | 156 // 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. | 157 // That in turn will be true only if the link is non-empty. |
152 for (StyleRanges::const_iterator i(style_ranges_.begin()); | 158 for (StyleRanges::const_iterator i(style_ranges_.begin()); |
153 i != style_ranges_.end(); ++i) { | 159 i != style_ranges_.end(); ++i) { |
154 if (i->style_info.is_link && !i->range.is_empty()) { | 160 if (i->style_info.is_link && !i->range.is_empty()) { |
155 const gfx::Insets focus_border_padding( | 161 const gfx::Insets focus_border_padding( |
156 Label::kFocusBorderPadding, Label::kFocusBorderPadding, | 162 Label::kFocusBorderPadding, Label::kFocusBorderPadding, |
157 Label::kFocusBorderPadding, Label::kFocusBorderPadding); | 163 Label::kFocusBorderPadding, Label::kFocusBorderPadding); |
158 insets += focus_border_padding; | 164 insets += focus_border_padding; |
159 break; | 165 break; |
160 } | 166 } |
161 } | 167 } |
162 | 168 |
163 return insets; | 169 return insets; |
164 } | 170 } |
165 | 171 |
166 int StyledLabel::GetHeightForWidth(int w) const { | 172 int StyledLabel::GetHeightForWidth(int w) const { |
167 if (w != calculated_size_.width()) { | 173 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout |
168 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout | 174 // 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 | 175 // 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 | 176 // be in the same codepath. |
171 // be in the same codepath. | 177 |
172 calculated_size_ = | 178 // do not store calculated_size_ for dry_run call. |
173 const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true); | 179 // calculated_size_ should be cached for non-dry calls only, |
174 } | 180 // because they create controls alongside with size calculation |
181 // otherwise all caching is useless | |
182 calculated_size_ = | |
183 const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true); | |
175 return calculated_size_.height(); | 184 return calculated_size_.height(); |
176 } | 185 } |
177 | 186 |
178 void StyledLabel::Layout() { | 187 void StyledLabel::Layout() { |
179 calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); | 188 calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); |
189 layout_built_for_width_ = calculated_size_.width(); | |
180 } | 190 } |
181 | 191 |
182 void StyledLabel::PreferredSizeChanged() { | 192 void StyledLabel::PreferredSizeChanged() { |
183 calculated_size_ = gfx::Size(); | 193 calculated_size_ = gfx::Size(); |
184 View::PreferredSizeChanged(); | 194 View::PreferredSizeChanged(); |
185 } | 195 } |
186 | 196 |
187 void StyledLabel::LinkClicked(Link* source, int event_flags) { | 197 void StyledLabel::LinkClicked(Link* source, int event_flags) { |
188 if (listener_) | 198 if (listener_) |
189 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags); | 199 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags); |
190 } | 200 } |
191 | 201 |
192 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { | 202 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { |
203 width -= GetInsets().width(); | |
204 if (width == calculated_size_.width() && | |
205 (dry_run || layout_built_for_width_ == width)) | |
206 return calculated_size_; | |
207 | |
193 if (!dry_run) { | 208 if (!dry_run) { |
194 RemoveAllChildViews(true); | 209 RemoveAllChildViews(true); |
195 link_targets_.clear(); | 210 link_targets_.clear(); |
196 } | 211 } |
197 | 212 |
198 width -= GetInsets().width(); | |
199 if (width <= 0 || text_.empty()) | 213 if (width <= 0 || text_.empty()) |
200 return gfx::Size(); | 214 return gfx::Size(); |
201 | 215 |
202 const int line_height = specified_line_height_ > 0 ? specified_line_height_ | 216 const int line_height = specified_line_height_ > 0 ? specified_line_height_ |
203 : CalculateLineHeight(font_list_); | 217 : CalculateLineHeight(font_list_); |
204 // The index of the line we're on. | 218 // The index of the line we're on. |
205 int line = 0; | 219 int line = 0; |
206 // The x position (in pixels) of the line we're on, relative to content | 220 // The x position (in pixels) of the line we're on, relative to content |
207 // bounds. | 221 // bounds. |
208 int x = 0; | 222 int x = 0; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 } | 330 } |
317 | 331 |
318 // The user-specified line height only applies to interline spacing, so the | 332 // The user-specified line height only applies to interline spacing, so the |
319 // final line's height is unaffected. | 333 // final line's height is unaffected. |
320 int total_height = line * line_height + | 334 int total_height = line * line_height + |
321 CalculateLineHeight(font_list_) + GetInsets().height(); | 335 CalculateLineHeight(font_list_) + GetInsets().height(); |
322 return gfx::Size(width, total_height); | 336 return gfx::Size(width, total_height); |
323 } | 337 } |
324 | 338 |
325 } // namespace views | 339 } // namespace views |
OLD | NEW |