1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// Copyright 2024 The Chromium Authors
// 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/page_action/page_action_view.h"
#include <utility>
#include "base/callback_list.h"
#include "base/functional/bind.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h"
#include "chrome/browser/ui/views/page_action/page_action_controller.h"
#include "chrome/browser/ui/views/page_action/page_action_model.h"
#include "chrome/browser/ui/views/page_action/page_action_triggers.h"
#include "chrome/browser/ui/views/page_action/page_action_view_params.h"
#include "ui/actions/actions.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/view_class_properties.h"
namespace page_actions {
PageActionView::PageActionView(actions::ActionItem* action_item,
const PageActionViewParams& params,
ui::ElementIdentifier element_identifier)
: IconLabelBubbleView(gfx::FontList(), params.icon_label_bubble_delegate),
action_item_(action_item->GetAsWeakPtr()),
icon_size_(params.icon_size),
icon_insets_(params.icon_insets) {
CHECK(action_item_->GetActionId().has_value());
SetUpForAnimation(base::Milliseconds(600));
SetProperty(views::kElementIdentifierKey, element_identifier);
if (params.font_list) {
SetFontList(*params.font_list);
}
image_container_view()->SetFlipCanvasOnPaintForRTLUI(true);
views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::ON);
SetVisible(false);
label()->SetVisible(false);
SetUseTonalColorsWhenExpanded(true);
SetBackgroundVisibility(BackgroundVisibility::kWithLabel);
UpdateBorder();
SetExpandedLabelAdditionalInsets(views::Inset1D(4, 8));
label_visibility_changed_subscription_ =
label()->AddVisibleChangedCallback(base::BindRepeating(
&PageActionView::OnLabelVisibilityChanged, base::Unretained(this)));
}
PageActionView::~PageActionView() = default;
bool PageActionView::IsChipVisible() const {
return ShouldShowLabel();
}
base::CallbackListSubscription PageActionView::AddChipVisibilityChangedCallback(
ChipVisibilityChanged callback) {
return chip_visibility_changed_callbacks_.Add(std::move(callback));
}
void PageActionView::OnNewActiveController(PageActionController* controller) {
observation_.Reset();
action_item_controller_subscription_ = {};
if (controller) {
click_callback_ =
controller->GetClickCallback(action_item_->GetActionId().value());
controller->AddObserver(action_item_->GetActionId().value(), observation_);
// TODO(crbug.com/388524315): Have the controller manage its own ActionItem
// observation. See bug for more explanation.
action_item_controller_subscription_ =
controller->CreateActionItemSubscription(action_item_.get());
OnPageActionModelChanged(*observation_.GetSource());
} else {
SetVisible(false);
}
}
void PageActionView::OnPageActionModelChanged(
const PageActionModelInterface& model) {
SetEnabled(model.GetVisible());
SetVisible(model.GetVisible());
SetLabel(model.GetText(), model.GetAccessibleName());
SetTooltipText(model.GetTooltipText());
UpdateIconImage();
const bool was_chip_visible = IsChipVisible();
if (!model.GetVisible()) {
ResetSlideAnimation(/*show=*/false);
} else if (!model.GetShouldAnimateChip()) {
ResetSlideAnimation(/*show=*/model.GetShowSuggestionChip());
} else if (model.GetShowSuggestionChip()) {
AnimateIn(/*string_id=*/std::nullopt);
} else {
AnimateOut();
}
// Announce the chip only if announcements are enabled and the chip was
// newly shown.
if (model.GetShouldAnnounceChip() && !was_chip_visible && IsChipVisible()) {
GetViewAccessibility().AnnounceAlert(label()->GetText());
}
}
void PageActionView::OnPageActionModelWillBeDeleted(
const PageActionModelInterface& model) {
observation_.Reset();
action_item_controller_subscription_ = {};
SetVisible(false);
}
actions::ActionId PageActionView::GetActionId() const {
return action_item_->GetActionId().value();
}
void PageActionView::OnThemeChanged() {
IconLabelBubbleView::OnThemeChanged();
UpdateIconImage();
}
void PageActionView::OnTouchUiChanged() {
IconLabelBubbleView::OnTouchUiChanged();
UpdateIconImage();
}
void PageActionView::ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) {
View::ViewHierarchyChanged(details);
if (details.is_add && details.child == this) {
UpdateIconImage();
}
}
void PageActionView::UpdateBorder() {
gfx::Insets border_insets = icon_insets_;
// If the icon is not a vector icon ,
// and the view is in its chip state, apply chip padding.
if (observation_.IsObserving() &&
!observation_.GetSource()->GetImage().IsVectorIcon()) {
border_insets =
gfx::Insets::VH(GetLayoutConstant(LOCATION_BAR_CHILD_INTERIOR_PADDING),
GetLayoutConstant(LOCATION_BAR_CHIP_PADDING));
}
SetBorder(views::CreateEmptyBorder(border_insets));
}
bool PageActionView::ShouldShowSeparator() const {
return false;
}
bool PageActionView::ShouldShowLabelAfterAnimation() const {
return ShouldShowLabel();
}
bool PageActionView::ShouldUpdateInkDropOnClickCanceled() const {
return true;
}
void PageActionView::NotifyClick(const ui::Event& event) {
IconLabelBubbleView::NotifyClick(event);
PageActionTrigger trigger_source;
if (event.IsMouseEvent()) {
trigger_source = PageActionTrigger::kMouse;
} else if (event.IsKeyEvent()) {
trigger_source = PageActionTrigger::kKeyboard;
} else {
CHECK(event.IsGestureEvent());
trigger_source = PageActionTrigger::kGesture;
}
action_item_->InvokeAction(
actions::ActionInvocationContext::Builder()
.SetProperty(kPageActionTriggerKey,
static_cast<std::underlying_type_t<PageActionTrigger>>(
trigger_source))
.Build());
CHECK(click_callback_);
click_callback_.Run(trigger_source);
}
void PageActionView::UpdateIconImage() {
if (observation_.GetSource() == nullptr ||
observation_.GetSource()->GetImage().IsEmpty()) {
return;
}
const auto& icon_image = observation_.GetSource()->GetImage();
// If image does not have a vector icon, set it directly.
if (icon_image.IsVectorIcon()) {
const gfx::ImageSkia image =
gfx::CreateVectorIcon(*icon_image.GetVectorIcon().vector_icon(),
icon_size_, GetForegroundColor());
if (!image.isNull()) {
SetImageModel(ui::ImageModel::FromImageSkia(image));
}
} else {
SetImageModel(icon_image);
// For non-vector icons, the border needs to be updated to accommodate the
// icon, as the icon size may vary. For vector icons, the border gets
// set on instantiation and does not need to be updated again.
UpdateBorder();
}
}
void PageActionView::SetModel(PageActionModelInterface* model) {
observation_.Reset();
observation_.Observe(model);
}
gfx::Size PageActionView::GetMinimumSize() const {
gfx::Size icon_preferred_size = image_container_view()->GetPreferredSize();
icon_preferred_size.Enlarge(icon_insets_.width(), icon_insets_.height());
return icon_preferred_size;
}
bool PageActionView::IsBubbleShowing() const {
return observation_.IsObserving() &&
observation_.GetSource()->GetActionItemIsShowingBubble();
}
bool PageActionView::IsTriggerableEvent(const ui::Event& event) {
// Returns whether the bubble should be shown given the event. Only trigger an
// action when action UI isn't already showing (managed at the
// IconLabelBubbleView level), and if mouse input, when event is a left button
// click.
if (event.IsMouseEvent()) {
// IconLabelBubbleView allows any mouse click to be triggerable event so
// need to manually check here.
return IconLabelBubbleView::IsTriggerableEvent(event) &&
((GetTriggerableEventFlags() & event.flags()) != 0);
}
return IconLabelBubbleView::IsTriggerableEvent(event);
}
void PageActionView::OnLabelVisibilityChanged() {
UpdateBackground();
UpdateLabelColors();
UpdateIconImage();
chip_visibility_changed_callbacks_.Notify(this);
}
views::View* PageActionView::GetLabelForTesting() {
return label();
}
gfx::SlideAnimation& PageActionView::GetSlideAnimationForTesting() {
return slide_animation_;
}
BEGIN_METADATA(PageActionView)
END_METADATA
} // namespace page_actions
|