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/current_user_menu_item_view.h" | |
6 | |
7 #include "base/mac/scoped_nsobject.h" | |
8 #include "grit/ui_resources.h" | |
9 #include "ui/base/resource/resource_bundle.h" | |
10 | |
11 namespace { | |
12 | |
13 // Padding on the left of the indicator icon. | |
14 const CGFloat kMenuLeftMargin = 3; | |
15 | |
16 // Padding on the top and bottom of the menu item. | |
17 const CGFloat kMenuTopBottomPadding = 2; | |
18 | |
19 } | |
20 | |
21 @interface CurrentUserMenuItemView () | |
22 | |
23 // Adds a text label in the custom view in the menu showing the current user. | |
24 - (NSTextField*)addLabelWithFrame:(NSPoint)origin | |
25 labelText:(NSString*)labelText; | |
26 | |
27 @end | |
28 | |
29 @implementation CurrentUserMenuItemView | |
30 | |
31 - (id)initWithCurrentUser:(NSString*)userName | |
32 userEmail:(NSString*)userEmail { | |
33 if ((self = [super initWithFrame:NSZeroRect])) { | |
34 NSImage* userImage = ui::ResourceBundle::GetSharedInstance(). | |
35 GetNativeImageNamed(IDR_APP_LIST_USER_INDICATOR).AsNSImage(); | |
36 NSRect imageRect = NSMakeRect(kMenuLeftMargin, kMenuTopBottomPadding, 0, 0); | |
37 imageRect.size = [userImage size]; | |
38 base::scoped_nsobject<NSImageView> userImageView( | |
39 [[NSImageView alloc] initWithFrame:imageRect]); | |
40 [userImageView setImage:userImage]; | |
41 [self addSubview:userImageView]; | |
42 | |
43 NSPoint labelOrigin = NSMakePoint(NSMaxX(imageRect), kMenuTopBottomPadding); | |
44 NSTextField* userField = | |
45 [self addLabelWithFrame:labelOrigin | |
46 labelText:userName]; | |
47 | |
48 labelOrigin.y = NSMaxY([userField frame]); | |
49 NSTextField* emailField = | |
50 [self addLabelWithFrame:labelOrigin | |
51 labelText:userEmail]; | |
52 [emailField setTextColor:[NSColor disabledControlTextColor]]; | |
53 | |
54 // Size the container view to fit the longest label. | |
55 NSRect labelFrame = [emailField frame]; | |
56 if (NSWidth([userField frame]) > NSWidth(labelFrame)) | |
57 labelFrame.size.width = NSWidth([userField frame]); | |
58 [self setFrameSize:NSMakeSize( | |
59 NSMaxX(labelFrame) + NSMaxX(imageRect), | |
60 NSMaxY(labelFrame) + kMenuTopBottomPadding)]; | |
61 } | |
62 return self; | |
63 } | |
64 | |
65 - (NSTextField*)addLabelWithFrame:(NSPoint)origin | |
66 labelText:(NSString*)labelText { | |
67 NSRect labelFrame = NSZeroRect; | |
68 labelFrame.origin = origin; | |
69 base::scoped_nsobject<NSTextField> label( | |
70 [[NSTextField alloc] initWithFrame:labelFrame]); | |
71 [label setStringValue:labelText]; | |
72 [label setEditable:NO]; | |
73 [label setBordered:NO]; | |
74 [label setDrawsBackground:NO]; | |
75 [label setFont:[NSFont menuFontOfSize:0]]; | |
76 [label sizeToFit]; | |
77 [self addSubview:label]; | |
78 return label.autorelease(); | |
79 } | |
80 | |
81 - (BOOL)isFlipped { | |
82 return YES; | |
83 } | |
84 | |
85 @end | |
OLD | NEW |