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.
5 #import "ui/app_list/cocoa/current_user_menu_item_view.h"
7 #include "base/logging.h"
8 #include "base/mac/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"
16 // Padding on the left of the indicator icon.
17 const CGFloat kMenuLeftMargin = 3;
19 // Padding on the top and bottom of the menu item.
20 const CGFloat kMenuTopBottomPadding = 2;
24 @interface CurrentUserMenuItemView ()
26 // Adds a text label in the custom view in the menu showing the current user.
27 - (NSTextField*)addLabelWithFrame:(NSPoint)origin
28 labelText:(const string16&)labelText;
32 @implementation CurrentUserMenuItemView
34 - (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate {
36 if ((self = [super initWithFrame:NSZeroRect])) {
37 NSImage* userImage = ui::ResourceBundle::GetSharedInstance().
38 GetNativeImageNamed(IDR_APP_LIST_USER_INDICATOR).AsNSImage();
39 NSRect imageRect = NSMakeRect(kMenuLeftMargin, kMenuTopBottomPadding, 0, 0);
40 imageRect.size = [userImage size];
41 base::scoped_nsobject<NSImageView> userImageView(
42 [[NSImageView alloc] initWithFrame:imageRect]);
43 [userImageView setImage:userImage];
44 [self addSubview:userImageView];
46 NSPoint labelOrigin = NSMakePoint(NSMaxX(imageRect), kMenuTopBottomPadding);
47 NSTextField* userField =
48 [self addLabelWithFrame:labelOrigin
49 labelText:delegate->GetCurrentUserName()];
51 labelOrigin.y = NSMaxY([userField frame]);
52 NSTextField* emailField =
53 [self addLabelWithFrame:labelOrigin
54 labelText:delegate->GetCurrentUserEmail()];
55 [emailField setTextColor:[NSColor disabledControlTextColor]];
57 // Size the container view to fit the longest label.
58 NSRect labelFrame = [emailField frame];
59 if (NSWidth([userField frame]) > NSWidth(labelFrame))
60 labelFrame.size.width = NSWidth([userField frame]);
61 [self setFrameSize:NSMakeSize(
62 NSMaxX(labelFrame) + NSMaxX(imageRect),
63 NSMaxY(labelFrame) + kMenuTopBottomPadding)];
68 - (NSTextField*)addLabelWithFrame:(NSPoint)origin
69 labelText:(const string16&)labelText {
70 NSRect labelFrame = NSZeroRect;
71 labelFrame.origin = origin;
72 base::scoped_nsobject<NSTextField> label(
73 [[NSTextField alloc] initWithFrame:labelFrame]);
74 [label setStringValue:base::SysUTF16ToNSString(labelText)];
75 [label setEditable:NO];
76 [label setBordered:NO];
77 [label setDrawsBackground:NO];
78 [label setFont:[NSFont menuFontOfSize:0]];
80 [self addSubview:label];
81 return label.autorelease();