Index: ui/app_list/cocoa/app_list_menu_cocoa.mm |
diff --git a/ui/app_list/cocoa/app_list_menu_cocoa.mm b/ui/app_list/cocoa/app_list_menu_cocoa.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..567581e1ef9e660d71634efa3bc17add139f191e |
--- /dev/null |
+++ b/ui/app_list/cocoa/app_list_menu_cocoa.mm |
@@ -0,0 +1,171 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "ui/app_list/cocoa/app_list_menu_cocoa.h" |
+ |
+#include "base/logging.h" |
+#include "base/strings/sys_string_conversions.h" |
+#include "grit/ui_resources.h" |
+#include "ui/app_list/app_list_view_delegate.h" |
+#include "ui/base/resource/resource_bundle.h" |
+ |
+namespace { |
+ |
+// Padding either side of the user indicator icon. This should be the same as |
+// the horizontal padding OSX puts around menu items, to ensure layout is even. |
+const CGFloat kIndicatorIconPadding = 2; |
+ |
+} |
+ |
+// A custom NSPopUpButtonCell that permits a hover image, and draws only an |
+// image in its frame; no border, bezel or drop-down arrow. |
+@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
|
+ @private |
+ NSImage* hoverImage_; |
+ BOOL hovered_; |
+} |
+ |
+@property(retain, nonatomic) NSImage* hoverImage; |
+@property(assign, nonatomic) BOOL hovered; |
+ |
+@end |
+ |
+// The first item in the menu is the current user indicator. It shows a static |
+// icon in the margin, the profile name, and the signed-in email address. |
+@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 (
|
+ |
+- (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate; |
+ |
+- (NSTextField*)addLabelWithFrame:(NSRect)frame |
+ labelText:(const string16&)labelText; |
+ |
+@end |
+ |
+@implementation AppListMenuCocoa |
+ |
++ (NSView*)makeCurrentUserView:(app_list::AppListViewDelegate*)delegate { |
+ scoped_nsobject<CurrentUserMenuItemView> itemView( |
+ [[CurrentUserMenuItemView alloc] initWithDelegate:delegate]); |
+ return itemView.autorelease(); |
+} |
+ |
++ (Class)cellClass { |
+ return [AppListMenuCocoaCell class]; |
+} |
+ |
+- (id)initWithFrame:(NSRect)frameRect { |
+ if ((self = [super initWithFrame:frameRect |
+ pullsDown:YES])) { |
+ trackingArea_.reset( |
+ [[CrTrackingArea alloc] initWithRect:NSZeroRect |
+ options:NSTrackingInVisibleRect | |
+ NSTrackingMouseEnteredAndExited | |
+ NSTrackingActiveInKeyWindow |
+ owner:self |
+ userInfo:nil]); |
+ [self addTrackingArea:trackingArea_.get()]; |
+ |
+ [[self cell] setUsesItemFromMenu:NO]; |
+ |
+ ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
+ scoped_nsobject<NSMenuItem> buttonItem([[NSMenuItem alloc] init]); |
+ [buttonItem setImage:rb.GetNativeImageNamed( |
+ IDR_APP_LIST_TOOLS_NORMAL).AsNSImage()]; |
+ [[self cell] setMenuItem:buttonItem]; |
+ |
+ [[self cell] setAlternateImage:rb.GetNativeImageNamed( |
+ IDR_APP_LIST_TOOLS_PRESSED).AsNSImage()]; |
+ [[self cell] setHoverImage:rb.GetNativeImageNamed( |
+ IDR_APP_LIST_TOOLS_HOVER).AsNSImage()]; |
+ } |
+ return self; |
+} |
+ |
+- (void)mouseEntered:(NSEvent*)theEvent { |
+ [[self cell] setHovered:YES]; |
+ [self setNeedsDisplay:YES]; |
+} |
+ |
+- (void)mouseExited:(NSEvent*)theEvent { |
+ [[self cell] setHovered:NO]; |
+ [self setNeedsDisplay:YES]; |
+} |
+ |
+@end |
+ |
+@implementation AppListMenuCocoaCell |
+ |
+@synthesize hoverImage = hoverImage_; |
+@synthesize hovered = hovered_; |
+ |
+- (void)drawWithFrame:(NSRect)cellFrame |
+ inView:(NSView*)controlView { |
+ NSImage* image = [self isHighlighted] ? |
+ [self alternateImage] : |
+ (hovered_ ? [self hoverImage] : [self image]); |
+ [image drawInRect:cellFrame |
+ fromRect:NSZeroRect |
+ operation:NSCompositeSourceOver |
+ fraction:1.0 |
+ respectFlipped:YES |
+ hints:nil]; |
+} |
+ |
+@end |
+ |
+@implementation CurrentUserMenuItemView |
+ |
+// Adds a text label in the custom view in the menu showing the current user. |
+- (NSTextField*)addLabelWithFrame:(NSRect)frame |
+ labelText:(const string16&)labelText { |
+ scoped_nsobject<NSTextField> label([[NSTextField alloc] initWithFrame:frame]); |
+ [label setStringValue:base::SysUTF16ToNSString(labelText)]; |
+ [label setEditable:NO]; |
+ [label setBordered:NO]; |
+ [label sizeToFit]; |
+ [self addSubview:label]; |
+ return label.autorelease(); |
+} |
+ |
+- (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate { |
+ DCHECK(delegate); |
+ if ((self = [super initWithFrame:NSZeroRect])) { |
+ scoped_nsobject<NSImageView> userIndicator( |
+ [[NSImageView alloc] initWithFrame:NSZeroRect]); |
+ NSImage* indicatorIcon = ui::ResourceBundle::GetSharedInstance(). |
+ GetNativeImageNamed(IDR_APP_LIST_USER_INDICATOR).AsNSImage(); |
+ [userIndicator setImage:indicatorIcon]; |
+ NSRect imageRect = NSZeroRect; |
+ imageRect.size = [indicatorIcon size]; |
+ imageRect = |
+ NSInsetRect(imageRect, -kIndicatorIconPadding, -kIndicatorIconPadding); |
+ |
+ NSRect labelFrame = NSMakeRect(NSWidth(imageRect), 0, 0, 0); |
+ NSTextField* emailField = |
+ [self addLabelWithFrame:labelFrame |
+ labelText:delegate->GetCurrentUserEmail()]; |
+ [emailField setEnabled:NO]; |
+ |
+ labelFrame.origin.y = NSMaxY([emailField frame]); |
+ NSTextField* userField = |
+ [self addLabelWithFrame:labelFrame |
+ labelText:delegate->GetCurrentUserName()]; |
+ |
+ // Size the container view to fit, and ensure the longest label appears |
+ // centered (if it is the longest item in the menu). |
+ NSRect viewRect = NSUnionRect([emailField frame], [userField frame]); |
+ viewRect.size.width += NSMinX(viewRect) - kIndicatorIconPadding; |
+ viewRect = NSUnionRect(viewRect, imageRect); |
+ [self setFrameSize:viewRect.size]; |
+ |
+ // Align the user indicator icon with the top of the view. |
+ imageRect.origin.y = NSHeight(viewRect) - NSHeight(imageRect); |
+ imageRect.origin.x = kIndicatorIconPadding; |
+ [userIndicator setFrame:imageRect]; |
+ [self addSubview:userIndicator]; |
+ } |
+ return self; |
+} |
+ |
+@end |