d62cf36529dcd41ba64343be2210731fa679e8bb
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / browser / avatar_button_controller.mm
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.
4
5 #import "chrome/browser/ui/cocoa/browser/avatar_button_controller.h"
6
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"
17
18 namespace {
19
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;
23
24   gfx::FontList font_list = ui::ResourceBundle::GetSharedInstance().GetFontList(
25       ui::ResourceBundle::BaseFont);
26   return base::SysUTF16ToNSString(gfx::ElideText(
27       name,
28       font_list,
29       font_list.GetExpectedTextWidth(kMaxCharactersToDisplay),
30       gfx::ELIDE_AT_END));
31 }
32
33 }  // namespace
34
35 @interface AvatarButtonController (Private)
36 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent;
37 @end
38
39 @implementation AvatarButtonController
40
41 - (id)initWithBrowser:(Browser*)browser {
42   if ((self = [super initWithBrowser:browser])) {
43     button_.reset([[NSButton alloc] initWithFrame:NSZeroRect]);
44     [self setView:button_];
45
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:)];
53
54     [self updateAvatarButtonAndLayoutParent:NO];
55   }
56   return self;
57 }
58
59 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent {
60   [button_ setTitle:GetElidedProfileName(
61       profiles::GetActiveProfileDisplayName(browser_))];
62   [button_ sizeToFit];
63
64   // Resize the container.
65   [[self view] setFrameSize:[button_ frame].size];
66   [button_ setFrameOrigin:NSMakePoint(0, 0)];
67
68   if (layoutParent) {
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()]
73         layoutSubviews];
74   }
75 }
76
77 @end