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..917cefda2a574ac378af26b01f1ce5e912b116db 100644 |
--- a/ui/base/text/text_elider.cc |
+++ b/ui/base/text/text_elider.cc |
@@ -149,12 +149,53 @@ 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(); |
gab
2012/03/01 19:37:25
@asvitkine: I remember you said you prefer string1
asvitkine_google
2012/03/01 20:13:29
This is fine. The run time will be dominated by ca
|
+ |
+ const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); |
+ const int kEllipsisSize = font.GetStringWidth(kEllipsisUTF16); |
asvitkine_google
2012/03/01 20:13:29
Please rename all the variables that end in Size o
|
+ |
+ // Ignore the @ symbol and add it back at the end. |
+ const string16 kAtSignUTF16 = ASCIIToUTF16("@"); |
+ available_pixel_width -= font.GetStringWidth(kAtSignUTF16); |
+ |
+ // Reserve characters for each of: username ellipsis, @ symbol, and at least |
+ // one character remaining in the username. |
asvitkine_google
2012/03/01 20:13:29
I would comment this slightly differently - have t
|
+ const int available_domain_size = available_pixel_width - |
+ kEllipsisSize - |
+ font.GetStringWidth(username.substr(0, 1)); |
asvitkine_google
2012/03/01 20:13:29
Please use font.GetStringWidth(username.substr(0,
|
+ const int full_username_size = font.GetStringWidth(username); |
+ if (font.GetStringWidth(domain) > available_domain_size) { |
+ const int desired_domain_size = |
+ std::max(available_pixel_width - full_username_size, |
asvitkine_google
2012/03/01 20:13:29
Please comment the logic behind this.
gab
2012/03/01 21:48:18
I was trying to be less verbose by leaving all the
|
+ available_pixel_width / 2); |
+ domain = ElideText(domain, font, desired_domain_size, ELIDE_IN_MIDDLE); |
+ if (domain.length() <= 1U) |
+ return kEllipsisUTF16; |
+ } |
+ |
+ const int final_domain_size = font.GetStringWidth(domain); |
+ if (full_username_size + final_domain_size > available_pixel_width) { |
+ username = ElideText(username, |
+ font, |
+ available_pixel_width - final_domain_size, |
+ 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 +434,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, |