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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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_multiplier_(1.0), |
| 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 Loading... | |
| 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::SetLineHeightMultiplier(double multiplier) { | |
| 138 DCHECK_GT(multiplier, 1.0); | |
| 139 | |
| 140 line_height_multiplier_ = multiplier; | |
| 141 PreferredSizeChanged(); | |
| 142 } | |
| 143 | |
| 136 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { | 144 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { |
| 137 displayed_on_background_color_ = color; | 145 displayed_on_background_color_ = color; |
| 138 displayed_on_background_color_set_ = true; | 146 displayed_on_background_color_set_ = true; |
| 139 } | 147 } |
| 140 | 148 |
| 141 gfx::Insets StyledLabel::GetInsets() const { | 149 gfx::Insets StyledLabel::GetInsets() const { |
| 142 gfx::Insets insets = View::GetInsets(); | 150 gfx::Insets insets = View::GetInsets(); |
| 143 | 151 |
| 144 // We need a focus border iff we contain a link that will have a focus border. | 152 // 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. | 153 // 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 Loading... | |
| 186 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { | 194 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { |
| 187 if (!dry_run) { | 195 if (!dry_run) { |
| 188 RemoveAllChildViews(true); | 196 RemoveAllChildViews(true); |
| 189 link_targets_.clear(); | 197 link_targets_.clear(); |
| 190 } | 198 } |
| 191 | 199 |
| 192 width -= GetInsets().width(); | 200 width -= GetInsets().width(); |
| 193 if (width <= 0 || text_.empty()) | 201 if (width <= 0 || text_.empty()) |
| 194 return gfx::Size(); | 202 return gfx::Size(); |
| 195 | 203 |
| 196 const int line_height = CalculateLineHeight(font_list_); | 204 const int line_height = GetLineHeight(); |
| 197 // The index of the line we're on. | 205 // The index of the line we're on. |
| 198 int line = 0; | 206 int line = 0; |
| 199 // The x position (in pixels) of the line we're on, relative to content | 207 // The x position (in pixels) of the line we're on, relative to content |
| 200 // bounds. | 208 // bounds. |
| 201 int x = 0; | 209 int x = 0; |
| 202 | 210 |
| 203 base::string16 remaining_string = text_; | 211 base::string16 remaining_string = text_; |
| 204 StyleRanges::const_iterator current_range = style_ranges_.begin(); | 212 StyleRanges::const_iterator current_range = style_ranges_.begin(); |
| 205 | 213 |
| 206 // Iterate over the text, creating a bunch of labels and links and laying them | 214 // 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 Loading... | |
| 288 | 296 |
| 289 if (displayed_on_background_color_set_) | 297 if (displayed_on_background_color_set_) |
| 290 label->SetBackgroundColor(displayed_on_background_color_); | 298 label->SetBackgroundColor(displayed_on_background_color_); |
| 291 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); | 299 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); |
| 292 | 300 |
| 293 // Calculate the size of the optional focus border, and overlap by that | 301 // Calculate the size of the optional focus border, and overlap by that |
| 294 // amount. Otherwise, "<a>link</a>," will render as "link ,". | 302 // amount. Otherwise, "<a>link</a>," will render as "link ,". |
| 295 gfx::Insets focus_border_insets(label->GetInsets()); | 303 gfx::Insets focus_border_insets(label->GetInsets()); |
| 296 focus_border_insets += -label->View::GetInsets(); | 304 focus_border_insets += -label->View::GetInsets(); |
| 297 const gfx::Size view_size = label->GetPreferredSize(); | 305 const gfx::Size view_size = label->GetPreferredSize(); |
| 298 DCHECK_EQ(line_height, view_size.height() - focus_border_insets.height()); | |
| 299 if (!dry_run) { | 306 if (!dry_run) { |
| 300 label->SetBoundsRect(gfx::Rect( | 307 label->SetBoundsRect(gfx::Rect( |
| 301 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), | 308 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), |
| 302 GetInsets().top() + line * line_height - | 309 GetInsets().top() + GetOffsetForLine(line) - |
| 303 focus_border_insets.top()), | 310 focus_border_insets.top()), |
| 304 view_size)); | 311 view_size)); |
| 305 AddChildView(label.release()); | 312 AddChildView(label.release()); |
| 306 } | 313 } |
| 307 x += view_size.width() - focus_border_insets.width(); | 314 x += view_size.width() - focus_border_insets.width(); |
| 308 | 315 |
| 309 remaining_string = remaining_string.substr(chunk.size()); | 316 remaining_string = remaining_string.substr(chunk.size()); |
| 310 } | 317 } |
| 311 | 318 |
| 312 return gfx::Size(width, (line + 1) * line_height + GetInsets().height()); | 319 return gfx::Size( |
|
Evan Stade
2014/06/26 03:28:08
revert this change?
Garrett Casto
2014/07/14 06:18:39
Turns out I needed to update this again, but I agr
| |
| 320 width, | |
| 321 (line + 1) * line_height + GetInsets().height()); | |
| 322 } | |
| 323 | |
| 324 int StyledLabel::GetLineHeight() const { | |
| 325 return CalculateLineHeight(font_list_) * line_height_multiplier_; | |
| 326 } | |
| 327 | |
| 328 int StyledLabel::GetOffsetForLine(int line) const { | |
| 329 int default_line_height = CalculateLineHeight(font_list_); | |
| 330 int line_height = GetLineHeight(); | |
| 331 | |
| 332 // Center text in the space available. | |
| 333 int interline_offset = (line_height - default_line_height) / 2; | |
| 334 return line_height * line + interline_offset; | |
| 313 } | 335 } |
| 314 | 336 |
| 315 } // namespace views | 337 } // namespace views |
| OLD | NEW |