diff options
author | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-06 21:27:37 +0000 |
---|---|---|
committer | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-06 21:27:37 +0000 |
commit | 0528372f5fc79ee479957fcbf10cac1f3429642a (patch) | |
tree | 566316ddae4764b6666b586ee4421c9fe93386a0 /chrome/common | |
parent | 3cb7115ddd010b44cb35ffe4491eca2362a3759a (diff) | |
download | chromium_src-0528372f5fc79ee479957fcbf10cac1f3429642a.zip chromium_src-0528372f5fc79ee479957fcbf10cac1f3429642a.tar.gz chromium_src-0528372f5fc79ee479957fcbf10cac1f3429642a.tar.bz2 |
Add a function for eliding filenames to a set pixel width while preserving the display of the extension. This touches a lot of files because it renames url_elider to text_elider (as it already contained a bunch of text ellision functions), and so requires header file reference changes. BUG=1208
Review URL: http://codereview.chromium.org/9613
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4912 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/common.scons | 2 | ||||
-rw-r--r-- | chrome/common/common.vcproj | 4 | ||||
-rw-r--r-- | chrome/common/gfx/text_elider.cc (renamed from chrome/common/gfx/url_elider.cc) | 31 | ||||
-rw-r--r-- | chrome/common/gfx/text_elider.h (renamed from chrome/common/gfx/url_elider.h) | 13 | ||||
-rw-r--r-- | chrome/common/gfx/text_elider_unittest.cc (renamed from chrome/common/gfx/url_elider_unittest.cc) | 46 |
5 files changed, 82 insertions, 14 deletions
diff --git a/chrome/common/common.scons b/chrome/common/common.scons index 07a3519..d0c5466 100644 --- a/chrome/common/common.scons +++ b/chrome/common/common.scons @@ -84,7 +84,7 @@ if env['PLATFORM'] == 'win32': 'gfx/emf.cc', 'gfx/icon_util.cc', 'gfx/path.cc', - 'gfx/url_elider.cc', + 'gfx/text_elider.cc', 'ipc_channel.cc', 'ipc_channel_proxy.cc', 'ipc_logging.cc', diff --git a/chrome/common/common.vcproj b/chrome/common/common.vcproj index 5359cfbd..65706a8 100644 --- a/chrome/common/common.vcproj +++ b/chrome/common/common.vcproj @@ -205,11 +205,11 @@ > </File> <File - RelativePath=".\gfx\url_elider.cc" + RelativePath=".\gfx\text_elider.cc" > </File> <File - RelativePath=".\gfx\url_elider.h" + RelativePath=".\gfx\text_elider.h" > </File> <File diff --git a/chrome/common/gfx/url_elider.cc b/chrome/common/gfx/text_elider.cc index b930557..a3d1c5d 100644 --- a/chrome/common/gfx/url_elider.cc +++ b/chrome/common/gfx/text_elider.cc @@ -2,9 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/file_util.h" #include "base/string_util.h" #include "chrome/common/gfx/chrome_font.h" -#include "chrome/common/gfx/url_elider.h" +#include "chrome/common/gfx/text_elider.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" #include "chrome/views/label.h" @@ -280,6 +281,34 @@ std::wstring ElideUrl(const GURL& url, return ElideText(final_elided_url_string, font, available_pixel_width); } +std::wstring ElideFilename(const std::wstring& filename, + const ChromeFont& font, + int available_pixel_width) { + int full_width = font.GetStringWidth(filename); + if (full_width <= available_pixel_width) + return filename; + + std::wstring extension = + file_util::GetFileExtensionFromPath(filename); + std::wstring rootname = + file_util::GetFilenameWithoutExtensionFromPath(filename); + + if (rootname.empty() || extension.empty()) + return ElideText(filename, font, available_pixel_width); + + extension = L"." + extension; + + int ext_width = font.GetStringWidth(extension); + int root_width = font.GetStringWidth(rootname); + + // We may have trimmed the path. + if (root_width + ext_width <= available_pixel_width) + return rootname + extension; + + int available_root_width = available_pixel_width - ext_width; + return gfx::ElideText(rootname, font, available_root_width) + extension; +} + // This function adds an ellipsis at the end of the text if the text // does not fit the given pixel width. std::wstring ElideText(const std::wstring& text, diff --git a/chrome/common/gfx/url_elider.h b/chrome/common/gfx/text_elider.h index 83815f0..26872db 100644 --- a/chrome/common/gfx/url_elider.h +++ b/chrome/common/gfx/text_elider.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_COMMON_GFX_URL_ELIDER_H_ -#define CHROME_COMMON_GFX_URL_ELIDER_H_ +#ifndef CHROME_COMMON_GFX_TEXT_ELIDER_H_ +#define CHROME_COMMON_GFX_TEXT_ELIDER_H_ #include <unicode/coll.h> #include <unicode/uchar.h> @@ -32,6 +32,13 @@ std::wstring ElideText(const std::wstring& text, const ChromeFont& font, int available_pixel_width); +// Elide a filename to fit a given pixel width, with an emphasis on not hiding +// the extension unless we have to. If filename contains a path, the path will +// be removed if filename doesn't fit into available_pixel_width. +std::wstring ElideFilename(const std::wstring& filename, + const ChromeFont& font, + int available_pixel_width); + // SortedDisplayURL maintains a string from a URL suitable for display to the // use. SortedDisplayURL also provides a function used for comparing two // SortedDisplayURLs for use in visually ordering the SortedDisplayURLs. @@ -66,4 +73,4 @@ class SortedDisplayURL { } // namespace gfx. -#endif // #ifndef CHROME_COMMON_GFX_URL_ELIDER_H_ +#endif // CHROME_COMMON_GFX_TEXT_ELIDER_H_ diff --git a/chrome/common/gfx/url_elider_unittest.cc b/chrome/common/gfx/text_elider_unittest.cc index 82feb80..94be302 100644 --- a/chrome/common/gfx/url_elider_unittest.cc +++ b/chrome/common/gfx/text_elider_unittest.cc @@ -4,7 +4,7 @@ #include "base/string_util.h" #include "chrome/common/gfx/chrome_font.h" -#include "chrome/common/gfx/url_elider.h" +#include "chrome/common/gfx/text_elider.h" #include "chrome/views/label.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" @@ -40,7 +40,7 @@ void RunTest(Testcase* testcases, size_t num_testcases) { } // namespace // Test eliding of commonplace URLs. -TEST(URLEliderTest, TestGeneralEliding) { +TEST(TextEliderTest, TestGeneralEliding) { const std::wstring kEllipsisStr(kEllipsis); Testcase testcases[] = { {"http://www.google.com/intl/en/ads/", @@ -66,7 +66,7 @@ TEST(URLEliderTest, TestGeneralEliding) { } // Test eliding of empty strings, URLs with ports, passwords, queries, etc. -TEST(URLEliderTest, TestMoreEliding) { +TEST(TextEliderTest, TestMoreEliding) { const std::wstring kEllipsisStr(kEllipsis); Testcase testcases[] = { {"http://www.google.com/foo?bar", L"http://www.google.com/foo?bar"}, @@ -104,7 +104,7 @@ TEST(URLEliderTest, TestMoreEliding) { } // Test eliding of file: URLs. -TEST(URLEliderTest, TestFileURLEliding) { +TEST(TextEliderTest, TestFileURLEliding) { const std::wstring kEllipsisStr(kEllipsis); Testcase testcases[] = { {"file:///C:/path1/path2/path3/filename", @@ -125,7 +125,39 @@ TEST(URLEliderTest, TestFileURLEliding) { RunTest(testcases, arraysize(testcases)); } -TEST(URLEliderTest, ElideTextLongStrings) { +TEST(TextEliderTest, TestFilenameEliding) { + const std::wstring kEllipsisStr(kEllipsis); + + WideTestcase testcases[] = { + {L"", L""}, + {L".", L"."}, + {L"filename.exe", L"filename.exe"}, + {L".longext", L".longext"}, + {L"pie", L"pie"}, + {L"c:\\path\\filename.pie", L"filename.pie"}, + {L"c:\\path\\longfilename.pie", L"long" + kEllipsisStr + L".pie"}, + {L"http://path.com/filename.pie", L"filename.pie"}, + {L"http://path.com/longfilename.pie", L"long" + kEllipsisStr + L".pie"}, + {L"piesmashingtacularpants", L"pie" + kEllipsisStr}, + {L".piesmashingtacularpants", L".pie" + kEllipsisStr}, + {L"cheese.", L"cheese."}, + {L"file name.longext", L"file" + kEllipsisStr + L".longext"}, + {L"fil ename.longext", L"fil " + kEllipsisStr + L".longext"}, + {L"filename.longext", L"file" + kEllipsisStr + L".longext"}, + {L"filename.middleext.longext", + L"filename.mid" + kEllipsisStr + L".longext"} + }; + + static const ChromeFont font; + for (size_t i = 0; i < arraysize(testcases); ++i) { + const std::wstring filename(testcases[i].input); + EXPECT_EQ(testcases[i].output, ElideFilename(filename, + font, + font.GetStringWidth(testcases[i].output))); + } +} + +TEST(TextEliderTest, ElideTextLongStrings) { const std::wstring kEllipsisStr(kEllipsis); std::wstring data_scheme(L"data:text/plain,"); @@ -163,13 +195,13 @@ TEST(URLEliderTest, ElideTextLongStrings) { } // Verifies display_url is set correctly. -TEST(URLEliderTest, SortedDisplayURL) { +TEST(TextEliderTest, SortedDisplayURL) { gfx::SortedDisplayURL d_url(GURL("http://www.google.com/"), std::wstring()); EXPECT_EQ(L"http://www.google.com/", d_url.display_url()); } // Verifies DisplayURL::Compare works correctly. -TEST(URLEliderTest, SortedDisplayURLCompare) { +TEST(TextEliderTest, SortedDisplayURLCompare) { UErrorCode create_status = U_ZERO_ERROR; scoped_ptr<Collator> collator(Collator::createInstance(create_status)); if (!U_SUCCESS(create_status)) |