1 // Copyright 2014 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 "chrome/browser/ui/cocoa/browser/avatar_button_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/browser/profiles/profiles_state.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
12 #include "grit/generated_resources.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/l10n/l10n_util_mac.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/text_elider.h"
20 NSString* GetElidedProfileName(const base::string16& name) {
21 // Maximum characters the button can be before the text will get elided.
22 const int kMaxCharactersToDisplay = 15;
24 gfx::FontList font_list = ui::ResourceBundle::GetSharedInstance().GetFontList(
25 ui::ResourceBundle::BaseFont);
26 return base::SysUTF16ToNSString(gfx::ElideText(
29 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay),
35 @interface AvatarButtonController (Private)
36 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent;
39 @implementation AvatarButtonController
41 - (id)initWithBrowser:(Browser*)browser {
42 if ((self = [super initWithBrowser:browser])) {
43 button_.reset([[NSButton alloc] initWithFrame:NSZeroRect]);
44 [self setView:button_];
46 [button_ setBezelStyle:NSTexturedRoundedBezelStyle];
47 [button_ setImage:ui::ResourceBundle::GetSharedInstance().
48 GetNativeImageNamed(IDR_APP_DROPARROW).ToNSImage()];
49 [button_ setImagePosition:NSImageRight];
50 [button_ setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
51 [button_ setTarget:self];
52 [button_ setAction:@selector(buttonClicked:)];
54 [self updateAvatarButtonAndLayoutParent:NO];
59 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent {
60 [button_ setTitle:GetElidedProfileName(
61 profiles::GetActiveProfileDisplayName(browser_))];
64 // Resize the container.
65 [[self view] setFrameSize:[button_ frame].size];
66 [button_ setFrameOrigin:NSMakePoint(0, 0)];
69 // Because the width of the button might have changed, the parent browser
70 // frame needs to recalculate the button bounds and redraw it.
71 [[BrowserWindowController
72 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()]