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..07b74fa60af61afba9b43b07143c6fc588f888a8 100644 |
--- a/ui/base/text/text_elider.cc |
+++ b/ui/base/text/text_elider.cc |
@@ -149,12 +149,60 @@ 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); |
sky
2012/03/06 16:56:16
Are you sure this is right? The spec seems to indi
gab
2012/03/06 22:06:21
I spent a while today looking through code search
|
+ 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 full_username_width = font.GetStringWidth(username); |
+ const int available_domain_width = |
+ available_pixel_width - |
+ std::min(full_username_width, |
gab
2012/03/02 23:37:21
I just realized that if the full username is small
|
+ 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)); |
gab
2012/03/02 23:37:21
@asvitkine re-thinking about it: you were right :)
|
+ 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 +441,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, |