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