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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// Copyright 2022 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/toolbar/toolbar_action_hover_card_bubble_view.h"
#include <string>
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/extensions/extensions_dialogs_utils.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/extension_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/flex_layout.h"
namespace {
using HoverCardState = ToolbarActionViewController::HoverCardState;
// Hover card fixed width. Toolbar actions are not visible when window is too
// small to display them, therefore hover cards wouldn't be displayed if the
// window is not big enough.
constexpr int kHoverCardWidth = 240;
// Hover card margins.
// TODO(crbug.com/1351778): Move to a base hover card class.
constexpr int kHorizontalMargin = 18;
constexpr int kVerticalMargin = 10;
std::u16string GetSiteAccessTitle(
ToolbarActionViewController::HoverCardState::SiteAccess state) {
int title_id = -1;
switch (state) {
case HoverCardState::SiteAccess::kAllExtensionsAllowed:
case HoverCardState::SiteAccess::kExtensionHasAccess:
title_id = IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_TITLE_HAS_ACCESS;
break;
case HoverCardState::SiteAccess::kAllExtensionsBlocked:
title_id = IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_TITLE_BLOCKED_ACCESS;
break;
case HoverCardState::SiteAccess::kExtensionRequestsAccess:
title_id = IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_TITLE_REQUESTS_ACCESS;
break;
case HoverCardState::SiteAccess::kExtensionDoesNotWantAccess:
NOTREACHED_NORETURN();
}
return l10n_util::GetStringUTF16(title_id);
}
std::u16string GetSiteAccessDescription(HoverCardState::SiteAccess state,
std::u16string host) {
int title_id = -1;
switch (state) {
case HoverCardState::SiteAccess::kAllExtensionsAllowed:
title_id =
IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_DESCRIPTION_ALL_EXTENSIONS_ALLOWED_ACCESS;
break;
case HoverCardState::SiteAccess::kAllExtensionsBlocked:
title_id =
IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_DESCRIPTION_ALL_EXTENSIONS_BLOCKED_ACCESS;
break;
case HoverCardState::SiteAccess::kExtensionHasAccess:
title_id =
IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_DESCRIPTION_EXTENSION_HAS_ACCESS;
break;
case HoverCardState::SiteAccess::kExtensionRequestsAccess:
title_id =
IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_DESCRIPTION_EXTENSION_REQUESTS_ACCESS;
break;
case HoverCardState::SiteAccess::kExtensionDoesNotWantAccess:
NOTREACHED_NORETURN();
}
return l10n_util::GetStringFUTF16(title_id, host);
}
std::u16string GetPolicyText(HoverCardState::AdminPolicy state) {
int text_id = -1;
switch (state) {
case HoverCardState::AdminPolicy::kPinnedByAdmin:
text_id =
IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_POLICY_LABEL_PINNED_TEXT;
break;
case HoverCardState::AdminPolicy::kInstalledByAdmin:
text_id =
IDS_EXTENSIONS_TOOLBAR_ACTION_HOVER_CARD_POLICY_LABEL_INSTALLED_TEXT;
break;
case HoverCardState::AdminPolicy::kNone:
NOTREACHED_NORETURN();
}
return l10n_util::GetStringUTF16(text_id);
}
// Label that renders its background in a solid color. Placed in front of a
// normal label either by being later in the draw order or on a layer, it can
// be used to animate a fade-out.
class SolidLabel : public views::Label {
public:
METADATA_HEADER(SolidLabel);
using Label::Label;
SolidLabel() = default;
~SolidLabel() override = default;
protected:
// views::Label:
void OnPaintBackground(gfx::Canvas* canvas) override {
canvas->DrawColor(GetBackgroundColor());
}
};
BEGIN_METADATA(SolidLabel, views::Label)
END_METADATA
} // namespace
// ToolbarActionHoverCardBubbleView::FadeLabel:
// ----------------------------------------------------------
// This view overlays and fades out an old version of the text of a label,
// while displaying the new text underneath. It is used to fade out the old
// value of the title and domain labels on the hover card when the tab switches
// or the tab title changes.
// TODO(crbug.com/1354321): ToolbarActionHoverCarBubbleView has the same
// FadeLabel. Move it to its own shared file.
class ToolbarActionHoverCardBubbleView::FadeLabel : public views::View {
public:
explicit FadeLabel(int context) {
primary_label_ = AddChildView(std::make_unique<views::Label>(
std::u16string(), context, views::style::STYLE_PRIMARY));
primary_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
primary_label_->SetVerticalAlignment(gfx::ALIGN_TOP);
primary_label_->SetMultiLine(true);
label_fading_out_ = AddChildView(std::make_unique<SolidLabel>(
std::u16string(), context, views::style::STYLE_PRIMARY));
label_fading_out_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
label_fading_out_->SetVerticalAlignment(gfx::ALIGN_TOP);
label_fading_out_->SetMultiLine(true);
label_fading_out_->GetViewAccessibility().OverrideIsIgnored(true);
SetLayoutManager(std::make_unique<views::FillLayout>());
}
~FadeLabel() override = default;
void SetText(std::u16string text) {
label_fading_out_->SetText(primary_label_->GetText());
primary_label_->SetText(text);
}
// Sets the fade-out of the label as `percent` in the range [0, 1]. Since
// FadeLabel is designed to mask new text with the old and then fade away, the
// higher the percentage the less opaque the label.
void SetFade(double percent) {
percent_ = std::min(1.0, percent);
if (percent_ == 1.0)
label_fading_out_->SetText(std::u16string());
const SkAlpha alpha = base::saturated_cast<SkAlpha>(
std::numeric_limits<SkAlpha>::max() * (1.0 - percent_));
label_fading_out_->SetBackgroundColor(
SkColorSetA(label_fading_out_->GetBackgroundColor(), alpha));
label_fading_out_->SetEnabledColor(
SkColorSetA(label_fading_out_->GetEnabledColor(), alpha));
}
void SetBackgroundColorId(ui::ColorId background_color_id) {
background_color_id_ = background_color_id;
}
void OnThemeChanged() override {
views::View::OnThemeChanged();
if (background_color_id_.has_value()) {
label_fading_out_->SetBackgroundColor(
GetColorProvider()->GetColor(background_color_id_.value()));
SetFade(percent_);
}
}
std::u16string GetText() const { return primary_label_->GetText(); }
protected:
// views::View:
gfx::Size GetMaximumSize() const override {
return gfx::Tween::SizeValueBetween(percent_,
label_fading_out_->GetPreferredSize(),
primary_label_->GetPreferredSize());
}
gfx::Size CalculatePreferredSize() const override {
return primary_label_->GetPreferredSize();
}
gfx::Size GetMinimumSize() const override {
return primary_label_->GetMinimumSize();
}
int GetHeightForWidth(int width) const override {
return primary_label_->GetHeightForWidth(width);
}
private:
raw_ptr<views::Label> primary_label_;
raw_ptr<SolidLabel> label_fading_out_;
double percent_ = 1.0;
absl::optional<ui::ColorId> background_color_id_;
};
// ToolbarActionHoverCardBubbleView:
// ----------------------------------------------------------
ToolbarActionHoverCardBubbleView::ToolbarActionHoverCardBubbleView(
ToolbarActionView* action_view)
: BubbleDialogDelegateView(action_view,
views::BubbleBorder::TOP_LEFT,
views::BubbleBorder::STANDARD_SHADOW) {
DCHECK(base::FeatureList::IsEnabled(
extensions_features::kExtensionsMenuAccessControl));
// Remove dialog's default buttons.
SetButtons(ui::DIALOG_BUTTON_NONE);
// Remove the accessible role so that hover cards are not read when they
// appear because tabs handle accessibility text.
SetAccessibleWindowRole(ax::mojom::Role::kNone);
// We'll do all of our own layout inside the bubble, so no need to inset this
// view inside the client view.
set_margins(gfx::Insets());
// Set so that when hovering over a toolbar action in a inactive window that
// window will not become active. Setting this to false creates the need to
// explicitly hide the hovercard on press, touch, and keyboard events.
SetCanActivate(false);
#if BUILDFLAG(IS_MAC)
set_accept_events(false);
#endif
// Set so that the toolbar action hover card is not focus traversable when
// keyboard navigating through the tab strip.
set_focus_traversable_from_anchor_view(false);
set_fixed_width(kHoverCardWidth);
// Let anchor point handle its own highlight, since the hover card is the
// same for multiple anchor points.
set_highlight_button_when_shown(false);
// Set up layout.
views::FlexLayout* const layout =
SetLayoutManager(std::make_unique<views::FlexLayout>());
layout->SetOrientation(views::LayoutOrientation::kVertical);
layout->SetMainAxisAlignment(views::LayoutAlignment::kStart);
layout->SetCrossAxisAlignment(views::LayoutAlignment::kStretch);
layout->SetCollapseMargins(true);
corner_radius_ = ChromeLayoutProvider::Get()->GetCornerRadiusMetric(
views::Emphasis::kHigh);
// Set up content.
auto create_label = [](int context, gfx::Insets insets) {
auto label = std::make_unique<FadeLabel>(context);
label->SetProperty(views::kMarginsKey, insets);
label->SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
views::MaximumFlexSizeRule::kScaleToMaximum));
return label;
};
auto create_separator = []() {
auto separator = std::make_unique<views::Separator>();
separator->SetProperty(views::kMarginsKey,
gfx::Insets::VH(kVerticalMargin, 0));
return separator;
};
title_label_ = AddChildView(
create_label(CONTEXT_TAB_HOVER_CARD_TITLE,
gfx::Insets::VH(kVerticalMargin, kHorizontalMargin)));
site_access_separator_ = AddChildView(create_separator());
site_access_title_label_ = AddChildView(
create_label(CONTEXT_TAB_HOVER_CARD_TITLE,
gfx::Insets::TLBR(kVerticalMargin, kHorizontalMargin, 0,
kHorizontalMargin)));
site_access_description_label_ = AddChildView(
create_label(views::style::CONTEXT_DIALOG_BODY_TEXT,
gfx::Insets::TLBR(0, kHorizontalMargin, kVerticalMargin,
kHorizontalMargin)));
policy_separator_ = AddChildView(create_separator());
policy_label_ = AddChildView(
create_label(views::style::CONTEXT_DIALOG_BODY_TEXT,
gfx::Insets::VH(kVerticalMargin, kHorizontalMargin)));
// Set up widget.
views::BubbleDialogDelegateView::CreateBubble(this);
set_adjust_if_offscreen(true);
GetBubbleFrameView()->SetPreferredArrowAdjustment(
views::BubbleFrameView::PreferredArrowAdjustment::kOffset);
GetBubbleFrameView()->set_hit_test_transparent(true);
if (using_rounded_corners())
GetBubbleFrameView()->SetCornerRadius(corner_radius_.value());
// Start in the fully "faded-in" position so that whatever text we initially
// display is visible.
SetTextFade(1.0);
}
void ToolbarActionHoverCardBubbleView::UpdateCardContent(
const ToolbarActionViewController* action_controller,
content::WebContents* web_contents) {
title_label_->SetText(action_controller->GetActionName());
HoverCardState state = action_controller->GetHoverCardState(web_contents);
bool show_site_access_labels =
state.site_access !=
HoverCardState::SiteAccess::kExtensionDoesNotWantAccess;
bool show_policy_label = state.policy != HoverCardState::AdminPolicy::kNone;
site_access_separator_->SetVisible(show_site_access_labels);
site_access_title_label_->SetVisible(show_site_access_labels);
site_access_description_label_->SetVisible(show_site_access_labels);
if (show_site_access_labels) {
site_access_title_label_->SetText(GetSiteAccessTitle(state.site_access));
site_access_description_label_->SetText(GetSiteAccessDescription(
state.site_access, GetCurrentHost(web_contents)));
}
policy_separator_->SetVisible(show_policy_label);
policy_label_->SetVisible(show_policy_label);
if (show_policy_label)
policy_label_->SetText(GetPolicyText(state.policy));
}
void ToolbarActionHoverCardBubbleView::SetTextFade(double percent) {
title_label_->SetFade(percent);
site_access_title_label_->SetFade(percent);
site_access_description_label_->SetFade(percent);
policy_label_->SetFade(percent);
}
std::u16string ToolbarActionHoverCardBubbleView::GetTitleTextForTesting()
const {
return title_label_->GetText();
}
bool ToolbarActionHoverCardBubbleView::IsSiteAccessSeparatorVisible() const {
return site_access_separator_->GetVisible();
}
bool ToolbarActionHoverCardBubbleView::IsSiteAccessTitleVisible() const {
return site_access_title_label_->GetVisible();
}
bool ToolbarActionHoverCardBubbleView::IsSiteAccessDescriptionVisible() const {
return site_access_description_label_->GetVisible();
}
bool ToolbarActionHoverCardBubbleView::IsPolicySeparatorVisible() const {
return policy_separator_->GetVisible();
}
bool ToolbarActionHoverCardBubbleView::IsPolicyLabelVisible() const {
return policy_label_->GetVisible();
}
void ToolbarActionHoverCardBubbleView::OnThemeChanged() {
BubbleDialogDelegateView::OnThemeChanged();
// Bubble closes if the theme changes to the point where the border has to be
// regenerated. See crbug.com/1140256
if (!using_rounded_corners()) {
GetWidget()->Close();
return;
}
}
ToolbarActionHoverCardBubbleView::~ToolbarActionHoverCardBubbleView() = default;
BEGIN_METADATA(ToolbarActionHoverCardBubbleView,
views::BubbleDialogDelegateView)
END_METADATA
|