Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #import "ui/app_list/cocoa/app_list_menu_cocoa.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/strings/sys_string_conversions.h" | |
| 9 #include "grit/ui_resources.h" | |
| 10 #include "ui/app_list/app_list_view_delegate.h" | |
| 11 #include "ui/base/resource/resource_bundle.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Padding either side of the user indicator icon. This should be the same as | |
| 16 // the horizontal padding OSX puts around menu items, to ensure layout is even. | |
| 17 const CGFloat kIndicatorIconPadding = 2; | |
| 18 | |
| 19 } | |
| 20 | |
| 21 // A custom NSPopUpButtonCell that permits a hover image, and draws only an | |
| 22 // image in its frame; no border, bezel or drop-down arrow. | |
| 23 @interface AppListMenuCocoaCell : NSPopUpButtonCell { | |
|
sail
2013/05/31 01:20:16
How about moving this to ui/base/cocoa and making
tapted
2013/05/31 06:05:13
I tried using HoverImageButton, and incorporating
Robert Sesek
2013/05/31 18:25:35
I think this is because HoverButton isn't examinin
| |
| 24 @private | |
| 25 NSImage* hoverImage_; | |
| 26 BOOL hovered_; | |
| 27 } | |
| 28 | |
| 29 @property(retain, nonatomic) NSImage* hoverImage; | |
| 30 @property(assign, nonatomic) BOOL hovered; | |
| 31 | |
| 32 @end | |
| 33 | |
| 34 // The first item in the menu is the current user indicator. It shows a static | |
| 35 // icon in the margin, the profile name, and the signed-in email address. | |
| 36 @interface CurrentUserMenuItemView : NSView; | |
|
sail
2013/05/31 01:20:16
Can we just use a normal menu item and set it's ic
tapted
2013/05/31 06:05:13
I gave this a go too, using an attributed string (
| |
| 37 | |
| 38 - (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate; | |
| 39 | |
| 40 - (NSTextField*)addLabelWithFrame:(NSRect)frame | |
| 41 labelText:(const string16&)labelText; | |
| 42 | |
| 43 @end | |
| 44 | |
| 45 @implementation AppListMenuCocoa | |
| 46 | |
| 47 + (NSView*)makeCurrentUserView:(app_list::AppListViewDelegate*)delegate { | |
| 48 scoped_nsobject<CurrentUserMenuItemView> itemView( | |
| 49 [[CurrentUserMenuItemView alloc] initWithDelegate:delegate]); | |
| 50 return itemView.autorelease(); | |
| 51 } | |
| 52 | |
| 53 + (Class)cellClass { | |
| 54 return [AppListMenuCocoaCell class]; | |
| 55 } | |
| 56 | |
| 57 - (id)initWithFrame:(NSRect)frameRect { | |
| 58 if ((self = [super initWithFrame:frameRect | |
| 59 pullsDown:YES])) { | |
| 60 trackingArea_.reset( | |
| 61 [[CrTrackingArea alloc] initWithRect:NSZeroRect | |
| 62 options:NSTrackingInVisibleRect | | |
| 63 NSTrackingMouseEnteredAndExited | | |
| 64 NSTrackingActiveInKeyWindow | |
| 65 owner:self | |
| 66 userInfo:nil]); | |
| 67 [self addTrackingArea:trackingArea_.get()]; | |
| 68 | |
| 69 [[self cell] setUsesItemFromMenu:NO]; | |
| 70 | |
| 71 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 72 scoped_nsobject<NSMenuItem> buttonItem([[NSMenuItem alloc] init]); | |
| 73 [buttonItem setImage:rb.GetNativeImageNamed( | |
| 74 IDR_APP_LIST_TOOLS_NORMAL).AsNSImage()]; | |
| 75 [[self cell] setMenuItem:buttonItem]; | |
| 76 | |
| 77 [[self cell] setAlternateImage:rb.GetNativeImageNamed( | |
| 78 IDR_APP_LIST_TOOLS_PRESSED).AsNSImage()]; | |
| 79 [[self cell] setHoverImage:rb.GetNativeImageNamed( | |
| 80 IDR_APP_LIST_TOOLS_HOVER).AsNSImage()]; | |
| 81 } | |
| 82 return self; | |
| 83 } | |
| 84 | |
| 85 - (void)mouseEntered:(NSEvent*)theEvent { | |
| 86 [[self cell] setHovered:YES]; | |
| 87 [self setNeedsDisplay:YES]; | |
| 88 } | |
| 89 | |
| 90 - (void)mouseExited:(NSEvent*)theEvent { | |
| 91 [[self cell] setHovered:NO]; | |
| 92 [self setNeedsDisplay:YES]; | |
| 93 } | |
| 94 | |
| 95 @end | |
| 96 | |
| 97 @implementation AppListMenuCocoaCell | |
| 98 | |
| 99 @synthesize hoverImage = hoverImage_; | |
| 100 @synthesize hovered = hovered_; | |
| 101 | |
| 102 - (void)drawWithFrame:(NSRect)cellFrame | |
| 103 inView:(NSView*)controlView { | |
| 104 NSImage* image = [self isHighlighted] ? | |
| 105 [self alternateImage] : | |
| 106 (hovered_ ? [self hoverImage] : [self image]); | |
| 107 [image drawInRect:cellFrame | |
| 108 fromRect:NSZeroRect | |
| 109 operation:NSCompositeSourceOver | |
| 110 fraction:1.0 | |
| 111 respectFlipped:YES | |
| 112 hints:nil]; | |
| 113 } | |
| 114 | |
| 115 @end | |
| 116 | |
| 117 @implementation CurrentUserMenuItemView | |
| 118 | |
| 119 // Adds a text label in the custom view in the menu showing the current user. | |
| 120 - (NSTextField*)addLabelWithFrame:(NSRect)frame | |
| 121 labelText:(const string16&)labelText { | |
| 122 scoped_nsobject<NSTextField> label([[NSTextField alloc] initWithFrame:frame]); | |
| 123 [label setStringValue:base::SysUTF16ToNSString(labelText)]; | |
| 124 [label setEditable:NO]; | |
| 125 [label setBordered:NO]; | |
| 126 [label sizeToFit]; | |
| 127 [self addSubview:label]; | |
| 128 return label.autorelease(); | |
| 129 } | |
| 130 | |
| 131 - (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate { | |
| 132 DCHECK(delegate); | |
| 133 if ((self = [super initWithFrame:NSZeroRect])) { | |
| 134 scoped_nsobject<NSImageView> userIndicator( | |
| 135 [[NSImageView alloc] initWithFrame:NSZeroRect]); | |
| 136 NSImage* indicatorIcon = ui::ResourceBundle::GetSharedInstance(). | |
| 137 GetNativeImageNamed(IDR_APP_LIST_USER_INDICATOR).AsNSImage(); | |
| 138 [userIndicator setImage:indicatorIcon]; | |
| 139 NSRect imageRect = NSZeroRect; | |
| 140 imageRect.size = [indicatorIcon size]; | |
| 141 imageRect = | |
| 142 NSInsetRect(imageRect, -kIndicatorIconPadding, -kIndicatorIconPadding); | |
| 143 | |
| 144 NSRect labelFrame = NSMakeRect(NSWidth(imageRect), 0, 0, 0); | |
| 145 NSTextField* emailField = | |
| 146 [self addLabelWithFrame:labelFrame | |
| 147 labelText:delegate->GetCurrentUserEmail()]; | |
| 148 [emailField setEnabled:NO]; | |
| 149 | |
| 150 labelFrame.origin.y = NSMaxY([emailField frame]); | |
| 151 NSTextField* userField = | |
| 152 [self addLabelWithFrame:labelFrame | |
| 153 labelText:delegate->GetCurrentUserName()]; | |
| 154 | |
| 155 // Size the container view to fit, and ensure the longest label appears | |
| 156 // centered (if it is the longest item in the menu). | |
| 157 NSRect viewRect = NSUnionRect([emailField frame], [userField frame]); | |
| 158 viewRect.size.width += NSMinX(viewRect) - kIndicatorIconPadding; | |
| 159 viewRect = NSUnionRect(viewRect, imageRect); | |
| 160 [self setFrameSize:viewRect.size]; | |
| 161 | |
| 162 // Align the user indicator icon with the top of the view. | |
| 163 imageRect.origin.y = NSHeight(viewRect) - NSHeight(imageRect); | |
| 164 imageRect.origin.x = kIndicatorIconPadding; | |
| 165 [userIndicator setFrame:imageRect]; | |
| 166 [self addSubview:userIndicator]; | |
| 167 } | |
| 168 return self; | |
| 169 } | |
| 170 | |
| 171 @end | |
| OLD | NEW |