OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file defines utility functions for eliding and formatting UI text. | 5 // This file defines utility functions for eliding and formatting UI text. |
6 | 6 |
7 #ifndef UI_BASE_TEXT_TEXT_ELIDER_H_ | 7 #ifndef UI_BASE_TEXT_TEXT_ELIDER_H_ |
8 #define UI_BASE_TEXT_TEXT_ELIDER_H_ | 8 #define UI_BASE_TEXT_TEXT_ELIDER_H_ |
9 #pragma once | 9 #pragma once |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/string16.h" | 15 #include "base/string16.h" |
16 #include "ui/base/ui_export.h" | 16 #include "ui/base/ui_export.h" |
17 #include "ui/gfx/font.h" | 17 #include "ui/gfx/font.h" |
18 #include "unicode/coll.h" | 18 #include "unicode/coll.h" |
19 #include "unicode/uchar.h" | 19 #include "unicode/uchar.h" |
20 | 20 |
21 class FilePath; | 21 class FilePath; |
22 class GURL; | 22 class GURL; |
23 | 23 |
24 namespace ui { | 24 namespace ui { |
25 | 25 |
26 UI_EXPORT extern const char kEllipsis[]; | 26 UI_EXPORT extern const char kEllipsis[]; |
27 | 27 |
28 // Elides only the username portion of the |email|, if possible, so that the | |
29 // returned email is at most |available_pixel_width| wide under |font|. | |
asvitkine_google
2012/03/01 20:13:29
I would suggest having the first sentence describe
| |
30 // If the domain name by itself is already wider than the width available: it is | |
31 // elided in the middle so that it splits the available width equally with | |
32 // the elided username (should the username be short enough that it doesn't | |
33 // require half the available width: the domain will take that extra width). | |
34 // This function guarantees that the string returned will contain at least one | |
35 // character, other than the ellipsis-es, on either side of the '@'. If it is | |
36 // impossible to achieve these requirements: only an ellipsis will be returned. | |
37 UI_EXPORT string16 ElideEmail(const string16& email, | |
38 const gfx::Font& font, | |
39 int available_pixel_width); | |
40 | |
28 // This function takes a GURL object and elides it. It returns a string | 41 // This function takes a GURL object and elides it. It returns a string |
29 // which composed of parts from subdomain, domain, path, filename and query. | 42 // which composed of parts from subdomain, domain, path, filename and query. |
30 // A "..." is added automatically at the end if the elided string is bigger | 43 // A "..." is added automatically at the end if the elided string is bigger |
31 // than the available pixel width. For available pixel width = 0, empty | 44 // than the |available_pixel_width|. For |available_pixel_width| == 0, a |
32 // string is returned. |languages| is a comma separated list of ISO 639 | 45 // formatted, but un-elided, string is returned. |languages| is a comma |
33 // language codes and is used to determine what characters are understood | 46 // separated list of ISO 639 language codes and is used to determine what |
34 // by a user. It should come from |prefs::kAcceptLanguages|. | 47 // characters are understood by a user. It should come from |
48 // |prefs::kAcceptLanguages|. | |
35 // | 49 // |
36 // Note: in RTL locales, if the URL returned by this function is going to be | 50 // Note: in RTL locales, if the URL returned by this function is going to be |
37 // displayed in the UI, then it is likely that the string needs to be marked | 51 // displayed in the UI, then it is likely that the string needs to be marked |
38 // as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it | 52 // as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it |
39 // is displayed properly in an RTL context. Please refer to | 53 // is displayed properly in an RTL context. Please refer to |
40 // http://crbug.com/6487 for more information. | 54 // http://crbug.com/6487 for more information. |
41 UI_EXPORT string16 ElideUrl(const GURL& url, | 55 UI_EXPORT string16 ElideUrl(const GURL& url, |
42 const gfx::Font& font, | 56 const gfx::Font& font, |
43 int available_pixel_width, | 57 int available_pixel_width, |
44 const std::string& languages); | 58 const std::string& languages); |
45 | 59 |
46 enum ElideBehavior { | 60 enum ElideBehavior { |
47 // Add ellipsis at the end of the string. | 61 // Add ellipsis at the end of the string. |
48 ELIDE_AT_END, | 62 ELIDE_AT_END, |
49 // Add ellipsis in the middle of the string. | 63 // Add ellipsis in the middle of the string. |
50 ELIDE_IN_MIDDLE, | 64 ELIDE_IN_MIDDLE, |
51 // Truncate the end of the string. | 65 // Truncate the end of the string. |
52 TRUNCATE_AT_END | 66 TRUNCATE_AT_END |
53 }; | 67 }; |
54 | 68 |
55 // Elides |text| to fit in |available_pixel_width| according to the specified | 69 // This function, if the |text| does not fit the |available_pixel_width|, cuts |
56 // |elide_behavior|. | 70 // |text| and adds an ellipsis where specified by |elide_behavior|. |
asvitkine_google
2012/03/01 20:13:29
I like the previous wording better. If you want, y
gab
2012/03/01 21:48:18
Right, one of the versions of the code I tried yes
| |
71 // If we must elide, but the ellipsis itself doesn't even fit in | |
72 // |available_pixel_width|, this returns an empty string. | |
57 UI_EXPORT string16 ElideText(const string16& text, | 73 UI_EXPORT string16 ElideText(const string16& text, |
58 const gfx::Font& font, | 74 const gfx::Font& font, |
59 int available_pixel_width, | 75 int available_pixel_width, |
60 ElideBehavior elide_behavior); | 76 ElideBehavior elide_behavior); |
61 | 77 |
62 // Elide a filename to fit a given pixel width, with an emphasis on not hiding | 78 // Elide a filename to fit a given pixel width, with an emphasis on not hiding |
63 // the extension unless we have to. If filename contains a path, the path will | 79 // the extension unless we have to. If filename contains a path, the path will |
64 // be removed if filename doesn't fit into available_pixel_width. The elided | 80 // be removed if filename doesn't fit into available_pixel_width. The elided |
65 // filename is forced to have LTR directionality, which means that in RTL UI | 81 // filename is forced to have LTR directionality, which means that in RTL UI |
66 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and | 82 // the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 // Truncates the string to length characters. This breaks the string at | 185 // Truncates the string to length characters. This breaks the string at |
170 // the first word break before length, adding the horizontal ellipsis | 186 // the first word break before length, adding the horizontal ellipsis |
171 // character (unicode character 0x2026) to render ... | 187 // character (unicode character 0x2026) to render ... |
172 // The supplied string is returned if the string has length characters or | 188 // The supplied string is returned if the string has length characters or |
173 // less. | 189 // less. |
174 UI_EXPORT string16 TruncateString(const string16& string, size_t length); | 190 UI_EXPORT string16 TruncateString(const string16& string, size_t length); |
175 | 191 |
176 } // namespace ui | 192 } // namespace ui |
177 | 193 |
178 #endif // UI_BASE_TEXT_TEXT_ELIDER_H_ | 194 #endif // UI_BASE_TEXT_TEXT_ELIDER_H_ |
OLD | NEW |