diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-20 16:11:54 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-20 16:11:54 +0000 |
commit | e667f718463a78b7d1de5a51e71982211f00f875 (patch) | |
tree | 463c36805d7dc65e05a62621eae528357e8a4430 /chrome/browser | |
parent | 1932c872cfeaceb7915d78133cd45d3058cc9bb4 (diff) | |
download | chromium_src-e667f718463a78b7d1de5a51e71982211f00f875.zip chromium_src-e667f718463a78b7d1de5a51e71982211f00f875.tar.gz chromium_src-e667f718463a78b7d1de5a51e71982211f00f875.tar.bz2 |
Various omnibox UI fixes:
* Truncate match contents to 70% of the available width, reserving 30% for the
description.
* Animate omnibox shrinkage.
BUG=14898
TEST=Omnibox should animate smaller, but not bigger/opened/closed. Match
description should always be partially visible, if present.
Review URL: http://codereview.chromium.org/173002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23821 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
3 files changed, 80 insertions, 37 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.h b/chrome/browser/autocomplete/autocomplete_popup_view_mac.h index c58328b..7c2b004 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.h @@ -9,6 +9,7 @@ #include <string> +#include "app/gfx/font.h" #include "base/basictypes.h" #include "base/scoped_ptr.h" #include "base/scoped_nsobject.h" @@ -24,6 +25,9 @@ class Profile; // Implements AutocompletePopupView using a raw NSWindow containing an // NSTableView. +// +// TODO(rohitrao): This class is set up in a way that makes testing hard. +// Refactor and write unittests. http://crbug.com/9977 class AutocompletePopupViewMac : public AutocompletePopupView { public: @@ -74,7 +78,8 @@ class AutocompletePopupViewMac : public AutocompletePopupView { // contents and description. Result will be in |font|, with the // boldfaced version used for matches. static NSAttributedString* MatchText(const AutocompleteMatch& match, - NSFont* font); + gfx::Font& font, + float cellWidth); // Helper for MatchText() to allow sharing code between the contents // and description cases. Returns NSMutableAttributedString as a @@ -82,7 +87,7 @@ class AutocompletePopupViewMac : public AutocompletePopupView { static NSMutableAttributedString* DecorateMatchedString( const std::wstring &matchString, const AutocompleteMatch::ACMatchClassifications &classifications, - NSColor* textColor, NSFont* font); + NSColor* textColor, gfx::Font& font); private: // Create the popup_ instance if needed. diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm index c545ee4..9d68b74 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm @@ -4,6 +4,7 @@ #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" +#include "app/gfx/text_elider.h" #include "base/sys_string_conversions.h" #include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" @@ -25,12 +26,19 @@ const int kCellHeightAdjust = 7.0; // buttons. const CGFloat kPopupRoundingRadius = 4.0; -// How far to offset the image column from the left. -const CGFloat kImageXOffset = 8.0; +// How much space to leave for the left and right margins. +const CGFloat kLeftRightMargin = 8.0; // How far to offset the text column from the left. const CGFloat kTextXOffset = 33.0; +// Animation duration when animating the popup window smaller. +const float kShrinkAnimationDuration = 0.1; + +// Maximum fraction of the popup width that can be used to display match +// contents. +const float kMaxMatchContentsWidth = 0.7; + // Background colors for different states of the popup elements. NSColor* BackgroundColor() { return [NSColor controlBackgroundColor]; @@ -99,14 +107,14 @@ NSImage* MatchIcon(const AutocompleteMatch& match) { NSMutableAttributedString* AutocompletePopupViewMac::DecorateMatchedString( const std::wstring &matchString, const AutocompleteMatch::ACMatchClassifications &classifications, - NSColor* textColor, NSFont* font) { + NSColor* textColor, gfx::Font& font) { // Cache for on-demand computation of the bold version of |font|. NSFont* boldFont = nil; // Start out with a string using the default style info. NSString* s = base::SysWideToNSString(matchString); NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: - font, NSFontAttributeName, + font.nativeFont(), NSFontAttributeName, textColor, NSForegroundColorAttributeName, nil]; NSMutableAttributedString* as = @@ -131,7 +139,8 @@ NSMutableAttributedString* AutocompletePopupViewMac::DecorateMatchedString( if (0 != (i->style & ACMatchClassification::MATCH)) { if (!boldFont) { NSFontManager* fontManager = [NSFontManager sharedFontManager]; - boldFont = [fontManager convertFont:font toHaveTrait:NSBoldFontMask]; + boldFont = [fontManager convertFont:font.nativeFont() + toHaveTrait:NSBoldFontMask]; } [as addAttribute:NSFontAttributeName value:boldFont range:range]; } @@ -144,9 +153,18 @@ NSMutableAttributedString* AutocompletePopupViewMac::DecorateMatchedString( // contents and description. Result will be in |font|, with the // boldfaced version used for matches. NSAttributedString* AutocompletePopupViewMac::MatchText( - const AutocompleteMatch& match, NSFont* font) { + const AutocompleteMatch& match, gfx::Font& font, float cellWidth) { + // If there is a description, then the URL can take at most 70% of the + // available width, with 30% being reserved for the description. If there is + // no description, then the URL can take the full 100%. + float availableWidth = cellWidth - kTextXOffset - kLeftRightMargin; + BOOL hasDescription = match.description.empty() ? NO : YES; + float urlWidth = hasDescription ? availableWidth * kMaxMatchContentsWidth + : availableWidth; + NSMutableAttributedString *as = - DecorateMatchedString(match.contents, match.contents_class, + DecorateMatchedString(gfx::ElideText(match.contents, font, urlWidth), + match.contents_class, ContentTextColor(), font); // If there is a description, append it, separated from the contents @@ -154,7 +172,7 @@ NSAttributedString* AutocompletePopupViewMac::MatchText( if (!match.description.empty()) { NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: - font, NSFontAttributeName, + font.nativeFont(), NSFontAttributeName, ContentTextColor(), NSForegroundColorAttributeName, nil]; NSString* rawEnDash = [NSString stringWithFormat:@" %C ", 0x2013]; @@ -297,25 +315,6 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() { CreatePopupIfNeeded(); - // The popup's font is a slightly smaller version of what |field_| - // uses. - NSFont* fieldFont = [field_ font]; - const CGFloat resultFontSize = [fieldFont pointSize] + kEditFontAdjust; - NSFont* resultFont = - [NSFont fontWithName:[fieldFont fontName] size:resultFontSize]; - - // Load the results into the popup's matrix. - AutocompleteMatrix* matrix = [popup_ contentView]; - const size_t rows = model_->result().size(); - DCHECK_GT(rows, 0U); - [matrix renewRows:rows columns:1]; - for (size_t ii = 0; ii < rows; ++ii) { - AutocompleteButtonCell* cell = [matrix cellAtRow:ii column:0]; - const AutocompleteMatch& match = model_->result().match_at(ii); - [cell setImage:MatchIcon(match)]; - [cell setAttributedTitle:MatchText(match, resultFont)]; - } - // Layout the popup and size it to land underneath the field. // TODO(shess) Consider refactoring to remove this depenency, // because the popup doesn't need any of the field-like aspects of @@ -334,6 +333,26 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() { r.origin = [[field_ window] convertBaseToScreen:r.origin]; DCHECK_GT(r.size.width, 0.0); + // The popup's font is a slightly smaller version of what |field_| + // uses. + NSFont* fieldFont = [field_ font]; + const CGFloat resultFontSize = [fieldFont pointSize] + kEditFontAdjust; + gfx::Font resultFont = gfx::Font::CreateFont( + base::SysNSStringToWide([fieldFont fontName]), (int)resultFontSize); + + // Load the results into the popup's matrix. The popup window must be + // correctly sized before calling MatchText(). + AutocompleteMatrix* matrix = [popup_ contentView]; + const size_t rows = model_->result().size(); + DCHECK_GT(rows, 0U); + [matrix renewRows:rows columns:1]; + for (size_t ii = 0; ii < rows; ++ii) { + AutocompleteButtonCell* cell = [matrix cellAtRow:ii column:0]; + const AutocompleteMatch& match = model_->result().match_at(ii); + [cell setImage:MatchIcon(match)]; + [cell setAttributedTitle:MatchText(match, resultFont, r.size.width)]; + } + // Set the cell size to fit a line of text in the cell's font. All // cells should use the same font and each should layout in one // line, so they should all be about the same height. @@ -352,7 +371,19 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() { // Update the selection. PaintUpdatesNow(); - [popup_ setFrame:r display:YES]; + // Animate the frame change if the only change is that the height got smaller. + // Otherwise, resize immediately. + NSRect oldFrame = [popup_ frame]; + if (r.size.height < oldFrame.size.height && + r.origin.x == oldFrame.origin.x && + r.size.width == oldFrame.size.width) { + [NSAnimationContext beginGrouping]; + [[NSAnimationContext currentContext] setDuration:kShrinkAnimationDuration]; + [[popup_ animator] setFrame:r display:YES]; + [NSAnimationContext endGrouping]; + } else { + [popup_ setFrame:r display:YES]; + } if (!IsOpen()) { [[field_ window] addChildWindow:popup_ ordered:NSWindowAbove]; @@ -431,7 +462,7 @@ void AutocompletePopupViewMac::AcceptInput() { imageRect.size = [image size]; imageRect.origin.y += floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2); - imageRect.origin.x += kImageXOffset; + imageRect.origin.x += kLeftRightMargin; [self drawImage:image withFrame:imageRect inView:controlView]; } @@ -439,7 +470,7 @@ void AutocompletePopupViewMac::AcceptInput() { NSAttributedString* title = [self attributedTitle]; if (title) { NSRect titleRect = cellFrame; - titleRect.size.width -= kTextXOffset; + titleRect.size.width -= (kTextXOffset + kLeftRightMargin); titleRect.origin.x += kTextXOffset; [self drawTitle:title withFrame:titleRect inView:controlView]; } diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm index 31c1aa9..d845ee6 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm @@ -4,12 +4,15 @@ #import "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" +#include "base/scoped_ptr.h" #include "base/sys_string_conversions.h" #include "chrome/browser/autocomplete/autocomplete.h" #include "testing/platform_test.h" namespace { +const float kLargeWidth = 10000; + class AutocompletePopupViewMacTest : public PlatformTest { public: AutocompletePopupViewMacTest() {} @@ -20,7 +23,8 @@ class AutocompletePopupViewMacTest : public PlatformTest { // These are here because there is no autorelease pool for the // constructor. color_ = [NSColor blackColor]; - font_ = [NSFont userFontOfSize:12]; + font_ = gfx::Font::CreateFont( + base::SysNSStringToWide([[NSFont userFontOfSize:12] fontName]), 12); } // Returns the length of the run starting at |location| for which @@ -88,7 +92,7 @@ class AutocompletePopupViewMacTest : public PlatformTest { } NSColor* color_; // weak - NSFont* font_; // weak + gfx::Font font_; }; // Simple inputs with no matches should result in styled output who's @@ -288,7 +292,8 @@ TEST_F(AutocompletePopupViewMacTest, MatchText) { AutocompleteMatch m = MakeMatch(base::SysNSStringToWide(contents), base::SysNSStringToWide(description)); - NSAttributedString* decorated = AutocompletePopupViewMac::MatchText(m, font_); + NSAttributedString* decorated = + AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth); // Result contains the characters of the input in the right places. EXPECT_GT([decorated length], [contents length] + [description length]); @@ -332,7 +337,8 @@ TEST_F(AutocompletePopupViewMacTest, MatchTextContentsMatch) { ACMatchClassification(runLength1 + runLength2, ACMatchClassification::NONE)); - NSAttributedString* decorated = AutocompletePopupViewMac::MatchText(m, font_); + NSAttributedString* decorated = + AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth); // Result has same characters as the input. EXPECT_EQ([decorated length], [contents length]); @@ -376,7 +382,8 @@ TEST_F(AutocompletePopupViewMacTest, MatchTextDescriptionMatch) { m.description_class.push_back( ACMatchClassification(runLength1, ACMatchClassification::NONE)); - NSAttributedString* decorated = AutocompletePopupViewMac::MatchText(m, font_); + NSAttributedString* decorated = + AutocompletePopupViewMac::MatchText(m, font_, kLargeWidth); // Result contains the characters of the input. EXPECT_GT([decorated length], [contents length] + [description length]); |