OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/views/profiles/avatar_button.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "chrome/app/vector_icons/vector_icons.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/profiles/profiles_state.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "components/signin/core/browser/signin_manager.h" |
| 15 #include "ui/gfx/color_palette.h" |
| 16 #include "ui/gfx/paint_vector_icon.h" |
| 17 |
| 18 #if defined(OS_WIN) |
| 19 #include "base/win/windows_version.h" |
| 20 #endif |
| 21 |
| 22 AvatarButton::AvatarButton(views::MenuButtonListener* listener, |
| 23 Profile* profile) |
| 24 : MenuButton(base::string16(), listener, false), |
| 25 error_controller_(this, profile), |
| 26 profile_(profile) { |
| 27 set_notify_action(CustomButton::NOTIFY_ON_PRESS); |
| 28 set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON | |
| 29 ui::EF_RIGHT_MOUSE_BUTTON); |
| 30 set_animate_on_state_change(false); |
| 31 SetEnabledTextColors(SK_ColorWHITE); |
| 32 SetTextSubpixelRenderingEnabled(false); |
| 33 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 34 |
| 35 g_browser_process->profile_manager() |
| 36 ->GetProfileAttributesStorage() |
| 37 .AddObserver(this); |
| 38 |
| 39 // The largest text height that fits in the button. If the font list height |
| 40 // is larger than this, it will be shrunk to match it. |
| 41 // TODO(noms): Calculate this constant algorithmically from the button's size. |
| 42 const int kDisplayFontHeight = 16; |
| 43 SetFontList( |
| 44 label()->font_list().DeriveWithHeightUpperBound(kDisplayFontHeight)); |
| 45 } |
| 46 |
| 47 AvatarButton::~AvatarButton() { |
| 48 g_browser_process->profile_manager() |
| 49 ->GetProfileAttributesStorage() |
| 50 .RemoveObserver(this); |
| 51 } |
| 52 |
| 53 bool AvatarButton::IsTriggerableEvent(const ui::Event& event) { |
| 54 return event.type() == ui::ET_GESTURE_LONG_PRESS || |
| 55 MenuButton::IsTriggerableEvent(event); |
| 56 } |
| 57 |
| 58 void AvatarButton::OnAvatarErrorChanged() { |
| 59 Update(); |
| 60 } |
| 61 |
| 62 void AvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| 63 Update(); |
| 64 } |
| 65 |
| 66 void AvatarButton::OnProfileWasRemoved(const base::FilePath& profile_path, |
| 67 const base::string16& profile_name) { |
| 68 // If deleting the active profile, don't bother updating the avatar |
| 69 // button, as the browser window is being closed anyway. |
| 70 if (profile_->GetPath() != profile_path) |
| 71 Update(); |
| 72 } |
| 73 |
| 74 void AvatarButton::OnProfileNameChanged( |
| 75 const base::FilePath& profile_path, |
| 76 const base::string16& old_profile_name) { |
| 77 if (profile_->GetPath() == profile_path) |
| 78 Update(); |
| 79 } |
| 80 |
| 81 void AvatarButton::OnProfileSupervisedUserIdChanged( |
| 82 const base::FilePath& profile_path) { |
| 83 if (profile_->GetPath() == profile_path) |
| 84 Update(); |
| 85 } |
| 86 |
| 87 void AvatarButton::Update() { |
| 88 ProfileAttributesStorage& storage = |
| 89 g_browser_process->profile_manager()->GetProfileAttributesStorage(); |
| 90 |
| 91 // If we have a single local profile, then use the generic avatar |
| 92 // button instead of the profile name. Never use the generic button if |
| 93 // the active profile is Guest. |
| 94 const bool use_generic_button = |
| 95 !profile_->IsGuestSession() && storage.GetNumberOfProfiles() == 1 && |
| 96 !SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated(); |
| 97 |
| 98 UpdateButton(use_generic_button); |
| 99 |
| 100 PreferredSizeChanged(); |
| 101 } |
| 102 |
| 103 void AvatarButton::UpdateButton(bool use_generic_button) { |
| 104 SetText(use_generic_button |
| 105 ? base::string16() |
| 106 : profiles::GetAvatarButtonTextForProfile(profile_)); |
| 107 |
| 108 // If the button has no text, clear the text shadows to make sure the |
| 109 // image is centered correctly. |
| 110 SetTextShadows( |
| 111 use_generic_button |
| 112 ? gfx::ShadowValues() |
| 113 : gfx::ShadowValues( |
| 114 10, gfx::ShadowValue(gfx::Vector2d(), 2.0f, SK_ColorDKGRAY))); |
| 115 |
| 116 // We want the button to resize if the new text is shorter. |
| 117 SetMinSize(gfx::Size()); |
| 118 |
| 119 if (use_generic_button) { |
| 120 SetImage(views::Button::STATE_NORMAL, generic_avatar_); |
| 121 } else if (error_controller_.HasAvatarError()) { |
| 122 SetImage(views::Button::STATE_NORMAL, |
| 123 gfx::CreateVectorIcon(kSyncProblemIcon, 16, gfx::kGoogleRed700)); |
| 124 } else { |
| 125 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
| 126 } |
| 127 |
| 128 // If we are not using the generic button, then reset the spacing between |
| 129 // the text and the possible authentication error icon. |
| 130 const int kDefaultImageTextSpacing = 5; |
| 131 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
| 132 } |
OLD | NEW |