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
3 // found in the LICENSE file.
5 #include "ui/message_center/views/bounded_label.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/text_elider.h"
13 #include "ui/gfx/text_utils.h"
14 #include "ui/views/controls/label.h"
18 const size_t kPreferredLinesCacheSize = 10;
22 namespace message_center {
24 // InnerBoundedLabel ///////////////////////////////////////////////////////////
26 // InnerBoundedLabel is a views::Label subclass that does all of the work for
27 // BoundedLabel. It is kept private to prevent outside code from calling a
28 // number of views::Label methods like SetFontList() that break BoundedLabel's
29 // caching but can't be overridden.
31 // TODO(dharcourt): Move the line limiting functionality to views::Label to make
34 class InnerBoundedLabel : public views::Label {
36 InnerBoundedLabel(const BoundedLabel& owner);
37 virtual ~InnerBoundedLabel();
39 void SetNativeTheme(const ui::NativeTheme* theme);
41 // Pass in a -1 width to use the preferred width, a -1 limit to skip limits.
42 int GetLinesForWidthAndLimit(int width, int limit);
43 gfx::Size GetSizeForWidthAndLines(int width, int lines);
44 std::vector<base::string16> GetWrappedText(int width, int lines);
46 // Overridden from views::Label.
47 virtual void SetText(const base::string16& text) OVERRIDE;
50 // Overridden from views::Label.
51 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
52 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
58 int GetCachedLines(int width);
59 void SetCachedLines(int width, int lines);
60 gfx::Size GetCachedSize(const std::pair<int, int>& width_and_lines);
61 void SetCachedSize(std::pair<int, int> width_and_lines, gfx::Size size);
63 const BoundedLabel* owner_; // Weak reference.
64 base::string16 wrapped_text_;
65 int wrapped_text_width_;
66 int wrapped_text_lines_;
67 std::map<int, int> lines_cache_;
68 std::list<int> lines_widths_; // Most recently used in front.
69 std::map<std::pair<int, int>, gfx::Size> size_cache_;
70 std::list<std::pair<int, int> > size_widths_and_lines_; // Recent in front.
72 DISALLOW_COPY_AND_ASSIGN(InnerBoundedLabel);
75 InnerBoundedLabel::InnerBoundedLabel(const BoundedLabel& owner)
77 wrapped_text_width_(0),
78 wrapped_text_lines_(0) {
80 SetAllowCharacterBreak(true);
81 SetHorizontalAlignment(gfx::ALIGN_LEFT);
82 set_collapse_when_hidden(true);
85 InnerBoundedLabel::~InnerBoundedLabel() {
88 void InnerBoundedLabel::SetNativeTheme(const ui::NativeTheme* theme) {
90 OnNativeThemeChanged(theme);
93 int InnerBoundedLabel::GetLinesForWidthAndLimit(int width, int limit) {
94 if (width == 0 || limit == 0)
96 int lines = GetCachedLines(width);
97 if (lines == std::numeric_limits<int>::max()) {
98 int text_width = std::max(width - owner_->GetInsets().width(), 0);
99 lines = GetWrappedText(text_width, lines).size();
100 SetCachedLines(width, lines);
102 return (limit < 0 || lines <= limit) ? lines : limit;
105 gfx::Size InnerBoundedLabel::GetSizeForWidthAndLines(int width, int lines) {
106 if (width == 0 || lines == 0)
108 std::pair<int, int> key(width, lines);
109 gfx::Size size = GetCachedSize(key);
110 if (size.height() == std::numeric_limits<int>::max()) {
111 gfx::Insets insets = owner_->GetInsets();
112 int text_width = (width < 0) ? std::numeric_limits<int>::max() :
113 std::max(width - insets.width(), 0);
114 int text_height = std::numeric_limits<int>::max();
115 std::vector<base::string16> wrapped = GetWrappedText(text_width, lines);
116 gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font_list(),
117 &text_width, &text_height,
118 owner_->GetLineHeight(),
120 size.set_width(text_width + insets.width());
121 size.set_height(text_height + insets.height());
122 SetCachedSize(key, size);
127 std::vector<base::string16> InnerBoundedLabel::GetWrappedText(int width,
129 // Short circuit simple case.
130 if (width == 0 || lines == 0)
131 return std::vector<base::string16>();
133 // Restrict line limit to ensure (lines + 1) * line_height <= INT_MAX and
134 // use it to calculate a reasonable text height.
135 int height = std::numeric_limits<int>::max();
137 int line_height = std::max(font_list().GetHeight(),
138 2); // At least 2 pixels.
139 int max_lines = std::numeric_limits<int>::max() / line_height - 1;
140 lines = std::min(lines, max_lines);
141 height = (lines + 1) * line_height;
144 // Try to ensure that the width is no smaller than the width of the text's
145 // characters to avoid the http://crbug.com/237700 infinite loop.
146 // TODO(dharcourt): Remove when http://crbug.com/237700 is fixed.
147 width = std::max(width,
148 2 * gfx::GetStringWidth(base::UTF8ToUTF16("W"),
151 // Wrap, using INT_MAX for -1 widths that indicate no wrapping.
152 std::vector<base::string16> wrapped;
153 gfx::ElideRectangleText(text(), font_list(),
154 (width < 0) ? std::numeric_limits<int>::max() : width,
155 height, gfx::WRAP_LONG_WORDS, &wrapped);
157 // Elide if necessary.
158 if (lines > 0 && wrapped.size() > static_cast<unsigned int>(lines)) {
159 // Add an ellipsis to the last line. If this ellipsis makes the last line
160 // too wide, that line will be further elided by the gfx::ElideText below,
161 // so for example "ABC" could become "ABC..." and then "AB...".
162 base::string16 last =
163 wrapped[lines - 1] + base::UTF8ToUTF16(gfx::kEllipsis);
164 if (width > 0 && gfx::GetStringWidth(last, font_list()) > width)
165 last = gfx::ElideText(last, font_list(), width, gfx::ELIDE_AT_END);
166 wrapped.resize(lines - 1);
167 wrapped.push_back(last);
173 void InnerBoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) {
175 views::Label::OnBoundsChanged(previous_bounds);
178 void InnerBoundedLabel::OnPaint(gfx::Canvas* canvas) {
179 views::Label::OnPaintBackground(canvas);
180 views::Label::OnPaintBorder(canvas);
181 int lines = owner_->GetLineLimit();
182 int height = GetSizeForWidthAndLines(width(), lines).height();
184 gfx::Rect bounds(width(), height);
185 bounds.Inset(owner_->GetInsets());
186 if (bounds.width() != wrapped_text_width_ || lines != wrapped_text_lines_) {
187 wrapped_text_ = JoinString(GetWrappedText(bounds.width(), lines), '\n');
188 wrapped_text_width_ = bounds.width();
189 wrapped_text_lines_ = lines;
191 bounds.set_x(GetMirroredXForRect(bounds));
192 PaintText(canvas, wrapped_text_, bounds, GetTextFlags());
196 void InnerBoundedLabel::SetText(const base::string16& text) {
197 views::Label::SetText(text);
201 int InnerBoundedLabel::GetTextFlags() {
202 int flags = gfx::Canvas::MULTI_LINE | gfx::Canvas::CHARACTER_BREAK;
204 // We can't use subpixel rendering if the background is non-opaque.
205 if (SkColorGetA(background_color()) != 0xFF)
206 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
208 if (directionality_mode() ==
209 views::Label::AUTO_DETECT_DIRECTIONALITY) {
210 base::i18n::TextDirection direction =
211 base::i18n::GetFirstStrongCharacterDirection(text());
212 if (direction == base::i18n::RIGHT_TO_LEFT)
213 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
215 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
221 void InnerBoundedLabel::ClearCaches() {
222 wrapped_text_width_ = 0;
223 wrapped_text_lines_ = 0;
224 lines_cache_.clear();
225 lines_widths_.clear();
227 size_widths_and_lines_.clear();
230 int InnerBoundedLabel::GetCachedLines(int width) {
231 int lines = std::numeric_limits<int>::max();
232 std::map<int, int>::const_iterator found;
233 if ((found = lines_cache_.find(width)) != lines_cache_.end()) {
234 lines = found->second;
235 lines_widths_.remove(width);
236 lines_widths_.push_front(width);
241 void InnerBoundedLabel::SetCachedLines(int width, int lines) {
242 if (lines_cache_.size() >= kPreferredLinesCacheSize) {
243 lines_cache_.erase(lines_widths_.back());
244 lines_widths_.pop_back();
246 lines_cache_[width] = lines;
247 lines_widths_.push_front(width);
250 gfx::Size InnerBoundedLabel::GetCachedSize(
251 const std::pair<int, int>& width_and_lines) {
252 gfx::Size size(width_and_lines.first, std::numeric_limits<int>::max());
253 std::map<std::pair<int, int>, gfx::Size>::const_iterator found;
254 if ((found = size_cache_.find(width_and_lines)) != size_cache_.end()) {
255 size = found->second;
256 size_widths_and_lines_.remove(width_and_lines);
257 size_widths_and_lines_.push_front(width_and_lines);
262 void InnerBoundedLabel::SetCachedSize(std::pair<int, int> width_and_lines,
264 if (size_cache_.size() >= kPreferredLinesCacheSize) {
265 size_cache_.erase(size_widths_and_lines_.back());
266 size_widths_and_lines_.pop_back();
268 size_cache_[width_and_lines] = size;
269 size_widths_and_lines_.push_front(width_and_lines);
272 // BoundedLabel ///////////////////////////////////////////////////////////
274 BoundedLabel::BoundedLabel(const base::string16& text,
275 const gfx::FontList& font_list)
277 label_.reset(new InnerBoundedLabel(*this));
278 label_->SetFontList(font_list);
279 label_->SetText(text);
282 BoundedLabel::BoundedLabel(const base::string16& text)
284 label_.reset(new InnerBoundedLabel(*this));
285 label_->SetText(text);
288 BoundedLabel::~BoundedLabel() {
291 void BoundedLabel::SetColors(SkColor textColor, SkColor backgroundColor) {
292 label_->SetEnabledColor(textColor);
293 label_->SetBackgroundColor(backgroundColor);
296 void BoundedLabel::SetLineHeight(int height) {
297 label_->SetLineHeight(height);
300 void BoundedLabel::SetLineLimit(int lines) {
301 line_limit_ = std::max(lines, -1);
304 void BoundedLabel::SetText(const base::string16& text) {
305 label_->SetText(text);
308 int BoundedLabel::GetLineHeight() const {
309 return label_->line_height();
312 int BoundedLabel::GetLineLimit() const {
316 int BoundedLabel::GetLinesForWidthAndLimit(int width, int limit) {
317 return visible() ? label_->GetLinesForWidthAndLimit(width, limit) : 0;
320 gfx::Size BoundedLabel::GetSizeForWidthAndLines(int width, int lines) {
322 label_->GetSizeForWidthAndLines(width, lines) : gfx::Size();
325 int BoundedLabel::GetBaseline() const {
326 return label_->GetBaseline();
329 gfx::Size BoundedLabel::GetPreferredSize() {
330 return visible() ? label_->GetSizeForWidthAndLines(-1, -1) : gfx::Size();
333 int BoundedLabel::GetHeightForWidth(int width) {
335 label_->GetSizeForWidthAndLines(width, line_limit_).height() : 0;
338 void BoundedLabel::Paint(gfx::Canvas* canvas) {
340 label_->Paint(canvas);
343 bool BoundedLabel::HitTestRect(const gfx::Rect& rect) const {
344 return label_->HitTestRect(rect);
347 void BoundedLabel::GetAccessibleState(ui::AXViewState* state) {
348 label_->GetAccessibleState(state);
351 void BoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) {
352 label_->SetBoundsRect(bounds());
353 views::View::OnBoundsChanged(previous_bounds);
356 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) {
357 label_->SetNativeTheme(theme);
360 base::string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) {
361 return JoinString(label_->GetWrappedText(width, lines), '\n');
364 } // namespace message_center