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

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

Issue 342833002: [Password Generation] Update Aura UI (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More mac fixes Created 6 years, 5 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 | Annotate | Revision Log
« 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 <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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 bool StyledLabel::StyleRange::operator<( 87 bool StyledLabel::StyleRange::operator<(
88 const StyledLabel::StyleRange& other) const { 88 const StyledLabel::StyleRange& other) const {
89 return range.start() < other.range.start(); 89 return range.start() < other.range.start();
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 : listener_(listener), 97 : line_height_(-1),
98 listener_(listener),
98 displayed_on_background_color_set_(false), 99 displayed_on_background_color_set_(false),
99 auto_color_readability_enabled_(true) { 100 auto_color_readability_enabled_(true) {
100 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); 101 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_);
101 } 102 }
102 103
103 StyledLabel::~StyledLabel() {} 104 StyledLabel::~StyledLabel() {}
104 105
105 void StyledLabel::SetText(const base::string16& text) { 106 void StyledLabel::SetText(const base::string16& text) {
106 text_ = text; 107 text_ = text;
107 style_ranges_.clear(); 108 style_ranges_.clear();
(...skipping 18 matching lines...) Expand all
126 style_ranges_.merge(new_range); 127 style_ranges_.merge(new_range);
127 128
128 PreferredSizeChanged(); 129 PreferredSizeChanged();
129 } 130 }
130 131
131 void StyledLabel::SetDefaultStyle(const RangeStyleInfo& style_info) { 132 void StyledLabel::SetDefaultStyle(const RangeStyleInfo& style_info) {
132 default_style_info_ = style_info; 133 default_style_info_ = style_info;
133 PreferredSizeChanged(); 134 PreferredSizeChanged();
134 } 135 }
135 136
137 void StyledLabel::SetLineHeight(int line_height) {
138 line_height_ = line_height;
139 PreferredSizeChanged();
140 }
141
136 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { 142 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) {
137 displayed_on_background_color_ = color; 143 displayed_on_background_color_ = color;
138 displayed_on_background_color_set_ = true; 144 displayed_on_background_color_set_ = true;
139 } 145 }
140 146
141 gfx::Insets StyledLabel::GetInsets() const { 147 gfx::Insets StyledLabel::GetInsets() const {
142 gfx::Insets insets = View::GetInsets(); 148 gfx::Insets insets = View::GetInsets();
143 149
144 // We need a focus border iff we contain a link that will have a focus border. 150 // We need a focus border iff we contain a link that will have a focus border.
145 // That in turn will be true only if the link is non-empty. 151 // That in turn will be true only if the link is non-empty.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { 192 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
187 if (!dry_run) { 193 if (!dry_run) {
188 RemoveAllChildViews(true); 194 RemoveAllChildViews(true);
189 link_targets_.clear(); 195 link_targets_.clear();
190 } 196 }
191 197
192 width -= GetInsets().width(); 198 width -= GetInsets().width();
193 if (width <= 0 || text_.empty()) 199 if (width <= 0 || text_.empty())
194 return gfx::Size(); 200 return gfx::Size();
195 201
196 const int line_height = CalculateLineHeight(font_list_); 202 const int line_height = GetLineHeight();
197 // The index of the line we're on. 203 // The index of the line we're on.
198 int line = 0; 204 int line = 0;
199 // The x position (in pixels) of the line we're on, relative to content 205 // The x position (in pixels) of the line we're on, relative to content
200 // bounds. 206 // bounds.
201 int x = 0; 207 int x = 0;
202 208
203 base::string16 remaining_string = text_; 209 base::string16 remaining_string = text_;
204 StyleRanges::const_iterator current_range = style_ranges_.begin(); 210 StyleRanges::const_iterator current_range = style_ranges_.begin();
205 211
206 // Iterate over the text, creating a bunch of labels and links and laying them 212 // Iterate over the text, creating a bunch of labels and links and laying them
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 294
289 if (displayed_on_background_color_set_) 295 if (displayed_on_background_color_set_)
290 label->SetBackgroundColor(displayed_on_background_color_); 296 label->SetBackgroundColor(displayed_on_background_color_);
291 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); 297 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_);
292 298
293 // Calculate the size of the optional focus border, and overlap by that 299 // Calculate the size of the optional focus border, and overlap by that
294 // amount. Otherwise, "<a>link</a>," will render as "link ,". 300 // amount. Otherwise, "<a>link</a>," will render as "link ,".
295 gfx::Insets focus_border_insets(label->GetInsets()); 301 gfx::Insets focus_border_insets(label->GetInsets());
296 focus_border_insets += -label->View::GetInsets(); 302 focus_border_insets += -label->View::GetInsets();
297 const gfx::Size view_size = label->GetPreferredSize(); 303 const gfx::Size view_size = label->GetPreferredSize();
298 DCHECK_EQ(line_height, view_size.height() - focus_border_insets.height());
299 if (!dry_run) { 304 if (!dry_run) {
300 label->SetBoundsRect(gfx::Rect( 305 label->SetBoundsRect(gfx::Rect(
301 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), 306 gfx::Point(GetInsets().left() + x - focus_border_insets.left(),
302 GetInsets().top() + line * line_height - 307 GetInsets().top() + GetOffsetForLine(line) -
303 focus_border_insets.top()), 308 focus_border_insets.top()),
304 view_size)); 309 view_size));
305 AddChildView(label.release()); 310 AddChildView(label.release());
306 } 311 }
307 x += view_size.width() - focus_border_insets.width(); 312 x += view_size.width() - focus_border_insets.width();
308 313
309 remaining_string = remaining_string.substr(chunk.size()); 314 remaining_string = remaining_string.substr(chunk.size());
310 } 315 }
311 316
312 return gfx::Size(width, (line + 1) * line_height + GetInsets().height()); 317 return gfx::Size(
318 width,
319 (line + 1) * line_height + GetInsets().height());
320 }
321
322 int StyledLabel::GetLineHeight() {
sky 2014/06/25 21:53:48 nit: const
Garrett Casto 2014/06/25 22:06:43 Done.
323 if (line_height_ >= 0)
324 return line_height_;
325 return CalculateLineHeight(font_list_);
326 }
327
328 int StyledLabel::GetOffsetForLine(int line) {
sky 2014/06/25 21:53:48 nit: const
Garrett Casto 2014/06/25 22:06:43 Done.
329 int default_line_height = CalculateLineHeight(font_list_);
330
331 // If no user override, return default.
332 if (line_height_ < 0)
333 return default_line_height * line;
334
335 // If line height is less than default, no centering needed.
336 if (line_height_ < default_line_height)
337 return line_height_ * line;
338
339 // Center text in the space available.
sky 2014/06/25 21:53:48 Why do you need this or the if on 336. I guess mor
Garrett Casto 2014/06/25 22:06:43 I don't have a use case for it, but estade@ though
340 int interline_offset = (line_height_ - default_line_height) / 2;
341 return line_height_ * line + interline_offset;
313 } 342 }
314 343
315 } // namespace views 344 } // 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