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 #include "chrome/browser/ui/views/profiles/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 "chrome/browser/ui/browser.h"
12 #include "grit/generated_resources.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/color_utils.h"
18 #include "ui/gfx/font_list.h"
19 #include "ui/gfx/text_elider.h"
20 #include "ui/views/border.h"
21 #include "ui/views/painter.h"
25 // Text padding within the button border.
26 const int kInset = 10;
28 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[],
29 const int hot_image_set[],
30 const int pushed_image_set[]) {
31 scoped_ptr<views::TextButtonDefaultBorder> border(
32 new views::TextButtonDefaultBorder());
34 border->SetInsets(gfx::Insets(kInset, kInset, kInset, kInset));
35 border->set_normal_painter(
36 views::Painter::CreateImageGridPainter(normal_image_set));
37 border->set_hot_painter(
38 views::Painter::CreateImageGridPainter(hot_image_set));
39 border->set_pushed_painter(
40 views::Painter::CreateImageGridPainter(pushed_image_set));
42 return border.PassAs<views::Border>();
45 base::string16 GetElidedText(const base::string16& original_text) {
46 // Maximum characters the button can be before the text will get elided.
47 const int kMaxCharactersToDisplay = 15;
49 const gfx::FontList font_list;
50 return gfx::ElideText(
53 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay),
59 NewAvatarButton::NewAvatarButton(
60 views::ButtonListener* listener,
61 const base::string16& profile_name,
62 AvatarButtonStyle button_style,
64 : MenuButton(listener, GetElidedText(profile_name), NULL, true),
66 set_animate_on_state_change(false);
68 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
72 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8;
75 if (button_style == THEMED_BUTTON) {
76 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL);
77 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER);
78 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED);
80 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
82 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia());
84 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL);
85 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER);
86 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED);
88 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
90 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia());
92 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL);
93 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER);
94 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED);
96 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
98 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia());
101 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this);
105 NewAvatarButton::~NewAvatarButton() {
106 g_browser_process->profile_manager()->
107 GetProfileInfoCache().RemoveObserver(this);
110 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) {
111 // From TextButton::PaintButton, draw everything but the text.
112 OnPaintBackground(canvas);
113 OnPaintBorder(canvas);
114 views::Painter::PaintFocusPainter(this, canvas, focus_painter());
117 // In RTL languages the marker gets drawn leftmost, so account for its offset.
118 if (base::i18n::IsRTL())
119 rect = gfx::Rect(-kInset, 0, size().width(), size().height());
121 rect = gfx::Rect(kInset, 0, size().width(), size().height());
123 canvas->DrawStringRectWithHalo(
129 gfx::Canvas::NO_SUBPIXEL_RENDERING);
131 // From MenuButton::PaintButton, paint the marker
132 PaintMenuMarker(canvas);
135 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) {
136 UpdateAvatarButtonAndRelayoutParent();
139 void NewAvatarButton::OnProfileWasRemoved(
140 const base::FilePath& profile_path,
141 const base::string16& profile_name) {
142 UpdateAvatarButtonAndRelayoutParent();
145 void NewAvatarButton::OnProfileNameChanged(
146 const base::FilePath& profile_path,
147 const base::string16& old_profile_name) {
148 UpdateAvatarButtonAndRelayoutParent();
151 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() {
152 // We want the button to resize if the new text is shorter.
154 SetText(GetElidedText(
155 profiles::GetAvatarNameForProfile(browser_->profile())));
157 // Because the width of the button might have changed, the parent browser
158 // frame needs to recalculate the button bounds and redraw it.