| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/profiles/profile_attributes_entry.h" | 12 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/browser/profiles/profiles_state.h" | 14 #include "chrome/browser/profiles/profiles_state.h" |
| 15 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 16 #include "chrome/browser/ui/views/profiles/avatar_button_delegate.h" | 15 #include "chrome/browser/ui/views/profiles/avatar_button_delegate.h" |
| 17 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h" | 16 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h" |
| 18 #include "components/browser_sync/browser/profile_sync_service.h" | |
| 19 #include "components/signin/core/common/profile_management_switches.h" | 17 #include "components/signin/core/common/profile_management_switches.h" |
| 20 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
| 23 #include "ui/gfx/color_palette.h" | 21 #include "ui/gfx/color_palette.h" |
| 24 #include "ui/gfx/geometry/vector2d.h" | 22 #include "ui/gfx/geometry/vector2d.h" |
| 25 #include "ui/gfx/paint_vector_icon.h" | 23 #include "ui/gfx/paint_vector_icon.h" |
| 26 #include "ui/gfx/vector_icons_public.h" | 24 #include "ui/gfx/vector_icons_public.h" |
| 27 #include "ui/views/border.h" | 25 #include "ui/views/border.h" |
| 28 #include "ui/views/controls/button/label_button_border.h" | 26 #include "ui/views/controls/button/label_button_border.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 46 const int kTopInset = 2; | 44 const int kTopInset = 2; |
| 47 const int kBottomInset = 4; | 45 const int kBottomInset = 4; |
| 48 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, | 46 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, |
| 49 kBottomInset, kLeftRightInset)); | 47 kBottomInset, kLeftRightInset)); |
| 50 | 48 |
| 51 return std::move(border); | 49 return std::move(border); |
| 52 } | 50 } |
| 53 | 51 |
| 54 } // namespace | 52 } // namespace |
| 55 | 53 |
| 56 SyncErrorController* GetSyncErrorControllerIfNeeded(Profile* profile) { | |
| 57 if (!switches::IsMaterialDesignUserMenu()) | |
| 58 return nullptr; | |
| 59 ProfileSyncService* sync_service = | |
| 60 ProfileSyncServiceFactory::GetForProfile(profile); | |
| 61 return sync_service ? sync_service->sync_error_controller() : nullptr; | |
| 62 } | |
| 63 | |
| 64 NewAvatarButton::SigninErrorObserver::SigninErrorObserver( | |
| 65 NewAvatarButton* parent_button, | |
| 66 Profile* profile) | |
| 67 : parent_button_(parent_button), profile_(profile) { | |
| 68 // Subscribe to authentication error changes so that the avatar button can | |
| 69 // update itself. Note that guest mode profiles won't have a token service. | |
| 70 SigninErrorController* signin_error_controller = | |
| 71 profiles::GetSigninErrorController(profile_); | |
| 72 if (signin_error_controller) | |
| 73 signin_error_controller->AddObserver(this); | |
| 74 } | |
| 75 | |
| 76 NewAvatarButton::SigninErrorObserver::~SigninErrorObserver() { | |
| 77 SigninErrorController* signin_error_controller = | |
| 78 profiles::GetSigninErrorController(profile_); | |
| 79 if (signin_error_controller) | |
| 80 signin_error_controller->RemoveObserver(this); | |
| 81 } | |
| 82 | |
| 83 void NewAvatarButton::SigninErrorObserver::OnErrorChanged() { | |
| 84 parent_button_->OnErrorChanged(); | |
| 85 } | |
| 86 | |
| 87 NewAvatarButton::SyncErrorObserver::SyncErrorObserver( | |
| 88 NewAvatarButton* parent_button, | |
| 89 Profile* profile) | |
| 90 : parent_button_(parent_button), profile_(profile) { | |
| 91 SyncErrorController* sync_error_controller = | |
| 92 GetSyncErrorControllerIfNeeded(profile_); | |
| 93 if (sync_error_controller) | |
| 94 sync_error_controller->AddObserver(this); | |
| 95 } | |
| 96 | |
| 97 NewAvatarButton::SyncErrorObserver::~SyncErrorObserver() { | |
| 98 SyncErrorController* sync_error_controller = | |
| 99 GetSyncErrorControllerIfNeeded(profile_); | |
| 100 if (sync_error_controller) | |
| 101 sync_error_controller->RemoveObserver(this); | |
| 102 } | |
| 103 | |
| 104 void NewAvatarButton::SyncErrorObserver::OnErrorChanged() { | |
| 105 parent_button_->OnErrorChanged(); | |
| 106 } | |
| 107 | |
| 108 NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, | 54 NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, |
| 109 AvatarButtonStyle button_style, | 55 AvatarButtonStyle button_style, |
| 110 Profile* profile) | 56 Profile* profile) |
| 111 : LabelButton(delegate, base::string16()), | 57 : LabelButton(delegate, base::string16()), |
| 112 signin_error_observer_(this, profile), | |
| 113 sync_error_observer_(this, profile), | |
| 114 delegate_(delegate), | 58 delegate_(delegate), |
| 59 error_controller_(this, profile), |
| 115 profile_(profile), | 60 profile_(profile), |
| 116 has_error_(false), | |
| 117 suppress_mouse_released_action_(false) { | 61 suppress_mouse_released_action_(false) { |
| 118 set_triggerable_event_flags( | 62 set_triggerable_event_flags( |
| 119 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); | 63 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); |
| 120 set_animate_on_state_change(false); | 64 set_animate_on_state_change(false); |
| 121 SetEnabledTextColors(SK_ColorWHITE); | 65 SetEnabledTextColors(SK_ColorWHITE); |
| 122 SetTextSubpixelRenderingEnabled(false); | 66 SetTextSubpixelRenderingEnabled(false); |
| 123 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 67 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 124 | 68 |
| 125 // The largest text height that fits in the button. If the font list height | 69 // The largest text height that fits in the button. If the font list height |
| 126 // is larger than this, it will be shrunk to match it. | 70 // is larger than this, it will be shrunk to match it. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 152 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_HOVER); | 96 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_HOVER); |
| 153 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); | 97 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); |
| 154 | 98 |
| 155 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 99 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| 156 generic_avatar_ = | 100 generic_avatar_ = |
| 157 *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia(); | 101 *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia(); |
| 158 } | 102 } |
| 159 | 103 |
| 160 g_browser_process->profile_manager()-> | 104 g_browser_process->profile_manager()-> |
| 161 GetProfileAttributesStorage().AddObserver(this); | 105 GetProfileAttributesStorage().AddObserver(this); |
| 162 | 106 Update(); |
| 163 OnErrorChanged(); | |
| 164 SchedulePaint(); | 107 SchedulePaint(); |
| 165 } | 108 } |
| 166 | 109 |
| 167 NewAvatarButton::~NewAvatarButton() { | 110 NewAvatarButton::~NewAvatarButton() { |
| 168 g_browser_process->profile_manager()-> | 111 g_browser_process->profile_manager()-> |
| 169 GetProfileAttributesStorage().RemoveObserver(this); | 112 GetProfileAttributesStorage().RemoveObserver(this); |
| 170 } | 113 } |
| 171 | 114 |
| 172 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { | 115 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 173 // Prevent the bubble from being re-shown if it's already showing. | 116 // Prevent the bubble from being re-shown if it's already showing. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 186 // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since | 129 // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since |
| 187 // no other UI button based on CustomButton appears to handle mouse | 130 // no other UI button based on CustomButton appears to handle mouse |
| 188 // right-click. If other cases are identified, it may make sense to move this | 131 // right-click. If other cases are identified, it may make sense to move this |
| 189 // check to CustomButton. | 132 // check to CustomButton. |
| 190 if (event->type() == ui::ET_GESTURE_LONG_PRESS) | 133 if (event->type() == ui::ET_GESTURE_LONG_PRESS) |
| 191 NotifyClick(*event); | 134 NotifyClick(*event); |
| 192 else | 135 else |
| 193 LabelButton::OnGestureEvent(event); | 136 LabelButton::OnGestureEvent(event); |
| 194 } | 137 } |
| 195 | 138 |
| 196 void NewAvatarButton::OnErrorChanged() { | 139 void NewAvatarButton::OnAvatarErrorChanged() { |
| 197 // If there is a signin error, show a warning icon. | |
| 198 const SigninErrorController* signin_error_controller = | |
| 199 profiles::GetSigninErrorController(profile_); | |
| 200 has_error_ = signin_error_controller && signin_error_controller->HasError(); | |
| 201 | |
| 202 // Also show a warning icon for sync errors for the material design user menu. | |
| 203 ProfileSyncService* sync_service = | |
| 204 ProfileSyncServiceFactory::GetForProfile(profile_); | |
| 205 if (switches::IsMaterialDesignUserMenu() && sync_service) { | |
| 206 SyncErrorController* sync_error_controller = | |
| 207 sync_service->sync_error_controller(); | |
| 208 ProfileSyncService::Status status; | |
| 209 sync_service->QueryDetailedSyncStatus(&status); | |
| 210 has_error_ |= | |
| 211 (sync_service->HasUnrecoverableError() || | |
| 212 status.sync_protocol_error.action == syncer::UPGRADE_CLIENT || | |
| 213 (sync_error_controller && sync_error_controller->HasError())); | |
| 214 } | |
| 215 | |
| 216 Update(); | 140 Update(); |
| 217 } | 141 } |
| 218 | 142 |
| 219 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { | 143 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| 220 Update(); | 144 Update(); |
| 221 } | 145 } |
| 222 | 146 |
| 223 void NewAvatarButton::OnProfileWasRemoved( | 147 void NewAvatarButton::OnProfileWasRemoved( |
| 224 const base::FilePath& profile_path, | 148 const base::FilePath& profile_path, |
| 225 const base::string16& profile_name) { | 149 const base::string16& profile_name) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 use_generic_button | 188 use_generic_button |
| 265 ? gfx::ShadowValues() | 189 ? gfx::ShadowValues() |
| 266 : gfx::ShadowValues( | 190 : gfx::ShadowValues( |
| 267 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); | 191 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); |
| 268 | 192 |
| 269 // We want the button to resize if the new text is shorter. | 193 // We want the button to resize if the new text is shorter. |
| 270 SetMinSize(gfx::Size()); | 194 SetMinSize(gfx::Size()); |
| 271 | 195 |
| 272 if (use_generic_button) { | 196 if (use_generic_button) { |
| 273 SetImage(views::Button::STATE_NORMAL, generic_avatar_); | 197 SetImage(views::Button::STATE_NORMAL, generic_avatar_); |
| 274 } else if (has_error_) { | 198 } else if (error_controller_.HasAvatarError()) { |
| 275 if (switches::IsMaterialDesignUserMenu()) { | 199 if (switches::IsMaterialDesignUserMenu()) { |
| 276 SetImage(views::Button::STATE_NORMAL, | 200 SetImage(views::Button::STATE_NORMAL, |
| 277 gfx::CreateVectorIcon(gfx::VectorIconId::SYNC_PROBLEM, 13, | 201 gfx::CreateVectorIcon(gfx::VectorIconId::SYNC_PROBLEM, 13, |
| 278 gfx::kGoogleRed700)); | 202 gfx::kGoogleRed700)); |
| 279 } else { | 203 } else { |
| 280 SetImage(views::Button::STATE_NORMAL, | 204 SetImage(views::Button::STATE_NORMAL, |
| 281 gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 13, | 205 gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 13, |
| 282 gfx::kGoogleYellow700)); | 206 gfx::kGoogleYellow700)); |
| 283 } | 207 } |
| 284 } else { | 208 } else { |
| 285 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); | 209 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
| 286 } | 210 } |
| 287 | 211 |
| 288 // If we are not using the generic button, then reset the spacing between | 212 // If we are not using the generic button, then reset the spacing between |
| 289 // the text and the possible authentication error icon. | 213 // the text and the possible authentication error icon. |
| 290 const int kDefaultImageTextSpacing = 5; | 214 const int kDefaultImageTextSpacing = 5; |
| 291 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); | 215 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
| 292 | 216 |
| 293 PreferredSizeChanged(); | 217 PreferredSizeChanged(); |
| 294 delegate_->ButtonPreferredSizeChanged(); | 218 delegate_->ButtonPreferredSizeChanged(); |
| 295 } | 219 } |
| OLD | NEW |