Chromium Code Reviews| Index: ui/app_list/cocoa/current_user_menu_item_view.mm |
| diff --git a/ui/app_list/cocoa/current_user_menu_item_view.mm b/ui/app_list/cocoa/current_user_menu_item_view.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b2c6ff5318001ca12d653be186b19a654bb7cd8f |
| --- /dev/null |
| +++ b/ui/app_list/cocoa/current_user_menu_item_view.mm |
| @@ -0,0 +1,92 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ui/app_list/cocoa/current_user_menu_item_view.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_nsobject.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "grit/ui_resources.h" |
| +#include "ui/app_list/app_list_view_delegate.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +namespace { |
| + |
| +// Padding either side of the user indicator icon. This should be the same as |
| +// the horizontal padding OSX puts around menu items, to ensure layout is even. |
| +const CGFloat kIndicatorIconPadding = 2; |
| + |
| +} |
| + |
| +// 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.
|
| +// icon in the margin, the profile name, and the signed-in email address. |
| +@interface CurrentUserMenuItemView () |
| + |
| +- (NSTextField*)addLabelWithFrame:(NSRect)frame |
| + labelText:(const string16&)labelText |
| + usingFont:(NSFont*)font; |
| + |
| +@end |
| + |
| +@implementation CurrentUserMenuItemView |
| + |
| +// Adds a text label in the custom view in the menu showing the current user. |
| +- (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.
|
| + labelText:(const string16&)labelText |
| + usingFont:(NSFont*)font { |
| + scoped_nsobject<NSTextField> label([[NSTextField alloc] initWithFrame:frame]); |
| + [label setStringValue:base::SysUTF16ToNSString(labelText)]; |
| + [label setEditable:NO]; |
| + [label setBordered:NO]; |
| + [label setDrawsBackground:NO]; |
| + [label setFont:font]; |
| + [label sizeToFit]; |
| + [self addSubview:label]; |
| + return label.autorelease(); |
| +} |
| + |
| +- (id)initWithDelegate:(app_list::AppListViewDelegate*)delegate |
| + usingFont:(NSFont*)font { |
| + DCHECK(delegate); |
| + if ((self = [super initWithFrame:NSZeroRect])) { |
| + scoped_nsobject<NSImageView> userIndicator( |
|
sail
2013/05/31 19:07:03
how about naming this userImageView?
tapted
2013/06/03 12:49:18
Done.
|
| + [[NSImageView alloc] initWithFrame:NSZeroRect]); |
| + 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.
|
| + GetNativeImageNamed(IDR_APP_LIST_USER_INDICATOR).AsNSImage(); |
| + [userIndicator setImage:indicatorIcon]; |
| + NSRect imageRect = NSZeroRect; |
| + imageRect.size = [indicatorIcon size]; |
| + imageRect = |
| + NSInsetRect(imageRect, -kIndicatorIconPadding, -kIndicatorIconPadding); |
| + |
| + NSRect labelFrame = NSMakeRect(NSWidth(imageRect), 0, 0, 0); |
| + NSTextField* emailField = |
| + [self addLabelWithFrame:labelFrame |
| + labelText:delegate->GetCurrentUserEmail() |
| + usingFont:font]; |
| + [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
|
| + |
| + labelFrame.origin.y = NSMaxY([emailField frame]); |
| + NSTextField* userField = |
| + [self addLabelWithFrame:labelFrame |
| + labelText:delegate->GetCurrentUserName() |
| + usingFont:font]; |
| + |
| + // Size the container view to fit, and ensure the longest label appears |
| + // centered (if it is the longest item in the menu). |
| + NSRect viewRect = NSUnionRect([emailField frame], [userField frame]); |
| + viewRect.size.width += NSMinX(viewRect) - kIndicatorIconPadding; |
| + viewRect = NSUnionRect(viewRect, imageRect); |
| + [self setFrameSize:viewRect.size]; |
| + |
| + // Align the user indicator icon with the top of the view. |
| + imageRect.origin.y = NSHeight(viewRect) - NSHeight(imageRect); |
| + 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
|
| + [userIndicator setFrame:imageRect]; |
| + [self addSubview:userIndicator]; |
| + } |
| + return self; |
| +} |
| + |
| +@end |