Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / new_avatar_button.cc
blob0b7a2134ea59c66c1cec2b61198cba0c66c641de
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 views::TextButtonDefaultBorder* CreateBorder(const int normal_image_set[],
28 const int hot_image_set[],
29 const int pushed_image_set[]) {
30 views::TextButtonDefaultBorder* border = new views::TextButtonDefaultBorder();
32 border->SetInsets(gfx::Insets(kInset, kInset, kInset, kInset));
33 border->set_normal_painter(
34 views::Painter::CreateImageGridPainter(normal_image_set));
35 border->set_hot_painter(
36 views::Painter::CreateImageGridPainter(hot_image_set));
37 border->set_pushed_painter(
38 views::Painter::CreateImageGridPainter(pushed_image_set));
40 return border;
43 base::string16 GetElidedText(const base::string16& original_text) {
44 // Maximum characters the button can be before the text will get elided.
45 const int kMaxCharactersToDisplay = 15;
47 const gfx::FontList font_list;
48 return gfx::ElideText(
49 original_text,
50 font_list,
51 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay),
52 gfx::ELIDE_AT_END);
55 } // namespace
57 NewAvatarButton::NewAvatarButton(
58 views::ButtonListener* listener,
59 const base::string16& profile_name,
60 AvatarButtonStyle button_style,
61 Browser* browser)
62 : MenuButton(listener, GetElidedText(profile_name), NULL, true),
63 browser_(browser) {
64 set_animate_on_state_change(false);
66 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
68 bool is_win8 = false;
69 #if defined(OS_WIN)
70 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8;
71 #endif
73 if (button_style == THEMED_BUTTON) {
74 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL);
75 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER);
76 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED);
78 set_border(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
79 set_menu_marker(
80 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia());
81 } else if (is_win8) {
82 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL);
83 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER);
84 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED);
86 set_border(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
87 set_menu_marker(
88 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia());
89 } else {
90 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL);
91 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER);
92 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED);
94 set_border(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
95 set_menu_marker(
96 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia());
99 avatar_menu_.reset(new AvatarMenu(
100 &g_browser_process->profile_manager()->GetProfileInfoCache(),
101 this,
102 browser_));
103 avatar_menu_->RebuildMenu();
105 SchedulePaint();
108 NewAvatarButton::~NewAvatarButton() {
111 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) {
112 // From TextButton::PaintButton, draw everything but the text.
113 OnPaintBackground(canvas);
114 OnPaintBorder(canvas);
115 views::Painter::PaintFocusPainter(this, canvas, focus_painter());
117 gfx::Rect rect;
118 // In RTL languages the marker gets drawn leftmost, so account for its offset.
119 if (base::i18n::IsRTL())
120 rect = gfx::Rect(-kInset, 0, size().width(), size().height());
121 else
122 rect = gfx::Rect(kInset, 0, size().width(), size().height());
124 canvas->DrawStringRectWithHalo(
125 text(),
126 gfx::FontList(),
127 SK_ColorWHITE,
128 SK_ColorDKGRAY,
129 rect,
130 gfx::Canvas::NO_SUBPIXEL_RENDERING);
132 // From MenuButton::PaintButton, paint the marker
133 PaintMenuMarker(canvas);
136 void NewAvatarButton::OnAvatarMenuChanged(AvatarMenu* avatar_menu) {
137 // We want the button to resize if the new text is shorter.
138 ClearMaxTextSize();
139 SetText(GetElidedText(profiles::GetActiveProfileDisplayName(browser_)));
141 // Because the width of the button might have changed, the parent browser
142 // frame needs to recalculate the button bounds and redraw it.
143 parent()->Layout();