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

Unified 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 chars in last test 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 side-by-side diff with in-line comments
Download patch
Index: ui/base/text/text_elider.cc
diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc
index 525cea0df61eb373507b91372f144dbf327cf419..62ad7d062b68cd937715e3098ceae2cdfe1fd56c 100644
--- a/ui/base/text/text_elider.cc
+++ b/ui/base/text/text_elider.cc
@@ -149,12 +149,66 @@ string16 ElideComponentizedPath(const string16& url_path_prefix,
} // namespace
-// This function takes a GURL object and elides it. It returns a string
-// which composed of parts from subdomain, domain, path, filename and query.
-// A "..." is added automatically at the end if the elided string is bigger
-// than the available pixel width. For available pixel width = 0, a formatted,
-// but un-elided, string is returned.
-//
+string16 ElideEmail(const string16& email,
+ const gfx::Font& font,
+ int available_pixel_width) {
+ if (font.GetStringWidth(email) <= available_pixel_width)
+ return email;
+
+ // Split the email into its local-part (username) and domain-part. The email
+ // spec technically allows for @ symbols in the local-part (username) of the
+ // email under some special requirements. It is guaranteed that there is no @
+ // symbol in the domain part of the email however so splitting at the last @
+ // symbol is safe.
+ const size_t split_index = email.find_last_of('@');
+ DCHECK_NE(split_index, string16::npos);
+ string16 username = email.substr(0, split_index);
+ string16 domain = email.substr(split_index + 1);
+ DCHECK(!username.empty());
+ DCHECK(!domain.empty());
+
+ const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
+
+ // Subtract the @ symbol from the available width as it is mandatory.
+ const string16 kAtSignUTF16 = ASCIIToUTF16("@");
+ available_pixel_width -= font.GetStringWidth(kAtSignUTF16);
+
+ // Check whether eliding the domain is necessary: if eliding the username
+ // is sufficient, the domain will not be elided.
+ const int full_username_width = font.GetStringWidth(username);
+ const int available_domain_width =
+ available_pixel_width -
+ std::min(full_username_width,
+ font.GetStringWidth(username.substr(0, 1) + kEllipsisUTF16));
+ if (font.GetStringWidth(domain) > available_domain_width) {
+ // Elide the domain so that it only takes half of the available width.
+ // Should the username not need all the width available in its half, the
+ // domain will occupy the leftover width.
+ // If |desired_domain_width| is greater than |available_domain_width|: the
+ // minimal username elision allowed by the specifications will not fit; thus
+ // |desired_domain_width| must be <= |available_domain_width| at all cost.
+ const int desired_domain_width =
+ std::min(available_domain_width,
+ std::max(available_pixel_width - full_username_width,
+ available_pixel_width / 2));
+ domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE);
+ // Failing to elide the domain such that at least one character remains
+ // (other than the ellipsis itself) remains: return a single ellipsis.
+ if (domain.length() <= 1U)
+ return kEllipsisUTF16;
+ }
+
+ // Fit the username in the remaining width (at this point the elided username
+ // is guaranteed to fit with at least one character remaining given all the
+ // precautions taken earlier).
+ username = ElideText(username,
+ font,
+ available_pixel_width - font.GetStringWidth(domain),
+ ELIDE_AT_END);
+
+ return username + kAtSignUTF16 + domain;
+}
+
// TODO(pkasting): http://crbug.com/77883 This whole function gets
// kerning/ligatures/etc. issues potentially wrong by assuming that the width of
// a rendered string is always the sum of the widths of its substrings. Also I
@@ -393,8 +447,6 @@ string16 ElideFilename(const FilePath& filename,
return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
}
-// This function adds an ellipsis at the end of the text if the text
-// does not fit the given pixel width.
string16 ElideText(const string16& text,
const gfx::Font& font,
int available_pixel_width,

Powered by Google App Engine
This is Rietveld 408576698