File: toolbar_action_hover_card_bubble_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (330 lines) | stat: -rw-r--r-- 12,563 bytes parent folder | download | duplicates (4)
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
// 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 <string_view>

#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "chrome/browser/ui/color/chrome_color_id.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/base/mojom/dialog_button.mojom.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/style/typography.h"
#include "ui/views/view_class_properties.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/40857356): Move to a base hover card class.
constexpr int kHorizontalMargin = 12;
constexpr int kVerticalMargin = 12;

// Maximum number of lines that a label occupies.
constexpr int kHoverCardLabelMaxLines = 3;

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();
  }
  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();
  }
  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();
  }
  return l10n_util::GetStringUTF16(text_id);
}

}  // namespace

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(static_cast<int>(ui::mojom::DialogButton::kNone));

  // 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);

  // Set up content.
  auto create_label = [](int context, int text_style,
                         std::optional<ui::ColorId> color_id,
                         gfx::Insets insets) {
    auto label = std::make_unique<FadeLabelView>(kHoverCardLabelMaxLines,
                                                 context, text_style);
    if (color_id) {
      label->SetEnabledColor(color_id.value());
    }
    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, views::style::STYLE_BODY_3_EMPHASIS,
      /*color_id=*/std::nullopt,
      gfx::Insets::VH(kVerticalMargin, kHorizontalMargin)));
  action_title_label_ = AddChildView(create_label(
      views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_BODY_4,
      /*color_id=*/kColorTabHoverCardSecondaryText,
      gfx::Insets::TLBR(0, kHorizontalMargin, kVerticalMargin,
                        kHorizontalMargin)));

  site_access_separator_ = AddChildView(create_separator());
  site_access_title_label_ = AddChildView(create_label(
      CONTEXT_TAB_HOVER_CARD_TITLE, views::style::STYLE_BODY_3_EMPHASIS,
      /*color_id=*/std::nullopt,
      gfx::Insets::TLBR(kVerticalMargin, kHorizontalMargin, 0,
                        kHorizontalMargin)));
  site_access_description_label_ = AddChildView(create_label(
      views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_BODY_4,
      /*color_id=*/kColorTabHoverCardSecondaryText,
      gfx::Insets::TLBR(0, kHorizontalMargin, kVerticalMargin,
                        kHorizontalMargin)));

  policy_separator_ = AddChildView(create_separator());
  policy_label_ = AddChildView(create_label(
      views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_BODY_4,
      /*color_id=*/kColorTabHoverCardSecondaryText,
      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);

  const int corner_radius = ChromeLayoutProvider::Get()->GetCornerRadiusMetric(
      views::Emphasis::kHigh);
  GetBubbleFrameView()->SetRoundedCorners(gfx::RoundedCornersF(corner_radius));

  // Start in the fully "faded-in" position so that whatever text we initially
  // display is visible.
  SetTextFade(1.0);
}

void ToolbarActionHoverCardBubbleView::UpdateCardContent(
    const std::u16string& extension_name,
    const std::u16string& action_title,
    ToolbarActionViewController::HoverCardState state,
    content::WebContents* web_contents) {
  title_label_->SetData({extension_name, /*is_filename=*/false});

  // We need to adjust the bottom margin of `title_label_` depending on
  // `action_title_` visibility.
  if (action_title.empty()) {
    title_label_->SetProperty(
        views::kMarginsKey,
        gfx::Insets::VH(kVerticalMargin, kHorizontalMargin));
    action_title_label_->SetVisible(false);
  } else {
    title_label_->SetProperty(
        views::kMarginsKey,
        gfx::Insets::TLBR(kVerticalMargin, kHorizontalMargin, 0,
                          kHorizontalMargin));
    action_title_label_->SetData({action_title, /*is_filename=*/false});
    action_title_label_->SetVisible(true);
  }

  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_->SetData(
        {GetSiteAccessTitle(state.site_access), /*is_filename=*/false});
    site_access_description_label_->SetData(
        {GetSiteAccessDescription(state.site_access,
                                  GetCurrentHost(web_contents)),
         /*is_filename=*/false});
  }

  policy_separator_->SetVisible(show_policy_label);
  policy_label_->SetVisible(show_policy_label);
  if (show_policy_label) {
    policy_label_->SetData({GetPolicyText(state.policy), false});
  }
}

void ToolbarActionHoverCardBubbleView::SetTextFade(double percent) {
  title_label_->SetFade(percent);
  action_title_label_->SetFade(percent);
  site_access_title_label_->SetFade(percent);
  site_access_description_label_->SetFade(percent);
  policy_label_->SetFade(percent);
}

std::u16string_view ToolbarActionHoverCardBubbleView::GetTitleTextForTesting()
    const {
  return title_label_->GetText();
}

std::u16string_view
ToolbarActionHoverCardBubbleView::GetActionTitleTextForTesting() const {
  return action_title_label_->GetText();
}

std::u16string_view
ToolbarActionHoverCardBubbleView::GetSiteAccessTitleTextForTesting() const {
  return site_access_title_label_->GetText();
}

std::u16string_view
ToolbarActionHoverCardBubbleView::GetSiteAccessDescriptionTextForTesting()
    const {
  return site_access_description_label_->GetText();
}

bool ToolbarActionHoverCardBubbleView::IsActionTitleVisible() const {
  return action_title_label_->GetVisible();
}

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();
}

ToolbarActionHoverCardBubbleView::~ToolbarActionHoverCardBubbleView() = default;

BEGIN_METADATA(ToolbarActionHoverCardBubbleView)
END_METADATA