OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" | 5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <cmath> | 8 #include <cmath> |
8 | 9 |
10 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | |
11 | |
9 namespace { | 12 namespace { |
10 | 13 |
11 // How far to offset image column from the left. | 14 // How far to offset image column from the left. |
12 const CGFloat kImageXOffset = 5.0; | 15 const CGFloat kImageXOffset = 5.0; |
13 | 16 |
14 // How far to offset the text column from the left. | 17 // How far to offset the text column from the left. |
15 const CGFloat kTextXOffset = 28.0; | 18 const CGFloat kTextXOffset = 28.0; |
16 | 19 |
20 // Maximum fraction of the popup width that can be used to display match | |
21 // contents. | |
22 const CGFloat kMinDescriptionFraction = 0.7; | |
23 | |
17 // Rounding radius of selection and hover background on popup items. | 24 // Rounding radius of selection and hover background on popup items. |
18 const CGFloat kCellRoundingRadius = 2.0; | 25 const CGFloat kCellRoundingRadius = 2.0; |
19 | 26 |
20 NSColor* SelectedBackgroundColor() { | 27 void DrawFadeTruncatingTitle(NSAttributedString* title, |
21 return [NSColor selectedControlColor]; | 28 NSRect titleRect, |
22 } | 29 NSColor* backgroundColor) { |
23 NSColor* HoveredBackgroundColor() { | 30 gfx::ScopedNSGraphicsContextSaveGState scopedGState; |
24 return [NSColor controlHighlightColor]; | 31 NSRectClip(titleRect); |
32 | |
33 // Draw the entire text. | |
34 NSSize textSize = [title size]; | |
35 NSPoint textOrigin = titleRect.origin; | |
36 textOrigin.y += roundf((NSHeight(titleRect) - textSize.height) / 2.0) - 1.0; | |
37 [title drawAtPoint:textOrigin]; | |
38 | |
39 // Empirically, Cocoa will draw an extra 2 pixels past NSWidth(titleRect) | |
40 // before it clips the text. | |
41 const CGFloat kOverflowBeforeClip = 2.0; | |
42 CGFloat clipped_width = NSWidth(titleRect) + kOverflowBeforeClip; | |
43 if (textSize.width <= clipped_width) | |
44 return; | |
45 | |
46 // 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.
| |
47 CGFloat gradientWidth = std::min(textSize.height, NSWidth(titleRect) / 4); | |
48 | |
49 // Draw the gradient part. | |
50 NSColor *alphaColor = [backgroundColor colorWithAlphaComponent:0.0]; | |
51 base::scoped_nsobject<NSGradient> mask( | |
52 [[NSGradient alloc] initWithStartingColor:alphaColor | |
53 endingColor:backgroundColor]); | |
54 [mask drawFromPoint:NSMakePoint(NSMaxX(titleRect) - gradientWidth, | |
55 NSMinY(titleRect)) | |
56 toPoint:NSMakePoint(NSMaxX(titleRect), | |
57 NSMinY(titleRect)) | |
58 options:NSGradientDrawsBeforeStartingLocation]; | |
25 } | 59 } |
26 | 60 |
27 } // namespace | 61 } // namespace |
28 | 62 |
29 @implementation OmniboxPopupCell | 63 @implementation OmniboxPopupCell |
30 | 64 |
31 - (id)init { | 65 - (id)init { |
32 self = [super init]; | 66 if ((self = [super init])) { |
33 if (self) { | |
34 [self setImagePosition:NSImageLeft]; | 67 [self setImagePosition:NSImageLeft]; |
35 [self setBordered:NO]; | 68 [self setBordered:NO]; |
36 [self setButtonType:NSRadioButton]; | 69 [self setButtonType:NSRadioButton]; |
37 | 70 |
38 // Without this highlighting messes up white areas of images. | 71 // Without this highlighting messes up white areas of images. |
39 [self setHighlightsBy:NSNoCellMask]; | 72 [self setHighlightsBy:NSNoCellMask]; |
40 } | 73 } |
41 return self; | 74 return self; |
42 } | 75 } |
43 | 76 |
44 // The default NSButtonCell drawing leaves the image flush left and | 77 - (void)setContentText:(NSAttributedString*)contentText { |
45 // the title next to the image. This spaces things out to line up | 78 contentText_.reset([contentText retain]); |
46 // with the star button and autocomplete field. | 79 } |
80 | |
81 - (void)setDescriptionText:(NSAttributedString*)descriptionText { | |
82 base::scoped_nsobject<NSMutableAttributedString> dashDescriptionText( | |
83 [[NSMutableAttributedString alloc] | |
84 initWithAttributedString:descriptionText]); | |
85 NSString* rawEnDash = @" \u2013 "; | |
86 [dashDescriptionText replaceCharactersInRange:NSMakeRange(0, 0) | |
87 withString:rawEnDash]; | |
88 descriptionText_.reset(dashDescriptionText.release()); | |
89 } | |
90 | |
47 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { | 91 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { |
92 NSColor* backgroundColor = [NSColor controlBackgroundColor]; | |
48 if ([self state] == NSOnState || [self isHighlighted]) { | 93 if ([self state] == NSOnState || [self isHighlighted]) { |
49 if ([self state] == NSOnState) | 94 if ([self state] == NSOnState) |
50 [SelectedBackgroundColor() set]; | 95 backgroundColor = [NSColor selectedControlColor]; |
51 else | 96 else |
52 [HoveredBackgroundColor() set]; | 97 backgroundColor = [NSColor controlHighlightColor]; |
98 [backgroundColor set]; | |
53 NSBezierPath* path = | 99 NSBezierPath* path = |
54 [NSBezierPath bezierPathWithRoundedRect:cellFrame | 100 [NSBezierPath bezierPathWithRoundedRect:cellFrame |
55 xRadius:kCellRoundingRadius | 101 xRadius:kCellRoundingRadius |
56 yRadius:kCellRoundingRadius]; | 102 yRadius:kCellRoundingRadius]; |
57 [path fill]; | 103 [path fill]; |
58 } | 104 } |
59 | 105 |
60 // Put the image centered vertically but in a fixed column. | 106 // Put the image centered vertically but in a fixed column. |
61 NSImage* image = [self image]; | 107 NSImage* image = [self image]; |
62 if (image) { | 108 if (image) { |
63 NSRect imageRect = cellFrame; | 109 NSRect imageRect = cellFrame; |
64 imageRect.size = [image size]; | 110 imageRect.size = [image size]; |
65 imageRect.origin.y += | 111 imageRect.origin.y += |
66 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); | 112 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); |
67 imageRect.origin.x += kImageXOffset; | 113 imageRect.origin.x += kImageXOffset; |
68 [image drawInRect:imageRect | 114 [image drawInRect:imageRect |
69 fromRect:NSZeroRect // Entire image | 115 fromRect:NSZeroRect // Entire image |
70 operation:NSCompositeSourceOver | 116 operation:NSCompositeSourceOver |
71 fraction:1.0 | 117 fraction:1.0 |
72 respectFlipped:YES | 118 respectFlipped:YES |
73 hints:nil]; | 119 hints:nil]; |
74 } | 120 } |
75 | 121 |
76 // Adjust the title position to be lined up under the field's text. | 122 // Adjust the title position to be lined up under the field's text. |
77 NSAttributedString* title = [self attributedTitle]; | 123 if ([contentText_ length]) { |
78 if (title && [title length]) { | 124 NSRect availRect = cellFrame; |
79 NSRect titleRect = cellFrame; | 125 availRect.size.width = NSWidth(cellFrame) - kTextXOffset; |
80 titleRect.size.width -= kTextXOffset; | 126 availRect.origin.x += kTextXOffset; |
81 titleRect.origin.x += kTextXOffset; | 127 CGFloat availWidth = NSWidth(availRect); |
82 [self drawTitle:title withFrame:titleRect inView:controlView]; | 128 CGFloat contentWidth = [contentText_ size].width; |
129 CGFloat descWidth = | |
130 [descriptionText_ length] ? [descriptionText_ size].width : 0; | |
131 | |
132 CGFloat tempDescWidth = | |
133 std::min(descWidth, kMinDescriptionFraction * availWidth); | |
134 contentWidth = std::min(contentWidth, availWidth - tempDescWidth); | |
135 | |
136 NSRect contentRect; | |
137 NSRect descRect; | |
138 NSDivideRect( | |
139 availRect, &contentRect, &descRect, contentWidth, NSMinXEdge); | |
140 | |
141 DrawFadeTruncatingTitle(contentText_, contentRect, backgroundColor); | |
142 if ([descriptionText_ length]) | |
143 DrawFadeTruncatingTitle(descriptionText_, descRect, backgroundColor); | |
83 } | 144 } |
84 } | 145 } |
85 | 146 |
86 @end | 147 @end |
OLD | NEW |