Index: chrome/browser/ui/views/profiles/avatar_button.cc |
diff --git a/chrome/browser/ui/views/profiles/avatar_button.cc b/chrome/browser/ui/views/profiles/avatar_button.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7109a87574274855f2aa38300fa032c5d9f7f356 |
--- /dev/null |
+++ b/chrome/browser/ui/views/profiles/avatar_button.cc |
@@ -0,0 +1,131 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/views/profiles/avatar_button.h" |
+ |
+#include <utility> |
+ |
+#include "chrome/app/vector_icons/vector_icons.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/profiles/profiles_state.h" |
+#include "chrome/browser/signin/signin_manager_factory.h" |
+#include "ui/gfx/color_palette.h" |
+#include "ui/gfx/paint_vector_icon.h" |
+ |
+#if defined(OS_WIN) |
+#include "base/win/windows_version.h" |
+#endif |
+ |
+AvatarButton::AvatarButton(views::MenuButtonListener* listener, |
+ Profile* profile) |
+ : MenuButton(base::string16(), listener, false), |
+ error_controller_(this, profile), |
+ profile_(profile) { |
+ set_notify_action(CustomButton::NOTIFY_ON_PRESS); |
+ set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON | |
+ ui::EF_RIGHT_MOUSE_BUTTON); |
+ set_animate_on_state_change(false); |
+ SetEnabledTextColors(SK_ColorWHITE); |
+ SetTextSubpixelRenderingEnabled(false); |
+ SetHorizontalAlignment(gfx::ALIGN_CENTER); |
+ |
+ g_browser_process->profile_manager() |
+ ->GetProfileAttributesStorage() |
+ .AddObserver(this); |
+ |
+ // The largest text height that fits in the button. If the font list height |
+ // is larger than this, it will be shrunk to match it. |
+ // TODO(noms): Calculate this constant algorithmically from the button's size. |
+ const int kDisplayFontHeight = 16; |
+ SetFontList( |
+ label()->font_list().DeriveWithHeightUpperBound(kDisplayFontHeight)); |
+} |
+ |
+AvatarButton::~AvatarButton() { |
+ g_browser_process->profile_manager() |
+ ->GetProfileAttributesStorage() |
+ .RemoveObserver(this); |
+} |
+ |
+bool AvatarButton::IsTriggerableEvent(const ui::Event& event) { |
+ return event.type() == ui::ET_GESTURE_LONG_PRESS || |
+ MenuButton::IsTriggerableEvent(event); |
+} |
+ |
+void AvatarButton::OnAvatarErrorChanged() { |
+ Update(); |
+} |
+ |
+void AvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
+ Update(); |
+} |
+ |
+void AvatarButton::OnProfileWasRemoved(const base::FilePath& profile_path, |
+ const base::string16& profile_name) { |
+ // If deleting the active profile, don't bother updating the avatar |
+ // button, as the browser window is being closed anyway. |
+ if (profile_->GetPath() != profile_path) |
+ Update(); |
+} |
+ |
+void AvatarButton::OnProfileNameChanged( |
+ const base::FilePath& profile_path, |
+ const base::string16& old_profile_name) { |
+ if (profile_->GetPath() == profile_path) |
+ Update(); |
+} |
+ |
+void AvatarButton::OnProfileSupervisedUserIdChanged( |
+ const base::FilePath& profile_path) { |
+ if (profile_->GetPath() == profile_path) |
+ Update(); |
+} |
+ |
+void AvatarButton::Update() { |
+ ProfileAttributesStorage& storage = |
+ g_browser_process->profile_manager()->GetProfileAttributesStorage(); |
+ |
+ // If we have a single local profile, then use the generic avatar |
+ // button instead of the profile name. Never use the generic button if |
+ // the active profile is Guest. |
+ const bool use_generic_button = |
+ !profile_->IsGuestSession() && storage.GetNumberOfProfiles() == 1 && |
+ !SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated(); |
+ |
+ UpdateButton(use_generic_button); |
+ |
+ PreferredSizeChanged(); |
+} |
+ |
+void AvatarButton::UpdateButton(bool use_generic_button) { |
+ SetText(use_generic_button |
+ ? base::string16() |
+ : profiles::GetAvatarButtonTextForProfile(profile_)); |
+ |
+ // If the button has no text, clear the text shadows to make sure the |
+ // image is centered correctly. |
+ SetTextShadows( |
+ use_generic_button |
+ ? gfx::ShadowValues() |
+ : gfx::ShadowValues( |
+ 10, gfx::ShadowValue(gfx::Vector2d(), 2.0f, SK_ColorDKGRAY))); |
+ |
+ // We want the button to resize if the new text is shorter. |
+ SetMinSize(gfx::Size()); |
+ |
+ if (use_generic_button) { |
+ SetImage(views::Button::STATE_NORMAL, generic_avatar_); |
+ } else if (error_controller_.HasAvatarError()) { |
+ SetImage(views::Button::STATE_NORMAL, |
+ gfx::CreateVectorIcon(kSyncProblemIcon, 16, gfx::kGoogleRed700)); |
+ } else { |
+ SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
+ } |
+ |
+ // If we are not using the generic button, then reset the spacing between |
+ // the text and the possible authentication error icon. |
+ const int kDefaultImageTextSpacing = 5; |
+ SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
+} |