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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h"
11 // How far to offset image column from the left.
12 const CGFloat kImageXOffset = 5.0;
14 // How far to offset the text column from the left.
15 const CGFloat kTextXOffset = 28.0;
17 // Rounding radius of selection and hover background on popup items.
18 const CGFloat kCellRoundingRadius = 2.0;
20 NSColor* SelectedBackgroundColor() {
21 return [NSColor selectedControlColor];
23 NSColor* HoveredBackgroundColor() {
24 return [NSColor controlHighlightColor];
29 @implementation OmniboxPopupCell
34 [self setImagePosition:NSImageLeft];
35 [self setBordered:NO];
36 [self setButtonType:NSRadioButton];
38 // Without this highlighting messes up white areas of images.
39 [self setHighlightsBy:NSNoCellMask];
44 // The default NSButtonCell drawing leaves the image flush left and
45 // the title next to the image. This spaces things out to line up
46 // with the star button and autocomplete field.
47 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
48 if ([self state] == NSOnState || [self isHighlighted]) {
49 if ([self state] == NSOnState)
50 [SelectedBackgroundColor() set];
52 [HoveredBackgroundColor() set];
54 [NSBezierPath bezierPathWithRoundedRect:cellFrame
55 xRadius:kCellRoundingRadius
56 yRadius:kCellRoundingRadius];
60 // Put the image centered vertically but in a fixed column.
61 NSImage* image = [self image];
63 NSRect imageRect = cellFrame;
64 imageRect.size = [image size];
66 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0);
67 imageRect.origin.x += kImageXOffset;
68 [image drawInRect:imageRect
69 fromRect:NSZeroRect // Entire image
70 operation:NSCompositeSourceOver
76 // Adjust the title position to be lined up under the field's text.
77 NSAttributedString* title = [self attributedTitle];
78 if (title && [title length]) {
79 NSRect titleRect = cellFrame;
80 titleRect.size.width -= kTextXOffset;
81 titleRect.origin.x += kTextXOffset;
82 [self drawTitle:title withFrame:titleRect inView:controlView];