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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
// Copyright 2023 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/autofill/popup/popup_row_view.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/i18n/rtl.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "chrome/browser/ui/autofill/autofill_popup_controller.h"
#include "chrome/browser/ui/views/autofill/popup/popup_cell_utils.h"
#include "chrome/browser/ui/views/autofill/popup/popup_row_content_view.h"
#include "chrome/browser/ui/views/autofill/popup/popup_row_with_button_view.h"
#include "chrome/browser/ui/views/autofill/popup/popup_view_utils.h"
#include "chrome/browser/ui/views/autofill/popup/popup_view_views.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "components/autofill/core/browser/filling/filling_product.h"
#include "components/autofill/core/browser/suggestions/suggestion.h"
#include "components/autofill/core/browser/suggestions/suggestion_type.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/strings/grit/components_strings.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/base_type_conversion.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/events/event_handler.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/outsets_f.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
namespace autofill {
namespace {
// Utility event handler for mouse enter/exit and tap events.
class EnterExitHandler : public ui::EventHandler {
public:
EnterExitHandler(base::RepeatingClosure enter_callback,
base::RepeatingClosure exit_callback);
EnterExitHandler(const EnterExitHandler&) = delete;
EnterExitHandler& operator=(const EnterExitHandler&) = delete;
~EnterExitHandler() override;
void OnEvent(ui::Event* event) override;
private:
base::RepeatingClosure enter_callback_;
base::RepeatingClosure exit_callback_;
};
constexpr int kExpandChildSuggestionsViewWidth = 24;
constexpr int kExpandChildSuggestionsIconWidth = 16;
constexpr int kExpandChildSuggestionsViewHorizontalPadding =
(kExpandChildSuggestionsViewWidth - kExpandChildSuggestionsIconWidth) / 2;
// The suggestion is considered acceptable if the following is true:
// row view visible area >= total area * kAcceptingGuardVisibleAreaPortion.
// See how it is used in `PopupRowView::OnVisibleBoundsChanged()` for details.
constexpr float kAcceptingGuardVisibleAreaPortion = 0.5;
// Computes the position and set size of the suggestion at `suggestion_index` in
// `controller`'s suggestions ignoring `SuggestionType::kSeparator`s.
// Returns a pair of numbers: <position, size>. The position value is 1-base.
std::pair<int, int> ComputePositionInSet(
base::WeakPtr<AutofillPopupController> controller,
int suggestion_index) {
CHECK(controller);
int set_size = 0;
int set_index = suggestion_index + 1;
for (int i = 0; i < controller->GetLineCount(); ++i) {
if (controller->GetSuggestionAt(i).type != SuggestionType::kSeparator) {
++set_size;
continue;
}
if (i < suggestion_index) {
--set_index;
}
}
return {set_index, set_size};
}
std::u16string GetSuggestionA11yString(const Suggestion& suggestion,
bool add_call_to_action_if_expandable) {
std::vector<std::u16string> text(
{popup_cell_utils::GetVoiceOverStringFromSuggestion(suggestion)});
if (!suggestion.children.empty()) {
CHECK(IsExpandableSuggestionType(suggestion.type));
if (suggestion.type == SuggestionType::kAddressEntry &&
add_call_to_action_if_expandable) {
text.push_back(l10n_util::GetStringUTF16(
IDS_AUTOFILL_EXPANDABLE_SUGGESTION_FILL_ADDRESS_A11Y_ADDON));
}
std::u16string shortcut = l10n_util::GetStringUTF16(
base::i18n::IsRTL()
? IDS_AUTOFILL_EXPANDABLE_SUGGESTION_EXPAND_SHORTCUT_RTL
: IDS_AUTOFILL_EXPANDABLE_SUGGESTION_EXPAND_SHORTCUT);
text.push_back(l10n_util::GetStringFUTF16(
IDS_AUTOFILL_EXPANDABLE_SUGGESTION_SUBMENU_HINT, shortcut));
}
return base::JoinString(text, u". ");
}
} // namespace
EnterExitHandler::EnterExitHandler(base::RepeatingClosure enter_callback,
base::RepeatingClosure exit_callback)
: enter_callback_(std::move(enter_callback)),
exit_callback_(std::move(exit_callback)) {}
EnterExitHandler::~EnterExitHandler() = default;
void EnterExitHandler::OnEvent(ui::Event* event) {
switch (event->type()) {
case ui::EventType::kMouseEntered:
enter_callback_.Run();
break;
case ui::EventType::kMouseExited:
exit_callback_.Run();
break;
case ui::EventType::kGestureTapDown:
enter_callback_.Run();
break;
case ui::EventType::kGestureTapCancel:
case ui::EventType::kGestureEnd:
exit_callback_.Run();
break;
default:
break;
}
}
// static
int PopupRowView::GetHorizontalMargin() {
return ChromeLayoutProvider::Get()->GetDistanceMetric(
DISTANCE_CONTENT_LIST_VERTICAL_SINGLE);
}
PopupRowView::PopupRowView(
AccessibilitySelectionDelegate& a11y_selection_delegate,
SelectionDelegate& selection_delegate,
base::WeakPtr<AutofillPopupController> controller,
int line_number,
std::unique_ptr<PopupRowContentView> content_view)
: a11y_selection_delegate_(a11y_selection_delegate),
selection_delegate_(selection_delegate),
controller_(controller),
line_number_(line_number),
should_ignore_mouse_observed_outside_item_bounds_check_(
controller &&
controller->ShouldIgnoreMouseObservedOutsideItemBoundsCheck()),
suggestion_is_acceptable_(
controller && line_number < controller->GetLineCount() &&
controller->GetSuggestionAt(line_number).IsAcceptable()),
highlight_on_select_(
controller && line_number < controller->GetLineCount() &&
controller->GetSuggestionAt(line_number).highlight_on_select) {
CHECK(content_view);
CHECK(controller_);
CHECK_LT(line_number_, controller_->GetLineCount());
SetFocusBehavior(FocusBehavior::ALWAYS);
SetNotifyEnterExitOnChild(true);
SetProperty(views::kMarginsKey, gfx::Insets::VH(0, GetHorizontalMargin()));
SetBackground(views::CreateSolidBackground(ui::kColorDropdownBackground));
views::BoxLayout* layout =
SetLayoutManager(std::make_unique<views::BoxLayout>());
auto set_exit_enter_callbacks = [&](CellType type, views::View& cell) {
auto handler = std::make_unique<EnterExitHandler>(
/*enter_callback=*/base::BindRepeating(
[](PopupRowView* view, CellType type) {
// `OnMouseEntered()` does not imply that the mouse had been
// outside of the item's bounds before: `OnMouseEntered()` fires
// if the mouse moves just a little bit on the item. If the
// trigger source is not manual fallback we don't want to show a
// preview in such a case. In this case of manual fallback we do
// not care since the user has made a specific choice of opening
// the autofill popup.
bool can_select_suggestion =
view->mouse_observed_outside_item_bounds_ ||
view->should_ignore_mouse_observed_outside_item_bounds_check_;
if (can_select_suggestion) {
view->OnCellSelected(type, PopupCellSelectionSource::kMouse);
}
},
this, type),
/*exit_callback=*/base::BindRepeating(
&PopupRowView::OnCellSelected, base::Unretained(this), std::nullopt,
PopupCellSelectionSource::kMouse));
// Setting this handler on the cell view removes its original event handler
// (i.e. overridden methods like OnMouse*). Make sure the root view doesn't
// handle events itself and consider using `ui::ScopedTargetHandler` if it
// actually needs them.
cell.SetTargetHandler(handler.get());
return handler;
};
const Suggestion& suggestion = controller_->GetSuggestionAt(line_number);
content_view_ = AddChildView(std::move(content_view));
content_view_->SetFocusBehavior(FocusBehavior::ALWAYS);
content_view_observer_.Observe(content_view_);
content_view_->GetViewAccessibility().SetRole(
ax::mojom::Role::kListBoxOption);
content_view_->GetViewAccessibility().SetName(
GetSuggestionA11yString(suggestion,
/*add_call_to_action_if_expandable=*/
suggestion.IsAcceptable()),
ax::mojom::NameFrom::kAttribute);
auto [position, set_size] = ComputePositionInSet(controller_, line_number);
content_view_->GetViewAccessibility().SetPosInSet(position);
content_view_->GetViewAccessibility().SetSetSize(set_size);
content_view_->GetViewAccessibility().SetIsSelected(false);
GetViewAccessibility().SetRole(ax::mojom::Role::kListBoxOption);
GetViewAccessibility().SetName(
GetSuggestionA11yString(suggestion,
/*add_call_to_action_if_expandable=*/false));
GetViewAccessibility().SetPosInSet(position);
GetViewAccessibility().SetSetSize(set_size);
content_event_handler_ =
set_exit_enter_callbacks(CellType::kContent, *content_view_);
layout->SetFlexForView(content_view_.get(), 1);
if (!suggestion.children.empty()) {
expand_child_suggestions_view_ =
AddChildView(std::make_unique<views::View>());
expand_child_suggestions_view_->SetNotifyEnterExitOnChild(true);
expand_child_suggestions_view_->SetLayoutManager(
std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal,
gfx::Insets(kExpandChildSuggestionsViewHorizontalPadding)));
expand_child_suggestions_view_->AddChildView(
std::make_unique<views::ImageView>(
popup_cell_utils::ImageModelFromVectorIcon(
popup_cell_utils::GetExpandableMenuIcon(suggestion.type),
kExpandChildSuggestionsIconWidth)));
expand_child_suggestions_view_observer_.Observe(
expand_child_suggestions_view_);
control_event_handler_ = set_exit_enter_callbacks(
CellType::kControl, *expand_child_suggestions_view_);
layout->SetFlexForView(expand_child_suggestions_view_.get(), 0);
}
}
PopupRowView::~PopupRowView() = default;
bool PopupRowView::OnMouseDragged(const ui::MouseEvent& event) {
// Return `true` to be informed about subsequent `OnMouseReleased` events.
return true;
}
bool PopupRowView::OnMousePressed(const ui::MouseEvent& event) {
// Return `true` to be informed about subsequent `OnMouseReleased` events.
return true;
}
void PopupRowView::OnMouseExited(const ui::MouseEvent& event) {
// `OnMouseExited()` does not imply that the mouse has left the item's screen
// bounds: `OnMouseExited()` fires (on Windows, at least) when another popup
// overlays this item and the mouse is above the new popup
// (crbug.com/1287364).
mouse_observed_outside_item_bounds_ |= !IsMouseHovered();
}
void PopupRowView::OnMouseReleased(const ui::MouseEvent& event) {
// For trigger sources different from manual fallback we ignore mouse clicks
// unless the user made the explicit choice to select the current item. In
// the manual fallback case the user has made an explicit choice of opening
// the popup and so will not select an address by accident.
if (!mouse_observed_outside_item_bounds_ &&
!should_ignore_mouse_observed_outside_item_bounds_check_) {
return;
}
if (event.IsOnlyLeftMouseButton() &&
content_view_->HitTestPoint(event.location()) && controller_ &&
IsViewVisibleEnough()) {
controller_->AcceptSuggestion(line_number_);
}
}
void PopupRowView::OnGestureEvent(ui::GestureEvent* event) {
switch (event->type()) {
case ui::EventType::kGestureTap:
if (content_view_->HitTestPoint(event->location()) && controller_ &&
IsViewVisibleEnough()) {
controller_->AcceptSuggestion(line_number_);
}
break;
default:
return;
}
}
void PopupRowView::OnPaint(gfx::Canvas* canvas) {
views::View::OnPaint(canvas);
mouse_observed_outside_item_bounds_ |= !IsMouseHovered();
}
bool PopupRowView::GetNeedsNotificationWhenVisibleBoundsChange() const {
return base::FeatureList::IsEnabled(
features::kAutofillPopupDontAcceptNonVisibleEnoughSuggestion);
}
void PopupRowView::OnVisibleBoundsChanged() {
if (GetVisibleBounds().size().GetArea() >=
size().GetArea() * kAcceptingGuardVisibleAreaPortion) {
barrier_for_accepting_ = NextIdleBarrier::CreateNextIdleBarrierWithDelay(
AutofillSuggestionController::kIgnoreEarlyClicksOnSuggestionsDuration);
} else {
barrier_for_accepting_.reset();
}
}
void PopupRowView::OnViewFocused(views::View* view) {
CHECK(view == content_view_ || view == expand_child_suggestions_view_);
CellType type =
view == content_view_ ? CellType::kContent : CellType::kControl;
// Focus may come not only from the keyboard (e.g. from devices used for
// a11y), but for selection purposes these non-mouse sources are similar
// enough to treat them equally as a keyboard.
OnCellSelected(type, PopupCellSelectionSource::kKeyboard);
}
void PopupRowView::SetSelectedCell(std::optional<CellType> new_cell) {
if (!controller_) {
return;
}
if (new_cell == selected_cell_) {
return;
}
// If the previous cell was content, set it as unselected.
if (selected_cell_ == CellType::kContent) {
content_view_->UpdateStyle(/*selected=*/false);
content_view_->GetViewAccessibility().SetIsSelected(false);
controller_->UnselectSuggestion();
}
if ((new_cell == CellType::kControl && expand_child_suggestions_view_) ||
(new_cell == CellType::kContent && !suggestion_is_acceptable_)) {
// TODO(crbug.com/370695550): `SetIsSelected()` must go after
// `NotifyAXSelection()` as the latter calls `SetPopupFocusOverride()` that
// is required for a11y focus working on a non-activatable popup. Consider
// moving `SetIsSelected()` into `NotifyAXSelection()` (and rename it) to
// hide this API complexity from clients.
GetA11ySelectionDelegate().NotifyAXSelection(*this);
GetViewAccessibility().SetIsSelected(true);
NotifyAccessibilityEventDeprecated(
ax::mojom::Event::kSelectedChildrenChanged, true);
selected_cell_ = new_cell;
} else if (new_cell == CellType::kContent) {
controller_->SelectSuggestion(line_number_);
content_view_->UpdateStyle(/*selected=*/highlight_on_select_);
GetA11ySelectionDelegate().NotifyAXSelection(*content_view_);
content_view_->GetViewAccessibility().SetIsSelected(true);
NotifyAccessibilityEventDeprecated(
ax::mojom::Event::kSelectedChildrenChanged, true);
selected_cell_ = new_cell;
} else {
// Set the selected cell to none in case an invalid choice was made (e.g.
// selecting a control cell when none exists) or the cell was reset
// explicitly with `std::nullopt`.
selected_cell_ = std::nullopt;
GetViewAccessibility().SetIsSelected(false);
content_view_->GetViewAccessibility().SetIsSelected(false);
}
UpdateBackground();
}
void PopupRowView::SetChildSuggestionsDisplayed(
bool child_suggestions_displayed) {
child_suggestions_displayed_ = child_suggestions_displayed;
UpdateBackground();
}
gfx::RectF PopupRowView::GetControlCellBounds() const {
// The view is expected to be present.
gfx::RectF bounds =
gfx::RectF(expand_child_suggestions_view_->GetBoundsInScreen());
// Depending on the RTL expand the bounds on the outer side only, so that
// the inner sides don't have gaps which may cause unnecessary mouse events
// on the parent in case of overlapping by its sub-popup.
gfx::OutsetsF extension =
base::i18n::IsRTL()
? gfx::OutsetsF::TLBR(0, /*left=*/GetHorizontalMargin(), 0, 0)
: gfx::OutsetsF::TLBR(0, 0, 0, /*right=*/GetHorizontalMargin());
bounds.Outset(extension);
return bounds;
}
bool PopupRowView::HandleKeyPressEvent(
const input::NativeWebKeyboardEvent& event) {
// Some cells may want to define their own behavior.
CHECK(GetSelectedCell());
switch (event.windows_key_code) {
case ui::VKEY_RETURN: {
const bool kHasKeyModifierPressed =
event.GetModifiers() & blink::WebInputEvent::kKeyModifiers;
if (*GetSelectedCell() == CellType::kContent && controller_ &&
!kHasKeyModifierPressed && IsViewVisibleEnough()) {
controller_->AcceptSuggestion(line_number_);
return true;
}
return false;
}
default:
return false;
}
}
bool PopupRowView::IsSelectable() const {
return controller_ && line_number_ < controller_->GetLineCount() &&
!controller_->GetSuggestionAt(line_number_).HasDeactivatedStyle();
}
void PopupRowView::OnCellSelected(std::optional<CellType> type,
PopupCellSelectionSource source) {
selection_delegate_->SetSelectedCell(
type ? std::make_optional(PopupViewViews::CellIndex{line_number_, *type})
: std::nullopt,
source);
}
void PopupRowView::UpdateBackground() {
const bool is_highlighted = [&]() {
if (!highlight_on_select_) {
return false;
}
// The whole row is highlighted when the subpopup is open, or ...
if (child_suggestions_displayed_) {
return true;
}
// the expanding control view is being hovered, or ...
if (selected_cell_ == CellType::kControl) {
return true;
}
// the suggestion is not acceptable and either the control or content part
// is being hovered.
return !suggestion_is_acceptable_ && selected_cell_;
}();
SetBackground(views::CreateRoundedRectBackground(
is_highlighted ? ui::kColorDropdownBackgroundSelected
: ui::kColorDropdownBackground,
ChromeLayoutProvider::Get()->GetCornerRadiusMetric(
views::Emphasis::kMedium)));
}
bool PopupRowView::IsViewVisibleEnough() const {
if (controller_ &&
!controller_->IsViewVisibilityAcceptingThresholdEnabled()) {
return true;
}
if (!base::FeatureList::IsEnabled(
features::kAutofillPopupDontAcceptNonVisibleEnoughSuggestion)) {
return true;
}
bool visible_enough =
barrier_for_accepting_ && barrier_for_accepting_->value();
base::UmaHistogramBoolean(
"Autofill.AcceptedSuggestionDesktopRowViewVisibleEnough", visible_enough);
return visible_enough;
}
BEGIN_METADATA(PopupRowView)
ADD_PROPERTY_METADATA(std::optional<PopupRowView::CellType>, SelectedCell)
END_METADATA
} // namespace autofill
DEFINE_ENUM_CONVERTERS(autofill::PopupRowView::CellType,
{autofill::PopupRowView::CellType::kContent,
u"kContent"},
{autofill::PopupRowView::CellType::kControl,
u"kControl"})
|