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/message_center/views/bounded_label.h" | 5 #include "ui/message_center/views/bounded_label.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 class InnerBoundedLabel : public views::Label { | 33 class InnerBoundedLabel : public views::Label { |
34 public: | 34 public: |
35 InnerBoundedLabel(const BoundedLabel& owner); | 35 InnerBoundedLabel(const BoundedLabel& owner); |
36 virtual ~InnerBoundedLabel(); | 36 virtual ~InnerBoundedLabel(); |
37 | 37 |
38 void SetNativeTheme(const ui::NativeTheme* theme); | 38 void SetNativeTheme(const ui::NativeTheme* theme); |
39 | 39 |
40 // Pass in a -1 width to use the preferred width, a -1 limit to skip limits. | 40 // Pass in a -1 width to use the preferred width, a -1 limit to skip limits. |
41 int GetLinesForWidthAndLimit(int width, int limit); | 41 int GetLinesForWidthAndLimit(int width, int limit); |
42 gfx::Size GetSizeForWidthAndLines(int width, int lines); | 42 gfx::Size GetSizeForWidthAndLines(int width, int lines); |
43 std::vector<string16> GetWrappedText(int width, int lines); | 43 std::vector<base::string16> GetWrappedText(int width, int lines); |
44 | 44 |
45 protected: | 45 protected: |
46 // Overridden from views::Label. | 46 // Overridden from views::Label. |
47 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; | 47 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; |
48 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 48 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
49 | 49 |
50 private: | 50 private: |
51 int GetTextFlags(); | 51 int GetTextFlags(); |
52 | 52 |
53 void ClearCaches(); | 53 void ClearCaches(); |
54 int GetCachedLines(int width); | 54 int GetCachedLines(int width); |
55 void SetCachedLines(int width, int lines); | 55 void SetCachedLines(int width, int lines); |
56 gfx::Size GetCachedSize(const std::pair<int, int>& width_and_lines); | 56 gfx::Size GetCachedSize(const std::pair<int, int>& width_and_lines); |
57 void SetCachedSize(std::pair<int, int> width_and_lines, gfx::Size size); | 57 void SetCachedSize(std::pair<int, int> width_and_lines, gfx::Size size); |
58 | 58 |
59 const BoundedLabel* owner_; // Weak reference. | 59 const BoundedLabel* owner_; // Weak reference. |
60 string16 wrapped_text_; | 60 base::string16 wrapped_text_; |
61 int wrapped_text_width_; | 61 int wrapped_text_width_; |
62 int wrapped_text_lines_; | 62 int wrapped_text_lines_; |
63 std::map<int, int> lines_cache_; | 63 std::map<int, int> lines_cache_; |
64 std::list<int> lines_widths_; // Most recently used in front. | 64 std::list<int> lines_widths_; // Most recently used in front. |
65 std::map<std::pair<int, int>, gfx::Size> size_cache_; | 65 std::map<std::pair<int, int>, gfx::Size> size_cache_; |
66 std::list<std::pair<int, int> > size_widths_and_lines_; // Recent in front. | 66 std::list<std::pair<int, int> > size_widths_and_lines_; // Recent in front. |
67 | 67 |
68 DISALLOW_COPY_AND_ASSIGN(InnerBoundedLabel); | 68 DISALLOW_COPY_AND_ASSIGN(InnerBoundedLabel); |
69 }; | 69 }; |
70 | 70 |
(...skipping 30 matching lines...) Expand all Loading... |
101 gfx::Size InnerBoundedLabel::GetSizeForWidthAndLines(int width, int lines) { | 101 gfx::Size InnerBoundedLabel::GetSizeForWidthAndLines(int width, int lines) { |
102 if (width == 0 || lines == 0) | 102 if (width == 0 || lines == 0) |
103 return gfx::Size(); | 103 return gfx::Size(); |
104 std::pair<int, int> key(width, lines); | 104 std::pair<int, int> key(width, lines); |
105 gfx::Size size = GetCachedSize(key); | 105 gfx::Size size = GetCachedSize(key); |
106 if (size.height() == std::numeric_limits<int>::max()) { | 106 if (size.height() == std::numeric_limits<int>::max()) { |
107 gfx::Insets insets = owner_->GetInsets(); | 107 gfx::Insets insets = owner_->GetInsets(); |
108 int text_width = (width < 0) ? std::numeric_limits<int>::max() : | 108 int text_width = (width < 0) ? std::numeric_limits<int>::max() : |
109 std::max(width - insets.width(), 0); | 109 std::max(width - insets.width(), 0); |
110 int text_height = std::numeric_limits<int>::max(); | 110 int text_height = std::numeric_limits<int>::max(); |
111 std::vector<string16> wrapped = GetWrappedText(text_width, lines); | 111 std::vector<base::string16> wrapped = GetWrappedText(text_width, lines); |
112 gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font(), | 112 gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font(), |
113 &text_width, &text_height, | 113 &text_width, &text_height, |
114 owner_->GetLineHeight(), | 114 owner_->GetLineHeight(), |
115 GetTextFlags()); | 115 GetTextFlags()); |
116 size.set_width(text_width + insets.width()); | 116 size.set_width(text_width + insets.width()); |
117 size.set_height(text_height + insets.height()); | 117 size.set_height(text_height + insets.height()); |
118 SetCachedSize(key, size); | 118 SetCachedSize(key, size); |
119 } | 119 } |
120 return size; | 120 return size; |
121 } | 121 } |
122 | 122 |
123 std::vector<string16> InnerBoundedLabel::GetWrappedText(int width, int lines) { | 123 std::vector<base::string16> InnerBoundedLabel::GetWrappedText(int width, |
| 124 int lines) { |
124 // Short circuit simple case. | 125 // Short circuit simple case. |
125 if (width == 0 || lines == 0) | 126 if (width == 0 || lines == 0) |
126 return std::vector<string16>(); | 127 return std::vector<base::string16>(); |
127 | 128 |
128 // Restrict line limit to ensure (lines + 1) * line_height <= INT_MAX and | 129 // Restrict line limit to ensure (lines + 1) * line_height <= INT_MAX and |
129 // use it to calculate a reasonable text height. | 130 // use it to calculate a reasonable text height. |
130 int height = std::numeric_limits<int>::max(); | 131 int height = std::numeric_limits<int>::max(); |
131 if (lines > 0) { | 132 if (lines > 0) { |
132 int line_height = std::max(font().GetHeight(), 2); // At least 2 pixels. | 133 int line_height = std::max(font().GetHeight(), 2); // At least 2 pixels. |
133 int max_lines = std::numeric_limits<int>::max() / line_height - 1; | 134 int max_lines = std::numeric_limits<int>::max() / line_height - 1; |
134 lines = std::min(lines, max_lines); | 135 lines = std::min(lines, max_lines); |
135 height = (lines + 1) * line_height; | 136 height = (lines + 1) * line_height; |
136 } | 137 } |
137 | 138 |
138 // Try to ensure that the width is no smaller than the width of the text's | 139 // Try to ensure that the width is no smaller than the width of the text's |
139 // characters to avoid the http://crbug.com/237700 infinite loop. | 140 // characters to avoid the http://crbug.com/237700 infinite loop. |
140 // TODO(dharcourt): Remove when http://crbug.com/237700 is fixed. | 141 // TODO(dharcourt): Remove when http://crbug.com/237700 is fixed. |
141 width = std::max(width, 2 * font().GetStringWidth(UTF8ToUTF16("W"))); | 142 width = std::max(width, 2 * font().GetStringWidth(UTF8ToUTF16("W"))); |
142 | 143 |
143 // Wrap, using INT_MAX for -1 widths that indicate no wrapping. | 144 // Wrap, using INT_MAX for -1 widths that indicate no wrapping. |
144 std::vector<string16> wrapped; | 145 std::vector<base::string16> wrapped; |
145 gfx::ElideRectangleText(text(), font_list(), | 146 gfx::ElideRectangleText(text(), font_list(), |
146 (width < 0) ? std::numeric_limits<int>::max() : width, | 147 (width < 0) ? std::numeric_limits<int>::max() : width, |
147 height, gfx::WRAP_LONG_WORDS, &wrapped); | 148 height, gfx::WRAP_LONG_WORDS, &wrapped); |
148 | 149 |
149 // Elide if necessary. | 150 // Elide if necessary. |
150 if (lines > 0 && wrapped.size() > static_cast<unsigned int>(lines)) { | 151 if (lines > 0 && wrapped.size() > static_cast<unsigned int>(lines)) { |
151 // Add an ellipsis to the last line. If this ellipsis makes the last line | 152 // Add an ellipsis to the last line. If this ellipsis makes the last line |
152 // too wide, that line will be further elided by the gfx::ElideText below, | 153 // too wide, that line will be further elided by the gfx::ElideText below, |
153 // so for example "ABC" could become "ABC..." and then "AB...". | 154 // so for example "ABC" could become "ABC..." and then "AB...". |
154 string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); | 155 base::string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); |
155 if (width > 0 && font().GetStringWidth(last) > width) | 156 if (width > 0 && font().GetStringWidth(last) > width) |
156 last = gfx::ElideText(last, font(), width, gfx::ELIDE_AT_END); | 157 last = gfx::ElideText(last, font(), width, gfx::ELIDE_AT_END); |
157 wrapped.resize(lines - 1); | 158 wrapped.resize(lines - 1); |
158 wrapped.push_back(last); | 159 wrapped.push_back(last); |
159 } | 160 } |
160 | 161 |
161 return wrapped; | 162 return wrapped; |
162 } | 163 } |
163 | 164 |
164 void InnerBoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 165 void InnerBoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 if (size_cache_.size() >= kPreferredLinesCacheSize) { | 251 if (size_cache_.size() >= kPreferredLinesCacheSize) { |
251 size_cache_.erase(size_widths_and_lines_.back()); | 252 size_cache_.erase(size_widths_and_lines_.back()); |
252 size_widths_and_lines_.pop_back(); | 253 size_widths_and_lines_.pop_back(); |
253 } | 254 } |
254 size_cache_[width_and_lines] = size; | 255 size_cache_[width_and_lines] = size; |
255 size_widths_and_lines_.push_front(width_and_lines); | 256 size_widths_and_lines_.push_front(width_and_lines); |
256 } | 257 } |
257 | 258 |
258 // BoundedLabel /////////////////////////////////////////////////////////// | 259 // BoundedLabel /////////////////////////////////////////////////////////// |
259 | 260 |
260 BoundedLabel::BoundedLabel(const string16& text, const gfx::FontList& font_list) | 261 BoundedLabel::BoundedLabel(const base::string16& text, |
| 262 const gfx::FontList& font_list) |
261 : line_limit_(-1) { | 263 : line_limit_(-1) { |
262 label_.reset(new InnerBoundedLabel(*this)); | 264 label_.reset(new InnerBoundedLabel(*this)); |
263 label_->SetFontList(font_list); | 265 label_->SetFontList(font_list); |
264 label_->SetText(text); | 266 label_->SetText(text); |
265 } | 267 } |
266 | 268 |
267 BoundedLabel::BoundedLabel(const string16& text) | 269 BoundedLabel::BoundedLabel(const base::string16& text) |
268 : line_limit_(-1) { | 270 : line_limit_(-1) { |
269 label_.reset(new InnerBoundedLabel(*this)); | 271 label_.reset(new InnerBoundedLabel(*this)); |
270 label_->SetText(text); | 272 label_->SetText(text); |
271 } | 273 } |
272 | 274 |
273 BoundedLabel::~BoundedLabel() { | 275 BoundedLabel::~BoundedLabel() { |
274 } | 276 } |
275 | 277 |
276 void BoundedLabel::SetColors(SkColor textColor, SkColor backgroundColor) { | 278 void BoundedLabel::SetColors(SkColor textColor, SkColor backgroundColor) { |
277 label_->SetEnabledColor(textColor); | 279 label_->SetEnabledColor(textColor); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 | 333 |
332 void BoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 334 void BoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
333 label_->SetBoundsRect(bounds()); | 335 label_->SetBoundsRect(bounds()); |
334 views::View::OnBoundsChanged(previous_bounds); | 336 views::View::OnBoundsChanged(previous_bounds); |
335 } | 337 } |
336 | 338 |
337 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 339 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
338 label_->SetNativeTheme(theme); | 340 label_->SetNativeTheme(theme); |
339 } | 341 } |
340 | 342 |
341 string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { | 343 base::string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { |
342 return JoinString(label_->GetWrappedText(width, lines), '\n'); | 344 return JoinString(label_->GetWrappedText(width, lines), '\n'); |
343 } | 345 } |
344 | 346 |
345 } // namespace message_center | 347 } // namespace message_center |
OLD | NEW |