Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: ui/views/controls/styled_label.cc

Issue 843023002: [Smart Lock] Add a private API to show an error bubble anchored to the Smart Lock app window. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/styled_label.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <limits>
7 #include <vector> 8 #include <vector>
8 9
9 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
10 #include "ui/gfx/font_list.h" 11 #include "ui/gfx/font_list.h"
11 #include "ui/gfx/text_elider.h" 12 #include "ui/gfx/text_elider.h"
12 #include "ui/native_theme/native_theme.h" 13 #include "ui/native_theme/native_theme.h"
13 #include "ui/views/controls/label.h" 14 #include "ui/views/controls/label.h"
14 #include "ui/views/controls/link.h" 15 #include "ui/views/controls/link.h"
15 #include "ui/views/controls/styled_label_listener.h" 16 #include "ui/views/controls/styled_label_listener.h"
16 17
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 93
93 // StyledLabel ---------------------------------------------------------------- 94 // StyledLabel ----------------------------------------------------------------
94 95
95 // static 96 // static
96 const char StyledLabel::kViewClassName[] = "StyledLabel"; 97 const char StyledLabel::kViewClassName[] = "StyledLabel";
97 98
98 StyledLabel::StyledLabel(const base::string16& text, 99 StyledLabel::StyledLabel(const base::string16& text,
99 StyledLabelListener* listener) 100 StyledLabelListener* listener)
100 : specified_line_height_(0), 101 : specified_line_height_(0),
101 listener_(listener), 102 listener_(listener),
103 width_at_last_size_calculation_(0),
102 width_at_last_layout_(0), 104 width_at_last_layout_(0),
103 displayed_on_background_color_(SkColorSetRGB(0xFF, 0xFF, 0xFF)), 105 displayed_on_background_color_(SkColorSetRGB(0xFF, 0xFF, 0xFF)),
104 displayed_on_background_color_set_(false), 106 displayed_on_background_color_set_(false),
105 auto_color_readability_enabled_(true) { 107 auto_color_readability_enabled_(true) {
106 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); 108 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_);
107 } 109 }
108 110
109 StyledLabel::~StyledLabel() {} 111 StyledLabel::~StyledLabel() {}
110 112
111 void StyledLabel::SetText(const base::string16& text) { 113 void StyledLabel::SetText(const base::string16& text) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 displayed_on_background_color_ = color; 154 displayed_on_background_color_ = color;
153 displayed_on_background_color_set_ = true; 155 displayed_on_background_color_set_ = true;
154 156
155 for (int i = 0, count = child_count(); i < count; ++i) { 157 for (int i = 0, count = child_count(); i < count; ++i) {
156 DCHECK((child_at(i)->GetClassName() == Label::kViewClassName) || 158 DCHECK((child_at(i)->GetClassName() == Label::kViewClassName) ||
157 (child_at(i)->GetClassName() == Link::kViewClassName)); 159 (child_at(i)->GetClassName() == Link::kViewClassName));
158 static_cast<Label*>(child_at(i))->SetBackgroundColor(color); 160 static_cast<Label*>(child_at(i))->SetBackgroundColor(color);
159 } 161 }
160 } 162 }
161 163
164 void StyledLabel::SizeToFit(int max_width) {
165 if (max_width == 0)
166 max_width = std::numeric_limits<int>::max();
167
168 SetSize(CalculateAndDoLayout(max_width, true));
169 }
170
162 const char* StyledLabel::GetClassName() const { 171 const char* StyledLabel::GetClassName() const {
163 return kViewClassName; 172 return kViewClassName;
164 } 173 }
165 174
166 gfx::Insets StyledLabel::GetInsets() const { 175 gfx::Insets StyledLabel::GetInsets() const {
167 gfx::Insets insets = View::GetInsets(); 176 gfx::Insets insets = View::GetInsets();
168 177
169 // We need a focus border iff we contain a link that will have a focus border. 178 // We need a focus border iff we contain a link that will have a focus border.
170 // That in turn will be true only if the link is non-empty. 179 // That in turn will be true only if the link is non-empty.
171 for (StyleRanges::const_iterator i(style_ranges_.begin()); 180 for (StyleRanges::const_iterator i(style_ranges_.begin());
172 i != style_ranges_.end(); ++i) { 181 i != style_ranges_.end(); ++i) {
173 if (i->style_info.is_link && !i->range.is_empty()) { 182 if (i->style_info.is_link && !i->range.is_empty()) {
174 const gfx::Insets focus_border_padding( 183 const gfx::Insets focus_border_padding(
175 Label::kFocusBorderPadding, Label::kFocusBorderPadding, 184 Label::kFocusBorderPadding, Label::kFocusBorderPadding,
176 Label::kFocusBorderPadding, Label::kFocusBorderPadding); 185 Label::kFocusBorderPadding, Label::kFocusBorderPadding);
177 insets += focus_border_padding; 186 insets += focus_border_padding;
178 break; 187 break;
179 } 188 }
180 } 189 }
181 190
182 return insets; 191 return insets;
183 } 192 }
184 193
194 gfx::Size StyledLabel::GetPreferredSize() const {
195 return calculated_size_;
196 }
197
185 int StyledLabel::GetHeightForWidth(int w) const { 198 int StyledLabel::GetHeightForWidth(int w) const {
186 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout 199 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout
187 // doesn't actually make any changes to member variables when |dry_run| is 200 // doesn't actually make any changes to member variables when |dry_run| is
188 // set to true. In general, the mutating and non-mutating parts shouldn't 201 // set to true. In general, the mutating and non-mutating parts shouldn't
189 // be in the same codepath. 202 // be in the same codepath.
190 calculated_size_ = 203 return const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true).height();
191 const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true);
192 return calculated_size_.height();
193 } 204 }
194 205
195 void StyledLabel::Layout() { 206 void StyledLabel::Layout() {
196 calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); 207 CalculateAndDoLayout(GetLocalBounds().width(), false);
197 width_at_last_layout_ = calculated_size_.width();
198 } 208 }
199 209
200 void StyledLabel::PreferredSizeChanged() { 210 void StyledLabel::PreferredSizeChanged() {
201 calculated_size_ = gfx::Size(); 211 calculated_size_ = gfx::Size();
212 width_at_last_size_calculation_ = 0;
213 width_at_last_layout_ = 0;
202 View::PreferredSizeChanged(); 214 View::PreferredSizeChanged();
203 } 215 }
204 216
205 void StyledLabel::LinkClicked(Link* source, int event_flags) { 217 void StyledLabel::LinkClicked(Link* source, int event_flags) {
206 if (listener_) 218 if (listener_)
207 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags); 219 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags);
208 } 220 }
209 221
210 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { 222 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
223 if (width == width_at_last_size_calculation_ &&
224 (dry_run || width == width_at_last_layout_))
225 return calculated_size_;
226
227 width_at_last_size_calculation_ = width;
228 if (!dry_run)
229 width_at_last_layout_ = width;
230
211 width -= GetInsets().width(); 231 width -= GetInsets().width();
212 if (width == calculated_size_.width() &&
213 (dry_run || width_at_last_layout_ == width))
214 return calculated_size_;
215 232
216 if (!dry_run) { 233 if (!dry_run) {
217 RemoveAllChildViews(true); 234 RemoveAllChildViews(true);
218 link_targets_.clear(); 235 link_targets_.clear();
219 } 236 }
220 237
221 if (width <= 0 || text_.empty()) 238 if (width <= 0 || text_.empty())
222 return gfx::Size(); 239 return gfx::Size();
223 240
224 const int line_height = specified_line_height_ > 0 ? specified_line_height_ 241 const int line_height = specified_line_height_ > 0 ? specified_line_height_
225 : CalculateLineHeight(font_list_); 242 : CalculateLineHeight(font_list_);
226 // The index of the line we're on. 243 // The index of the line we're on.
227 int line = 0; 244 int line = 0;
228 // The x position (in pixels) of the line we're on, relative to content 245 // The x position (in pixels) of the line we're on, relative to content
229 // bounds. 246 // bounds.
230 int x = 0; 247 int x = 0;
248 // The width that was actually used. Guaranteed to be no larger than |width|.
249 int used_width = 0;
231 250
232 base::string16 remaining_string = text_; 251 base::string16 remaining_string = text_;
233 StyleRanges::const_iterator current_range = style_ranges_.begin(); 252 StyleRanges::const_iterator current_range = style_ranges_.begin();
234 253
235 // Iterate over the text, creating a bunch of labels and links and laying them 254 // Iterate over the text, creating a bunch of labels and links and laying them
236 // out in the appropriate positions. 255 // out in the appropriate positions.
237 while (!remaining_string.empty()) { 256 while (!remaining_string.empty()) {
238 // Don't put whitespace at beginning of a line with an exception for the 257 // Don't put whitespace at beginning of a line with an exception for the
239 // first line (so the text's leading whitespace is respected). 258 // first line (so the text's leading whitespace is respected).
240 if (x == 0 && line > 0) { 259 if (x == 0 && line > 0) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 const gfx::Size view_size = label->GetPreferredSize(); 345 const gfx::Size view_size = label->GetPreferredSize();
327 if (!dry_run) { 346 if (!dry_run) {
328 label->SetBoundsRect(gfx::Rect( 347 label->SetBoundsRect(gfx::Rect(
329 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), 348 gfx::Point(GetInsets().left() + x - focus_border_insets.left(),
330 GetInsets().top() + line * line_height - 349 GetInsets().top() + line * line_height -
331 focus_border_insets.top()), 350 focus_border_insets.top()),
332 view_size)); 351 view_size));
333 AddChildView(label.release()); 352 AddChildView(label.release());
334 } 353 }
335 x += view_size.width() - focus_border_insets.width(); 354 x += view_size.width() - focus_border_insets.width();
355 used_width = std::max(used_width, x);
336 356
337 remaining_string = remaining_string.substr(chunk.size()); 357 remaining_string = remaining_string.substr(chunk.size());
338 } 358 }
339 359
360 DCHECK_LE(used_width, width);
340 // The user-specified line height only applies to interline spacing, so the 361 // The user-specified line height only applies to interline spacing, so the
341 // final line's height is unaffected. 362 // final line's height is unaffected.
342 int total_height = line * line_height + 363 int total_height = line * line_height +
343 CalculateLineHeight(font_list_) + GetInsets().height(); 364 CalculateLineHeight(font_list_) + GetInsets().height();
344 return gfx::Size(width, total_height); 365 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height);
366 return calculated_size_;
345 } 367 }
346 368
347 } // namespace views 369 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/styled_label.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698