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

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

Issue 867003002: Cache gfx::RenderText instances in views::Label. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: do not overuse the memory Created 5 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/label.h" 5 #include "ui/views/controls/label.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/profiler/scoped_tracker.h" 14 #include "base/profiler/scoped_tracker.h"
15 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
18 #include "ui/accessibility/ax_view_state.h" 17 #include "ui/accessibility/ax_view_state.h"
19 #include "ui/gfx/canvas.h" 18 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/color_utils.h" 19 #include "ui/gfx/color_utils.h"
21 #include "ui/gfx/geometry/insets.h" 20 #include "ui/gfx/geometry/insets.h"
22 #include "ui/gfx/text_elider.h" 21 #include "ui/gfx/text_elider.h"
23 #include "ui/gfx/text_utils.h"
24 #include "ui/gfx/utf16_indexing.h"
25 #include "ui/native_theme/native_theme.h" 22 #include "ui/native_theme/native_theme.h"
26 #include "ui/views/background.h"
27
28 namespace {
29
30 const int kCachedSizeLimit = 10;
31 const base::char16 kPasswordReplacementChar = '*';
32
33 } // namespace
34 23
35 namespace views { 24 namespace views {
36 25
37 // static 26 // static
38 const char Label::kViewClassName[] = "Label"; 27 const char Label::kViewClassName[] = "Label";
39 const int Label::kFocusBorderPadding = 1; 28 const int Label::kFocusBorderPadding = 1;
40 29
30 Label::RenderData::RenderData(const gfx::RenderText& render_text)
31 : text(render_text.text()),
32 font_list(render_text.font_list()),
33 shadows(render_text.shadows()),
34 obscured(render_text.obscured()) {}
35
36 Label::RenderData::~RenderData() {}
37
38 scoped_ptr<gfx::RenderText> Label::RenderData::CreateRenderText() {
39 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance());
40 render_text->SetText(text);
41 render_text->SetFontList(font_list);
42 render_text->set_shadows(shadows);
43 render_text->SetObscured(obscured);
44 return render_text.Pass();
45 }
46
41 Label::Label() { 47 Label::Label() {
42 Init(base::string16(), gfx::FontList()); 48 Init(base::string16(), gfx::FontList());
43 } 49 }
44 50
45 Label::Label(const base::string16& text) { 51 Label::Label(const base::string16& text) {
46 Init(text, gfx::FontList()); 52 Init(text, gfx::FontList());
47 } 53 }
48 54
49 Label::Label(const base::string16& text, const gfx::FontList& font_list) { 55 Label::Label(const base::string16& text, const gfx::FontList& font_list) {
50 Init(text, font_list); 56 Init(text, font_list);
51 } 57 }
52 58
53 Label::~Label() { 59 Label::~Label() {
54 } 60 }
55 61
56 void Label::SetFontList(const gfx::FontList& font_list) { 62 void Label::SetFontList(const gfx::FontList& font_list) {
57 is_first_paint_text_ = true; 63 is_first_paint_text_ = true;
58 font_list_ = font_list; 64 if (render_text_)
59 ResetLayoutCache(); 65 render_text_->SetFontList(font_list);
60 PreferredSizeChanged(); 66 else
61 SchedulePaint(); 67 render_data_->font_list = font_list;
68 ResetLayout();
62 } 69 }
63 70
64 void Label::SetText(const base::string16& text) { 71 void Label::SetText(const base::string16& new_text) {
65 if (text != text_) 72 if (new_text == text())
66 SetTextInternal(text); 73 return;
67 }
68
69 void Label::SetTextInternal(const base::string16& text) {
70 is_first_paint_text_ = true; 74 is_first_paint_text_ = true;
71 text_ = text; 75 if (render_text_)
72 76 render_text_->SetText(new_text);
73 if (obscured_) { 77 else
74 size_t obscured_text_length = 78 render_data_->text.assign(new_text);
75 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, text_.length())); 79 ResetLayout();
76 layout_text_.assign(obscured_text_length, kPasswordReplacementChar);
77 } else {
78 layout_text_ = text_;
79 }
80
81 ResetLayoutCache();
82 PreferredSizeChanged();
83 SchedulePaint();
84 } 80 }
85 81
86 void Label::SetAutoColorReadabilityEnabled(bool enabled) { 82 void Label::SetAutoColorReadabilityEnabled(bool enabled) {
83 if (auto_color_readability_ == enabled)
84 return;
87 is_first_paint_text_ = true; 85 is_first_paint_text_ = true;
88 auto_color_readability_ = enabled; 86 auto_color_readability_ = enabled;
89 RecalculateColors(); 87 RecalculateColors();
90 } 88 }
91 89
92 void Label::SetEnabledColor(SkColor color) { 90 void Label::SetEnabledColor(SkColor color) {
91 if (enabled_color_set_ && requested_enabled_color_ == color)
92 return;
93 is_first_paint_text_ = true; 93 is_first_paint_text_ = true;
94 requested_enabled_color_ = color; 94 requested_enabled_color_ = color;
95 enabled_color_set_ = true; 95 enabled_color_set_ = true;
96 RecalculateColors(); 96 RecalculateColors();
97 } 97 }
98 98
99 void Label::SetDisabledColor(SkColor color) { 99 void Label::SetDisabledColor(SkColor color) {
100 if (disabled_color_set_ && requested_disabled_color_ == color)
101 return;
100 is_first_paint_text_ = true; 102 is_first_paint_text_ = true;
101 requested_disabled_color_ = color; 103 requested_disabled_color_ = color;
102 disabled_color_set_ = true; 104 disabled_color_set_ = true;
103 RecalculateColors(); 105 RecalculateColors();
104 } 106 }
105 107
106 void Label::SetBackgroundColor(SkColor color) { 108 void Label::SetBackgroundColor(SkColor color) {
109 if (background_color_set_ && background_color_ == color)
110 return;
107 is_first_paint_text_ = true; 111 is_first_paint_text_ = true;
108 background_color_ = color; 112 background_color_ = color;
109 background_color_set_ = true; 113 background_color_set_ = true;
110 RecalculateColors(); 114 RecalculateColors();
111 cached_draw_params_.text.clear();
112 } 115 }
113 116
114 void Label::SetShadows(const gfx::ShadowValues& shadows) { 117 void Label::SetShadows(const gfx::ShadowValues& shadows) {
118 // TODO(mukai): early exit if the specified shadows are same.
115 is_first_paint_text_ = true; 119 is_first_paint_text_ = true;
116 shadows_ = shadows; 120 if (render_text_)
117 ResetLayoutCache(); 121 render_text_->set_shadows(shadows);
122 else
123 render_data_->shadows = shadows;
124 ResetLayout();
118 } 125 }
119 126
120 void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) { 127 void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) {
128 if (subpixel_rendering_enabled_ == subpixel_rendering_enabled)
129 return;
121 is_first_paint_text_ = true; 130 is_first_paint_text_ = true;
122 subpixel_rendering_enabled_ = subpixel_rendering_enabled; 131 subpixel_rendering_enabled_ = subpixel_rendering_enabled;
132 RecalculateColors();
123 } 133 }
124 134
125 void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { 135 void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
126 is_first_paint_text_ = true;
127 // If the UI layout is right-to-left, flip the alignment direction. 136 // If the UI layout is right-to-left, flip the alignment direction.
128 if (base::i18n::IsRTL() && 137 if (base::i18n::IsRTL() &&
129 (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) { 138 (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) {
130 alignment = (alignment == gfx::ALIGN_LEFT) ? 139 alignment = (alignment == gfx::ALIGN_LEFT) ?
131 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; 140 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
132 } 141 }
133 if (horizontal_alignment_ != alignment) { 142 if (horizontal_alignment_ == alignment)
134 horizontal_alignment_ = alignment; 143 return;
135 SchedulePaint(); 144 is_first_paint_text_ = true;
136 } 145 horizontal_alignment_ = alignment;
137 } 146 ResetLayout();
138
139 gfx::HorizontalAlignment Label::GetHorizontalAlignment() const {
140 if (horizontal_alignment_ != gfx::ALIGN_TO_HEAD)
141 return horizontal_alignment_;
142
143 const base::i18n::TextDirection dir =
144 base::i18n::GetFirstStrongCharacterDirection(layout_text_);
145 return dir == base::i18n::RIGHT_TO_LEFT ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
146 } 147 }
147 148
148 void Label::SetLineHeight(int height) { 149 void Label::SetLineHeight(int height) {
150 if (line_height_ == height)
151 return;
149 is_first_paint_text_ = true; 152 is_first_paint_text_ = true;
150 if (height != line_height_) { 153 line_height_ = height;
151 line_height_ = height; 154 ResetLayout();
152 ResetLayoutCache();
153 PreferredSizeChanged();
154 SchedulePaint();
155 }
156 } 155 }
157 156
158 void Label::SetMultiLine(bool multi_line) { 157 void Label::SetMultiLine(bool multi_line) {
159 is_first_paint_text_ = true;
160 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL || 158 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL ||
161 elide_behavior_ == gfx::NO_ELIDE)); 159 elide_behavior_ == gfx::NO_ELIDE));
162 if (multi_line != multi_line_) { 160 if (multi_line_ == multi_line)
163 multi_line_ = multi_line; 161 return;
164 ResetLayoutCache(); 162 is_first_paint_text_ = true;
165 PreferredSizeChanged(); 163 multi_line_ = multi_line;
166 SchedulePaint(); 164 ResetLayout();
167 }
168 } 165 }
169 166
170 void Label::SetObscured(bool obscured) { 167 void Label::SetObscured(bool obscured) {
168 if (this->obscured() == obscured)
169 return;
171 is_first_paint_text_ = true; 170 is_first_paint_text_ = true;
172 if (obscured != obscured_) { 171 if (render_text_)
173 obscured_ = obscured; 172 render_text_->SetObscured(obscured);
174 SetTextInternal(text_); 173 else
175 } 174 render_data_->obscured = obscured;
175 ResetLayout();
176 } 176 }
177 177
178 void Label::SetAllowCharacterBreak(bool allow_character_break) { 178 void Label::SetAllowCharacterBreak(bool allow_character_break) {
179 if (allow_character_break_ == allow_character_break)
180 return;
179 is_first_paint_text_ = true; 181 is_first_paint_text_ = true;
180 if (allow_character_break != allow_character_break_) { 182 allow_character_break_ = allow_character_break;
181 allow_character_break_ = allow_character_break; 183 ResetLayout();
182 ResetLayoutCache();
183 PreferredSizeChanged();
184 SchedulePaint();
185 }
186 } 184 }
187 185
188 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) { 186 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
189 is_first_paint_text_ = true;
190 DCHECK(!multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL || 187 DCHECK(!multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL ||
191 elide_behavior_ == gfx::NO_ELIDE)); 188 elide_behavior_ == gfx::NO_ELIDE));
192 if (elide_behavior != elide_behavior_) { 189 if (elide_behavior_ == elide_behavior)
193 elide_behavior_ = elide_behavior; 190 return;
194 ResetLayoutCache(); 191 is_first_paint_text_ = true;
195 PreferredSizeChanged(); 192 elide_behavior_ = elide_behavior;
196 SchedulePaint(); 193 ResetLayout();
197 }
198 } 194 }
199 195
200 void Label::SetTooltipText(const base::string16& tooltip_text) { 196 void Label::SetTooltipText(const base::string16& tooltip_text) {
201 DCHECK(handles_tooltips_); 197 DCHECK(handles_tooltips_);
202 tooltip_text_ = tooltip_text; 198 tooltip_text_ = tooltip_text;
203 } 199 }
204 200
205 void Label::SetHandlesTooltips(bool enabled) { 201 void Label::SetHandlesTooltips(bool enabled) {
206 handles_tooltips_ = enabled; 202 handles_tooltips_ = enabled;
207 } 203 }
208 204
209 void Label::SizeToFit(int max_width) { 205 void Label::SizeToFit(int max_width) {
210 DCHECK(multi_line_); 206 DCHECK(multi_line_);
211 207 if (max_width_ == max_width)
212 std::vector<base::string16> lines; 208 return;
213 base::SplitString(layout_text_, '\n', &lines); 209 max_width_ = max_width;
214
215 int label_width = 0;
216 for (std::vector<base::string16>::const_iterator iter = lines.begin();
217 iter != lines.end(); ++iter) {
218 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_));
219 }
220
221 label_width += GetInsets().width();
222
223 if (max_width > 0)
224 label_width = std::min(label_width, max_width);
225
226 SetBounds(x(), y(), label_width, 0);
227 SizeToPreferredSize(); 210 SizeToPreferredSize();
228 } 211 }
229 212
230 const base::string16& Label::GetLayoutTextForTesting() const { 213 const base::string16& Label::GetLayoutTextForTesting() const {
231 return layout_text_; 214 return render_text_ ? render_text_->layout_text() : render_data_->text;
msw 2015/02/05 20:16:41 I don't think that returning render_data_->text is
Jun Mukai 2015/02/19 22:01:17 Done -- Label is reshaped and |render_data_| is go
232 } 215 }
233 216
234 gfx::Insets Label::GetInsets() const { 217 gfx::Insets Label::GetInsets() const {
235 gfx::Insets insets = View::GetInsets(); 218 gfx::Insets insets = View::GetInsets();
236 if (focusable()) { 219 if (focusable()) {
237 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, 220 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding,
238 kFocusBorderPadding, kFocusBorderPadding); 221 kFocusBorderPadding, kFocusBorderPadding);
239 } 222 }
240 return insets; 223 return insets;
241 } 224 }
242 225
243 int Label::GetBaseline() const { 226 int Label::GetBaseline() const {
244 return GetInsets().top() + font_list_.GetBaseline(); 227 return GetInsets().top() + font_list().GetBaseline();
245 } 228 }
246 229
247 gfx::Size Label::GetPreferredSize() const { 230 gfx::Size Label::GetPreferredSize() const {
248 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 231 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
249 tracked_objects::ScopedTracker tracking_profile( 232 tracked_objects::ScopedTracker tracking_profile(
250 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::GetPreferredSize")); 233 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::GetPreferredSize"));
251 234
252 // Return a size of (0, 0) if the label is not visible and if the 235 // Return a size of (0, 0) if the label is not visible and if the
253 // collapse_when_hidden_ flag is set. 236 // collapse_when_hidden_ flag is set.
254 // TODO(munjal): This logic probably belongs to the View class. But for now, 237 // TODO(munjal): This logic probably belongs to the View class. But for now,
255 // put it here since putting it in View class means all inheriting classes 238 // put it here since putting it in View class means all inheriting classes
256 // need ot respect the collapse_when_hidden_ flag. 239 // need ot respect the collapse_when_hidden_ flag.
257 if (!visible() && collapse_when_hidden_) 240 if (!visible() && collapse_when_hidden_)
258 return gfx::Size(); 241 return gfx::Size();
259 242
243 if (multi_line_ && max_width_ != 0 && !text().empty())
244 return gfx::Size(max_width_, GetHeightForWidth(max_width_));
245
260 gfx::Size size(GetTextSize()); 246 gfx::Size size(GetTextSize());
261 gfx::Insets insets = GetInsets(); 247 const gfx::Insets insets = GetInsets();
262 size.Enlarge(insets.width(), insets.height()); 248 size.Enlarge(insets.width(), insets.height());
263 return size; 249 return size;
264 } 250 }
265 251
266 gfx::Size Label::GetMinimumSize() const { 252 gfx::Size Label::GetMinimumSize() const {
267 gfx::Size text_size(GetTextSize()); 253 if (!visible() && collapse_when_hidden_)
268 if ((!visible() && collapse_when_hidden_) || text_size.IsEmpty())
269 return gfx::Size(); 254 return gfx::Size();
270 255
271 gfx::Size size(gfx::GetStringWidth(base::string16(gfx::kEllipsisUTF16), 256 gfx::Size size(0, font_list().GetHeight());
272 font_list_), 257 if (elide_behavior_ == gfx::ELIDE_HEAD ||
273 font_list_.GetHeight()); 258 elide_behavior_ == gfx::ELIDE_MIDDLE ||
274 size.SetToMin(text_size); // The actual text may be shorter than an ellipsis. 259 elide_behavior_ == gfx::ELIDE_TAIL ||
275 gfx::Insets insets = GetInsets(); 260 elide_behavior_ == gfx::ELIDE_EMAIL) {
276 size.Enlarge(insets.width(), insets.height()); 261 size.set_width(gfx::Canvas::GetStringWidth(
262 base::string16(gfx::kEllipsisUTF16), font_list()));
263 }
264 if (!multi_line_)
265 size.SetToMin(GetTextSize());
266 size.Enlarge(GetInsets().width(), GetInsets().height());
277 return size; 267 return size;
278 } 268 }
279 269
280 int Label::GetHeightForWidth(int w) const { 270 int Label::GetHeightForWidth(int w) const {
281 if (!multi_line_) 271 if (!visible() && collapse_when_hidden_)
282 return View::GetHeightForWidth(w); 272 return 0;
283 273
284 w = std::max(0, w - GetInsets().width()); 274 w -= GetInsets().width();
275 if (!multi_line_ || text().empty() || w <= 0 || !render_text_)
276 return std::max(line_height_, font_list().GetHeight());
285 277
286 for (size_t i = 0; i < cached_heights_.size(); ++i) { 278 int height = 0;
287 const gfx::Size& s = cached_heights_[i]; 279 if (gfx::RenderText::MultilineSupported()) {
msw 2015/02/05 20:16:41 It's a bit worrisome to add code for a case that's
Jun Mukai 2015/02/12 21:43:04 Yes, I've tested (and now it's in).
288 if (s.width() == w) 280 gfx::Rect original_rect = render_text_->display_rect();
289 return s.height() + GetInsets().height(); 281 render_text_->SetDisplayRect(gfx::Rect(0, 0, w, 0));
282 height = render_text_->GetStringSize().height();
283 render_text_->SetDisplayRect(original_rect);
284 } else {
285 std::vector<base::string16> lines = GetLinesForWidth(w);
286 height = lines.size() * std::max(line_height_, font_list().GetHeight());
287 }
288 height -= gfx::ShadowValue::GetMargin(render_text_->shadows()).height();
289 return height + GetInsets().height();
290 }
291
292 void Label::Layout() {
293 lines_.clear();
294 gfx::Rect rect = GetContentsBounds();
295 if (rect.width() == 0 || rect.height() == 0 || !render_text_)
msw 2015/02/05 20:16:42 Shouldn't this instead return early if !visible(),
Jun Mukai 2015/02/19 22:01:17 Layout() now gets much simpler, so this early-quit
296 return;
297
298 gfx::HorizontalAlignment alignment = horizontal_alignment_;
299 gfx::DirectionalityMode directionality = render_text_->directionality_mode();
300 if (multi_line_) {
301 // Force the directionality and alignment of the first line on other lines.
302 bool rtl = render_text_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
303 if (alignment == gfx::ALIGN_TO_HEAD)
304 alignment = rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
305 directionality =
306 rtl ? gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR;
290 } 307 }
291 308
292 int cache_width = w; 309 if (!multi_line_ || gfx::RenderText::MultilineSupported()) {
310 render_text_->SetMultiline(multi_line_);
311 render_text_->SetReplaceNewlineCharsWithSymbols(!multi_line_);
312 render_text_->SetHorizontalAlignment(alignment);
313 render_text_->SetDirectionalityMode(directionality);
314 render_text_->SetDisplayRect(rect);
msw 2015/02/05 20:16:41 I designed this patch with the idea of reserving |
Jun Mukai 2015/02/12 21:43:04 I came to realize your point finally, thank you fo
Jun Mukai 2015/02/12 21:43:04 Your concern is right and hits some UI (I didn't n
msw 2015/02/17 22:16:45 I think oshima's change will address my performanc
Jun Mukai 2015/02/19 22:01:17 Now my CL is reformed (which is similar to your or
315 render_text_->SetElideBehavior(elide_behavior_);
316 } else {
317 std::vector<base::string16> lines = GetLinesForWidth(rect.width());
318 if (lines.size() > 1)
319 rect.set_height(std::max(line_height_, font_list().GetHeight()));
293 320
294 int h = font_list_.GetHeight(); 321 const int bottom = GetContentsBounds().bottom();
295 // Flags returned in the cached |DrawStringParams| has a different value 322 for (size_t i = 0; i < lines.size() && rect.y() <= bottom; ++i) {
296 // from the result of |ComputeDrawStringFlags()|. The latter is needed here. 323 scoped_ptr<gfx::RenderText> line(gfx::RenderText::CreateInstance());
297 const int flags = ComputeDrawStringFlags(); 324 line->SetHorizontalAlignment(alignment);
298 gfx::Canvas::SizeStringInt( 325 line->SetDirectionalityMode(directionality);
299 layout_text_, font_list_, &w, &h, line_height_, flags); 326 line->SetElideBehavior(elide_behavior_);
300 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); 327 line->SetFontList(font_list());
301 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; 328 line->set_shadows(shadows());
302 return h + GetInsets().height(); 329 line->SetCursorEnabled(false);
330 line->SetText(lines[i]);
331 line->SetDisplayRect(rect);
332 lines_.push_back(line.release());
333 rect.set_y(rect.y() + rect.height());
334 }
335 for (size_t i = lines_.size(); i < lines.size(); ++i)
336 lines_.back()->SetText(lines_.back()->text() + lines[i]);
337 }
338 RecalculateColors();
303 } 339 }
304 340
305 const char* Label::GetClassName() const { 341 const char* Label::GetClassName() const {
306 return kViewClassName; 342 return kViewClassName;
307 } 343 }
308 344
309 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { 345 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) {
310 if (!handles_tooltips_ || 346 if (!handles_tooltips_ ||
311 (tooltip_text_.empty() && !ShouldShowDefaultTooltip())) 347 (tooltip_text_.empty() && !ShouldShowDefaultTooltip()))
312 return NULL; 348 return NULL;
313 349
314 return HitTestPoint(point) ? this : NULL; 350 return HitTestPoint(point) ? this : NULL;
315 } 351 }
316 352
317 bool Label::CanProcessEventsWithinSubtree() const { 353 bool Label::CanProcessEventsWithinSubtree() const {
318 // Send events to the parent view for handling. 354 // Send events to the parent view for handling.
319 return false; 355 return false;
320 } 356 }
321 357
322 void Label::GetAccessibleState(ui::AXViewState* state) { 358 void Label::GetAccessibleState(ui::AXViewState* state) {
323 state->role = ui::AX_ROLE_STATIC_TEXT; 359 state->role = ui::AX_ROLE_STATIC_TEXT;
324 state->AddStateFlag(ui::AX_STATE_READ_ONLY); 360 state->AddStateFlag(ui::AX_STATE_READ_ONLY);
325 state->name = layout_text_; 361 state->name = render_text_->layout_text();
326 } 362 }
327 363
328 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { 364 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const {
329 if (!handles_tooltips_) 365 if (!handles_tooltips_)
330 return false; 366 return false;
331 367
332 if (!tooltip_text_.empty()) { 368 if (!tooltip_text_.empty()) {
333 tooltip->assign(tooltip_text_); 369 tooltip->assign(tooltip_text_);
334 return true; 370 return true;
335 } 371 }
336 372
337 if (ShouldShowDefaultTooltip()) { 373 if (ShouldShowDefaultTooltip()) {
338 *tooltip = layout_text_; 374 tooltip->assign(render_text_->layout_text());
339 return true; 375 return true;
340 } 376 }
341 377
342 return false; 378 return false;
343 } 379 }
344 380
345 void Label::PaintText(gfx::Canvas* canvas, 381 void Label::OnEnabledChanged() {
346 const base::string16& text, 382 RecalculateColors();
347 const gfx::Rect& text_bounds, 383 }
348 int flags) { 384
349 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_; 385 void Label::PaintText(gfx::Canvas* canvas) {
350 if (elide_behavior_ == gfx::FADE_TAIL && 386 if (!multi_line_ || gfx::RenderText::MultilineSupported()) {
351 text_bounds.width() < GetTextSize().width()) { 387 render_text_->Draw(canvas);
352 canvas->DrawFadedString(text, font_list_, color, text_bounds, flags);
353 } else { 388 } else {
354 canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds, 389 for (size_t i = 0; i < lines_.size(); ++i)
355 line_height_, flags, shadows_); 390 lines_[i]->Draw(canvas);
356 }
357
358 if (HasFocus()) {
359 gfx::Rect focus_bounds = text_bounds;
360 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
361 canvas->DrawFocusRect(focus_bounds);
362 } 391 }
363 } 392 }
364 393
365 gfx::Size Label::GetTextSize() const {
366 if (!text_size_valid_) {
367 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
368 tracked_objects::ScopedTracker tracking_profile1(
369 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::GetTextSize1"));
370
371 // For single-line strings, we supply the largest possible width, because
372 // while adding NO_ELLIPSIS to the flags works on Windows for forcing
373 // SizeStringInt() to calculate the desired width, it doesn't seem to work
374 // on Linux.
375 int w = multi_line_ ?
376 GetAvailableRect().width() : std::numeric_limits<int>::max();
377 int h = font_list_.GetHeight();
378 // For single-line strings, ignore the available width and calculate how
379 // wide the text wants to be.
380 // Call |ComputeDrawStringFlags()| instead of |CalculateDrawStringParams()|
381 // here since the latter calls this function and causes infinite recursion.
382 int flags = ComputeDrawStringFlags();
383 if (!multi_line_)
384 flags |= gfx::Canvas::NO_ELLIPSIS;
385 {
386 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
387 // fixed.
388 tracked_objects::ScopedTracker tracking_profile2(
389 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::GetTextSize2"));
390
391 gfx::Canvas::SizeStringInt(layout_text_, font_list_, &w, &h, line_height_,
392 flags);
393 }
394 text_size_.SetSize(w, h);
395 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows_);
396 text_size_.Enlarge(shadow_margin.width(), shadow_margin.height());
397 text_size_valid_ = true;
398 }
399
400 return text_size_;
401 }
402
403 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { 394 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) {
404 text_size_valid_ &= !multi_line_; 395 if (previous_bounds.size() != size())
405 cached_draw_params_.text.clear(); 396 InvalidateLayout();
406 } 397 }
407 398
408 void Label::OnPaint(gfx::Canvas* canvas) { 399 void Label::OnPaint(gfx::Canvas* canvas) {
409 OnPaintBackground(canvas); 400 View::OnPaint(canvas);
410 // We skip painting the focus border because it is being handled seperately by
411 // some subclasses of Label. We do not want View's focus border painting to
412 // interfere with that.
413 OnPaintBorder(canvas);
414 if (layout_text_.empty())
415 return;
416
417 const DrawStringParams* params = CalculateDrawStringParams();
418 if (is_first_paint_text_) { 401 if (is_first_paint_text_) {
419 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 402 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
420 tracked_objects::ScopedTracker tracking_profile( 403 tracked_objects::ScopedTracker tracking_profile(
421 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText first")); 404 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText first"));
422 405
423 is_first_paint_text_ = false; 406 is_first_paint_text_ = false;
424 PaintText(canvas, params->text, params->bounds, params->flags); 407 PaintText(canvas);
425 } else { 408 } else {
426 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 409 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
427 tracked_objects::ScopedTracker tracking_profile( 410 tracked_objects::ScopedTracker tracking_profile(
428 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText not first")); 411 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText not first"));
429 412
430 PaintText(canvas, params->text, params->bounds, params->flags); 413 PaintText(canvas);
414 }
415 if (HasFocus()) {
416 gfx::Rect focus_bounds = GetLocalBounds();
417 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
418 canvas->DrawFocusRect(focus_bounds);
431 } 419 }
432 } 420 }
433 421
434 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { 422 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) {
435 UpdateColorsFromTheme(theme); 423 UpdateColorsFromTheme(theme);
436 } 424 }
437 425
438 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) { 426 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) {
439 View::OnDeviceScaleFactorChanged(device_scale_factor); 427 View::OnDeviceScaleFactorChanged(device_scale_factor);
440 // When the device scale factor is changed, some font rendering parameters is 428 // When the device scale factor is changed, some font rendering parameters is
441 // changed (especially, hinting). The bounding box of the text has to be 429 // changed (especially, hinting). The bounding box of the text has to be
442 // re-computed based on the new parameters. See crbug.com/441439 430 // re-computed based on the new parameters. See crbug.com/441439
443 ResetLayoutCache(); 431 ResetLayout();
432 }
433
434 void Label::VisibilityChanged(View* starting_from, bool is_visible) {
435 if (is_visible) {
436 if (!render_text_) {
437 render_text_ = render_data_->CreateRenderText();
msw 2015/02/05 20:16:41 Flipping visibility at different states of initial
Jun Mukai 2015/02/12 21:43:04 Test added.
438 render_data_.reset();
439 }
440 } else {
441 if (render_text_) {
442 render_data_.reset(new RenderData(*render_text_));
443 render_text_.reset();
444 lines_.clear();
445 }
446 }
447 }
448
449 void Label::Init(const base::string16& text, const gfx::FontList& font_list) {
450 render_text_.reset(gfx::RenderText::CreateInstance());
451 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
452 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT);
453 render_text_->SetElideBehavior(gfx::NO_ELIDE);
454 render_text_->SetFontList(font_list);
455 render_text_->SetCursorEnabled(false);
456
457 elide_behavior_ = gfx::ELIDE_TAIL;
458 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false;
459 subpixel_rendering_enabled_ = true;
460 auto_color_readability_ = true;
461 UpdateColorsFromTheme(ui::NativeTheme::instance());
462 collapse_when_hidden_ = false;
463 allow_character_break_ = false;
464 multi_line_ = false;
465 line_height_ = 0;
466 max_width_ = 0;
467 is_first_paint_text_ = true;
468 SetText(text);
469 }
470
471 void Label::ResetLayout() {
472 InvalidateLayout();
444 PreferredSizeChanged(); 473 PreferredSizeChanged();
445 SchedulePaint(); 474 SchedulePaint();
446 } 475 }
447 476
448 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { 477 std::vector<base::string16> Label::GetLinesForWidth(int width) const {
449 font_list_ = font_list; 478 std::vector<base::string16> lines;
450 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; 479 const gfx::WordWrapBehavior wrap =
451 subpixel_rendering_enabled_ = true; 480 allow_character_break_ ? gfx::WRAP_LONG_WORDS : gfx::TRUNCATE_LONG_WORDS;
452 auto_color_readability_ = true; 481 gfx::ElideRectangleText(render_text_->layout_text(),
453 UpdateColorsFromTheme(ui::NativeTheme::instance()); 482 font_list(),
454 horizontal_alignment_ = gfx::ALIGN_CENTER; 483 width,
455 line_height_ = 0; 484 std::numeric_limits<int>::max(),
456 multi_line_ = false; 485 wrap,
457 obscured_ = false; 486 &lines);
458 allow_character_break_ = false; 487 return lines;
459 elide_behavior_ = gfx::ELIDE_TAIL; 488 }
460 handles_tooltips_ = true;
461 collapse_when_hidden_ = false;
462 cached_heights_.resize(kCachedSizeLimit);
463 ResetLayoutCache();
464 is_first_paint_text_ = true;
465 489
466 SetText(text); 490 gfx::Size Label::GetTextSize() const {
491 gfx::Size size;
492 if (text().empty() || !render_text_) {
493 size = gfx::Size(0, std::max(line_height_, font_list().GetHeight()));
494 } else if (!multi_line_ || gfx::RenderText::MultilineSupported()) {
495 size = render_text_->GetStringSize();
496 } else {
497 // Get the natural text size, unelided and only wrapped on newlines.
498 std::vector<base::string16> lines;
499 base::SplitString(render_text_->layout_text(), '\n', &lines);
500 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance());
501 render_text->SetFontList(font_list());
502 for (size_t i = 0; i < lines.size(); ++i) {
503 render_text->SetText(lines[i]);
504 const gfx::Size line = render_text->GetStringSize();
505 size.set_width(std::max(size.width(), line.width()));
506 size.set_height(size.height() + std::max(line_height_, line.height()));
507 }
508 }
509 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows());
510 size.Enlarge(shadow_margin.width(), shadow_margin.height());
511 return size;
467 } 512 }
468 513
469 void Label::RecalculateColors() { 514 void Label::RecalculateColors() {
515 if (!render_text_)
516 return;
470 actual_enabled_color_ = auto_color_readability_ ? 517 actual_enabled_color_ = auto_color_readability_ ?
471 color_utils::GetReadableColor(requested_enabled_color_, 518 color_utils::GetReadableColor(requested_enabled_color_,
472 background_color_) : 519 background_color_) :
473 requested_enabled_color_; 520 requested_enabled_color_;
474 actual_disabled_color_ = auto_color_readability_ ? 521 actual_disabled_color_ = auto_color_readability_ ?
475 color_utils::GetReadableColor(requested_disabled_color_, 522 color_utils::GetReadableColor(requested_disabled_color_,
476 background_color_) : 523 background_color_) :
477 requested_disabled_color_; 524 requested_disabled_color_;
478 }
479 525
480 gfx::Rect Label::GetTextBounds() const { 526 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_;
481 gfx::Rect available(GetAvailableRect()); 527 bool background_is_transparent =
482 gfx::Size text_size(GetTextSize()); 528 SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_;
483 text_size.set_width(std::min(available.width(), text_size.width())); 529 if (!multi_line_ || gfx::RenderText::MultilineSupported()) {
484 gfx::Point origin(GetInsets().left(), GetInsets().top()); 530 render_text_->SetColor(color);
485 switch (GetHorizontalAlignment()) { 531 render_text_->set_background_is_transparent(background_is_transparent);
486 case gfx::ALIGN_LEFT: 532 } else {
487 break; 533 for (size_t i = 0; i < lines_.size(); ++i) {
488 case gfx::ALIGN_CENTER: 534 lines_[i]->SetColor(color);
489 // Put any extra margin pixel on the left to match the legacy behavior 535 lines_[i]->set_background_is_transparent(background_is_transparent);
490 // from the use of GetTextExtentPoint32() on Windows. 536 }
491 origin.Offset((available.width() + 1 - text_size.width()) / 2, 0);
492 break;
493 case gfx::ALIGN_RIGHT:
494 origin.set_x(available.right() - text_size.width());
495 break;
496 default:
497 NOTREACHED();
498 break;
499 } 537 }
500 if (!multi_line_) 538 SchedulePaint();
501 text_size.set_height(available.height());
502 // Support vertical centering of multi-line labels: http://crbug.com/429595
503 origin.Offset(0, std::max(0, (available.height() - text_size.height())) / 2);
504 return gfx::Rect(origin, text_size);
505 }
506
507 int Label::ComputeDrawStringFlags() const {
508 int flags = 0;
509
510 // We can't use subpixel rendering if the background is non-opaque.
511 if (SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_)
512 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
513
514 base::i18n::TextDirection direction =
515 base::i18n::GetFirstStrongCharacterDirection(layout_text_);
516 if (direction == base::i18n::RIGHT_TO_LEFT)
517 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
518 else
519 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
520
521 switch (GetHorizontalAlignment()) {
522 case gfx::ALIGN_LEFT:
523 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
524 break;
525 case gfx::ALIGN_CENTER:
526 flags |= gfx::Canvas::TEXT_ALIGN_CENTER;
527 break;
528 case gfx::ALIGN_RIGHT:
529 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
530 break;
531 default:
532 NOTREACHED();
533 break;
534 }
535
536 if (!multi_line_)
537 return flags;
538
539 flags |= gfx::Canvas::MULTI_LINE;
540 #if !defined(OS_WIN)
541 // Don't elide multiline labels on Linux.
542 // Todo(davemoore): Do we depend on eliding multiline text?
543 // Pango insists on limiting the number of lines to one if text is
544 // elided. You can get around this if you can pass a maximum height
545 // but we don't currently have that data when we call the pango code.
546 flags |= gfx::Canvas::NO_ELLIPSIS;
547 #endif
548 if (allow_character_break_)
549 flags |= gfx::Canvas::CHARACTER_BREAK;
550
551 return flags;
552 }
553
554 gfx::Rect Label::GetAvailableRect() const {
555 gfx::Rect bounds(size());
556 bounds.Inset(GetInsets());
557 return bounds;
558 }
559
560 const Label::DrawStringParams* Label::CalculateDrawStringParams() const {
561 if (cached_draw_params_.text.empty()) {
562 const bool forbid_ellipsis = elide_behavior_ == gfx::NO_ELIDE ||
563 elide_behavior_ == gfx::FADE_TAIL;
564 if (multi_line_ || forbid_ellipsis) {
565 cached_draw_params_.text = layout_text_;
566 } else {
567 cached_draw_params_.text = gfx::ElideText(layout_text_, font_list_,
568 GetAvailableRect().width(), elide_behavior_);
569 }
570
571 cached_draw_params_.bounds = GetTextBounds();
572 cached_draw_params_.flags = ComputeDrawStringFlags();
573 // TODO(msw): Elide multi-line text with ElideRectangleText instead.
574 if (!multi_line_ || forbid_ellipsis)
575 cached_draw_params_.flags |= gfx::Canvas::NO_ELLIPSIS;
576 }
577
578 return &cached_draw_params_;
579 } 539 }
580 540
581 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { 541 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
582 if (!enabled_color_set_) { 542 if (!enabled_color_set_) {
583 requested_enabled_color_ = theme->GetSystemColor( 543 requested_enabled_color_ = theme->GetSystemColor(
584 ui::NativeTheme::kColorId_LabelEnabledColor); 544 ui::NativeTheme::kColorId_LabelEnabledColor);
585 } 545 }
586 if (!disabled_color_set_) { 546 if (!disabled_color_set_) {
587 requested_disabled_color_ = theme->GetSystemColor( 547 requested_disabled_color_ = theme->GetSystemColor(
588 ui::NativeTheme::kColorId_LabelDisabledColor); 548 ui::NativeTheme::kColorId_LabelDisabledColor);
589 } 549 }
590 if (!background_color_set_) { 550 if (!background_color_set_) {
591 background_color_ = theme->GetSystemColor( 551 background_color_ = theme->GetSystemColor(
592 ui::NativeTheme::kColorId_LabelBackgroundColor); 552 ui::NativeTheme::kColorId_LabelBackgroundColor);
593 } 553 }
594 RecalculateColors(); 554 RecalculateColors();
595 } 555 }
596 556
597 void Label::ResetLayoutCache() {
598 cached_draw_params_.text.clear();
599 text_size_valid_ = false;
600 cached_heights_cursor_ = 0;
601 for (int i = 0; i < kCachedSizeLimit; ++i)
602 cached_heights_[i] = gfx::Size();
603 }
604
605 bool Label::ShouldShowDefaultTooltip() const { 557 bool Label::ShouldShowDefaultTooltip() const {
606 const gfx::Size text_size = GetTextSize(); 558 const gfx::Size text_size = GetTextSize();
607 const gfx::Size size = GetContentsBounds().size(); 559 const gfx::Size size = GetContentsBounds().size();
608 return !obscured() && (text_size.width() > size.width() || 560 return !obscured() && (text_size.width() > size.width() ||
609 (multi_line_ && text_size.height() > size.height())); 561 (multi_line_ && text_size.height() > size.height()));
610 } 562 }
611 563
612 } // namespace views 564 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698