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/logging.h" | |
8 #include "base/memory/scoped_nsobject.h" | |
9 #include "base/strings/sys_string_conversions.h" | |
10 #include "grit/ui_resources.h" | |
11 #include "ui/app_list/app_list_view_delegate.h" | |
12 #include "ui/base/resource/resource_bundle.h" | |
13 | |
14 namespace { | |
15 | |
16 // Padding on the left of the indicator icon. | |
17 const CGFloat kMenuLeftMargin = 3; | |
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:(const string16&)labelText; | |
26 | |
27 @end | |
28 | |
29 @implementation CurrentUserMenuItemView | |
30 | |
31 - (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate { | |
32 DCHECK(delegate); | |
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, 0, 0, 0); | |
37 imageRect.size = [userImage size]; | |
38 scoped_nsobject<NSImageView> userImageView( | |
39 [[NSImageView alloc] initWithFrame:imageRect]); | |
40 [userImageView setImage:userImage]; | |
41 [self addSubview:userImageView]; | |
42 | |
43 NSPoint labelOrigin = NSMakePoint(NSMaxX(imageRect), 0); | |
44 NSTextField* userField = | |
45 [self addLabelWithFrame:labelOrigin | |
46 labelText:delegate->GetCurrentUserName()]; | |
47 | |
48 labelOrigin.y = NSMaxY([userField frame]); | |
49 NSTextField* emailField = | |
50 [self addLabelWithFrame:labelOrigin | |
51 labelText:delegate->GetCurrentUserEmail()]; | |
52 [emailField setTextColor:[NSColor disabledControlTextColor]]; | |
53 | |
54 // Size the container view to fit, and ensure the longest label appears | |
sail
2013/06/11 00:55:22
Don't need comma
tapted
2013/06/11 02:32:44
Done.
| |
55 // centered (if it is the longest item in the menu). | |
sail
2013/06/11 00:55:22
I don't understand how the labels would appear cen
tapted
2013/06/11 02:32:44
I guess a better way to say it would be to ensure
| |
56 NSRect labelFrame = [emailField frame]; | |
57 if (NSWidth([userField frame]) > NSWidth(labelFrame)) | |
58 labelFrame.size.width = NSWidth([userField frame]); | |
59 [self setFrameSize:NSMakeSize( | |
60 NSMaxX(labelFrame) + NSMaxX(imageRect), | |
61 NSMaxY(labelFrame))]; | |
62 } | |
63 return self; | |
64 } | |
65 | |
66 - (NSTextField*)addLabelWithFrame:(NSPoint)origin | |
67 labelText:(const string16&)labelText { | |
68 NSRect labelFrame = NSZeroRect; | |
69 labelFrame.origin = origin; | |
70 scoped_nsobject<NSTextField> label( | |
71 [[NSTextField alloc] initWithFrame:labelFrame]); | |
72 [label setStringValue:base::SysUTF16ToNSString(labelText)]; | |
73 [label setEditable:NO]; | |
74 [label setBordered:NO]; | |
75 [label setDrawsBackground:NO]; | |
76 [label setFont:[NSFont menuFontOfSize:0]]; | |
77 [label sizeToFit]; | |
78 [self addSubview:label]; | |
79 return label.autorelease(); | |
80 } | |
81 | |
82 - (BOOL)isFlipped { | |
83 return YES; | |
84 } | |
85 | |
86 @end | |
OLD | NEW |