Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3340)

Unified Diff: chrome/browser/ui/views/profiles/new_avatar_button.cc

Issue 2832823002: Update avatar button to MD (Closed)
Patch Set: Fix for --force-device-scale-factor, vector icon, review comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..39d7099af5390a62055ba3e968dda673a8920bb4 100644
--- a/chrome/browser/ui/views/profiles/new_avatar_button.cc
+++ b/chrome/browser/ui/views/profiles/new_avatar_button.cc
@@ -12,8 +12,11 @@
#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/frame/minimize_button_metrics_win.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 +25,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,148 +35,86 @@
#include "base/win/windows_version.h"
#endif
-namespace {
-
-std::unique_ptr<views::Border> CreateBorder(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;
- 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()),
+// 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 = 20;
+const int kButtonMdIconHeight = 16;
+const SkColor kButtonMdIconColor = SkColorSetARGB(138, 0, 0, 0); // Alpha 54%
+// 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;
+
+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);
+AvatarButton::~AvatarButton() {
+ g_browser_process->profile_manager()
+ ->GetProfileAttributesStorage()
+ .RemoveObserver(this);
}
-void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) {
- if (suppress_mouse_released_action_)
- suppress_mouse_released_action_ = false;
- else
- LabelButton::OnMouseReleased(event);
+bool AvatarButton::IsTriggerableEvent(const ui::Event& event) {
+ return event.type() == ui::ET_GESTURE_LONG_PRESS ||
+ MenuButton::IsTriggerableEvent(event);
}
-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);
-}
-
-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();
@@ -179,13 +122,21 @@ void NewAvatarButton::Update() {
// 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 &&
- !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 +151,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 +162,157 @@ void NewAvatarButton::Update() {
// the text and the possible authentication error icon.
const int kDefaultImageTextSpacing = 5;
SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing);
+}
- PreferredSizeChanged();
- delegate_->ButtonPreferredSizeChanged();
+//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
+
+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);
+ SetButtonAvatar(IDR_AVATAR_THEMED_BUTTON_AVATAR);
+ SetBorder(CreateBorder(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(CreateBorder(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(CreateBorder(kNormalImageSet, kHoverImageSet, kPressedImageSet));
+ }
+
+ Update();
+ SchedulePaint();
+}
+
+NewAvatarButton::~NewAvatarButton() {}
+
+gfx::Size NewAvatarButton::GetPreferredSize() const {
+ gfx::Size ps = views::MenuButton::GetPreferredSize();
Evan Stade 2017/04/27 01:30:55 please don't use abbreviations like this for varia
emx 2017/04/27 16:30:58 Done.
+ ps.set_height(kButtonNonMdHeight);
+ return ps;
+}
+
+std::unique_ptr<views::Border> NewAvatarButton::CreateBorder(
+ const int normal_image_set[],
+ const int hot_image_set[],
+ const int pushed_image_set[]) const {
+ 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;
+ border->set_insets(
+ gfx::Insets(kTopInset, kLeftRightInset, kBottomInset, kLeftRightInset));
+
+ return std::move(border);
+}
+
+void NewAvatarButton::SetButtonAvatar(const int avatar_idr) {
+ ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
+ set_generic_avatar(*rb->GetImageNamed(avatar_idr).ToImageSkia());
+}
+
+//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
+
+MaterialDesignAvatarButton::MaterialDesignAvatarButton(
+ AvatarButtonDelegate* delegate,
+ Profile* profile,
+ BrowserView* browser_view)
+ : AvatarButton(delegate, profile), browser_view_(browser_view) {
+ DCHECK(browser_view);
+
+ set_generic_avatar(gfx::CreateVectorIcon(
+ kAccountCircleIcon, kButtonMdIconHeight, kButtonMdIconColor));
+ SetBorder(CreateBorder());
+
+ 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(MinimizeButtonMetrics::GetCaptionButtonHeight());
+ return ps;
+}
+
+std::unique_ptr<views::Border> MaterialDesignAvatarButton::CreateBorder()
+ const {
+ // 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);
+}
+
+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;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698