Chromium Code Reviews| 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..3743af60c60001d8c8317052941c1e24f748aa27 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/profiles/avatar_button.cc |
| @@ -0,0 +1,295 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
Peter Kasting
2017/05/06 02:23:09
Any way to get this to appear as a moved file w/di
emx
2017/05/09 16:26:52
I don't believe so. Git decides automatically whet
Peter Kasting
2017/05/09 17:40:28
In that case I'd suggest doing the file rename as
emx
2017/05/10 17:38:48
OK, I've made it a separate CL.
|
| +// 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 "chrome/browser/themes/theme_service.h" |
| +#include "chrome/browser/themes/theme_service_factory.h" |
| +#include "chrome/grit/theme_resources.h" |
| +#include "components/signin/core/browser/signin_manager.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/color_palette.h" |
| +#include "ui/gfx/paint_vector_icon.h" |
| +#include "ui/views/animation/flood_fill_ink_drop_ripple.h" |
| +#include "ui/views/animation/ink_drop_impl.h" |
| +#include "ui/views/controls/button/label_button_border.h" |
| + |
| +#if defined(OS_WIN) |
| +#include "base/win/windows_version.h" |
| +#endif |
| + |
| +namespace { |
| + |
| +const int kThemedButtonHeight = 20; |
|
Peter Kasting
2017/05/06 02:23:08
Nit: Prefer constexpr to const for compile-time co
emx
2017/05/09 16:26:52
Done.
|
| + |
| +const int kMdButtonMinWidth = 48; |
|
Peter Kasting
2017/05/06 02:23:08
Should these values be computed based on average c
emx
2017/05/09 16:26:52
The minimum width should be the caption button wid
Peter Kasting
2017/05/09 17:40:29
Implementation-wise, the spec is a design suggesti
emx
2017/05/10 17:38:49
OK, let's wait for Alan to reply to the email.
Peter Kasting
2017/05/11 01:03:42
If he doesn't get back with something quickly, I w
|
| +const int kMdButtonMaxWidth = 96; |
| +// Height of the "cozy" MD button that slides over the tabstrip |
|
Peter Kasting
2017/05/06 02:23:09
Nit: Can avoid the term "Cozy" and just talk about
emx
2017/05/09 16:26:52
Done.
|
| +// ("tall" MD button height is computed) |
| +const int kMdButtonCozyHeight = 20; |
| +const int kMdButtonIconHeight = 16; |
|
Peter Kasting
2017/05/06 02:23:09
This height differs slightly from the 20 DIP heigh
emx
2017/05/09 16:26:52
The spec clearly says 16 and it looks fine to me.
Peter Kasting
2017/05/09 17:40:29
See comment earlier about specs being suggestions
emx
2017/05/10 17:38:48
So if the spec is a suggestion, then who actually
Peter Kasting
2017/05/11 01:03:43
It's collaborative, ultimately -- UI Review has fi
emx
2017/05/11 12:56:10
Thanks for the detailed explanation. I agree with
|
| +const SkColor kMdButtonIconColor = SkColorSetA(SK_ColorBLACK, 0.54 * 0xFF); |
|
Peter Kasting
2017/05/06 02:23:09
These kinds of one-off constants (for opacity here
emx
2017/05/09 16:26:52
The highlight opacity of 0.08 is in the spec. Whet
Peter Kasting
2017/05/09 17:40:28
The way I would start is to come back to Alan and
|
| +// Opacity of the ink drop on hover |
| +const float kInkDropHighlightOpacity = 0.08f; |
| +// Opacity of the ink drop on click, which is added to kInkDropHighlightOpacity |
| +const float kInkDropRippleOpacity = 0.04f; |
| + |
| +std::unique_ptr<views::Border> CreateThemedBorder( |
| + const int normal_image_set[], |
| + const int hot_image_set[], |
| + const int pushed_image_set[]) { |
| + std::unique_ptr<views::LabelButtonAssetBorder> border( |
| + new views::LabelButtonAssetBorder(views::Button::STYLE_TEXTBUTTON)); |
| + |
| + border->SetPainter(false, views::Button::STATE_NORMAL, |
| + views::Painter::CreateImageGridPainter(normal_image_set)); |
| + border->SetPainter(false, views::Button::STATE_HOVERED, |
| + views::Painter::CreateImageGridPainter(hot_image_set)); |
| + border->SetPainter(false, views::Button::STATE_PRESSED, |
| + views::Painter::CreateImageGridPainter(pushed_image_set)); |
| + |
| + const int kLeftRightInset = 8; |
| + const int kTopInset = 2; |
| + const int kBottomInset = 4; |
|
Peter Kasting
2017/05/06 02:23:09
Where did these insets come from? They, and the W
emx
2017/05/09 16:26:52
I don't know - this is existing code and I haven't
Peter Kasting
2017/05/09 17:40:28
OK. We really shouldn't do this sort of thing, bu
|
| + border->set_insets( |
| + gfx::Insets(kTopInset, kLeftRightInset, kBottomInset, kLeftRightInset)); |
| + |
| + return std::move(border); |
| +} |
| + |
| +std::unique_ptr<views::Border> CreateWin10NativeBorder() { |
|
Peter Kasting
2017/05/06 02:23:09
Is this really Win10-specific? Or is this basical
emx
2017/05/09 16:26:52
I arrived at these insets by experimentation on Wi
Peter Kasting
2017/05/09 17:40:28
Can you say more about "look OK"? For example, wh
emx
2017/05/10 17:38:48
Having tested the insets again, it actually looks
Peter Kasting
2017/05/11 01:03:43
Sounds good, but it doesn't look like those change
emx
2017/05/11 12:56:10
Yes, sorry, I replied to the comments, but didn't
|
| + // These insets look OK at 100%, 125%, 150%, 175% and 200% scaling. |
| + const int kLeftRightInset = 8; |
| + const int kTopInset = 1; |
| + const int kBottomInset = 3; |
| + return views::CreateEmptyBorder(kTopInset, kLeftRightInset, kBottomInset, |
| + kLeftRightInset); |
| +} |
| + |
| +} // namespace |
| + |
| +AvatarButton::AvatarButton(views::ButtonListener* listener, |
| + AvatarButtonStyle button_style, |
| + Profile* profile) |
| + : LabelButton(listener, base::string16()), |
| + error_controller_(this, profile), |
| + profile_(profile), |
| + use_win10_native_button_(false) { |
| + 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)); |
| + |
| +#if defined(OS_WIN) |
| + // TODO: use MD button in other cases, too [http://crbug.com/591586] |
|
Peter Kasting
2017/05/06 02:23:09
Nit: Which other cases? All of them? Only Window
emx
2017/05/09 16:26:52
I don't know which other cases. I added this TODO
Peter Kasting
2017/05/09 17:40:28
It seems like Evan is the likely person to do this
emx
2017/05/11 12:56:10
Makes sense, thanks for explaining. I didn't reali
|
| + if ((base::win::GetVersion() >= base::win::VERSION_WIN10) && |
| + ThemeServiceFactory::GetForProfile(profile)->UsingSystemTheme()) { |
| + DCHECK_EQ(AvatarButtonStyle::NATIVE, button_style); |
| + use_win10_native_button_ = true; |
|
Peter Kasting
2017/05/06 02:23:08
Nit: Again, is "win10 native button" really the ri
emx
2017/05/09 16:26:52
"Win10NativeAvatarButton" was the class name sugge
Peter Kasting
2017/05/09 17:40:28
In the interest of not blocking you, go ahead and
|
| + } |
| +#endif // defined(OS_WIN) |
| + |
| + if (use_win10_native_button_) { |
| + generic_avatar_ = gfx::CreateVectorIcon( |
| + kAccountCircleIcon, kMdButtonIconHeight, kMdButtonIconColor); |
| + SetBorder(CreateWin10NativeBorder()); |
| + |
| + SetInkDropMode(InkDropMode::ON); |
| + set_has_ink_drop_action_on_click(true); |
| + SetFocusPainter(nullptr); |
| + set_ink_drop_base_color(SK_ColorBLACK); |
| + set_ink_drop_visible_opacity(kInkDropRippleOpacity); |
| + } else { |
| + if (button_style == AvatarButtonStyle::THEMED) { |
| + const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| + const int kHoverImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
| + const int kPressedImageSet[] = |
| + IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
| + SetButtonAvatar(IDR_AVATAR_THEMED_BUTTON_AVATAR); |
| + SetBorder(CreateThemedBorder(kNormalImageSet, kHoverImageSet, |
| + kPressedImageSet)); |
| +#if defined(OS_WIN) |
| + } else if (base::win::GetVersion() < base::win::VERSION_WIN8) { |
| + const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
| + const int kHoverImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| + const int kPressedImageSet[] = |
| + IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| + SetButtonAvatar(IDR_AVATAR_GLASS_BUTTON_AVATAR); |
| + SetBorder(CreateThemedBorder(kNormalImageSet, kHoverImageSet, |
| + kPressedImageSet)); |
| +#endif |
| + } else { |
| + const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_NORMAL); |
| + const int kHoverImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_HOVER); |
| + const int kPressedImageSet[] = |
| + IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); |
| + SetButtonAvatar(IDR_AVATAR_NATIVE_BUTTON_AVATAR); |
| + SetBorder(CreateThemedBorder(kNormalImageSet, kHoverImageSet, |
| + kPressedImageSet)); |
| + } |
| + } |
| + |
| + Update(); |
| + SchedulePaint(); |
| +} |
| + |
| +AvatarButton::~AvatarButton() { |
| + g_browser_process->profile_manager() |
| + ->GetProfileAttributesStorage() |
| + .RemoveObserver(this); |
|
Peter Kasting
2017/05/06 02:23:08
Nit: Can we use ScopedObserver to avoid this expli
emx
2017/05/09 16:26:52
Done.
|
| +} |
| + |
| +void AvatarButton::OnGestureEvent(ui::GestureEvent* event) { |
| + // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since |
| + // no other UI button based on CustomButton appears to handle mouse |
| + // right-click. If other cases are identified, it may make sense to move this |
| + // check to CustomButton. |
| + if (event->type() == ui::ET_GESTURE_LONG_PRESS) |
| + NotifyClick(*event); |
| + else |
| + LabelButton::OnGestureEvent(event); |
| +} |
| + |
| +gfx::Size AvatarButton::GetMinimumSize() const { |
| + if (use_win10_native_button_) { |
| + // Returns the "cozy" size of the button. Called by |
| + // GlassBrowserFrameView::LayoutProfileSwitcher() when it calculates that |
| + // the button needs to slide over the tabstrip. |
| + return gfx::Size(kMdButtonMinWidth, kMdButtonCozyHeight); |
| + } |
| + |
| + return LabelButton::GetMinimumSize(); |
| +} |
| + |
| +gfx::Size AvatarButton::GetPreferredSize() const { |
| + gfx::Size size = LabelButton::GetPreferredSize(); |
| + |
| + if (use_win10_native_button_) { |
| + // Returns the "tall" (normal) size of the button. Its height should match |
| + // the caption button height. |
| + size.set_width( |
| + std::min(std::max(size.width(), kMdButtonMinWidth), kMdButtonMaxWidth)); |
|
Peter Kasting
2017/05/06 02:23:09
Nit: MathUtil::ClampToRange()?
emx
2017/05/09 16:26:52
I tried that, but "git cl upload" failed:
** Pres
Peter Kasting
2017/05/11 01:03:43
Sadness. I'm dealing with this separately, don't
|
| + // TODO(emx): get the proper height here - see http://crrev/2833363002 |
|
Peter Kasting
2017/05/06 02:23:08
You can mark this patchset as dependent on that on
emx
2017/05/09 16:26:52
That one has been committed now.
|
| + size.set_height(30); |
| + } else { |
| + size.set_height(kThemedButtonHeight); |
| + } |
| + |
| + return size; |
| +} |
| + |
| +bool AvatarButton::ShouldUseFloodFillInkDrop() const { |
| + return true; |
| +} |
| + |
| +std::unique_ptr<views::InkDropHighlight> AvatarButton::CreateInkDropHighlight() |
| + const { |
| + auto center = gfx::RectF(GetLocalBounds()).CenterPoint(); |
| + auto ink_drop_highlight = base::MakeUnique<views::InkDropHighlight>( |
| + size(), 0, center, GetInkDropBaseColor()); |
| + ink_drop_highlight->set_visible_opacity(kInkDropHighlightOpacity); |
| + return ink_drop_highlight; |
| +} |
| + |
| +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(); |
| + |
|
Peter Kasting
2017/05/06 02:23:09
Nit: Blank line not necessary. Or, leave the func
emx
2017/05/09 16:26:52
Inlined.
|
| + 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); |
| +} |
| + |
| +void AvatarButton::SetButtonAvatar(int avatar_idr) { |
| + ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| + generic_avatar_ = *rb->GetImageNamed(avatar_idr).ToImageSkia(); |
| +} |