Index: chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm |
diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm |
index db647f8f6cf83e360090cc8347955909ec406ba6..ec6142f7bb1142ca44383847623add87d809edfc 100644 |
--- a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm |
+++ b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm |
@@ -4,8 +4,11 @@ |
#import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" |
+#include <algorithm> |
#include <cmath> |
+#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
+ |
namespace { |
// How far to offset image column from the left. |
@@ -14,14 +17,45 @@ const CGFloat kImageXOffset = 5.0; |
// How far to offset the text column from the left. |
const CGFloat kTextXOffset = 28.0; |
+// Maximum fraction of the popup width that can be used to display match |
+// contents. |
+const CGFloat kMinDescriptionFraction = 0.7; |
+ |
// Rounding radius of selection and hover background on popup items. |
const CGFloat kCellRoundingRadius = 2.0; |
-NSColor* SelectedBackgroundColor() { |
- return [NSColor selectedControlColor]; |
-} |
-NSColor* HoveredBackgroundColor() { |
- return [NSColor controlHighlightColor]; |
+void DrawFadeTruncatingTitle(NSAttributedString* title, |
+ NSRect titleRect, |
+ NSColor* backgroundColor) { |
+ gfx::ScopedNSGraphicsContextSaveGState scopedGState; |
+ NSRectClip(titleRect); |
+ |
+ // Draw the entire text. |
+ NSSize textSize = [title size]; |
+ NSPoint textOrigin = titleRect.origin; |
+ textOrigin.y += roundf((NSHeight(titleRect) - textSize.height) / 2.0) - 1.0; |
+ [title drawAtPoint:textOrigin]; |
+ |
+ // Empirically, Cocoa will draw an extra 2 pixels past NSWidth(titleRect) |
+ // before it clips the text. |
+ const CGFloat kOverflowBeforeClip = 2.0; |
+ CGFloat clipped_width = NSWidth(titleRect) + kOverflowBeforeClip; |
+ if (textSize.width <= clipped_width) |
+ return; |
+ |
+ // The gradient width is the same as the line height. |
sail
2013/08/12 17:39:31
Changed this from 2*line_height to 1*line_height.
|
+ CGFloat gradientWidth = std::min(textSize.height, NSWidth(titleRect) / 4); |
+ |
+ // Draw the gradient part. |
+ NSColor *alphaColor = [backgroundColor colorWithAlphaComponent:0.0]; |
+ base::scoped_nsobject<NSGradient> mask( |
+ [[NSGradient alloc] initWithStartingColor:alphaColor |
+ endingColor:backgroundColor]); |
+ [mask drawFromPoint:NSMakePoint(NSMaxX(titleRect) - gradientWidth, |
+ NSMinY(titleRect)) |
+ toPoint:NSMakePoint(NSMaxX(titleRect), |
+ NSMinY(titleRect)) |
+ options:NSGradientDrawsBeforeStartingLocation]; |
} |
} // namespace |
@@ -29,8 +63,7 @@ NSColor* HoveredBackgroundColor() { |
@implementation OmniboxPopupCell |
- (id)init { |
- self = [super init]; |
- if (self) { |
+ if ((self = [super init])) { |
[self setImagePosition:NSImageLeft]; |
[self setBordered:NO]; |
[self setButtonType:NSRadioButton]; |
@@ -41,15 +74,28 @@ NSColor* HoveredBackgroundColor() { |
return self; |
} |
-// The default NSButtonCell drawing leaves the image flush left and |
-// the title next to the image. This spaces things out to line up |
-// with the star button and autocomplete field. |
+- (void)setContentText:(NSAttributedString*)contentText { |
+ contentText_.reset([contentText retain]); |
+} |
+ |
+- (void)setDescriptionText:(NSAttributedString*)descriptionText { |
+ base::scoped_nsobject<NSMutableAttributedString> dashDescriptionText( |
+ [[NSMutableAttributedString alloc] |
+ initWithAttributedString:descriptionText]); |
+ NSString* rawEnDash = @" \u2013 "; |
+ [dashDescriptionText replaceCharactersInRange:NSMakeRange(0, 0) |
+ withString:rawEnDash]; |
+ descriptionText_.reset(dashDescriptionText.release()); |
+} |
+ |
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { |
+ NSColor* backgroundColor = [NSColor controlBackgroundColor]; |
if ([self state] == NSOnState || [self isHighlighted]) { |
if ([self state] == NSOnState) |
- [SelectedBackgroundColor() set]; |
+ backgroundColor = [NSColor selectedControlColor]; |
else |
- [HoveredBackgroundColor() set]; |
+ backgroundColor = [NSColor controlHighlightColor]; |
+ [backgroundColor set]; |
NSBezierPath* path = |
[NSBezierPath bezierPathWithRoundedRect:cellFrame |
xRadius:kCellRoundingRadius |
@@ -74,12 +120,27 @@ NSColor* HoveredBackgroundColor() { |
} |
// Adjust the title position to be lined up under the field's text. |
- NSAttributedString* title = [self attributedTitle]; |
- if (title && [title length]) { |
- NSRect titleRect = cellFrame; |
- titleRect.size.width -= kTextXOffset; |
- titleRect.origin.x += kTextXOffset; |
- [self drawTitle:title withFrame:titleRect inView:controlView]; |
+ if ([contentText_ length]) { |
+ NSRect availRect = cellFrame; |
+ availRect.size.width = NSWidth(cellFrame) - kTextXOffset; |
+ availRect.origin.x += kTextXOffset; |
+ CGFloat availWidth = NSWidth(availRect); |
+ CGFloat contentWidth = [contentText_ size].width; |
+ CGFloat descWidth = |
+ [descriptionText_ length] ? [descriptionText_ size].width : 0; |
+ |
+ CGFloat tempDescWidth = |
+ std::min(descWidth, kMinDescriptionFraction * availWidth); |
+ contentWidth = std::min(contentWidth, availWidth - tempDescWidth); |
+ |
+ NSRect contentRect; |
+ NSRect descRect; |
+ NSDivideRect( |
+ availRect, &contentRect, &descRect, contentWidth, NSMinXEdge); |
+ |
+ DrawFadeTruncatingTitle(contentText_, contentRect, backgroundColor); |
+ if ([descriptionText_ length]) |
+ DrawFadeTruncatingTitle(descriptionText_, descRect, backgroundColor); |
} |
} |