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

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: rev2 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 string16 ElideEmail(const string16& email, size_t max_email_length) {
155 DCHECK_GE(max_email_length, 5U);
156
157 if (email.length() <= max_email_length)
158 return email;
159
160 std::vector<string16> email_split;
161 base::SplitString(email, '@', &email_split);
162 DCHECK_EQ(email_split.size(), 2U);
163 string16 username = email_split.front();
164 string16 domain = email_split.back();
165
166 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
167 const size_t kEllipsisLength = kEllipsisUTF16.length();
168
169 // Reserve characters for each of: username ellipsis, @ sign, and at least
170 // one character remaining in the username.
171 const size_t available_domain_chars = max_email_length - kEllipsisLength - 2;
172 if (domain.length() > available_domain_chars) {
173 StringSlicer slicer(domain, kEllipsisUTF16, true);
174 const size_t length = max_email_length / 2 - kEllipsisLength;
175 domain = slicer.CutString(length, true);
176 }
177
178 const size_t final_domain_length = domain.length();
179 if (username.length() + final_domain_length + 1 > max_email_length) {
180 StringSlicer slicer(username, kEllipsisUTF16, false);
181 const size_t length = max_email_length - final_domain_length -
182 kEllipsisLength - 1;
183 username = slicer.CutString(length, true);
184 }
185
186 return username + ASCIIToUTF16("@") + domain;
187 }
188
152 // This function takes a GURL object and elides it. It returns a string 189 // 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. 190 // 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 191 // 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, 192 // than the available pixel width. For available pixel width = 0, a formatted,
156 // but un-elided, string is returned. 193 // but un-elided, string is returned.
157 // 194 //
158 // TODO(pkasting): http://crbug.com/77883 This whole function gets 195 // TODO(pkasting): http://crbug.com/77883 This whole function gets
159 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of 196 // 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 197 // a rendered string is always the sum of the widths of its substrings. Also I
161 // suspect it could be made simpler. 198 // suspect it could be made simpler.
(...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 index = char_iterator.getIndex(); 1098 index = char_iterator.getIndex();
1062 } else { 1099 } else {
1063 // String has leading whitespace, return the elide string. 1100 // String has leading whitespace, return the elide string.
1064 return kElideString; 1101 return kElideString;
1065 } 1102 }
1066 } 1103 }
1067 return string.substr(0, index) + kElideString; 1104 return string.substr(0, index) + kElideString;
1068 } 1105 }
1069 1106
1070 } // namespace ui 1107 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698