1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/message_center/views/bounded_label.h"
#include <limits>
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/text_elider.h"
#include "ui/gfx/text_utils.h"
#include "ui/views/controls/label.h"
namespace {
const size_t kPreferredLinesCacheSize = 10;
} // namespace
namespace message_center {
// InnerBoundedLabel ///////////////////////////////////////////////////////////
// InnerBoundedLabel is a views::Label subclass that does all of the work for
// BoundedLabel. It is kept private to prevent outside code from calling a
// number of views::Label methods like SetFontList() that break BoundedLabel's
// caching but can't be overridden.
//
// TODO(dharcourt): Move the line limiting functionality to views::Label to make
// this unnecessary.
class InnerBoundedLabel : public views::Label {
public:
InnerBoundedLabel(const BoundedLabel& owner);
virtual ~InnerBoundedLabel();
void SetNativeTheme(const ui::NativeTheme* theme);
// Pass in a -1 width to use the preferred width, a -1 limit to skip limits.
int GetLinesForWidthAndLimit(int width, int limit);
gfx::Size GetSizeForWidthAndLines(int width, int lines);
std::vector<base::string16> GetWrappedText(int width, int lines);
protected:
// Overridden from views::Label.
virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
private:
int GetTextFlags();
void ClearCaches();
int GetCachedLines(int width);
void SetCachedLines(int width, int lines);
gfx::Size GetCachedSize(const std::pair<int, int>& width_and_lines);
void SetCachedSize(std::pair<int, int> width_and_lines, gfx::Size size);
const BoundedLabel* owner_; // Weak reference.
base::string16 wrapped_text_;
int wrapped_text_width_;
int wrapped_text_lines_;
std::map<int, int> lines_cache_;
std::list<int> lines_widths_; // Most recently used in front.
std::map<std::pair<int, int>, gfx::Size> size_cache_;
std::list<std::pair<int, int> > size_widths_and_lines_; // Recent in front.
DISALLOW_COPY_AND_ASSIGN(InnerBoundedLabel);
};
InnerBoundedLabel::InnerBoundedLabel(const BoundedLabel& owner)
: owner_(&owner),
wrapped_text_width_(0),
wrapped_text_lines_(0) {
SetMultiLine(true);
SetAllowCharacterBreak(true);
SetHorizontalAlignment(gfx::ALIGN_LEFT);
set_collapse_when_hidden(true);
}
InnerBoundedLabel::~InnerBoundedLabel() {
}
void InnerBoundedLabel::SetNativeTheme(const ui::NativeTheme* theme) {
ClearCaches();
OnNativeThemeChanged(theme);
}
int InnerBoundedLabel::GetLinesForWidthAndLimit(int width, int limit) {
if (width == 0 || limit == 0)
return 0;
int lines = GetCachedLines(width);
if (lines == std::numeric_limits<int>::max()) {
int text_width = std::max(width - owner_->GetInsets().width(), 0);
lines = GetWrappedText(text_width, lines).size();
SetCachedLines(width, lines);
}
return (limit < 0 || lines <= limit) ? lines : limit;
}
gfx::Size InnerBoundedLabel::GetSizeForWidthAndLines(int width, int lines) {
if (width == 0 || lines == 0)
return gfx::Size();
std::pair<int, int> key(width, lines);
gfx::Size size = GetCachedSize(key);
if (size.height() == std::numeric_limits<int>::max()) {
gfx::Insets insets = owner_->GetInsets();
int text_width = (width < 0) ? std::numeric_limits<int>::max() :
std::max(width - insets.width(), 0);
int text_height = std::numeric_limits<int>::max();
std::vector<base::string16> wrapped = GetWrappedText(text_width, lines);
gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font_list(),
&text_width, &text_height,
owner_->GetLineHeight(),
GetTextFlags());
size.set_width(text_width + insets.width());
size.set_height(text_height + insets.height());
SetCachedSize(key, size);
}
return size;
}
std::vector<base::string16> InnerBoundedLabel::GetWrappedText(int width,
int lines) {
// Short circuit simple case.
if (width == 0 || lines == 0)
return std::vector<base::string16>();
// Restrict line limit to ensure (lines + 1) * line_height <= INT_MAX and
// use it to calculate a reasonable text height.
int height = std::numeric_limits<int>::max();
if (lines > 0) {
int line_height = std::max(font_list().GetHeight(),
2); // At least 2 pixels.
int max_lines = std::numeric_limits<int>::max() / line_height - 1;
lines = std::min(lines, max_lines);
height = (lines + 1) * line_height;
}
// Try to ensure that the width is no smaller than the width of the text's
// characters to avoid the http://crbug.com/237700 infinite loop.
// TODO(dharcourt): Remove when http://crbug.com/237700 is fixed.
width = std::max(width,
2 * gfx::GetStringWidth(base::UTF8ToUTF16("W"),
font_list()));
// Wrap, using INT_MAX for -1 widths that indicate no wrapping.
std::vector<base::string16> wrapped;
gfx::ElideRectangleText(text(), font_list(),
(width < 0) ? std::numeric_limits<int>::max() : width,
height, gfx::WRAP_LONG_WORDS, &wrapped);
// Elide if necessary.
if (lines > 0 && wrapped.size() > static_cast<unsigned int>(lines)) {
// Add an ellipsis to the last line. If this ellipsis makes the last line
// too wide, that line will be further elided by the gfx::ElideText below,
// so for example "ABC" could become "ABC..." and then "AB...".
base::string16 last =
wrapped[lines - 1] + base::UTF8ToUTF16(gfx::kEllipsis);
if (width > 0 && gfx::GetStringWidth(last, font_list()) > width)
last = gfx::ElideText(last, font_list(), width, gfx::ELIDE_AT_END);
wrapped.resize(lines - 1);
wrapped.push_back(last);
}
return wrapped;
}
void InnerBoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) {
ClearCaches();
views::Label::OnBoundsChanged(previous_bounds);
}
void InnerBoundedLabel::OnPaint(gfx::Canvas* canvas) {
views::Label::OnPaintBackground(canvas);
views::Label::OnPaintBorder(canvas);
int lines = owner_->GetLineLimit();
int height = GetSizeForWidthAndLines(width(), lines).height();
if (height > 0) {
gfx::Rect bounds(width(), height);
bounds.Inset(owner_->GetInsets());
if (bounds.width() != wrapped_text_width_ || lines != wrapped_text_lines_) {
wrapped_text_ = JoinString(GetWrappedText(bounds.width(), lines), '\n');
wrapped_text_width_ = bounds.width();
wrapped_text_lines_ = lines;
}
bounds.set_x(GetMirroredXForRect(bounds));
PaintText(canvas, wrapped_text_, bounds, GetTextFlags());
}
}
int InnerBoundedLabel::GetTextFlags() {
int flags = gfx::Canvas::MULTI_LINE | gfx::Canvas::CHARACTER_BREAK;
// We can't use subpixel rendering if the background is non-opaque.
if (SkColorGetA(background_color()) != 0xFF)
flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
if (directionality_mode() ==
views::Label::AUTO_DETECT_DIRECTIONALITY) {
base::i18n::TextDirection direction =
base::i18n::GetFirstStrongCharacterDirection(text());
if (direction == base::i18n::RIGHT_TO_LEFT)
flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
else
flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
}
return flags;
}
void InnerBoundedLabel::ClearCaches() {
wrapped_text_width_ = 0;
wrapped_text_lines_ = 0;
lines_cache_.clear();
lines_widths_.clear();
size_cache_.clear();
size_widths_and_lines_.clear();
}
int InnerBoundedLabel::GetCachedLines(int width) {
int lines = std::numeric_limits<int>::max();
std::map<int, int>::const_iterator found;
if ((found = lines_cache_.find(width)) != lines_cache_.end()) {
lines = found->second;
lines_widths_.remove(width);
lines_widths_.push_front(width);
}
return lines;
}
void InnerBoundedLabel::SetCachedLines(int width, int lines) {
if (lines_cache_.size() >= kPreferredLinesCacheSize) {
lines_cache_.erase(lines_widths_.back());
lines_widths_.pop_back();
}
lines_cache_[width] = lines;
lines_widths_.push_front(width);
}
gfx::Size InnerBoundedLabel::GetCachedSize(
const std::pair<int, int>& width_and_lines) {
gfx::Size size(width_and_lines.first, std::numeric_limits<int>::max());
std::map<std::pair<int, int>, gfx::Size>::const_iterator found;
if ((found = size_cache_.find(width_and_lines)) != size_cache_.end()) {
size = found->second;
size_widths_and_lines_.remove(width_and_lines);
size_widths_and_lines_.push_front(width_and_lines);
}
return size;
}
void InnerBoundedLabel::SetCachedSize(std::pair<int, int> width_and_lines,
gfx::Size size) {
if (size_cache_.size() >= kPreferredLinesCacheSize) {
size_cache_.erase(size_widths_and_lines_.back());
size_widths_and_lines_.pop_back();
}
size_cache_[width_and_lines] = size;
size_widths_and_lines_.push_front(width_and_lines);
}
// BoundedLabel ///////////////////////////////////////////////////////////
BoundedLabel::BoundedLabel(const base::string16& text,
const gfx::FontList& font_list)
: line_limit_(-1) {
label_.reset(new InnerBoundedLabel(*this));
label_->SetFontList(font_list);
label_->SetText(text);
}
BoundedLabel::BoundedLabel(const base::string16& text)
: line_limit_(-1) {
label_.reset(new InnerBoundedLabel(*this));
label_->SetText(text);
}
BoundedLabel::~BoundedLabel() {
}
void BoundedLabel::SetColors(SkColor textColor, SkColor backgroundColor) {
label_->SetEnabledColor(textColor);
label_->SetBackgroundColor(backgroundColor);
}
void BoundedLabel::SetLineHeight(int height) {
label_->SetLineHeight(height);
}
void BoundedLabel::SetLineLimit(int lines) {
line_limit_ = std::max(lines, -1);
}
int BoundedLabel::GetLineHeight() const {
return label_->line_height();
}
int BoundedLabel::GetLineLimit() const {
return line_limit_;
}
int BoundedLabel::GetLinesForWidthAndLimit(int width, int limit) {
return visible() ? label_->GetLinesForWidthAndLimit(width, limit) : 0;
}
gfx::Size BoundedLabel::GetSizeForWidthAndLines(int width, int lines) {
return visible() ?
label_->GetSizeForWidthAndLines(width, lines) : gfx::Size();
}
int BoundedLabel::GetBaseline() const {
return label_->GetBaseline();
}
gfx::Size BoundedLabel::GetPreferredSize() {
return visible() ? label_->GetSizeForWidthAndLines(-1, -1) : gfx::Size();
}
int BoundedLabel::GetHeightForWidth(int width) {
return visible() ?
label_->GetSizeForWidthAndLines(width, line_limit_).height() : 0;
}
void BoundedLabel::Paint(gfx::Canvas* canvas) {
if (visible())
label_->Paint(canvas);
}
bool BoundedLabel::HitTestRect(const gfx::Rect& rect) const {
return label_->HitTestRect(rect);
}
void BoundedLabel::GetAccessibleState(ui::AccessibleViewState* state) {
label_->GetAccessibleState(state);
}
void BoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) {
label_->SetBoundsRect(bounds());
views::View::OnBoundsChanged(previous_bounds);
}
void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) {
label_->SetNativeTheme(theme);
}
base::string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) {
return JoinString(label_->GetWrappedText(width, lines), '\n');
}
} // namespace message_center
|