From b236fc0da032b838cde817a92828d967f88c90fc Mon Sep 17 00:00:00 2001 From: dschuyler Date: Fri, 19 Jun 2015 15:32:24 -0700 Subject: [PATCH] [AiS] Mac AiS on two lines The AiS results are intended to be on two lines in the suggestion drop down. This CL makes that change. BUG=488215 Review URL: https://codereview.chromium.org/1128713002 Cr-Commit-Position: refs/heads/master@{#335368} --- .../browser/ui/cocoa/omnibox/omnibox_popup_cell.h | 10 ++- .../browser/ui/cocoa/omnibox/omnibox_popup_cell.mm | 87 ++++++++++------------ .../ui/cocoa/omnibox/omnibox_popup_matrix.h | 3 + .../ui/cocoa/omnibox/omnibox_popup_matrix.mm | 16 +++- 4 files changed, 63 insertions(+), 53 deletions(-) diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h index 703516311c17..58c37318982d 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h @@ -34,6 +34,10 @@ class OmniboxPopupViewMac; CGFloat contentsOffset_; BOOL isContentsRTL_; + + // Is this suggestion an answer or calculator result. + bool isAnswer_; + AutocompleteMatch::Type matchType_; } @@ -44,6 +48,7 @@ class OmniboxPopupViewMac; @property(readonly, retain, nonatomic) NSImage* answerImage; @property(readonly, nonatomic) CGFloat contentsOffset; @property(readonly, nonatomic) BOOL isContentsRTL; +@property(readonly, nonatomic) bool isAnswer; @property(readonly, nonatomic) AutocompleteMatch::Type matchType; - (instancetype)initWithMatch:(const AutocompleteMatch&)match @@ -51,9 +56,6 @@ class OmniboxPopupViewMac; image:(NSImage*)image answerImage:(NSImage*)answerImage; -// Each row is allowed to have a different value. -- (CGFloat)rowHeight; - // Returns the width of the match contents. - (CGFloat)getMatchContentsWidth; @@ -74,4 +76,6 @@ class OmniboxPopupViewMac; @end +const CGFloat kContentLineHeight = 25.0; + #endif // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_POPUP_CELL_H_ diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm index f542084f33c5..826160654910 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm @@ -25,16 +25,12 @@ namespace { -// How much to adjust the cell sizing up from the default determined -// by the font. -const CGFloat kCellHeightAdjust = 6.0; - -// How large the icon should be when displayed. -const CGFloat kImageSize = 19.0; - // How far to offset image column from the left. const CGFloat kImageXOffset = 5.0; +// How far to offset image and text. +const CGFloat kPaddingOffset = 3.0; + // How far to offset the text column from the left. const CGFloat kTextStartOffset = 28.0; @@ -53,15 +49,6 @@ NSRect FlipIfRTL(NSRect rect, NSRect frame) { return rect; } -// Shifts the left edge of the given |rect| by |dX| -NSRect ShiftRect(NSRect rect, CGFloat dX) { - DCHECK_LE(dX, NSWidth(rect)); - NSRect result = rect; - result.origin.x += dX; - result.size.width -= dX; - return result; -} - NSColor* SelectedBackgroundColor() { return [NSColor selectedControlColor]; } @@ -290,7 +277,7 @@ NSAttributedString* CreateClassifiedAttributedString( @interface OmniboxPopupCell () - (CGFloat)drawMatchPart:(NSAttributedString*)attributedString withFrame:(NSRect)cellFrame - atOffset:(CGFloat)offset + origin:(NSPoint)origin withMaxWidth:(int)maxWidth; - (CGFloat)drawMatchPrefixWithFrame:(NSRect)cellFrame tableView:(OmniboxPopupMatrix*)tableView @@ -307,6 +294,7 @@ NSAttributedString* CreateClassifiedAttributedString( @synthesize answerImage = answerImage_; @synthesize contentsOffset = contentsOffset_; @synthesize isContentsRTL = isContentsRTL_; +@synthesize isAnswer = isAnswer_; @synthesize matchType = matchType_; - (instancetype)initWithMatch:(const AutocompleteMatch&)match @@ -336,7 +324,8 @@ NSAttributedString* CreateClassifiedAttributedString( contents_ = [CreateClassifiedAttributedString( match.contents, ContentTextColor(), match.contents_class) retain]; - if (match.answer) { + isAnswer_ = match.answer; + if (isAnswer_) { base::scoped_nsobject answerString( [[NSMutableAttributedString alloc] init]); DCHECK(!match.answer->second_line().text_fields().empty()); @@ -377,10 +366,6 @@ NSAttributedString* CreateClassifiedAttributedString( return [contents_ size].width; } -- (CGFloat)rowHeight { - return kImageSize + kCellHeightAdjust; -} - @end @implementation OmniboxPopupCell @@ -418,12 +403,10 @@ NSAttributedString* CreateClassifiedAttributedString( !AutocompleteMatch::IsSearchType([cellData matchType]), &contentsMaxWidth, &descriptionMaxWidth); - // Put the image centered vertically but in a fixed column. NSRect imageRect = cellFrame; imageRect.size = [[cellData image] size]; - imageRect.origin.y += - std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); imageRect.origin.x += kImageXOffset; + imageRect.origin.y += kPaddingOffset; [[cellData image] drawInRect:FlipIfRTL(imageRect, cellFrame) fromRect:NSZeroRect operation:NSCompositeSourceOver @@ -431,26 +414,27 @@ NSAttributedString* CreateClassifiedAttributedString( respectFlipped:YES hints:nil]; - CGFloat offset = kTextStartOffset; + NSPoint origin = NSMakePoint(kTextStartOffset, kPaddingOffset); if ([cellData matchType] == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { // Infinite suggestions are rendered with a prefix (usually ellipsis), which // appear vertically stacked. - offset += [self drawMatchPrefixWithFrame:cellFrame - tableView:tableView - withContentsMaxWidth:&contentsMaxWidth]; + origin.x += [self drawMatchPrefixWithFrame:cellFrame + tableView:tableView + withContentsMaxWidth:&contentsMaxWidth]; } - offset += [self drawMatchPart:[cellData contents] - withFrame:cellFrame - atOffset:offset - withMaxWidth:contentsMaxWidth]; - - if (descriptionMaxWidth != 0) { - offset += [self drawMatchPart:[tableView separator] + origin.x += [self drawMatchPart:[cellData contents] withFrame:cellFrame - atOffset:offset - withMaxWidth:separatorWidth]; - NSRect imageRect = NSMakeRect(offset, NSMinY(cellFrame), - NSHeight(cellFrame), NSHeight(cellFrame)); + origin:origin + withMaxWidth:contentsMaxWidth]; + + if (descriptionMaxWidth > 0) { + if ([cellData isAnswer]) { + origin = + NSMakePoint(kTextStartOffset, kContentLineHeight - kPaddingOffset); + CGFloat imageSize = [tableView answerLineHeight]; + NSRect imageRect = + NSMakeRect(NSMinX(cellFrame) + origin.x, NSMinY(cellFrame) + origin.y, + imageSize, imageSize); [[cellData answerImage] drawInRect:FlipIfRTL(imageRect, cellFrame) fromRect:NSZeroRect operation:NSCompositeSourceOver @@ -458,10 +442,16 @@ NSAttributedString* CreateClassifiedAttributedString( respectFlipped:YES hints:nil]; if ([cellData answerImage]) - offset += NSWidth(imageRect); - offset += [self drawMatchPart:[cellData description] + origin.x += imageSize + kPaddingOffset; + } else { + origin.x += [self drawMatchPart:[tableView separator] + withFrame:cellFrame + origin:origin + withMaxWidth:separatorWidth]; + } + origin.x += [self drawMatchPart:[cellData description] withFrame:cellFrame - atOffset:offset + origin:origin withMaxWidth:descriptionMaxWidth]; } } @@ -503,21 +493,20 @@ NSAttributedString* CreateClassifiedAttributedString( *contentsMaxWidth); [self drawMatchPart:[cellData prefix] withFrame:cellFrame - atOffset:prefixOffset + kTextStartOffset + origin:NSMakePoint(prefixOffset + kTextStartOffset, 0) withMaxWidth:prefixWidth]; return offset; } - (CGFloat)drawMatchPart:(NSAttributedString*)attributedString withFrame:(NSRect)cellFrame - atOffset:(CGFloat)offset + origin:(NSPoint)origin withMaxWidth:(int)maxWidth { - if (offset > NSWidth(cellFrame)) - return 0.0f; - NSRect renderRect = ShiftRect(cellFrame, offset); + NSRect renderRect = NSIntersectionRect( + cellFrame, NSOffsetRect(cellFrame, origin.x, origin.y)); renderRect.size.width = std::min(NSWidth(renderRect), static_cast(maxWidth)); - if (NSWidth(renderRect) > 0.0) + if (!NSIsEmptyRect(renderRect)) [attributedString drawInRect:FlipIfRTL(renderRect, cellFrame)]; return NSWidth(renderRect); } diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h index 476811b41871..cfe5c4d71d23 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h @@ -65,10 +65,13 @@ class OmniboxPopupMatrixObserver { // The width of widest match contents in a set of infinite suggestions. CGFloat maxMatchContentsWidth_; + + CGFloat answerLineHeight_; } @property(retain, nonatomic) NSAttributedString* separator; @property(nonatomic) CGFloat maxMatchContentsWidth; +@property(nonatomic) CGFloat answerLineHeight; // Create a zero-size matrix. - (instancetype)initWithObserver:(OmniboxPopupMatrixObserver*)observer; diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm index 02c360510d30..f7282457066f 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.mm @@ -8,6 +8,7 @@ #include "base/mac/foundation_util.h" #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" +#include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" #include "components/omnibox/autocomplete_result.h" namespace { @@ -98,7 +99,13 @@ const NSInteger kMiddleButtonNumber = 2; } - (CGFloat)tableView:(NSTableView*)tableView heightOfRow:(NSInteger)row { - return [[array_ objectAtIndex:row] rowHeight]; + CGFloat height = kContentLineHeight; + if ([[array_ objectAtIndex:row] isAnswer]) { + OmniboxPopupMatrix* matrix = + base::mac::ObjCCastStrict(tableView); + height += [matrix answerLineHeight]; + } + return height; } @end @@ -114,6 +121,7 @@ const NSInteger kMiddleButtonNumber = 2; @synthesize separator = separator_; @synthesize maxMatchContentsWidth = maxMatchContentsWidth_; +@synthesize answerLineHeight = answerLineHeight_; - (instancetype)initWithObserver:(OmniboxPopupMatrixObserver*)observer { if ((self = [super initWithFrame:NSZeroRect])) { @@ -133,6 +141,12 @@ const NSInteger kMiddleButtonNumber = 2; [self deselectAll:self]; [self resetTrackingArea]; + + base::scoped_nsobject layoutManager( + [[NSLayoutManager alloc] init]); + answerLineHeight_ = + [layoutManager defaultLineHeightForFont:OmniboxViewMac::GetLargeFont( + gfx::Font::NORMAL)]; } return self; } -- 2.11.4.GIT