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