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

Unified Diff: ui/views/controls/styled_label.cc

Issue 2348433002: Make styled-label trimming less agressive, allowing whitespace (Closed)
Patch Set: Refactoring: de-duplicated wrap to new line code in styled label credits to estade for code suggest… 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/styled_label.cc
diff --git a/ui/views/controls/styled_label.cc b/ui/views/controls/styled_label.cc
index 1b4cf1c93aaa038342e0949d9ce75ff1a78478d3..5ad539ff653b6e47973bb0ab61d4f88139be799a 100644
--- a/ui/views/controls/styled_label.cc
+++ b/ui/views/controls/styled_label.cc
@@ -256,15 +256,24 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
base::string16 remaining_string = text_;
StyleRanges::const_iterator current_range = style_ranges_.begin();
+ bool first_loop_iteration = true;
+
// Iterate over the text, creating a bunch of labels and links and laying them
// out in the appropriate positions.
while (!remaining_string.empty()) {
- // Don't put whitespace at beginning of a line with an exception for the
- // first line (so the text's leading whitespace is respected).
- if (x == 0 && line > 0) {
- base::TrimWhitespace(remaining_string, base::TRIM_LEADING,
- &remaining_string);
+ if (x == 0 && !first_loop_iteration) {
+ if (remaining_string.front() == L'\n') {
+ // Wrapped to the next line on \n, remove it. Other whitespace,
+ // eg, spaces to indent next line, are preserved.
+ remaining_string.erase(0, 1);
+ } else {
+ // Wrapped on whitespace character or characters in the middle of the
+ // line - none of them are needed at the beginning of the next line.
+ base::TrimWhitespace(remaining_string, base::TRIM_LEADING,
+ &remaining_string);
+ }
}
+ first_loop_iteration = false;
gfx::Range range(gfx::Range::InvalidRange());
if (current_range != style_ranges_.end())
@@ -290,22 +299,18 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
gfx::WRAP_LONG_WORDS,
&substrings);
- if (substrings.empty() || substrings[0].empty()) {
+ if (substrings.empty()) {
+ // there is no room for anything; abort.
+ break;
+ }
+ if (substrings[0].empty()) {
+ x = 0;
// Nothing fits on this line. Start a new line.
- // If x is 0, first line may have leading whitespace that doesn't fit in a
- // single line, so try trimming those. Otherwise there is no room for
- // anything; abort.
- if (x == 0) {
- if (line == 0) {
- base::TrimWhitespace(remaining_string, base::TRIM_LEADING,
- &remaining_string);
- continue;
- }
- break;
+ // As for the first line, don't advance line number so that it will be
Evan Stade 2016/12/15 00:02:36 nit: instead of "it will be handled again" perhaps
+ // handled again at the beginning of the loop.
+ if (line > 0) {
+ ++line;
}
-
- x = 0;
- line++;
continue;
}
@@ -320,7 +325,7 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
// If the chunk should not be wrapped, try to fit it entirely on the
// next line.
x = 0;
- line++;
+ ++line;
continue;
}
« 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