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

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

Issue 2348433002: Make styled-label trimming less agressive, allowing whitespace (Closed)
Patch Set: Code style fixes - part 2 Created 4 years 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <limits> 9 #include <limits>
10 #include <vector> 10 #include <vector>
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 int total_height = 0; 252 int total_height = 0;
253 // The width that was actually used. Guaranteed to be no larger than |width|. 253 // The width that was actually used. Guaranteed to be no larger than |width|.
254 int used_width = 0; 254 int used_width = 0;
255 255
256 base::string16 remaining_string = text_; 256 base::string16 remaining_string = text_;
257 StyleRanges::const_iterator current_range = style_ranges_.begin(); 257 StyleRanges::const_iterator current_range = style_ranges_.begin();
258 258
259 // Iterate over the text, creating a bunch of labels and links and laying them 259 // Iterate over the text, creating a bunch of labels and links and laying them
260 // out in the appropriate positions. 260 // out in the appropriate positions.
261 while (!remaining_string.empty()) { 261 while (!remaining_string.empty()) {
262 // Don't put whitespace at beginning of a line with an exception for the
263 // first line (so the text's leading whitespace is respected).
264 if (x == 0 && line > 0) { 262 if (x == 0 && line > 0) {
265 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, 263 if (remaining_string.front() == L'\n') {
266 &remaining_string); 264 // Wrapped to the next line on \n, remove it. Other whitespace,
265 // eg, spaces to indent next line, are preserved.
266 remaining_string.erase(0, 1);
267 } else {
268 // Wrapped on whitespace character or characters in the middle of the
269 // line - none of them are needed at the beginning of the next line.
270 base::TrimWhitespace(remaining_string, base::TRIM_LEADING,
271 &remaining_string);
272 }
267 } 273 }
268 274
269 gfx::Range range(gfx::Range::InvalidRange()); 275 gfx::Range range(gfx::Range::InvalidRange());
270 if (current_range != style_ranges_.end()) 276 if (current_range != style_ranges_.end())
271 range = current_range->range; 277 range = current_range->range;
272 278
273 const size_t position = text_.size() - remaining_string.size(); 279 const size_t position = text_.size() - remaining_string.size();
274 280
275 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height); 281 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height);
276 std::vector<base::string16> substrings; 282 std::vector<base::string16> substrings;
277 gfx::FontList text_font_list = font_list_; 283 gfx::FontList text_font_list = font_list_;
278 // If the start of the remaining text is inside a styled range, the font 284 // If the start of the remaining text is inside a styled range, the font
279 // style may differ from the base font. The font specified by the range 285 // style may differ from the base font. The font specified by the range
280 // should be used when eliding text. 286 // should be used when eliding text.
281 if (position >= range.start()) { 287 if (position >= range.start()) {
282 text_font_list = 288 text_font_list =
283 text_font_list.Derive(0, current_range->style_info.font_style, 289 text_font_list.Derive(0, current_range->style_info.font_style,
284 current_range->style_info.weight); 290 current_range->style_info.weight);
285 } 291 }
286 gfx::ElideRectangleText(remaining_string, 292 gfx::ElideRectangleText(remaining_string,
287 text_font_list, 293 text_font_list,
288 chunk_bounds.width(), 294 chunk_bounds.width(),
289 chunk_bounds.height(), 295 chunk_bounds.height(),
290 gfx::WRAP_LONG_WORDS, 296 gfx::WRAP_LONG_WORDS,
291 &substrings); 297 &substrings);
292 298
293 if (substrings.empty() || substrings[0].empty()) { 299 if (substrings.empty() || substrings[0].empty()) {
294 // Nothing fits on this line. Start a new line. 300 // Nothing fits on this line. Start a new line.
295 // If x is 0, first line may have leading whitespace that doesn't fit in a 301 // If x is 0, first line may have leading whitespace that doesn't fit in a
296 // single line, so try trimming those. Otherwise there is no room for 302 // single line, so try trimming those. When first remaining char is \n,
297 // anything; abort. 303 // otput it as an empty line.
304 // Otherwise there is no room for anything; abort.
Evan Stade 2016/12/09 18:41:58 When I rewrite this portion of the function (L299-
298 if (x == 0) { 305 if (x == 0) {
299 if (line == 0) { 306 if (line == 0) {
300 base::TrimWhitespace(remaining_string, base::TRIM_LEADING, 307 base::TrimWhitespace(remaining_string, base::TRIM_LEADING,
301 &remaining_string); 308 &remaining_string);
302 continue; 309 continue;
310 } else if (remaining_string.empty() ||
311 remaining_string.front() != L'\n') {
312 break;
303 } 313 }
304 break;
305 } 314 }
306 315
307 x = 0; 316 x = 0;
308 line++; 317 ++line;
309 continue; 318 continue;
310 } 319 }
311 320
312 base::string16 chunk = substrings[0]; 321 base::string16 chunk = substrings[0];
313 322
314 std::unique_ptr<Label> label; 323 std::unique_ptr<Label> label;
315 if (position >= range.start()) { 324 if (position >= range.start()) {
316 const RangeStyleInfo& style_info = current_range->style_info; 325 const RangeStyleInfo& style_info = current_range->style_info;
317 326
318 if (style_info.disable_line_wrapping && chunk.size() < range.length() && 327 if (style_info.disable_line_wrapping && chunk.size() < range.length() &&
319 position == range.start() && x != 0) { 328 position == range.start() && x != 0) {
320 // If the chunk should not be wrapped, try to fit it entirely on the 329 // If the chunk should not be wrapped, try to fit it entirely on the
321 // next line. 330 // next line.
322 x = 0; 331 x = 0;
323 line++; 332 ++line;
324 continue; 333 continue;
325 } 334 }
326 335
327 if (chunk.size() > range.end() - position) 336 if (chunk.size() > range.end() - position)
328 chunk = chunk.substr(0, range.end() - position); 337 chunk = chunk.substr(0, range.end() - position);
329 338
330 label = CreateLabelRange(chunk, font_list_, style_info, this); 339 label = CreateLabelRange(chunk, font_list_, style_info, this);
331 340
332 if (style_info.is_link && !dry_run) 341 if (style_info.is_link && !dry_run)
333 link_targets_[label.get()] = range; 342 link_targets_[label.get()] = range;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 382
374 remaining_string = remaining_string.substr(chunk.size()); 383 remaining_string = remaining_string.substr(chunk.size());
375 } 384 }
376 385
377 DCHECK_LE(used_width, width); 386 DCHECK_LE(used_width, width);
378 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height); 387 calculated_size_ = gfx::Size(used_width + GetInsets().width(), total_height);
379 return calculated_size_; 388 return calculated_size_;
380 } 389 }
381 390
382 } // namespace views 391 } // namespace views
OLDNEW
« no previous file with comments | « components/autofill/core/browser/legal_message_line.h ('k') | ui/views/controls/styled_label_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698