NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / views / new_avatar_button.cc
blobb91c36a20e4e315ed79186c03ea13e0f226031bc
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 #include "chrome/browser/ui/views/new_avatar_button.h"
7 #include "base/win/windows_version.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/profiles/profiles_state.h"
11 #include "grit/generated_resources.h"
12 #include "grit/theme_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/color_utils.h"
17 #include "ui/gfx/font_list.h"
18 #include "ui/gfx/text_elider.h"
19 #include "ui/views/border.h"
20 #include "ui/views/painter.h"
22 namespace {
24 // Text padding within the button border.
25 const int kInset = 10;
27 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[],
28 const int hot_image_set[],
29 const int pushed_image_set[]) {
30 scoped_ptr<views::TextButtonDefaultBorder> border(
31 new views::TextButtonDefaultBorder());
33 border->SetInsets(gfx::Insets(kInset, kInset, kInset, kInset));
34 border->set_normal_painter(
35 views::Painter::CreateImageGridPainter(normal_image_set));
36 border->set_hot_painter(
37 views::Painter::CreateImageGridPainter(hot_image_set));
38 border->set_pushed_painter(
39 views::Painter::CreateImageGridPainter(pushed_image_set));
41 return border.PassAs<views::Border>();
44 base::string16 GetElidedText(const base::string16& original_text) {
45 // Maximum characters the button can be before the text will get elided.
46 const int kMaxCharactersToDisplay = 15;
48 const gfx::FontList font_list;
49 return gfx::ElideText(
50 original_text,
51 font_list,
52 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay),
53 gfx::ELIDE_AT_END);
56 } // namespace
58 NewAvatarButton::NewAvatarButton(
59 views::ButtonListener* listener,
60 const base::string16& profile_name,
61 AvatarButtonStyle button_style,
62 Browser* browser)
63 : MenuButton(listener, GetElidedText(profile_name), NULL, true),
64 browser_(browser) {
65 set_animate_on_state_change(false);
67 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
69 bool is_win8 = false;
70 #if defined(OS_WIN)
71 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8;
72 #endif
74 if (button_style == THEMED_BUTTON) {
75 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL);
76 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER);
77 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED);
79 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
80 set_menu_marker(
81 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia());
82 } else if (is_win8) {
83 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL);
84 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER);
85 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED);
87 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
88 set_menu_marker(
89 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia());
90 } else {
91 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL);
92 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER);
93 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED);
95 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
96 set_menu_marker(
97 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia());
100 avatar_menu_.reset(new AvatarMenu(
101 &g_browser_process->profile_manager()->GetProfileInfoCache(),
102 this,
103 browser_));
104 avatar_menu_->RebuildMenu();
106 SchedulePaint();
109 NewAvatarButton::~NewAvatarButton() {
112 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) {
113 // From TextButton::PaintButton, draw everything but the text.
114 OnPaintBackground(canvas);
115 OnPaintBorder(canvas);
116 views::Painter::PaintFocusPainter(this, canvas, focus_painter());
118 gfx::Rect rect;
119 // In RTL languages the marker gets drawn leftmost, so account for its offset.
120 if (base::i18n::IsRTL())
121 rect = gfx::Rect(-kInset, 0, size().width(), size().height());
122 else
123 rect = gfx::Rect(kInset, 0, size().width(), size().height());
125 canvas->DrawStringRectWithHalo(
126 text(),
127 gfx::FontList(),
128 SK_ColorWHITE,
129 SK_ColorDKGRAY,
130 rect,
131 gfx::Canvas::NO_SUBPIXEL_RENDERING);
133 // From MenuButton::PaintButton, paint the marker
134 PaintMenuMarker(canvas);
137 void NewAvatarButton::OnAvatarMenuChanged(AvatarMenu* avatar_menu) {
138 // We want the button to resize if the new text is shorter.
139 ClearMaxTextSize();
140 SetText(GetElidedText(profiles::GetActiveProfileDisplayName(browser_)));
142 // Because the width of the button might have changed, the parent browser
143 // frame needs to recalculate the button bounds and redraw it.
144 parent()->Layout();