Chromium Code Reviews| Index: chrome/browser/ui/views/profiles/new_avatar_button.cc |
| diff --git a/chrome/browser/ui/views/profiles/new_avatar_button.cc b/chrome/browser/ui/views/profiles/new_avatar_button.cc |
| index 795ca239a216e99c1280e4eaff0747fdef293a11..db59398f08e3ffbe5aa8e3c039cc4f68b63f3d0d 100644 |
| --- a/chrome/browser/ui/views/profiles/new_avatar_button.cc |
| +++ b/chrome/browser/ui/views/profiles/new_avatar_button.cc |
| @@ -12,8 +12,10 @@ |
| #include "chrome/browser/profiles/profile_attributes_entry.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/ui/views/profiles/avatar_button_delegate.h" |
| #include "chrome/browser/ui/views/profiles/profile_chooser_view.h" |
| +#include "chrome/browser/ui/views/tabs/tab_strip.h" |
| #include "chrome/grit/theme_resources.h" |
| #include "components/signin/core/common/profile_management_switches.h" |
| #include "ui/base/resource/resource_bundle.h" |
| @@ -22,6 +24,8 @@ |
| #include "ui/gfx/geometry/vector2d.h" |
| #include "ui/gfx/paint_vector_icon.h" |
| #include "ui/vector_icons/vector_icons.h" |
| +#include "ui/views/animation/flood_fill_ink_drop_ripple.h" |
| +#include "ui/views/animation/ink_drop_impl.h" |
| #include "ui/views/border.h" |
| #include "ui/views/controls/button/label_button_border.h" |
| #include "ui/views/painter.h" |
| @@ -30,6 +34,18 @@ |
| #include "base/win/windows_version.h" |
| #endif |
| +// Minimum and maximum width of the MD button |
| +const int kMdButtonMinWidth = 48; |
| +const int kMdButtonMaxWidth = 96; |
| +// Height of the non-MD (pre-Windows 10) button |
| +const int kButtonNonMdHeight = 20; |
| +// Height of the "cozy" MD button that ("tall" MD button height is computed) |
| +const int kButtonMdCozyHeight = 22; |
| +// Opacity of the ink drop on hover |
| +const float kHighlightInkDropOpacity = 0.08f; |
| +// Opacity of the ink drop on click, which is added to kHighlightInkDropOpacity |
| +const float kRippleInkDropOpacity = 0.04f; |
| + |
| namespace { |
| std::unique_ptr<views::Border> CreateBorder(const int normal_image_set[], |
| @@ -38,154 +54,112 @@ std::unique_ptr<views::Border> CreateBorder(const int normal_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)); |
| + views::Painter::CreateImageGridPainter(normal_image_set)); |
| border->SetPainter(false, views::Button::STATE_HOVERED, |
| - views::Painter::CreateImageGridPainter(hot_image_set)); |
| + views::Painter::CreateImageGridPainter(hot_image_set)); |
| border->SetPainter(false, views::Button::STATE_PRESSED, |
| - views::Painter::CreateImageGridPainter(pushed_image_set)); |
| + views::Painter::CreateImageGridPainter(pushed_image_set)); |
| const int kLeftRightInset = 8; |
| const int kTopInset = 2; |
| const int kBottomInset = 4; |
| - border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, |
| - kBottomInset, kLeftRightInset)); |
| + border->set_insets( |
| + gfx::Insets(kTopInset, kLeftRightInset, kBottomInset, kLeftRightInset)); |
| return std::move(border); |
| } |
| } // namespace |
| -NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, |
| - AvatarButtonStyle button_style, |
| - Profile* profile) |
| - : LabelButton(delegate, base::string16()), |
| +AvatarButton::AvatarButton(AvatarButtonDelegate* delegate, Profile* profile) |
| + : MenuButton(base::string16(), delegate, false), |
| delegate_(delegate), |
| error_controller_(this, profile), |
| - profile_(profile), |
| - suppress_mouse_released_action_(false) { |
| - set_triggerable_event_flags( |
| - ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); |
| + 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)); |
| - |
| - ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| - if (button_style == AvatarButtonStyle::THEMED) { |
| - const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| - const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
| - const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
| - |
| - SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| - generic_avatar_ = |
| - *rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_AVATAR).ToImageSkia(); |
| -#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 kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| - const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| - |
| - SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| - generic_avatar_ = |
| - *rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_AVATAR).ToImageSkia(); |
| -#endif |
| - } else { |
| - const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_NORMAL); |
| - const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_HOVER); |
| - const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); |
| - |
| - SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| - generic_avatar_ = |
| - *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia(); |
| - } |
| - |
| - g_browser_process->profile_manager()-> |
| - GetProfileAttributesStorage().AddObserver(this); |
| - Update(); |
| - SchedulePaint(); |
| -} |
| - |
| -NewAvatarButton::~NewAvatarButton() { |
| - g_browser_process->profile_manager()-> |
| - GetProfileAttributesStorage().RemoveObserver(this); |
| -} |
| - |
| -bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { |
| - // Prevent the bubble from being re-shown if it's already showing. |
| - suppress_mouse_released_action_ = ProfileChooserView::IsShowing(); |
| - return LabelButton::OnMousePressed(event); |
| } |
| -void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) { |
| - if (suppress_mouse_released_action_) |
| - suppress_mouse_released_action_ = false; |
| - else |
| - LabelButton::OnMouseReleased(event); |
| +AvatarButton::~AvatarButton() { |
| + g_browser_process->profile_manager() |
| + ->GetProfileAttributesStorage() |
| + .RemoveObserver(this); |
| } |
| -void NewAvatarButton::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); |
| +bool AvatarButton::IsTriggerableEvent(const ui::Event& event) { |
| + return event.type() == ui::ET_GESTURE_LONG_PRESS || |
| + MenuButton::IsTriggerableEvent(event); |
| } |
| -void NewAvatarButton::OnAvatarErrorChanged() { |
| +void AvatarButton::OnAvatarErrorChanged() { |
| Update(); |
| } |
| -void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| +void AvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| Update(); |
| } |
| -void NewAvatarButton::OnProfileWasRemoved( |
| - const base::FilePath& profile_path, |
| - const base::string16& profile_name) { |
| +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 NewAvatarButton::OnProfileNameChanged( |
| - const base::FilePath& profile_path, |
| - const base::string16& old_profile_name) { |
| +void AvatarButton::OnProfileNameChanged( |
| + const base::FilePath& profile_path, |
| + const base::string16& old_profile_name) { |
| if (profile_->GetPath() == profile_path) |
| Update(); |
| } |
| -void NewAvatarButton::OnProfileSupervisedUserIdChanged( |
| - const base::FilePath& profile_path) { |
| +void AvatarButton::OnProfileSupervisedUserIdChanged( |
| + const base::FilePath& profile_path) { |
| if (profile_->GetPath() == profile_path) |
| Update(); |
| } |
| -void NewAvatarButton::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. |
| + |
|
msarda
2017/04/21 09:43:28
Remove blank line.
emx
2017/04/24 16:23:10
Done.
|
| const bool use_generic_button = |
| - !profile_->IsGuestSession() && |
| - storage.GetNumberOfProfiles() == 1 && |
| - !storage.GetAllProfilesAttributes().front()->IsAuthenticated(); |
| + !profile_->IsGuestSession() && storage.GetNumberOfProfiles() == 1 && |
| + !SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated(); |
| + |
| + UpdateButton(use_generic_button, profile_, &error_controller_); |
| + PreferredSizeChanged(); |
| + delegate_->ButtonPreferredSizeChanged(); |
| +} |
| + |
| +void AvatarButton::UpdateButton(bool use_generic_button, |
| + Profile* profile, |
| + AvatarButtonErrorController* error_controller) { |
| SetText(use_generic_button |
| ? base::string16() |
| - : profiles::GetAvatarButtonTextForProfile(profile_)); |
| + : profiles::GetAvatarButtonTextForProfile(profile)); |
| // If the button has no text, clear the text shadows to make sure the |
| // image is centered correctly. |
| @@ -200,7 +174,7 @@ void NewAvatarButton::Update() { |
| if (use_generic_button) { |
| SetImage(views::Button::STATE_NORMAL, generic_avatar_); |
| - } else if (error_controller_.HasAvatarError()) { |
| + } else if (error_controller->HasAvatarError()) { |
| SetImage(views::Button::STATE_NORMAL, |
| gfx::CreateVectorIcon(kSyncProblemIcon, 16, gfx::kGoogleRed700)); |
| } else { |
| @@ -211,7 +185,130 @@ void NewAvatarButton::Update() { |
| // the text and the possible authentication error icon. |
| const int kDefaultImageTextSpacing = 5; |
| SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
| +} |
| - PreferredSizeChanged(); |
| - delegate_->ButtonPreferredSizeChanged(); |
| +void AvatarButton::SetButtonAndBorder(const int button_idr, |
| + const int normal_image_set[], |
| + const int hover_image_set[], |
| + const int pressed_image_set[]) { |
| + ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| + SetBorder(CreateBorder(normal_image_set, hover_image_set, pressed_image_set)); |
| + generic_avatar_ = *rb->GetImageNamed(button_idr).ToImageSkia(); |
| +} |
| + |
| +//----------------------------------------------------------------------- |
| +//----------------------------------------------------------------------- |
| +//----------------------------------------------------------------------- |
| + |
| +NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, |
| + AvatarButtonStyle button_style, |
| + Profile* profile) |
| + : AvatarButton(delegate, profile) { |
| + 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); |
| + SetButtonAndBorder(IDR_AVATAR_THEMED_BUTTON_AVATAR, 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); |
| + SetButtonAndBorder(IDR_AVATAR_GLASS_BUTTON_AVATAR, 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); |
| + SetButtonAndBorder(IDR_AVATAR_NATIVE_BUTTON_AVATAR, kNormalImageSet, |
| + kHoverImageSet, kPressedImageSet); |
| + } |
| + |
| + Update(); |
| + SchedulePaint(); |
| +} |
| + |
| +NewAvatarButton::~NewAvatarButton() {} |
| + |
| +gfx::Size NewAvatarButton::GetPreferredSize() const { |
| + gfx::Size ps = views::MenuButton::GetPreferredSize(); |
| + ps.set_height(kButtonNonMdHeight); |
| + return ps; |
| +} |
| + |
| +//----------------------------------------------------------------------- |
| +//----------------------------------------------------------------------- |
| +//----------------------------------------------------------------------- |
| + |
| +MaterialDesignAvatarButton::MaterialDesignAvatarButton( |
| + AvatarButtonDelegate* delegate, |
| + Profile* profile, |
| + BrowserView* browser_view) |
| + : AvatarButton(delegate, profile), browser_view_(browser_view) { |
| + DCHECK(browser_view); |
| + |
| + // The MD button uses the same set of (completely transparent) borders |
| + // for all 3 states - highlighting is provided by the ink drop. |
| + const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_MD_BUTTON_NORMAL); |
| + SetButtonAndBorder(IDR_AVATAR_MD_BUTTON_AVATAR, kNormalImageSet, |
| + kNormalImageSet, kNormalImageSet); |
| + |
| + Update(); |
| + SchedulePaint(); |
| + |
| + 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(kRippleInkDropOpacity); |
| +} |
| + |
| +MaterialDesignAvatarButton::~MaterialDesignAvatarButton() {} |
| + |
| +gfx::Size MaterialDesignAvatarButton::GetPreferredSize() const { |
| + gfx::Size ps = views::MenuButton::GetPreferredSize(); |
| + ps.set_width( |
| + std::min(std::max(ps.width(), kMdButtonMinWidth), kMdButtonMaxWidth)); |
| + ps.set_height(browser_view_->frame()->GetMinimizeButtonHeight()); |
| + return ps; |
| +} |
| + |
| +std::unique_ptr<views::InkDrop> MaterialDesignAvatarButton::CreateInkDrop() { |
| + return CreateDefaultFloodFillInkDropImpl(); |
| +} |
| + |
| +std::unique_ptr<views::InkDropHighlight> |
| +MaterialDesignAvatarButton::CreateInkDropHighlight() const { |
| + auto center = gfx::RectF(gfx::Rect(size())).CenterPoint(); |
| + auto ink_drop_highlight = base::MakeUnique<views::InkDropHighlight>( |
| + size(), 0, center, GetInkDropBaseColor()); |
| + ink_drop_highlight->set_visible_opacity(kHighlightInkDropOpacity); |
| + return ink_drop_highlight; |
| +} |
| + |
| +std::unique_ptr<views::InkDropRipple> |
| +MaterialDesignAvatarButton::CreateInkDropRipple() const { |
| + return base::MakeUnique<views::FloodFillInkDropRipple>( |
| + size(), GetInkDropCenterBasedOnLastEvent(), GetInkDropBaseColor(), |
| + ink_drop_visible_opacity()); |
| +} |
| + |
| +void MaterialDesignAvatarButton::UpdateButtonHeightForPosition( |
| + const int button_x, |
| + int* button_height) const { |
| + TabStrip* tab_strip = browser_view_->tabstrip(); |
| + if (!tab_strip) |
| + return; |
| + |
| + // In RTL the new tab button is on the left, so it can never slide under the |
| + // avatar button (which is still on the right). May as well use the space. |
| + if (base::i18n::IsRTL()) |
| + return; |
| + |
| + // Use the "cozy" button if the tabstrip can slide under it |
| + if (tab_strip->max_x() >= button_x) { |
| + *button_height = kButtonMdCozyHeight; |
| + } |
| } |