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

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: fix lint -- missing space Created 8 years, 10 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..4b42a0977303e4a2c1c873268151943d9a074690 100644
--- a/ui/base/text/text_elider.cc
+++ b/ui/base/text/text_elider.cc
@@ -149,12 +149,55 @@ 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;
+
+ std::vector<string16> email_split;
+ base::SplitString(email, '@', &email_split);
+ DCHECK_EQ(email_split.size(), 2U);
+ string16 username = email_split.front();
+ string16 domain = email_split.back();
+
+ const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis);
+
+ // Ignore the @ symbol and add it back at the end.
asvitkine_google 2012/03/01 22:10:56 Nit: Can you reword this better? The second part o
+ 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 available_domain_width =
+ available_pixel_width -
+ font.GetStringWidth(username.substr(0, 1) + kEllipsisUTF16);
+ const int full_username_width = font.GetStringWidth(username);
+ 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 the other half,
+ // let the domain take that extra space.
asvitkine_google 2012/03/01 22:10:56 If the username is short, shouldn't you elide the
gab 2012/03/01 23:03:46 Yes, this is exactly what this does. I reworded th
+ const int desired_domain_width =
+ std::max(available_pixel_width - full_username_width,
+ available_pixel_width / 2);
+ domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE);
+ if (domain.length() <= 1U)
+ return kEllipsisUTF16;
+ }
+
+ const int final_domain_width = font.GetStringWidth(domain);
asvitkine_google 2012/03/01 22:10:56 Nit: How about: available_pixel_width -= font.Get
gab 2012/03/01 23:03:46 I like that :)!
+ if (full_username_width + final_domain_width > available_pixel_width) {
+ username = ElideText(username,
+ font,
+ available_pixel_width - final_domain_width,
+ ELIDE_AT_END);
+ if (username.length() <= 1U)
+ return kEllipsisUTF16;
+ }
+
+ 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 +436,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