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 : LabelButton(listener, base::string16()), | 49 : AvatarBaseButton(browser), |
50 browser_(browser), | 50 LabelButton(listener, base::string16()), |
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 | |
100 // Subscribe to authentication error changes so that the avatar button can | 98 // 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. | 99 // update itself. Note that guest mode profiles won't have a token service. |
102 SigninErrorController* error = | 100 SigninErrorController* error = |
103 profiles::GetSigninErrorController(browser_->profile()); | 101 profiles::GetSigninErrorController(browser->profile()); |
104 if (error) { | 102 if (error) { |
105 error->AddObserver(this); | 103 error->AddObserver(this); |
106 OnErrorChanged(); // This calls Update(). | 104 OnErrorChanged(); // This calls Update(). |
107 } else { | 105 } else { |
108 Update(); | 106 Update(); |
109 } | 107 } |
110 SchedulePaint(); | 108 SchedulePaint(); |
111 } | 109 } |
112 | 110 |
113 NewAvatarButton::~NewAvatarButton() { | 111 NewAvatarButton::~NewAvatarButton() { |
114 g_browser_process->profile_manager()-> | |
115 GetProfileInfoCache().RemoveObserver(this); | |
116 SigninErrorController* error = | 112 SigninErrorController* error = |
117 profiles::GetSigninErrorController(browser_->profile()); | 113 profiles::GetSigninErrorController(browser()->profile()); |
118 if (error) | 114 if (error) |
119 error->RemoveObserver(this); | 115 error->RemoveObserver(this); |
120 } | 116 } |
121 | 117 |
122 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { | 118 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { |
123 // Prevent the bubble from being re-shown if it's already showing. | 119 // Prevent the bubble from being re-shown if it's already showing. |
124 suppress_mouse_released_action_ = ProfileChooserView::IsShowing(); | 120 suppress_mouse_released_action_ = ProfileChooserView::IsShowing(); |
125 return LabelButton::OnMousePressed(event); | 121 return LabelButton::OnMousePressed(event); |
126 } | 122 } |
127 | 123 |
128 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) { | 124 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) { |
129 if (suppress_mouse_released_action_) | 125 if (suppress_mouse_released_action_) |
130 suppress_mouse_released_action_ = false; | 126 suppress_mouse_released_action_ = false; |
131 else | 127 else |
132 LabelButton::OnMouseReleased(event); | 128 LabelButton::OnMouseReleased(event); |
133 } | 129 } |
134 | 130 |
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 | |
170 void NewAvatarButton::Update() { | 131 void NewAvatarButton::Update() { |
171 const ProfileInfoCache& cache = | 132 const ProfileInfoCache& cache = |
172 g_browser_process->profile_manager()->GetProfileInfoCache(); | 133 g_browser_process->profile_manager()->GetProfileInfoCache(); |
173 | 134 |
174 // If we have a single local profile, then use the generic avatar | 135 // 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 | 136 // button instead of the profile name. Never use the generic button if |
176 // the active profile is Guest. | 137 // the active profile is Guest. |
177 bool use_generic_button = (!browser_->profile()->IsGuestSession() && | 138 bool use_generic_button = (!browser()->profile()->IsGuestSession() && |
178 cache.GetNumberOfProfiles() == 1 && | 139 cache.GetNumberOfProfiles() == 1 && |
179 cache.GetUserNameOfProfileAtIndex(0).empty()); | 140 cache.GetUserNameOfProfileAtIndex(0).empty()); |
180 | 141 |
181 SetText(use_generic_button ? base::string16() : | 142 SetText(use_generic_button ? base::string16() : |
182 profiles::GetAvatarButtonTextForProfile(browser_->profile())); | 143 profiles::GetAvatarButtonTextForProfile(browser()->profile())); |
183 | 144 |
184 // If the button has no text, clear the text shadows to make sure the | 145 // If the button has no text, clear the text shadows to make sure the |
185 // image is centered correctly. | 146 // image is centered correctly. |
186 SetTextShadows( | 147 SetTextShadows( |
187 use_generic_button | 148 use_generic_button |
188 ? gfx::ShadowValues() | 149 ? gfx::ShadowValues() |
189 : gfx::ShadowValues( | 150 : gfx::ShadowValues( |
190 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); | 151 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); |
191 | 152 |
192 // We want the button to resize if the new text is shorter. | 153 // We want the button to resize if the new text is shorter. |
193 SetMinSize(gfx::Size()); | 154 SetMinSize(gfx::Size()); |
194 | 155 |
195 if (use_generic_button) { | 156 if (use_generic_button) { |
196 SetImage(views::Button::STATE_NORMAL, generic_avatar_); | 157 SetImage(views::Button::STATE_NORMAL, generic_avatar_); |
197 } else if (has_auth_error_) { | 158 } else if (has_auth_error_) { |
198 SetImage(views::Button::STATE_NORMAL, | 159 SetImage(views::Button::STATE_NORMAL, |
199 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 160 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
200 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia()); | 161 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia()); |
201 } else { | 162 } else { |
202 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); | 163 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
203 } | 164 } |
204 | 165 |
205 // If we are not using the generic button, then reset the spacing between | 166 // If we are not using the generic button, then reset the spacing between |
206 // the text and the possible authentication error icon. | 167 // the text and the possible authentication error icon. |
207 const int kDefaultImageTextSpacing = 5; | 168 const int kDefaultImageTextSpacing = 5; |
208 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); | 169 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
209 | 170 |
210 PreferredSizeChanged(); | 171 PreferredSizeChanged(); |
211 } | 172 } |
| 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 |