Commit fc6f4893 authored by Abigail Klein's avatar Abigail Klein Committed by Chromium LUCI CQ

[Live Caption] Allow all toolbar buttons to get a promo highlight.

This ensures that the MediaToolbarButtonView, which inherits from
ToolbarButton, gets the promo highlight. Before this change, the promo
highlight was only applied to the BrowserAppMenuButton.

Screenshot: https://screenshot.googleplex.com/FiSjzR3C4EZdDcJ

Bug: 1055150
Change-Id: I6d8aae51731ca7c5d22283088f689ef16cfc88b0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2588643Reviewed-by: default avatarCollin Baker <collinbaker@chromium.org>
Commit-Queue: Abigail Klein <abigailbklein@google.com>
Cr-Commit-Position: refs/heads/master@{#836745}
parent 089470bd
......@@ -19,14 +19,11 @@
#include "chrome/browser/ui/toolbar/app_menu_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_view_class_properties.h"
#include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/app_menu.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_ink_drop_util.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/views/user_education/feature_promo_colors.h"
#include "chrome/browser/ui/views/user_education/feature_promo_controller_views.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
......@@ -34,7 +31,6 @@
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_features.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/gfx/animation/throb_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
......@@ -43,7 +39,6 @@
#include "ui/views/animation/animation_delegate_views.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_highlight.h"
#include "ui/views/animation/ink_drop_mask.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/controls/button/label_button_border.h"
#include "ui/views/metrics.h"
......@@ -54,89 +49,6 @@
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
namespace {
// Cycle duration of ink drop pulsing animation used for in-product help.
constexpr base::TimeDelta kFeaturePromoPulseDuration =
base::TimeDelta::FromMilliseconds(800);
// Max inset for pulsing animation.
constexpr float kFeaturePromoPulseInsetDip = 3.0f;
// An InkDropMask used to animate the size of the BrowserAppMenuButton's ink
// drop. This is used when showing in-product help.
class PulsingInkDropMask : public views::AnimationDelegateViews,
public views::InkDropMask {
public:
PulsingInkDropMask(views::View* layer_container,
const gfx::Size& layer_size,
const gfx::Insets& margins,
float normal_corner_radius,
float max_inset)
: AnimationDelegateViews(layer_container),
views::InkDropMask(layer_size),
layer_container_(layer_container),
margins_(margins),
normal_corner_radius_(normal_corner_radius),
max_inset_(max_inset),
throb_animation_(this) {
throb_animation_.SetThrobDuration(kFeaturePromoPulseDuration);
throb_animation_.StartThrobbing(-1);
}
private:
// views::InkDropMask:
void OnPaintLayer(const ui::PaintContext& context) override {
cc::PaintFlags flags;
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setAntiAlias(true);
ui::PaintRecorder recorder(context, layer()->size());
gfx::RectF bounds(layer()->bounds());
bounds.Inset(margins_);
const float current_inset =
throb_animation_.CurrentValueBetween(0.0f, max_inset_);
bounds.Inset(gfx::InsetsF(current_inset));
const float corner_radius = normal_corner_radius_ - current_inset;
recorder.canvas()->DrawRoundRect(bounds, corner_radius, flags);
}
// views::AnimationDelegateViews:
void AnimationProgressed(const gfx::Animation* animation) override {
DCHECK_EQ(animation, &throb_animation_);
layer()->SchedulePaint(gfx::Rect(layer()->size()));
// This is a workaround for crbug.com/935808: for scale factors >1,
// invalidating the mask layer doesn't cause the whole layer to be repainted
// on screen. TODO(crbug.com/935808): remove this workaround once the bug is
// fixed.
layer_container_->SchedulePaint();
}
// The View that contains the InkDrop layer we're masking. This must outlive
// our instance.
views::View* const layer_container_;
// Margins between the layer bounds and the visible ink drop. We use this
// because sometimes the View we're masking is larger than the ink drop we
// want to show.
const gfx::Insets margins_;
// Normal corner radius of the ink drop without animation. This is also the
// corner radius at the largest instant of the animation.
const float normal_corner_radius_;
// Max inset, used at the smallest instant of the animation.
const float max_inset_;
gfx::ThrobAnimation throb_animation_;
};
} // namespace
// static
bool BrowserAppMenuButton::g_open_app_immediately_for_testing = false;
......@@ -218,16 +130,6 @@ void BrowserAppMenuButton::HandleMenuClosed() {
reopen_tab_promo_handle_.reset();
}
void BrowserAppMenuButton::AfterPropertyChange(const void* key,
int64_t old_value) {
if (key != kHasInProductHelpPromoKey)
return;
// FeaturePromoControllerViews sets the following property when a
// bubble is showing. When the state changes, update our highlight for
// the promo.
SetHasInProductHelpPromo(GetProperty(kHasInProductHelpPromoKey));
}
void BrowserAppMenuButton::UpdateTextAndHighlightColor() {
int tooltip_message_id;
base::string16 text;
......@@ -270,51 +172,10 @@ void BrowserAppMenuButton::UpdateTextAndHighlightColor() {
SetHighlight(text, color);
}
void BrowserAppMenuButton::SetHasInProductHelpPromo(
bool has_in_product_help_promo) {
if (has_in_product_help_promo_ == has_in_product_help_promo)
return;
has_in_product_help_promo_ = has_in_product_help_promo;
// We override GetInkDropBaseColor() and CreateInkDropMask(), returning the
// promo values if we are showing an in-product help promo. Calling
// HostSizeChanged() will force the new mask and color to be fetched.
//
// TODO(collinbaker): Consider adding explicit way to recreate mask instead of
// relying on HostSizeChanged() to do so.
GetInkDrop()->HostSizeChanged(size());
views::InkDropState next_state;
if (has_in_product_help_promo_ || IsMenuShowing()) {
// If we are showing a promo, we must use the ACTIVATED state to show the
// highlight. Otherwise, if the menu is currently showing, we need to keep
// the ink drop in the ACTIVATED state.
next_state = views::InkDropState::ACTIVATED;
} else {
// If we are not showing a promo and the menu is hidden, we use the
// DEACTIVATED state.
next_state = views::InkDropState::DEACTIVATED;
// TODO(collinbaker): this is brittle since we don't know if something else
// should keep this ACTIVATED or in some other state. Consider adding code
// to track the correct state and restore to that.
}
GetInkDrop()->AnimateToState(next_state);
UpdateIcon();
SchedulePaint();
}
const char* BrowserAppMenuButton::GetClassName() const {
return "BrowserAppMenuButton";
}
SkColor BrowserAppMenuButton::GetForegroundColor(ButtonState state) const {
return has_in_product_help_promo_
? GetFeaturePromoHighlightColorForToolbar(GetThemeProvider())
: ToolbarButton::GetForegroundColor(state);
}
bool BrowserAppMenuButton::GetDropFormats(
int* formats,
std::set<ui::ClipboardFormatType>* format_types) {
......@@ -366,37 +227,6 @@ BrowserAppMenuButton::CreateInkDropHighlight() const {
return CreateToolbarInkDropHighlight(this);
}
std::unique_ptr<views::InkDropMask> BrowserAppMenuButton::CreateInkDropMask()
const {
if (has_in_product_help_promo_) {
// This gets the latest ink drop insets. |SetTrailingMargin()| is called
// whenever our margins change (i.e. due to the window maximizing or
// minimizing) and updates our internal padding property accordingly.
const gfx::Insets ink_drop_insets = GetToolbarInkDropInsets(this);
const float corner_radius =
(height() - ink_drop_insets.top() - ink_drop_insets.bottom()) / 2.0f;
return std::make_unique<PulsingInkDropMask>(ink_drop_container(), size(),
ink_drop_insets, corner_radius,
kFeaturePromoPulseInsetDip);
}
return AppMenuButton::CreateInkDropMask();
}
SkColor BrowserAppMenuButton::GetInkDropBaseColor() const {
return has_in_product_help_promo_
? GetFeaturePromoHighlightColorForToolbar(GetThemeProvider())
: AppMenuButton::GetInkDropBaseColor();
}
base::string16 BrowserAppMenuButton::GetTooltipText(const gfx::Point& p) const {
// Suppress tooltip when IPH is showing.
if (has_in_product_help_promo_)
return base::string16();
return AppMenuButton::GetTooltipText(p);
}
void BrowserAppMenuButton::OnTouchUiChanged() {
UpdateColorsAndInsets();
PreferredSizeChanged();
......
......@@ -55,23 +55,11 @@ class BrowserAppMenuButton : public AppMenuButton {
int OnPerformDrop(const ui::DropTargetEvent& event) override;
std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight()
const override;
std::unique_ptr<views::InkDropMask> CreateInkDropMask() const override;
SkColor GetInkDropBaseColor() const override;
base::string16 GetTooltipText(const gfx::Point& p) const override;
void OnThemeChanged() override;
// Updates the presentation according to |severity_| and the theme provider.
void UpdateIcon() override;
void HandleMenuClosed() override;
// ui::PropertyHandler:
void AfterPropertyChange(const void* key, int64_t old_value) override;
protected:
// If the button is being used as an anchor for a promo, returns the best
// promo color given the current background color. Otherwise, returns the
// standard ToolbarButton foreground color for the given |state|.
SkColor GetForegroundColor(ButtonState state) const override;
private:
void OnTouchUiChanged();
......@@ -86,9 +74,6 @@ class BrowserAppMenuButton : public AppMenuButton {
// Our owning toolbar view.
ToolbarView* const toolbar_view_;
// Determines whether to highlight the button for in-product help.
bool has_in_product_help_promo_ = false;
base::Optional<FeaturePromoController::PromoHandle> reopen_tab_promo_handle_;
base::CallbackListSubscription subscription_ =
......
......@@ -4,6 +4,7 @@
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include <memory>
#include <utility>
#include "base/bind.h"
......@@ -21,11 +22,14 @@
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/chrome_view_class_properties.h"
#include "chrome/browser/ui/views/toolbar/toolbar_ink_drop_util.h"
#include "chrome/browser/ui/views/user_education/feature_promo_colors.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/models/image_model.h"
#include "ui/base/models/menu_model.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/color_utils.h"
......@@ -34,6 +38,7 @@
#include "ui/gfx/text_utils.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_highlight.h"
#include "ui/views/animation/ink_drop_mask.h"
#include "ui/views/animation/installable_ink_drop.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/label_button_border.h"
......@@ -57,6 +62,85 @@ SkColor GetDefaultTextColor(const ui::ThemeProvider* theme_provider) {
theme_provider->GetColor(ThemeProperties::COLOR_TOOLBAR));
}
// Cycle duration of ink drop pulsing animation used for in-product help.
constexpr base::TimeDelta kFeaturePromoPulseDuration =
base::TimeDelta::FromMilliseconds(800);
// Max inset for pulsing animation.
constexpr float kFeaturePromoPulseInsetDip = 3.0f;
// An InkDropMask used to animate the size of the BrowserAppMenuButton's ink
// drop. This is used when showing in-product help.
class PulsingInkDropMask : public views::AnimationDelegateViews,
public views::InkDropMask {
public:
PulsingInkDropMask(views::View* layer_container,
const gfx::Size& layer_size,
const gfx::Insets& margins,
float normal_corner_radius,
float max_inset)
: AnimationDelegateViews(layer_container),
views::InkDropMask(layer_size),
layer_container_(layer_container),
margins_(margins),
normal_corner_radius_(normal_corner_radius),
max_inset_(max_inset),
throb_animation_(this) {
throb_animation_.SetThrobDuration(kFeaturePromoPulseDuration);
throb_animation_.StartThrobbing(-1);
}
private:
// views::InkDropMask:
void OnPaintLayer(const ui::PaintContext& context) override {
cc::PaintFlags flags;
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setAntiAlias(true);
ui::PaintRecorder recorder(context, layer()->size());
gfx::RectF bounds(layer()->bounds());
bounds.Inset(margins_);
const float current_inset =
throb_animation_.CurrentValueBetween(0.0f, max_inset_);
bounds.Inset(gfx::InsetsF(current_inset));
const float corner_radius = normal_corner_radius_ - current_inset;
recorder.canvas()->DrawRoundRect(bounds, corner_radius, flags);
}
// views::AnimationDelegateViews:
void AnimationProgressed(const gfx::Animation* animation) override {
DCHECK_EQ(animation, &throb_animation_);
layer()->SchedulePaint(gfx::Rect(layer()->size()));
// This is a workaround for crbug.com/935808: for scale factors >1,
// invalidating the mask layer doesn't cause the whole layer to be repainted
// on screen. TODO(crbug.com/935808): remove this workaround once the bug is
// fixed.
layer_container_->SchedulePaint();
}
// The View that contains the InkDrop layer we're masking. This must outlive
// our instance.
views::View* const layer_container_;
// Margins between the layer bounds and the visible ink drop. We use this
// because sometimes the View we're masking is larger than the ink drop we
// want to show.
const gfx::Insets margins_;
// Normal corner radius of the ink drop without animation. This is also the
// corner radius at the largest instant of the animation.
const float normal_corner_radius_;
// Max inset, used at the smallest instant of the animation.
const float max_inset_;
gfx::ThrobAnimation throb_animation_;
};
} // namespace
ToolbarButton::ToolbarButton(PressedCallback callback)
......@@ -184,6 +268,8 @@ void ToolbarButton::UpdateColorsAndInsets() {
SkColor ToolbarButton::GetForegroundColor(ButtonState state) const {
const ui::ThemeProvider* tp = GetThemeProvider();
DCHECK(tp);
if (has_in_product_help_promo_)
return GetFeaturePromoHighlightColorForToolbar(tp);
switch (state) {
case ButtonState::STATE_HOVERED:
return tp->GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON_HOVERED);
......@@ -382,6 +468,12 @@ void ToolbarButton::GetAccessibleNodeData(ui::AXNodeData* node_data) {
node_data->SetHasPopup(ax::mojom::HasPopup::kMenu);
}
base::string16 ToolbarButton::GetTooltipText(const gfx::Point& p) const {
// Suppress tooltip when IPH is showing.
return has_in_product_help_promo_ ? base::string16()
: views::LabelButton::GetTooltipText(p);
}
std::unique_ptr<views::InkDrop> ToolbarButton::CreateInkDrop() {
// Ensure this doesn't get called when InstallableInkDrops are enabled.
DCHECK(!base::FeatureList::IsEnabled(views::kInstallableInkDropFeature));
......@@ -395,9 +487,27 @@ std::unique_ptr<views::InkDropHighlight> ToolbarButton::CreateInkDropHighlight()
return CreateToolbarInkDropHighlight(this);
}
std::unique_ptr<views::InkDropMask> ToolbarButton::CreateInkDropMask() const {
if (has_in_product_help_promo_) {
// This gets the latest ink drop insets. |SetTrailingMargin()| is called
// whenever our margins change (i.e. due to the window maximizing or
// minimizing) and updates our internal padding property accordingly.
const gfx::Insets ink_drop_insets = GetToolbarInkDropInsets(this);
const float corner_radius =
(height() - ink_drop_insets.top() - ink_drop_insets.bottom()) / 2.0f;
return std::make_unique<PulsingInkDropMask>(ink_drop_container(), size(),
ink_drop_insets, corner_radius,
kFeaturePromoPulseInsetDip);
}
return views::LabelButton::CreateInkDropMask();
}
SkColor ToolbarButton::GetInkDropBaseColor() const {
// Ensure this doesn't get called when InstallableInkDrops are enabled.
DCHECK(!base::FeatureList::IsEnabled(views::kInstallableInkDropFeature));
if (has_in_product_help_promo_)
return GetFeaturePromoHighlightColorForToolbar(GetThemeProvider());
base::Optional<SkColor> drop_base_color =
highlight_color_animation_.GetInkDropBaseColor();
if (drop_base_color)
......@@ -421,6 +531,45 @@ void ToolbarButton::ShowContextMenuForViewImpl(View* source,
ShowDropDownMenu(source_type);
}
void ToolbarButton::AfterPropertyChange(const void* key, int64_t old_value) {
if (key == kHasInProductHelpPromoKey)
SetHasInProductHelpPromo(GetProperty(kHasInProductHelpPromoKey));
}
void ToolbarButton::SetHasInProductHelpPromo(bool has_in_product_help_promo) {
if (has_in_product_help_promo_ == has_in_product_help_promo)
return;
has_in_product_help_promo_ = has_in_product_help_promo;
// We override GetInkDropBaseColor() and CreateInkDropMask(), returning the
// promo values if we are showing an in-product help promo. Calling
// HostSizeChanged() will force the new mask and color to be fetched.
//
// TODO(collinbaker): Consider adding explicit way to recreate mask instead
// of relying on HostSizeChanged() to do so.
GetInkDrop()->HostSizeChanged(size());
views::InkDropState next_state;
if (has_in_product_help_promo_ || GetVisible()) {
// If we are showing a promo, we must use the ACTIVATED state to show the
// highlight. Otherwise, if the menu is currently showing, we need to keep
// the ink drop in the ACTIVATED state.
next_state = views::InkDropState::ACTIVATED;
} else {
// If we are not showing a promo and the menu is hidden, we use the
// DEACTIVATED state.
next_state = views::InkDropState::DEACTIVATED;
// TODO(collinbaker): this is brittle since we don't know if something
// else should keep this ACTIVATED or in some other state. Consider adding
// code to track the correct state and restore to that.
}
GetInkDrop()->AnimateToState(next_state);
UpdateIcon();
SchedulePaint();
}
// static
SkColor ToolbarButton::AdjustHighlightColorForContrast(
const ui::ThemeProvider* theme_provider,
......
......@@ -103,9 +103,11 @@ class ToolbarButton : public views::LabelButton,
void OnMouseExited(const ui::MouseEvent& event) override;
void OnGestureEvent(ui::GestureEvent* event) override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
base::string16 GetTooltipText(const gfx::Point& p) const override;
std::unique_ptr<views::InkDrop> CreateInkDrop() override;
std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight()
const override;
std::unique_ptr<views::InkDropMask> CreateInkDropMask() const override;
views::InkDrop* GetInkDrop() override;
SkColor GetInkDropBaseColor() const override;
......@@ -114,6 +116,9 @@ class ToolbarButton : public views::LabelButton,
const gfx::Point& point,
ui::MenuSourceType source_type) override;
// ui::PropertyHandler:
void AfterPropertyChange(const void* key, int64_t old_value) override;
ui::MenuModel* menu_model_for_test() { return model_.get(); }
// Chooses from |desired_dark_color| and |desired_light_color| based on
......@@ -233,6 +238,11 @@ class ToolbarButton : public views::LabelButton,
// instead which sets an optional color as well.
void SetText(const base::string16& text) override;
// Sets the in product help promo. Called after the kHasInProductHelpPromoKey
// property changes. When this button has an in product help promo, the button
// gets a blue highlight that pulses.
void SetHasInProductHelpPromo(bool has_in_product_help_promo);
// The model that populates the attached menu.
std::unique_ptr<ui::MenuModel> model_;
......@@ -245,6 +255,9 @@ class ToolbarButton : public views::LabelButton,
// event.
const bool trigger_menu_on_long_press_;
// Determines whether to highlight the button for in-product help.
bool has_in_product_help_promo_ = false;
// Y position of mouse when left mouse button is pressed.
int y_position_on_lbuttondown_ = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment