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 the longest label. |
| 55 NSRect labelFrame = [emailField frame]; |
| 56 if (NSWidth([userField frame]) > NSWidth(labelFrame)) |
| 57 labelFrame.size.width = NSWidth([userField frame]); |
| 58 [self setFrameSize:NSMakeSize( |
| 59 NSMaxX(labelFrame) + NSMaxX(imageRect), |
| 60 NSMaxY(labelFrame))]; |
| 61 } |
| 62 return self; |
| 63 } |
| 64 |
| 65 - (NSTextField*)addLabelWithFrame:(NSPoint)origin |
| 66 labelText:(const string16&)labelText { |
| 67 NSRect labelFrame = NSZeroRect; |
| 68 labelFrame.origin = origin; |
| 69 scoped_nsobject<NSTextField> label( |
| 70 [[NSTextField alloc] initWithFrame:labelFrame]); |
| 71 [label setStringValue:base::SysUTF16ToNSString(labelText)]; |
| 72 [label setEditable:NO]; |
| 73 [label setBordered:NO]; |
| 74 [label setDrawsBackground:NO]; |
| 75 [label setFont:[NSFont menuFontOfSize:0]]; |
| 76 [label sizeToFit]; |
| 77 [self addSubview:label]; |
| 78 return label.autorelease(); |
| 79 } |
| 80 |
| 81 - (BOOL)isFlipped { |
| 82 return YES; |
| 83 } |
| 84 |
| 85 @end |
OLD | NEW |