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..9bff2a11110c3b28fe931e49747b6a3879f2db8a 100644 |
--- a/ui/base/text/text_elider.cc |
+++ b/ui/base/text/text_elider.cc |
@@ -149,12 +149,56 @@ 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); |
+ |
+ // 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 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 its half, the |
+ // domain will occupy the leftover width. |
+ const int desired_domain_width = |
+ std::max(available_pixel_width - full_username_width, |
+ available_pixel_width / 2); |
Alexei Svitkine (slow)
2012/03/02 20:20:44
I think the "available_pixel_width / 2" is not str
gab
2012/03/02 21:57:37
Actually, if available_pixel_width/2 > available_d
asvitkine_google
2012/03/02 22:08:41
Okay. Please add a comment here explaining this.
|
+ domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE); |
+ if (domain.length() <= 1U) |
+ return kEllipsisUTF16; |
+ } |
+ |
+ // Fit the username in the remaining width. |
+ available_pixel_width -= font.GetStringWidth(domain); |
+ if (full_username_width > available_pixel_width) { |
+ username = ElideText(username, |
Alexei Svitkine (slow)
2012/03/02 20:20:44
I think this can fit on one line now.
gab
2012/03/02 21:57:37
Yay :)!
|
+ font, |
+ available_pixel_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 +437,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, |