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

Side by Side Diff: ui/base/text/text_elider.cc

Issue 9489011: Elide long emails in the wrench and profile menus. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: more robust Created 8 years, 9 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
« no previous file with comments | « ui/base/text/text_elider.h ('k') | ui/base/text/text_elider_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // This file implements utility functions for eliding and formatting UI text. 5 // This file implements utility functions for eliding and formatting UI text.
6 // 6 //
7 // Note that several of the functions declared in text_elider.h are implemented 7 // Note that several of the functions declared in text_elider.h are implemented
8 // in this file using helper classes in the anonymous namespace. 8 // in this file using helper classes in the anonymous namespace.
9 9
10 #include "ui/base/text/text_elider.h" 10 #include "ui/base/text/text_elider.h"
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 if (available_pixel_width >= font.GetStringWidth(elided_path)) 142 if (available_pixel_width >= font.GetStringWidth(elided_path))
143 return ElideText(elided_path + url_query, 143 return ElideText(elided_path + url_query,
144 font, available_pixel_width, ELIDE_AT_END); 144 font, available_pixel_width, ELIDE_AT_END);
145 } 145 }
146 146
147 return string16(); 147 return string16();
148 } 148 }
149 149
150 } // namespace 150 } // namespace
151 151
152 // This function takes a GURL object and elides it. It returns a string 152 string16 ElideEmail(const string16& email,
153 // which composed of parts from subdomain, domain, path, filename and query. 153 const gfx::Font& font,
154 // A "..." is added automatically at the end if the elided string is bigger 154 int available_pixel_width) {
155 // than the available pixel width. For available pixel width = 0, a formatted, 155 if (font.GetStringWidth(email) <= available_pixel_width)
156 // but un-elided, string is returned. 156 return email;
157 // 157
158 std::vector<string16> email_split;
159 base::SplitString(email, '@', &email_split);
sky 2012/03/06 16:56:16 Are you sure this is right? The spec seems to indi
gab 2012/03/06 22:06:21 I spent a while today looking through code search
160 DCHECK_EQ(email_split.size(), 2U);
161 string16 username = email_split.front();
162 string16 domain = email_split.back();
163
164 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
165
166 // Subtract the @ symbol from the available width as it is mandatory.
167 const string16 kAtSignUTF16 = ASCIIToUTF16("@");
168 available_pixel_width -= font.GetStringWidth(kAtSignUTF16);
169
170 // Check whether eliding the domain is necessary: if eliding the username
171 // is sufficient, the domain will not be elided.
172 const int full_username_width = font.GetStringWidth(username);
173 const int available_domain_width =
174 available_pixel_width -
175 std::min(full_username_width,
gab 2012/03/02 23:37:21 I just realized that if the full username is small
176 font.GetStringWidth(username.substr(0, 1) + kEllipsisUTF16));
177 if (font.GetStringWidth(domain) > available_domain_width) {
178 // Elide the domain so that it only takes half of the available width.
179 // Should the username not need all the width available in its half, the
180 // domain will occupy the leftover width.
181 // If |desired_domain_width| is greater than |available_domain_width|: the
182 // minimal username elision allowed by the specifications will not fit; thus
183 // |desired_domain_width| must be <= |available_domain_width| at all cost.
184 const int desired_domain_width =
185 std::min(available_domain_width,
186 std::max(available_pixel_width - full_username_width,
187 available_pixel_width / 2));
gab 2012/03/02 23:37:21 @asvitkine re-thinking about it: you were right :)
188 domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE);
189 // Failing to elide the domain such that at least one character remains
190 // (other than the ellipsis itself) remains: return a single ellipsis.
191 if (domain.length() <= 1U)
192 return kEllipsisUTF16;
193 }
194
195 // Fit the username in the remaining width (at this point the elided username
196 // is guaranteed to fit with at least one character remaining given all the
197 // precautions taken earlier).
198 username = ElideText(username,
199 font,
200 available_pixel_width - font.GetStringWidth(domain),
201 ELIDE_AT_END);
202
203 return username + kAtSignUTF16 + domain;
204 }
205
158 // TODO(pkasting): http://crbug.com/77883 This whole function gets 206 // TODO(pkasting): http://crbug.com/77883 This whole function gets
159 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of 207 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of
160 // a rendered string is always the sum of the widths of its substrings. Also I 208 // a rendered string is always the sum of the widths of its substrings. Also I
161 // suspect it could be made simpler. 209 // suspect it could be made simpler.
162 string16 ElideUrl(const GURL& url, 210 string16 ElideUrl(const GURL& url,
163 const gfx::Font& font, 211 const gfx::Font& font,
164 int available_pixel_width, 212 int available_pixel_width,
165 const std::string& languages) { 213 const std::string& languages) {
166 // Get a formatted string and corresponding parsing of the url. 214 // Get a formatted string and corresponding parsing of the url.
167 url_parse::Parsed parsed; 215 url_parse::Parsed parsed;
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 434 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
387 } 435 }
388 436
389 int available_root_width = available_pixel_width - ext_width; 437 int available_root_width = available_pixel_width - ext_width;
390 string16 elided_name = 438 string16 elided_name =
391 ElideText(rootname, font, available_root_width, ELIDE_AT_END); 439 ElideText(rootname, font, available_root_width, ELIDE_AT_END);
392 elided_name += extension; 440 elided_name += extension;
393 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 441 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
394 } 442 }
395 443
396 // This function adds an ellipsis at the end of the text if the text
397 // does not fit the given pixel width.
398 string16 ElideText(const string16& text, 444 string16 ElideText(const string16& text,
399 const gfx::Font& font, 445 const gfx::Font& font,
400 int available_pixel_width, 446 int available_pixel_width,
401 ElideBehavior elide_behavior) { 447 ElideBehavior elide_behavior) {
402 if (text.empty()) 448 if (text.empty())
403 return text; 449 return text;
404 450
405 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); 451 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
406 452
407 const int current_text_pixel_width = font.GetStringWidth(text); 453 const int current_text_pixel_width = font.GetStringWidth(text);
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 index = char_iterator.getIndex(); 1107 index = char_iterator.getIndex();
1062 } else { 1108 } else {
1063 // String has leading whitespace, return the elide string. 1109 // String has leading whitespace, return the elide string.
1064 return kElideString; 1110 return kElideString;
1065 } 1111 }
1066 } 1112 }
1067 return string.substr(0, index) + kElideString; 1113 return string.substr(0, index) + kElideString;
1068 } 1114 }
1069 1115
1070 } // namespace ui 1116 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/text/text_elider.h ('k') | ui/base/text/text_elider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698