Chromium Code Reviews| 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> |
| 9 #include "base/i18n/rtl.h" | |
| 10 #include "base/logging.h" | |
| 8 | 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 |
| 17 // Rounding radius of selection and hover background on popup items. | 20 // Rounding radius of selection and hover background on popup items. |
| 18 const CGFloat kCellRoundingRadius = 2.0; | 21 const CGFloat kCellRoundingRadius = 2.0; |
| 19 | 22 |
| 20 NSColor* SelectedBackgroundColor() { | 23 NSColor* SelectedBackgroundColor() { |
| 21 return [NSColor selectedControlColor]; | 24 return [NSColor selectedControlColor]; |
| 22 } | 25 } |
| 26 | |
| 23 NSColor* HoveredBackgroundColor() { | 27 NSColor* HoveredBackgroundColor() { |
| 24 return [NSColor controlHighlightColor]; | 28 return [NSColor controlHighlightColor]; |
| 25 } | 29 } |
| 26 | 30 |
| 31 | |
| 32 // Flips the given |rect| in context of the given |frame|. | |
| 33 NSRect FlipVertical(NSRect rect, NSRect frame) { | |
| 34 NSRect result = rect; | |
| 35 // Translate the right margin of rect inside frame to the left margin. | |
| 36 result.origin.x = frame.origin.x + | |
| 37 ((frame.origin.x + frame.size.width) - (rect.origin.x + rect.size.width)); | |
|
Scott Hess - ex-Googler
2013/12/16 22:22:05
Try:
result.origin.x = NSMinX(frame) + (NSMaxX(f
Anuj
2013/12/19 05:36:13
Done.
| |
| 38 return result; | |
| 39 } | |
| 40 | |
| 27 } // namespace | 41 } // namespace |
| 28 | 42 |
| 29 @implementation OmniboxPopupCell | 43 @implementation OmniboxPopupCell |
| 30 | 44 |
| 45 - (void)setRequiredWidth:(CGFloat)requiredWidth { | |
| 46 requiredWidth_ = requiredWidth; | |
| 47 } | |
| 48 | |
| 49 - (void)setContentsWidth:(CGFloat)contentsWidth { | |
| 50 contentsWidth_ = contentsWidth; | |
| 51 } | |
| 52 | |
| 53 - (void)setMaxRequiredWidth:(CGFloat)maxRequiredWidth { | |
| 54 maxRequiredWidth_ = maxRequiredWidth; | |
| 55 } | |
| 56 | |
| 57 - (void)setMaxContentsWidth:(CGFloat)maxContentsWidth { | |
| 58 maxContentsWidth_ = maxContentsWidth; | |
| 59 } | |
| 60 | |
| 31 - (id)init { | 61 - (id)init { |
| 32 self = [super init]; | 62 self = [super init]; |
| 33 if (self) { | 63 if (self) { |
| 34 [self setImagePosition:NSImageLeft]; | 64 [self setImagePosition:NSImageLeft]; |
| 35 [self setBordered:NO]; | 65 [self setBordered:NO]; |
| 36 [self setButtonType:NSRadioButton]; | 66 [self setButtonType:NSRadioButton]; |
| 37 | 67 |
| 38 // Without this highlighting messes up white areas of images. | 68 // Without this highlighting messes up white areas of images. |
| 39 [self setHighlightsBy:NSNoCellMask]; | 69 [self setHighlightsBy:NSNoCellMask]; |
| 40 } | 70 } |
| 41 return self; | 71 return self; |
| 42 } | 72 } |
| 43 | 73 |
| 74 - (CGFloat)textOffset:(NSRect)cellFrame { | |
| 75 CGFloat offset = (cellFrame.size.width < maxRequiredWidth_ + kTextXOffset) ? | |
| 76 (cellFrame.size.width - maxContentsWidth_) : | |
| 77 (requiredWidth_ - contentsWidth_); | |
| 78 return kTextXOffset + std::max(offset, 0.0f); | |
| 79 } | |
| 80 | |
| 44 // The default NSButtonCell drawing leaves the image flush left and | 81 // The default NSButtonCell drawing leaves the image flush left and |
| 45 // the title next to the image. This spaces things out to line up | 82 // the title next to the image. This spaces things out to line up |
| 46 // with the star button and autocomplete field. | 83 // with the star button and autocomplete field. |
| 47 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { | 84 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { |
| 48 if ([self state] == NSOnState || [self isHighlighted]) { | 85 if ([self state] == NSOnState || [self isHighlighted]) { |
| 49 if ([self state] == NSOnState) | 86 if ([self state] == NSOnState) |
| 50 [SelectedBackgroundColor() set]; | 87 [SelectedBackgroundColor() set]; |
| 51 else | 88 else |
| 52 [HoveredBackgroundColor() set]; | 89 [HoveredBackgroundColor() set]; |
| 53 NSBezierPath* path = | 90 NSBezierPath* path = |
| 54 [NSBezierPath bezierPathWithRoundedRect:cellFrame | 91 [NSBezierPath bezierPathWithRoundedRect:cellFrame |
| 55 xRadius:kCellRoundingRadius | 92 xRadius:kCellRoundingRadius |
| 56 yRadius:kCellRoundingRadius]; | 93 yRadius:kCellRoundingRadius]; |
| 57 [path fill]; | 94 [path fill]; |
| 58 } | 95 } |
| 59 | 96 |
| 97 bool isRTL = base::i18n::IsRTL(); | |
| 98 | |
| 60 // Put the image centered vertically but in a fixed column. | 99 // Put the image centered vertically but in a fixed column. |
| 61 NSImage* image = [self image]; | 100 NSImage* image = [self image]; |
| 62 if (image) { | 101 if (image) { |
| 63 NSRect imageRect = cellFrame; | 102 NSRect imageRect = cellFrame; |
| 64 imageRect.size = [image size]; | 103 imageRect.size = [image size]; |
| 65 imageRect.origin.y += | 104 imageRect.origin.y += |
| 66 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); | 105 std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0); |
| 67 imageRect.origin.x += kImageXOffset; | 106 imageRect.origin.x += kImageXOffset; |
| 107 if (isRTL) | |
| 108 imageRect = FlipVertical(imageRect, cellFrame); | |
| 68 [image drawInRect:imageRect | 109 [image drawInRect:imageRect |
| 69 fromRect:NSZeroRect // Entire image | 110 fromRect:NSZeroRect // Entire image |
| 70 operation:NSCompositeSourceOver | 111 operation:NSCompositeSourceOver |
| 71 fraction:1.0 | 112 fraction:1.0 |
| 72 respectFlipped:YES | 113 respectFlipped:YES |
| 73 hints:nil]; | 114 hints:nil]; |
| 74 } | 115 } |
| 75 | 116 |
| 76 // Adjust the title position to be lined up under the field's text. | 117 // Adjust the title position to be lined up under the field's text. |
| 77 NSAttributedString* title = [self attributedTitle]; | 118 NSAttributedString* title = [self attributedTitle]; |
| 78 if (title && [title length]) { | 119 if (title && [title length]) { |
| 79 NSRect titleRect = cellFrame; | 120 NSRect titleRect = cellFrame; |
| 80 titleRect.size.width -= kTextXOffset; | 121 CGFloat textXOffset = [self textOffset:cellFrame]; |
| 81 titleRect.origin.x += kTextXOffset; | 122 titleRect.size.width -= textXOffset; |
| 123 titleRect.origin.x += textXOffset; | |
| 124 | |
| 125 if (isRTL) | |
| 126 titleRect = FlipVertical(titleRect, cellFrame); | |
| 82 [self drawTitle:title withFrame:titleRect inView:controlView]; | 127 [self drawTitle:title withFrame:titleRect inView:controlView]; |
| 83 } | 128 } |
| 84 } | 129 } |
| 85 | 130 |
| 86 @end | 131 @end |
| OLD | NEW |