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 either side of the user indicator icon. This should be the same as | |
17 // the horizontal padding OSX puts around menu items, to ensure layout is even. | |
18 const CGFloat kIndicatorIconPadding = 2; | |
19 | |
20 } | |
21 | |
22 // The first item in the menu is the current user indicator. It shows a static | |
sail
2013/05/31 19:07:03
Don't need this comment in this file since this me
tapted
2013/06/03 12:49:18
Done.
| |
23 // icon in the margin, the profile name, and the signed-in email address. | |
24 @interface CurrentUserMenuItemView () | |
25 | |
26 - (NSTextField*)addLabelWithFrame:(NSRect)frame | |
27 labelText:(const string16&)labelText | |
28 usingFont:(NSFont*)font; | |
29 | |
30 @end | |
31 | |
32 @implementation CurrentUserMenuItemView | |
33 | |
34 // Adds a text label in the custom view in the menu showing the current user. | |
35 - (NSTextField*)addLabelWithFrame:(NSRect)frame | |
sail
2013/05/31 19:07:03
move to bottom of file?
this doesn't actually use
tapted
2013/06/03 12:49:18
Done.
| |
36 labelText:(const string16&)labelText | |
37 usingFont:(NSFont*)font { | |
38 scoped_nsobject<NSTextField> label([[NSTextField alloc] initWithFrame:frame]); | |
39 [label setStringValue:base::SysUTF16ToNSString(labelText)]; | |
40 [label setEditable:NO]; | |
41 [label setBordered:NO]; | |
42 [label setDrawsBackground:NO]; | |
43 [label setFont:font]; | |
44 [label sizeToFit]; | |
45 [self addSubview:label]; | |
46 return label.autorelease(); | |
47 } | |
48 | |
49 - (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate | |
50 usingFont:(NSFont*)font { | |
51 DCHECK(delegate); | |
52 if ((self = [super initWithFrame:NSZeroRect])) { | |
53 scoped_nsobject<NSImageView> userIndicator( | |
sail
2013/05/31 19:07:03
how about naming this userImageView?
tapted
2013/06/03 12:49:18
Done.
| |
54 [[NSImageView alloc] initWithFrame:NSZeroRect]); | |
55 NSImage* indicatorIcon = ui::ResourceBundle::GetSharedInstance(). | |
sail
2013/05/31 19:07:03
how about naming this userImage?
tapted
2013/06/03 12:49:18
Done.
| |
56 GetNativeImageNamed(IDR_APP_LIST_USER_INDICATOR).AsNSImage(); | |
57 [userIndicator setImage:indicatorIcon]; | |
58 NSRect imageRect = NSZeroRect; | |
59 imageRect.size = [indicatorIcon size]; | |
60 imageRect = | |
61 NSInsetRect(imageRect, -kIndicatorIconPadding, -kIndicatorIconPadding); | |
62 | |
63 NSRect labelFrame = NSMakeRect(NSWidth(imageRect), 0, 0, 0); | |
64 NSTextField* emailField = | |
65 [self addLabelWithFrame:labelFrame | |
66 labelText:delegate->GetCurrentUserEmail() | |
67 usingFont:font]; | |
68 [emailField setTextColor:[NSColor grayColor]]; | |
sail
2013/05/31 19:07:03
Use constants shared with the views code?
tapted
2013/06/03 12:49:18
The views code doesn't have a constant... it just
| |
69 | |
70 labelFrame.origin.y = NSMaxY([emailField frame]); | |
71 NSTextField* userField = | |
72 [self addLabelWithFrame:labelFrame | |
73 labelText:delegate->GetCurrentUserName() | |
74 usingFont:font]; | |
75 | |
76 // Size the container view to fit, and ensure the longest label appears | |
77 // centered (if it is the longest item in the menu). | |
78 NSRect viewRect = NSUnionRect([emailField frame], [userField frame]); | |
79 viewRect.size.width += NSMinX(viewRect) - kIndicatorIconPadding; | |
80 viewRect = NSUnionRect(viewRect, imageRect); | |
81 [self setFrameSize:viewRect.size]; | |
82 | |
83 // Align the user indicator icon with the top of the view. | |
84 imageRect.origin.y = NSHeight(viewRect) - NSHeight(imageRect); | |
85 imageRect.origin.x = kIndicatorIconPadding; | |
sail
2013/05/31 19:07:03
This is a bit confusing, the image view is enlarge
tapted
2013/06/03 12:49:18
Good advice! Done. That additional offset actually
| |
86 [userIndicator setFrame:imageRect]; | |
87 [self addSubview:userIndicator]; | |
88 } | |
89 return self; | |
90 } | |
91 | |
92 @end | |
OLD | NEW |