BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / views / profiles / new_avatar_button.cc
blob1e940c03e19a2ee3419583cab5e693e3979bd5d3
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/gfx/paint_vector_icon.h"
18 #include "ui/gfx/vector_icons_public.h"
19 #include "ui/native_theme/common_theme.h"
20 #include "ui/native_theme/native_theme.h"
21 #include "ui/views/border.h"
22 #include "ui/views/controls/button/label_button_border.h"
23 #include "ui/views/painter.h"
25 namespace {
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::LabelButtonAssetBorder> border(
31 new views::LabelButtonAssetBorder(views::Button::STYLE_TEXTBUTTON));
32 border->SetPainter(false, views::Button::STATE_NORMAL,
33 views::Painter::CreateImageGridPainter(normal_image_set));
34 border->SetPainter(false, views::Button::STATE_HOVERED,
35 views::Painter::CreateImageGridPainter(hot_image_set));
36 border->SetPainter(false, views::Button::STATE_PRESSED,
37 views::Painter::CreateImageGridPainter(pushed_image_set));
39 const int kLeftRightInset = 8;
40 const int kTopInset = 2;
41 const int kBottomInset = 4;
42 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset,
43 kBottomInset, kLeftRightInset));
45 return border.Pass();
48 } // namespace
50 NewAvatarButton::NewAvatarButton(views::ButtonListener* listener,
51 AvatarButtonStyle button_style,
52 Browser* browser)
53 : LabelButton(listener, base::string16()),
54 browser_(browser),
55 has_auth_error_(false),
56 suppress_mouse_released_action_(false) {
57 set_triggerable_event_flags(
58 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON);
59 set_animate_on_state_change(false);
60 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE);
61 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE);
62 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE);
63 SetTextSubpixelRenderingEnabled(false);
64 SetHorizontalAlignment(gfx::ALIGN_CENTER);
66 // The largest text height that fits in the button. If the font list height
67 // is larger than this, it will be shrunk to match it.
68 // TODO(noms): Calculate this constant algorithmically from the button's size.
69 const int kDisplayFontHeight = 16;
70 SetFontList(GetFontList().DeriveWithHeightUpperBound(kDisplayFontHeight));
72 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
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 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
79 generic_avatar_ =
80 *rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_AVATAR).ToImageSkia();
81 #if defined(OS_WIN)
82 } else if (base::win::GetVersion() >= base::win::VERSION_WIN8 ||
83 browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) {
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));
89 generic_avatar_ =
90 *rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_AVATAR).ToImageSkia();
91 #endif
92 } else {
93 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL);
94 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER);
95 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED);
97 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
98 generic_avatar_ =
99 *rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_AVATAR).ToImageSkia();
102 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this);
104 // Subscribe to authentication error changes so that the avatar button can
105 // update itself. Note that guest mode profiles won't have a token service.
106 SigninErrorController* error =
107 profiles::GetSigninErrorController(browser_->profile());
108 if (error) {
109 error->AddObserver(this);
110 OnErrorChanged(); // This calls Update().
111 } else {
112 Update();
114 SchedulePaint();
117 NewAvatarButton::~NewAvatarButton() {
118 g_browser_process->profile_manager()->
119 GetProfileInfoCache().RemoveObserver(this);
120 SigninErrorController* error =
121 profiles::GetSigninErrorController(browser_->profile());
122 if (error)
123 error->RemoveObserver(this);
126 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) {
127 // Prevent the bubble from being re-shown if it's already showing.
128 suppress_mouse_released_action_ = ProfileChooserView::IsShowing();
129 return LabelButton::OnMousePressed(event);
132 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) {
133 if (suppress_mouse_released_action_)
134 suppress_mouse_released_action_ = false;
135 else
136 LabelButton::OnMouseReleased(event);
139 void NewAvatarButton::OnGestureEvent(ui::GestureEvent* event) {
140 // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since
141 // no other UI button based on CustomButton appears to handle mouse
142 // right-click. If other cases are identified, it may make sense to move this
143 // check to CustomButton.
144 if (event->type() == ui::ET_GESTURE_LONG_PRESS)
145 NotifyClick(*event);
146 else
147 LabelButton::OnGestureEvent(event);
150 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) {
151 Update();
154 void NewAvatarButton::OnProfileWasRemoved(
155 const base::FilePath& profile_path,
156 const base::string16& profile_name) {
157 // If deleting the active profile, don't bother updating the avatar
158 // button, as the browser window is being closed anyway.
159 if (browser_->profile()->GetPath() != profile_path)
160 Update();
163 void NewAvatarButton::OnProfileNameChanged(
164 const base::FilePath& profile_path,
165 const base::string16& old_profile_name) {
166 if (browser_->profile()->GetPath() == profile_path)
167 Update();
170 void NewAvatarButton::OnProfileSupervisedUserIdChanged(
171 const base::FilePath& profile_path) {
172 if (browser_->profile()->GetPath() == profile_path)
173 Update();
176 void NewAvatarButton::OnErrorChanged() {
177 // If there is an error, show an warning icon.
178 const SigninErrorController* error =
179 profiles::GetSigninErrorController(browser_->profile());
180 has_auth_error_ = error && error->HasError();
182 Update();
185 void NewAvatarButton::Update() {
186 const ProfileInfoCache& cache =
187 g_browser_process->profile_manager()->GetProfileInfoCache();
189 // If we have a single local profile, then use the generic avatar
190 // button instead of the profile name. Never use the generic button if
191 // the active profile is Guest.
192 bool use_generic_button = (!browser_->profile()->IsGuestSession() &&
193 cache.GetNumberOfProfiles() == 1 &&
194 !cache.ProfileIsAuthenticatedAtIndex(0));
196 SetText(use_generic_button ? base::string16() :
197 profiles::GetAvatarButtonTextForProfile(browser_->profile()));
199 // If the button has no text, clear the text shadows to make sure the
200 // image is centered correctly.
201 SetTextShadows(
202 use_generic_button
203 ? gfx::ShadowValues()
204 : gfx::ShadowValues(
205 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY)));
207 // We want the button to resize if the new text is shorter.
208 SetMinSize(gfx::Size());
210 if (use_generic_button) {
211 SetImage(views::Button::STATE_NORMAL, generic_avatar_);
212 } else if (has_auth_error_) {
213 SkColor icon_color;
214 ui::CommonThemeGetSystemColor(ui::NativeTheme::kColorId_Amber, &icon_color);
215 SetImage(views::Button::STATE_NORMAL,
216 gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 13, icon_color));
217 } else {
218 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia());
221 // If we are not using the generic button, then reset the spacing between
222 // the text and the possible authentication error icon.
223 const int kDefaultImageTextSpacing = 5;
224 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing);
226 PreferredSizeChanged();