OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" | 5 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" |
6 | 6 |
7 #include "base/win/windows_version.h" | 7 #include "base/win/windows_version.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
10 #include "chrome/browser/profiles/profiles_state.h" | 10 #include "chrome/browser/profiles/profiles_state.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 kBottomInset, kLeftRightInset)); | 39 kBottomInset, kLeftRightInset)); |
40 | 40 |
41 return border.Pass(); | 41 return border.Pass(); |
42 } | 42 } |
43 | 43 |
44 } // namespace | 44 } // namespace |
45 | 45 |
46 NewAvatarButton::NewAvatarButton(views::ButtonListener* listener, | 46 NewAvatarButton::NewAvatarButton(views::ButtonListener* listener, |
47 AvatarButtonStyle button_style, | 47 AvatarButtonStyle button_style, |
48 Browser* browser) | 48 Browser* browser) |
49 : AvatarBaseButton(browser), | 49 : LabelButton(listener, base::string16()), |
50 LabelButton(listener, base::string16()), | 50 browser_(browser), |
51 has_auth_error_(false), | 51 has_auth_error_(false), |
52 suppress_mouse_released_action_(false) { | 52 suppress_mouse_released_action_(false) { |
53 set_triggerable_event_flags( | 53 set_triggerable_event_flags( |
54 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); | 54 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); |
55 set_animate_on_state_change(false); | 55 set_animate_on_state_change(false); |
56 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); | 56 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); |
57 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); | 57 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); |
58 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); | 58 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); |
59 SetTextSubpixelRenderingEnabled(false); | 59 SetTextSubpixelRenderingEnabled(false); |
60 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 60 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
(...skipping 27 matching lines...) Expand all Loading... |
88 } else { | 88 } else { |
89 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 89 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
90 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 90 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
91 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 91 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
92 | 92 |
93 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 93 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
94 generic_avatar_ = | 94 generic_avatar_ = |
95 *rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_AVATAR).ToImageSkia(); | 95 *rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_AVATAR).ToImageSkia(); |
96 } | 96 } |
97 | 97 |
| 98 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); |
| 99 |
98 // Subscribe to authentication error changes so that the avatar button can | 100 // Subscribe to authentication error changes so that the avatar button can |
99 // update itself. Note that guest mode profiles won't have a token service. | 101 // update itself. Note that guest mode profiles won't have a token service. |
100 SigninErrorController* error = | 102 SigninErrorController* error = |
101 profiles::GetSigninErrorController(browser->profile()); | 103 profiles::GetSigninErrorController(browser_->profile()); |
102 if (error) { | 104 if (error) { |
103 error->AddObserver(this); | 105 error->AddObserver(this); |
104 OnErrorChanged(); // This calls Update(). | 106 OnErrorChanged(); // This calls Update(). |
105 } else { | 107 } else { |
106 Update(); | 108 Update(); |
107 } | 109 } |
108 SchedulePaint(); | 110 SchedulePaint(); |
109 } | 111 } |
110 | 112 |
111 NewAvatarButton::~NewAvatarButton() { | 113 NewAvatarButton::~NewAvatarButton() { |
| 114 g_browser_process->profile_manager()-> |
| 115 GetProfileInfoCache().RemoveObserver(this); |
112 SigninErrorController* error = | 116 SigninErrorController* error = |
113 profiles::GetSigninErrorController(browser()->profile()); | 117 profiles::GetSigninErrorController(browser_->profile()); |
114 if (error) | 118 if (error) |
115 error->RemoveObserver(this); | 119 error->RemoveObserver(this); |
116 } | 120 } |
117 | 121 |
118 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { | 122 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { |
119 // Prevent the bubble from being re-shown if it's already showing. | 123 // Prevent the bubble from being re-shown if it's already showing. |
120 suppress_mouse_released_action_ = ProfileChooserView::IsShowing(); | 124 suppress_mouse_released_action_ = ProfileChooserView::IsShowing(); |
121 return LabelButton::OnMousePressed(event); | 125 return LabelButton::OnMousePressed(event); |
122 } | 126 } |
123 | 127 |
124 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) { | 128 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) { |
125 if (suppress_mouse_released_action_) | 129 if (suppress_mouse_released_action_) |
126 suppress_mouse_released_action_ = false; | 130 suppress_mouse_released_action_ = false; |
127 else | 131 else |
128 LabelButton::OnMouseReleased(event); | 132 LabelButton::OnMouseReleased(event); |
129 } | 133 } |
130 | 134 |
| 135 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| 136 Update(); |
| 137 } |
| 138 |
| 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) |
| 145 Update(); |
| 146 } |
| 147 |
| 148 void NewAvatarButton::OnProfileNameChanged( |
| 149 const base::FilePath& profile_path, |
| 150 const base::string16& old_profile_name) { |
| 151 if (browser_->profile()->GetPath() == profile_path) |
| 152 Update(); |
| 153 } |
| 154 |
| 155 void NewAvatarButton::OnProfileSupervisedUserIdChanged( |
| 156 const base::FilePath& profile_path) { |
| 157 if (browser_->profile()->GetPath() == profile_path) |
| 158 Update(); |
| 159 } |
| 160 |
| 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(); |
| 166 |
| 167 Update(); |
| 168 } |
| 169 |
131 void NewAvatarButton::Update() { | 170 void NewAvatarButton::Update() { |
132 const ProfileInfoCache& cache = | 171 const ProfileInfoCache& cache = |
133 g_browser_process->profile_manager()->GetProfileInfoCache(); | 172 g_browser_process->profile_manager()->GetProfileInfoCache(); |
134 | 173 |
135 // If we have a single local profile, then use the generic avatar | 174 // If we have a single local profile, then use the generic avatar |
136 // button instead of the profile name. Never use the generic button if | 175 // button instead of the profile name. Never use the generic button if |
137 // the active profile is Guest. | 176 // the active profile is Guest. |
138 bool use_generic_button = (!browser()->profile()->IsGuestSession() && | 177 bool use_generic_button = (!browser_->profile()->IsGuestSession() && |
139 cache.GetNumberOfProfiles() == 1 && | 178 cache.GetNumberOfProfiles() == 1 && |
140 cache.GetUserNameOfProfileAtIndex(0).empty()); | 179 cache.GetUserNameOfProfileAtIndex(0).empty()); |
141 | 180 |
142 SetText(use_generic_button ? base::string16() : | 181 SetText(use_generic_button ? base::string16() : |
143 profiles::GetAvatarButtonTextForProfile(browser()->profile())); | 182 profiles::GetAvatarButtonTextForProfile(browser_->profile())); |
144 | 183 |
145 // If the button has no text, clear the text shadows to make sure the | 184 // If the button has no text, clear the text shadows to make sure the |
146 // image is centered correctly. | 185 // image is centered correctly. |
147 SetTextShadows( | 186 SetTextShadows( |
148 use_generic_button | 187 use_generic_button |
149 ? gfx::ShadowValues() | 188 ? gfx::ShadowValues() |
150 : gfx::ShadowValues( | 189 : gfx::ShadowValues( |
151 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); | 190 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); |
152 | 191 |
153 // We want the button to resize if the new text is shorter. | 192 // We want the button to resize if the new text is shorter. |
154 SetMinSize(gfx::Size()); | 193 SetMinSize(gfx::Size()); |
155 | 194 |
156 if (use_generic_button) { | 195 if (use_generic_button) { |
157 SetImage(views::Button::STATE_NORMAL, generic_avatar_); | 196 SetImage(views::Button::STATE_NORMAL, generic_avatar_); |
158 } else if (has_auth_error_) { | 197 } else if (has_auth_error_) { |
159 SetImage(views::Button::STATE_NORMAL, | 198 SetImage(views::Button::STATE_NORMAL, |
160 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 199 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
161 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia()); | 200 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia()); |
162 } else { | 201 } else { |
163 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); | 202 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
164 } | 203 } |
165 | 204 |
166 // If we are not using the generic button, then reset the spacing between | 205 // If we are not using the generic button, then reset the spacing between |
167 // the text and the possible authentication error icon. | 206 // the text and the possible authentication error icon. |
168 const int kDefaultImageTextSpacing = 5; | 207 const int kDefaultImageTextSpacing = 5; |
169 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); | 208 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
170 | 209 |
171 PreferredSizeChanged(); | 210 PreferredSizeChanged(); |
172 } | 211 } |
173 | |
174 void NewAvatarButton::OnErrorChanged() { | |
175 // If there is an error, show a warning. | |
176 const SigninErrorController* error = | |
177 profiles::GetSigninErrorController(browser()->profile()); | |
178 has_auth_error_ = error && error->HasError(); | |
179 | |
180 Update(); | |
181 } | |
OLD | NEW |