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 "chrome/browser/ui/views/profiles/profile_chooser_view.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/geometry/vector2d.h"
17 #include "ui/views/border.h"
18 #include "ui/views/controls/button/label_button_border.h"
19 #include "ui/views/painter.h"
23 scoped_ptr
<views::Border
> CreateBorder(const int normal_image_set
[],
24 const int hot_image_set
[],
25 const int pushed_image_set
[]) {
26 scoped_ptr
<views::LabelButtonBorder
> border(
27 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON
));
28 border
->SetPainter(false, views::Button::STATE_NORMAL
,
29 views::Painter::CreateImageGridPainter(normal_image_set
));
30 border
->SetPainter(false, views::Button::STATE_HOVERED
,
31 views::Painter::CreateImageGridPainter(hot_image_set
));
32 border
->SetPainter(false, views::Button::STATE_PRESSED
,
33 views::Painter::CreateImageGridPainter(pushed_image_set
));
35 const int kLeftRightInset
= 8;
36 const int kTopInset
= 2;
37 const int kBottomInset
= 4;
38 border
->set_insets(gfx::Insets(kTopInset
, kLeftRightInset
,
39 kBottomInset
, kLeftRightInset
));
46 NewAvatarButton::NewAvatarButton(views::ButtonListener
* listener
,
47 AvatarButtonStyle button_style
,
49 : LabelButton(listener
, base::string16()),
51 has_auth_error_(false),
52 suppress_mouse_released_action_(false) {
53 set_triggerable_event_flags(
54 ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_RIGHT_MOUSE_BUTTON
);
55 set_animate_on_state_change(false);
56 SetTextColor(views::Button::STATE_NORMAL
, SK_ColorWHITE
);
57 SetTextColor(views::Button::STATE_HOVERED
, SK_ColorWHITE
);
58 SetTextColor(views::Button::STATE_PRESSED
, SK_ColorWHITE
);
59 SetTextSubpixelRenderingEnabled(false);
60 SetHorizontalAlignment(gfx::ALIGN_CENTER
);
62 // The largest text height that fits in the button. If the font list height
63 // is larger than this, it will be shrunk to match it.
64 // TODO(noms): Calculate this constant algorithmically from the button's size.
65 const int kDisplayFontHeight
= 16;
66 SetFontList(GetFontList().DeriveWithHeightUpperBound(kDisplayFontHeight
));
68 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
69 if (button_style
== THEMED_BUTTON
) {
70 const int kNormalImageSet
[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL
);
71 const int kHotImageSet
[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER
);
72 const int kPushedImageSet
[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED
);
74 SetBorder(CreateBorder(kNormalImageSet
, kHotImageSet
, kPushedImageSet
));
76 *rb
->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_AVATAR
).ToImageSkia();
78 } else if (base::win::GetVersion() >= base::win::VERSION_WIN8
||
79 browser
->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
) {
80 const int kNormalImageSet
[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL
);
81 const int kHotImageSet
[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER
);
82 const int kPushedImageSet
[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED
);
84 SetBorder(CreateBorder(kNormalImageSet
, kHotImageSet
, kPushedImageSet
));
86 *rb
->GetImageNamed(IDR_AVATAR_METRO_BUTTON_AVATAR
).ToImageSkia();
89 const int kNormalImageSet
[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL
);
90 const int kHotImageSet
[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER
);
91 const int kPushedImageSet
[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED
);
93 SetBorder(CreateBorder(kNormalImageSet
, kHotImageSet
, kPushedImageSet
));
95 *rb
->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_AVATAR
).ToImageSkia();
98 g_browser_process
->profile_manager()->GetProfileInfoCache().AddObserver(this);
100 // Subscribe to authentication error changes so that the avatar button can
101 // update itself. Note that guest mode profiles won't have a token service.
102 SigninErrorController
* error
=
103 profiles::GetSigninErrorController(browser_
->profile());
105 error
->AddObserver(this);
106 OnErrorChanged(); // This calls Update().
113 NewAvatarButton::~NewAvatarButton() {
114 g_browser_process
->profile_manager()->
115 GetProfileInfoCache().RemoveObserver(this);
116 SigninErrorController
* error
=
117 profiles::GetSigninErrorController(browser_
->profile());
119 error
->RemoveObserver(this);
122 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent
& event
) {
123 // Prevent the bubble from being re-shown if it's already showing.
124 suppress_mouse_released_action_
= ProfileChooserView::IsShowing();
125 return LabelButton::OnMousePressed(event
);
128 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent
& event
) {
129 if (suppress_mouse_released_action_
)
130 suppress_mouse_released_action_
= false;
132 LabelButton::OnMouseReleased(event
);
135 void NewAvatarButton::OnProfileAdded(const base::FilePath
& profile_path
) {
139 void NewAvatarButton::OnProfileWasRemoved(
140 const base::FilePath
& profile_path
,
141 const base::string16
& profile_name
) {
142 // If deleting the active profile, don't bother updating the avatar
143 // button, as the browser window is being closed anyway.
144 if (browser_
->profile()->GetPath() != profile_path
)
148 void NewAvatarButton::OnProfileNameChanged(
149 const base::FilePath
& profile_path
,
150 const base::string16
& old_profile_name
) {
151 if (browser_
->profile()->GetPath() == profile_path
)
155 void NewAvatarButton::OnProfileSupervisedUserIdChanged(
156 const base::FilePath
& profile_path
) {
157 if (browser_
->profile()->GetPath() == profile_path
)
161 void NewAvatarButton::OnErrorChanged() {
162 // If there is an error, show an warning icon.
163 const SigninErrorController
* error
=
164 profiles::GetSigninErrorController(browser_
->profile());
165 has_auth_error_
= error
&& error
->HasError();
170 void NewAvatarButton::Update() {
171 const ProfileInfoCache
& cache
=
172 g_browser_process
->profile_manager()->GetProfileInfoCache();
174 // If we have a single local profile, then use the generic avatar
175 // button instead of the profile name. Never use the generic button if
176 // the active profile is Guest.
177 bool use_generic_button
= (!browser_
->profile()->IsGuestSession() &&
178 cache
.GetNumberOfProfiles() == 1 &&
179 cache
.GetUserNameOfProfileAtIndex(0).empty());
181 SetText(use_generic_button
? base::string16() :
182 profiles::GetAvatarButtonTextForProfile(browser_
->profile()));
184 // If the button has no text, clear the text shadows to make sure the
185 // image is centered correctly.
188 ? gfx::ShadowValues()
190 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f
, SK_ColorDKGRAY
)));
192 // We want the button to resize if the new text is shorter.
193 SetMinSize(gfx::Size());
195 if (use_generic_button
) {
196 SetImage(views::Button::STATE_NORMAL
, generic_avatar_
);
197 } else if (has_auth_error_
) {
198 SetImage(views::Button::STATE_NORMAL
,
199 *ui::ResourceBundle::GetSharedInstance().GetImageNamed(
200 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR
).ToImageSkia());
202 SetImage(views::Button::STATE_NORMAL
, gfx::ImageSkia());
205 // If we are not using the generic button, then reset the spacing between
206 // the text and the possible authentication error icon.
207 const int kDefaultImageTextSpacing
= 5;
208 SetImageLabelSpacing(use_generic_button
? 0 : kDefaultImageTextSpacing
);
210 PreferredSizeChanged();