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

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

Issue 1140083002: Make StyledLabel wrap long words (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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 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 <limits> 7 #include <limits>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // style may differ from the base font. The font specified by the range 274 // style may differ from the base font. The font specified by the range
275 // should be used when eliding text. 275 // should be used when eliding text.
276 if (position >= range.start()) { 276 if (position >= range.start()) {
277 text_font_list = text_font_list.DeriveWithStyle( 277 text_font_list = text_font_list.DeriveWithStyle(
278 current_range->style_info.font_style); 278 current_range->style_info.font_style);
279 } 279 }
280 gfx::ElideRectangleText(remaining_string, 280 gfx::ElideRectangleText(remaining_string,
281 text_font_list, 281 text_font_list,
282 chunk_bounds.width(), 282 chunk_bounds.width(),
283 chunk_bounds.height(), 283 chunk_bounds.height(),
284 gfx::IGNORE_LONG_WORDS, 284 gfx::TRUNCATE_LONG_WORDS,
msw 2015/05/13 23:05:39 Why doesn't this use WRAP_LONG_WORDS?
tbarzic 2015/05/14 00:02:28 WRAP_LONG_WORDS didn't work in first patch because
285 &substrings); 285 &substrings);
286 286
287 DCHECK(!substrings.empty()); 287 base::string16 chunk =
288 base::string16 chunk = substrings[0]; 288 substrings.empty() ? base::string16() : substrings[0];
289 if (chunk.empty()) { 289 if (chunk.empty()) {
msw 2015/05/13 23:05:39 nit: declare/assign |chunk| below this block and c
tbarzic 2015/05/14 00:02:28 Done.
290 // Nothing fits on this line. Start a new line. 290 // Nothing fits on this line. Start a new line.
291 // If x is 0, first line may have leading whitespace that doesn't fit in a 291 // If x is 0, first line may have leading whitespace that doesn't fit in a
292 // single line, so try trimming those. Otherwise there is no room for 292 // single line, so try trimming those. Otherwise there is no room for
293 // anything; abort. 293 // anything; abort.
294 if (x == 0) { 294 if (x == 0) {
295 if (line == 0) { 295 if (line == 0) {
296 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, 296 base::TrimWhitespace(remaining_string, base::TRIM_LEADING,
297 &remaining_string); 297 &remaining_string);
298 continue; 298 continue;
299 } 299 }
300 break; 300 break;
301 } 301 }
302 302
303 x = 0; 303 x = 0;
304 line++; 304 line++;
305 continue; 305 continue;
306 } 306 }
307 307
308 scoped_ptr<Label> label; 308 scoped_ptr<Label> label;
309 bool whole_chunk_used = true;
309 if (position >= range.start()) { 310 if (position >= range.start()) {
310 const RangeStyleInfo& style_info = current_range->style_info; 311 const RangeStyleInfo& style_info = current_range->style_info;
311 312
312 if (style_info.disable_line_wrapping && chunk.size() < range.length() && 313 if (style_info.disable_line_wrapping && chunk.size() < range.length() &&
313 position == range.start() && x != 0) { 314 position == range.start() && x != 0) {
314 // If the chunk should not be wrapped, try to fit it entirely on the 315 // If the chunk should not be wrapped, try to fit it entirely on the
315 // next line. 316 // next line.
316 x = 0; 317 x = 0;
317 line++; 318 line++;
318 continue; 319 continue;
319 } 320 }
320 321
321 chunk = chunk.substr(0, std::min(chunk.size(), range.end() - position)); 322 if (chunk.size() > range.end() - position) {
323 whole_chunk_used = false;
324 chunk = chunk.substr(0, range.end() - position);
325 }
322 326
323 label = CreateLabelRange(chunk, font_list_, style_info, this); 327 label = CreateLabelRange(chunk, font_list_, style_info, this);
324 328
325 if (style_info.is_link && !dry_run) 329 if (style_info.is_link && !dry_run)
326 link_targets_[label.get()] = range; 330 link_targets_[label.get()] = range;
327 331
328 if (position + chunk.size() >= range.end()) 332 if (position + chunk.size() >= range.end())
329 ++current_range; 333 ++current_range;
330 } else { 334 } else {
331 // This chunk is normal text. 335 // This chunk is normal text.
332 if (position + chunk.size() > range.start()) 336 if (position + chunk.size() > range.start()) {
337 whole_chunk_used = false;
333 chunk = chunk.substr(0, range.start() - position); 338 chunk = chunk.substr(0, range.start() - position);
339 }
334 label = CreateLabelRange(chunk, font_list_, default_style_info_, this); 340 label = CreateLabelRange(chunk, font_list_, default_style_info_, this);
335 } 341 }
336 342
337 if (displayed_on_background_color_set_) 343 if (displayed_on_background_color_set_)
338 label->SetBackgroundColor(displayed_on_background_color_); 344 label->SetBackgroundColor(displayed_on_background_color_);
339 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); 345 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_);
340 346
341 // Calculate the size of the optional focus border, and overlap by that 347 // Calculate the size of the optional focus border, and overlap by that
342 // amount. Otherwise, "<a>link</a>," will render as "link ,". 348 // amount. Otherwise, "<a>link</a>," will render as "link ,".
343 gfx::Insets focus_border_insets(label->GetInsets()); 349 gfx::Insets focus_border_insets(label->GetInsets());
344 focus_border_insets += -label->View::GetInsets(); 350 focus_border_insets += -label->View::GetInsets();
345 const gfx::Size view_size = label->GetPreferredSize(); 351 const gfx::Size view_size = label->GetPreferredSize();
346 if (!dry_run) { 352 if (!dry_run) {
347 label->SetBoundsRect(gfx::Rect( 353 label->SetBoundsRect(gfx::Rect(
348 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), 354 gfx::Point(GetInsets().left() + x - focus_border_insets.left(),
349 GetInsets().top() + line * line_height - 355 GetInsets().top() + line * line_height -
350 focus_border_insets.top()), 356 focus_border_insets.top()),
351 view_size)); 357 view_size));
352 AddChildView(label.release()); 358 AddChildView(label.release());
353 } 359 }
354 x += view_size.width() - focus_border_insets.width(); 360 x += view_size.width() - focus_border_insets.width();
355 used_width = std::max(used_width, x); 361 used_width = std::max(used_width, x);
356 362
363 if (substrings.size() > 1 && whole_chunk_used) {
msw 2015/05/13 23:05:39 I don't think you need |whole_chunk_used|, can you
tbarzic 2015/05/14 00:02:28 Done.
364 x = 0;
365 ++line;
366 }
367
357 remaining_string = remaining_string.substr(chunk.size()); 368 remaining_string = remaining_string.substr(chunk.size());
358 } 369 }
359 370
360 DCHECK_LE(used_width, width); 371 DCHECK_LE(used_width, width);
361 // The user-specified line height only applies to interline spacing, so the 372 // The user-specified line height only applies to interline spacing, so the
362 // final line's height is unaffected. 373 // final line's height is unaffected.
363 int total_height = line * line_height + 374 int total_height = line * line_height +
364 CalculateLineHeight(font_list_) + GetInsets().height(); 375 CalculateLineHeight(font_list_) + GetInsets().height();
365 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height); 376 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height);
366 return calculated_size_; 377 return calculated_size_;
367 } 378 }
368 379
369 } // namespace views 380 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/controls/styled_label_unittest.cc » ('j') | ui/views/controls/styled_label_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698