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

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: silly chrome OS..more Ls. 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
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 // Split the email into its local-part (username) and domain-part. The email
159 // spec technically allows for @ symbols in the local-part (username) of the
160 // email under some special requirements. It is guaranteed that there is no @
161 // symbol in the domain part of the email however so splitting at the last @
162 // symbol is safe.
163 const size_t split_index = email.find_last_of('@');
164 DCHECK_NE(split_index, string16::npos);
165 string16 username = email.substr(0, split_index);
166 string16 domain = email.substr(split_index + 1);
167 DCHECK(!username.empty());
168 DCHECK(!domain.empty());
169
170 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
171
172 // Subtract the @ symbol from the available width as it is mandatory.
173 const string16 kAtSignUTF16 = ASCIIToUTF16("@");
174 available_pixel_width -= font.GetStringWidth(kAtSignUTF16);
175
176 // Check whether eliding the domain is necessary: if eliding the username
177 // is sufficient, the domain will not be elided.
178 const int full_username_width = font.GetStringWidth(username);
179 const int available_domain_width =
180 available_pixel_width -
181 std::min(full_username_width,
182 font.GetStringWidth(username.substr(0, 1) + kEllipsisUTF16));
183 if (font.GetStringWidth(domain) > available_domain_width) {
184 // Elide the domain so that it only takes half of the available width.
185 // Should the username not need all the width available in its half, the
186 // domain will occupy the leftover width.
187 // If |desired_domain_width| is greater than |available_domain_width|: the
188 // minimal username elision allowed by the specifications will not fit; thus
189 // |desired_domain_width| must be <= |available_domain_width| at all cost.
190 const int desired_domain_width =
191 std::min(available_domain_width,
192 std::max(available_pixel_width - full_username_width,
193 available_pixel_width / 2));
194 domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE);
195 // Failing to elide the domain such that at least one character remains
196 // (other than the ellipsis itself) remains: return a single ellipsis.
197 if (domain.length() <= 1U)
198 return kEllipsisUTF16;
199 }
200
201 // Fit the username in the remaining width (at this point the elided username
202 // is guaranteed to fit with at least one character remaining given all the
203 // precautions taken earlier).
204 username = ElideText(username,
205 font,
206 available_pixel_width - font.GetStringWidth(domain),
207 ELIDE_AT_END);
208
209 return username + kAtSignUTF16 + domain;
210 }
211
158 // TODO(pkasting): http://crbug.com/77883 This whole function gets 212 // TODO(pkasting): http://crbug.com/77883 This whole function gets
159 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of 213 // 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 214 // a rendered string is always the sum of the widths of its substrings. Also I
161 // suspect it could be made simpler. 215 // suspect it could be made simpler.
162 string16 ElideUrl(const GURL& url, 216 string16 ElideUrl(const GURL& url,
163 const gfx::Font& font, 217 const gfx::Font& font,
164 int available_pixel_width, 218 int available_pixel_width,
165 const std::string& languages) { 219 const std::string& languages) {
166 // Get a formatted string and corresponding parsing of the url. 220 // Get a formatted string and corresponding parsing of the url.
167 url_parse::Parsed parsed; 221 url_parse::Parsed parsed;
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 440 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
387 } 441 }
388 442
389 int available_root_width = available_pixel_width - ext_width; 443 int available_root_width = available_pixel_width - ext_width;
390 string16 elided_name = 444 string16 elided_name =
391 ElideText(rootname, font, available_root_width, ELIDE_AT_END); 445 ElideText(rootname, font, available_root_width, ELIDE_AT_END);
392 elided_name += extension; 446 elided_name += extension;
393 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 447 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
394 } 448 }
395 449
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, 450 string16 ElideText(const string16& text,
399 const gfx::Font& font, 451 const gfx::Font& font,
400 int available_pixel_width, 452 int available_pixel_width,
401 ElideBehavior elide_behavior) { 453 ElideBehavior elide_behavior) {
402 if (text.empty()) 454 if (text.empty())
403 return text; 455 return text;
404 456
405 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); 457 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
406 458
407 const int current_text_pixel_width = font.GetStringWidth(text); 459 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(); 1113 index = char_iterator.getIndex();
1062 } else { 1114 } else {
1063 // String has leading whitespace, return the elide string. 1115 // String has leading whitespace, return the elide string.
1064 return kElideString; 1116 return kElideString;
1065 } 1117 }
1066 } 1118 }
1067 return string.substr(0, index) + kElideString; 1119 return string.substr(0, index) + kElideString;
1068 } 1120 }
1069 1121
1070 } // namespace ui 1122 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698