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

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: fixed lint 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 17 matching lines...) Expand all
28 #include "ui/gfx/font.h" 28 #include "ui/gfx/font.h"
29 #include "unicode/rbbi.h" 29 #include "unicode/rbbi.h"
30 #include "unicode/uloc.h" 30 #include "unicode/uloc.h"
31 31
32 namespace ui { 32 namespace ui {
33 33
34 // U+2026 in utf8 34 // U+2026 in utf8
35 const char kEllipsis[] = "\xE2\x80\xA6"; 35 const char kEllipsis[] = "\xE2\x80\xA6";
36 const char16 kForwardSlash = '/'; 36 const char16 kForwardSlash = '/';
37 37
38 const size_t kMaxProfileUsernameLength = 20;
39
38 namespace { 40 namespace {
39 41
40 // Helper class to split + elide text, while respecting UTF16 surrogate pairs. 42 // Helper class to split + elide text, while respecting UTF16 surrogate pairs.
41 class StringSlicer { 43 class StringSlicer {
42 public: 44 public:
43 StringSlicer(const string16& text, 45 StringSlicer(const string16& text,
44 const string16& ellipsis, 46 const string16& ellipsis,
45 bool elide_in_middle) 47 bool elide_in_middle)
46 : text_(text), 48 : text_(text),
47 ellipsis_(ellipsis), 49 ellipsis_(ellipsis),
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 if (available_pixel_width >= font.GetStringWidth(elided_path)) 144 if (available_pixel_width >= font.GetStringWidth(elided_path))
143 return ElideText(elided_path + url_query, 145 return ElideText(elided_path + url_query,
144 font, available_pixel_width, ELIDE_AT_END); 146 font, available_pixel_width, ELIDE_AT_END);
145 } 147 }
146 148
147 return string16(); 149 return string16();
148 } 150 }
149 151
150 } // namespace 152 } // namespace
151 153
154 // Elides only the username portion of the email, if possible, so that the
155 // overall email is at most max_email_length characters (e.g.
156 // longusername@host.com --> longuser...@host.com for max_email_length == 20).
157 // If the domain name by itself already has more characters than available,
158 // we elide the domain name in the middle so that only max_email_length / 2
159 // characters remain. We then proceed with the regular eliding described above.
160 // In any scenario: the string returned will never be longer than
161 // max_email_length including the inserted ellipses; and the elided strings
162 // will have at least one character remaining on either side of the '@'(other
163 // than the ellipsis-es).
164 // This function assumes max_email_length is >= 5.
Alexei Svitkine (slow) 2012/02/28 15:46:22 It is sufficient to only include the comment in th
gab 2012/02/28 19:20:11 Ok, was mimicking ElideUrl which has the comment i
165 string16 ElideEmail(const string16& email, size_t max_email_length) {
166 DCHECK_GE(max_email_length, size_t(5));
167
168 if (email.length() <= max_email_length)
169 return email;
170
171 std::vector<string16> email_split;
172 base::SplitString(email, '@', &email_split);
173 DCHECK_EQ(email_split.size(), size_t(2));
Alexei Svitkine (slow) 2012/02/28 15:46:22 We don't use constructor-style casts in the Chromi
174 string16& username = email_split.front();
175 string16& domain = email_split.back();
Alexei Svitkine (slow) 2012/02/28 15:46:22 Non-const references are discouraged - remove the
gab 2012/02/28 19:20:11 Ok, but this will force an implicit copy of the st
176
177 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
178 const size_t kEllipsisLength = kEllipsisUTF16.length();
179
180 // Reserve characters for each of: username ellipsis, @ sign, and at least
181 // one character remaining in the username.
182 const size_t kAvailableDomainChars = max_email_length - kEllipsisLength - 2;
Alexei Svitkine (slow) 2012/02/28 15:46:22 Rename to |available_domain_chars| since this is a
183 if (domain.length() > kAvailableDomainChars) {
184 StringSlicer slicer(domain, kEllipsisUTF16, true);
185 const size_t n = max_email_length / 2 - kEllipsisLength;
Alexei Svitkine (slow) 2012/02/28 15:46:22 Nit: Rename |n| to |length|. Same thing in the blo
186 domain = slicer.CutString(n, true);
187 }
188
189 const size_t kFinalDomainLength = domain.length();
Alexei Svitkine (slow) 2012/02/28 15:46:22 Same comment as for kAvailableDomainChars above. A
gab 2012/02/28 19:20:11 |domain.length()| is shorter, but is a function (h
190 if (username.length() + kFinalDomainLength + 1 > max_email_length) {
191 StringSlicer slicer(username, kEllipsisUTF16, false);
192 const size_t n = max_email_length - kFinalDomainLength -
193 kEllipsisLength - 1;
194 username = slicer.CutString(n, true);
195 }
196
197 return username + ASCIIToUTF16("@") + domain;
198 }
199
152 // This function takes a GURL object and elides it. It returns a string 200 // This function takes a GURL object and elides it. It returns a string
153 // which composed of parts from subdomain, domain, path, filename and query. 201 // which composed of parts from subdomain, domain, path, filename and query.
154 // A "..." is added automatically at the end if the elided string is bigger 202 // A "..." is added automatically at the end if the elided string is bigger
155 // than the available pixel width. For available pixel width = 0, a formatted, 203 // than the available pixel width. For available pixel width = 0, a formatted,
156 // but un-elided, string is returned. 204 // but un-elided, string is returned.
157 // 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.
(...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 index = char_iterator.getIndex(); 1109 index = char_iterator.getIndex();
1062 } else { 1110 } else {
1063 // String has leading whitespace, return the elide string. 1111 // String has leading whitespace, return the elide string.
1064 return kElideString; 1112 return kElideString;
1065 } 1113 }
1066 } 1114 }
1067 return string.substr(0, index) + kElideString; 1115 return string.substr(0, index) + kElideString;
1068 } 1116 }
1069 1117
1070 } // namespace ui 1118 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698