Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.mm

Issue 17774002: OmniboxPopupViewMac refactoring Part 3 (truncation) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 BOOL clipping =
43 std::floor(textSize.width) > NSWidth(titleRect) + kOverflowBeforeClip;
44 if (!clipping)
Scott Hess - ex-Googler 2013/07/08 20:53:11 You don't use |clipping| again, but the initializa
sail 2013/08/12 17:39:30 Done. Removed the floor.
45 return;
46
47 // Gradient is about twice our line height long.
48 CGFloat gradientWidth = std::min(textSize.height * 2, NSWidth(titleRect) / 4);
49
50 // Draw the gradient part.
51 NSColor *alphaColor = [backgroundColor colorWithAlphaComponent:0.0];
52 base::scoped_nsobject<NSGradient> mask(
53 [[NSGradient alloc] initWithStartingColor:alphaColor
54 endingColor:backgroundColor]);
55 [mask drawFromPoint:NSMakePoint(NSMaxX(titleRect) - gradientWidth,
56 NSMinY(titleRect))
57 toPoint:NSMakePoint(NSMaxX(titleRect),
58 NSMinY(titleRect))
59 options:NSGradientDrawsBeforeStartingLocation];
25 } 60 }
26 61
27 } // namespace 62 } // namespace
28 63
29 @implementation OmniboxPopupCell 64 @implementation OmniboxPopupCell
30 65
31 - (id)init { 66 - (id)init {
32 self = [super init]; 67 if ((self = [super init])) {
33 if (self) {
34 [self setImagePosition:NSImageLeft]; 68 [self setImagePosition:NSImageLeft];
35 [self setBordered:NO]; 69 [self setBordered:NO];
36 [self setButtonType:NSRadioButton]; 70 [self setButtonType:NSRadioButton];
37 71
38 // Without this highlighting messes up white areas of images. 72 // Without this highlighting messes up white areas of images.
39 [self setHighlightsBy:NSNoCellMask]; 73 [self setHighlightsBy:NSNoCellMask];
40 } 74 }
41 return self; 75 return self;
42 } 76 }
43 77
44 // The default NSButtonCell drawing leaves the image flush left and 78 - (void)setContentText:(NSAttributedString*)contentText {
45 // the title next to the image. This spaces things out to line up 79 contentText_.reset([contentText retain]);
46 // with the star button and autocomplete field. 80 }
81
82 - (void)setDescriptionText:(NSAttributedString*)descriptionText {
83 base::scoped_nsobject<NSMutableAttributedString> dashDescriptionText(
84 [[NSMutableAttributedString alloc]
85 initWithAttributedString:descriptionText]);
86 NSString* raw_en_dash = @" \u2013 ";
Scott Hess - ex-Googler 2013/07/08 20:53:11 Should this be rawEnDash now that this isn't embed
sail 2013/08/12 17:39:30 Done.
87 [dashDescriptionText replaceCharactersInRange:NSMakeRange(0, 0)
88 withString:raw_en_dash];
89 descriptionText_.reset(dashDescriptionText.release());
90 }
91
47 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 92 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
93 NSColor* backgroundColor = [NSColor controlBackgroundColor];
48 if ([self state] == NSOnState || [self isHighlighted]) { 94 if ([self state] == NSOnState || [self isHighlighted]) {
49 if ([self state] == NSOnState) 95 if ([self state] == NSOnState)
50 [SelectedBackgroundColor() set]; 96 backgroundColor = [NSColor selectedControlColor];
51 else 97 else
52 [HoveredBackgroundColor() set]; 98 backgroundColor = [NSColor controlHighlightColor];
99 [backgroundColor set];
53 NSBezierPath* path = 100 NSBezierPath* path =
54 [NSBezierPath bezierPathWithRoundedRect:cellFrame 101 [NSBezierPath bezierPathWithRoundedRect:cellFrame
55 xRadius:kCellRoundingRadius 102 xRadius:kCellRoundingRadius
56 yRadius:kCellRoundingRadius]; 103 yRadius:kCellRoundingRadius];
57 [path fill]; 104 [path fill];
58 } 105 }
59 106
60 // Put the image centered vertically but in a fixed column. 107 // Put the image centered vertically but in a fixed column.
61 NSImage* image = [self image]; 108 NSImage* image = [self image];
62 if (image) { 109 if (image) {
63 NSRect imageRect = cellFrame; 110 NSRect imageRect = cellFrame;
64 imageRect.size = [image size]; 111 imageRect.size = [image size];
65 imageRect.origin.y += 112 imageRect.origin.y +=
66 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); 113 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0);
67 imageRect.origin.x += kImageXOffset; 114 imageRect.origin.x += kImageXOffset;
68 [image drawInRect:imageRect 115 [image drawInRect:imageRect
69 fromRect:NSZeroRect // Entire image 116 fromRect:NSZeroRect // Entire image
70 operation:NSCompositeSourceOver 117 operation:NSCompositeSourceOver
71 fraction:1.0 118 fraction:1.0
72 respectFlipped:YES 119 respectFlipped:YES
73 hints:nil]; 120 hints:nil];
74 } 121 }
75 122
76 // Adjust the title position to be lined up under the field's text. 123 // Adjust the title position to be lined up under the field's text.
77 NSAttributedString* title = [self attributedTitle]; 124 if ([contentText_ length]) {
78 if (title && [title length]) { 125 NSRect availRect = cellFrame;
79 NSRect titleRect = cellFrame; 126 availRect.size.width = NSWidth(cellFrame) - kTextXOffset;
80 titleRect.size.width -= kTextXOffset; 127 availRect.origin.x += kTextXOffset;
81 titleRect.origin.x += kTextXOffset; 128 CGFloat availWidth = NSWidth(availRect);
82 [self drawTitle:title withFrame:titleRect inView:controlView]; 129 CGFloat contentWidth = [contentText_ size].width;
130 CGFloat descWidth =
131 [descriptionText_ length] ? [descriptionText_ size].width : 0;
132
133 CGFloat tempDescWidth =
134 std::min(descWidth, kMinDescriptionFraction * availWidth);
135 contentWidth = std::min(contentWidth, availWidth - tempDescWidth);
136
137 NSRect contentRect;
138 NSRect descRect;
139 NSDivideRect(
140 availRect, &contentRect, &descRect, contentWidth, NSMinXEdge);
141
142 DrawFadeTruncatingTitle(contentText_, contentRect, backgroundColor);
143 if ([descriptionText_ length])
144 DrawFadeTruncatingTitle(descriptionText_, descRect, backgroundColor);
83 } 145 }
84 } 146 }
85 147
86 @end 148 @end
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h ('k') | chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698