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