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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
|
// Copyright 2012 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/omnibox/omnibox_result_view.h"
#include <limits.h>
#include <algorithm>
#include <utility>
#include "base/check.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/omnibox/omnibox_theme.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/location_bar/selected_keyword_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_local_answer_header_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_match_cell_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_popup_view_views.h"
#include "chrome/browser/ui/views/omnibox/omnibox_suggestion_button_row_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_text_view.h"
#include "chrome/browser/ui/views/omnibox/remove_suggestion_bubble.h"
#include "chrome/browser/ui/views/omnibox/rounded_omnibox_results_frame.h"
#include "chrome/grit/generated_resources.h"
#include "components/omnibox/browser/actions/omnibox_pedal.h"
#include "components/omnibox/browser/autocomplete_match_type.h"
#include "components/omnibox/browser/omnibox.mojom-shared.h"
#include "components/omnibox/browser/omnibox_client.h"
#include "components/omnibox/browser/omnibox_controller.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_popup_selection.h"
#include "components/omnibox/browser/vector_icons.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
#include "third_party/omnibox_proto/answer_data.pb.h"
#include "third_party/omnibox_proto/rich_answer_template.pb.h"
#include "third_party/skia/include/core/SkColor.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/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_features.h"
#include "ui/color/color_id.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/image/image_skia_operations.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/background.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/link.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/metadata/type_conversion.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/atl.h"
#endif
namespace {
class OmniboxResultViewButton : public views::ImageButton {
METADATA_HEADER(OmniboxResultViewButton, views::ImageButton)
public:
OmniboxResultViewButton(int a11y_message_id, PressedCallback callback)
: ImageButton(std::move(callback)) {
views::ConfigureVectorImageButton(this);
SetAnimationDuration(base::TimeDelta());
views::InkDrop::Get(this)->GetInkDrop()->SetHoverHighlightFadeDuration(
base::TimeDelta());
SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
// Although this appears visually as a button, expose as a list box option
// so that it matches the other options within its list box container.
GetViewAccessibility().SetRole(ax::mojom::Role::kListBoxOption);
GetViewAccessibility().SetName(l10n_util::GetStringUTF16(a11y_message_id));
}
};
BEGIN_METADATA(OmniboxResultViewButton)
END_METADATA
constexpr float kIPHBackgroundBorderRadius = 8;
} // namespace
////////////////////////////////////////////////////////////////////////////////
// OmniboxResultSelectionIndicator
class OmniboxResultSelectionIndicator : public views::View {
METADATA_HEADER(OmniboxResultSelectionIndicator, views::View)
public:
const int kStrokeThickness = 4;
OmniboxResultSelectionIndicator() {
// Height will automatically match the parent's height.
SetPreferredSize(gfx::Size(kStrokeThickness, 0));
}
// views::View:
void OnPaint(gfx::Canvas* canvas) override {
SkPath path = GetPath();
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setColor(
GetColorProvider()->GetColor(kColorOmniboxResultsFocusIndicator));
flags.setStyle(cc::PaintFlags::kFill_Style);
canvas->DrawPath(path, flags);
}
private:
// The focus bar is a straight vertical line with half-rounded endcaps. Since
// this geometry is nontrivial to represent using primitives, it's instead
// represented using a fill path. This matches the style and implementation
// used in Tab Groups.
SkPath GetPath() const {
SkPath path;
path.moveTo(0, 0);
path.arcTo(kStrokeThickness, kStrokeThickness, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, kStrokeThickness, kStrokeThickness);
path.lineTo(kStrokeThickness, height() - kStrokeThickness);
path.arcTo(kStrokeThickness, kStrokeThickness, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, 0, height());
path.close();
return path;
}
};
BEGIN_METADATA(OmniboxResultSelectionIndicator)
END_METADATA
////////////////////////////////////////////////////////////////////////////////
// OmniboxResultView, public:
OmniboxResultView::OmniboxResultView(OmniboxPopupViewViews* popup_view,
size_t model_index)
: popup_view_(popup_view),
model_index_(model_index),
// Using base::Unretained is correct here. 'this' outlives the callback.
mouse_enter_exit_handler_(
base::BindRepeating(&OmniboxResultView::UpdateHoverState,
base::Unretained(this))) {
CHECK_GE(model_index, 0u);
// The view hierarchy is:
// OmniboxResultView (FillLayout)
// selection_indicator_
// local_answer_header_and_suggestion_and_buttons_ (BoxLayout vertical)
// local_answer_header_ (added lazily)
// suggestion_and_buttons (FlexLayout horizontal)
// suggestion_and_button_row (FlexLayout horizontal)
// suggestion_view_
// button_row_
// thumbs_up_button_
// thumbs_down_button_
// remove_suggestion_button_
// TODO(crbug.com/370088101): The division between `OmniboxResultView` and
// `suggestion_view_` is not clear. Should we inline `suggestion_view_`'s
// members in `OmniboxResultView`? Or should we move e.g. the thumb and
// remove buttons into `suggestion_view_`? `suggestion_view_` currently uses
// custom layout, so it's easier to add new views to `OmniboxResultView`
// when possible.
SetLayoutManager(std::make_unique<views::FillLayout>());
selection_indicator_ =
AddChildView(std::make_unique<OmniboxResultSelectionIndicator>());
local_answer_header_and_suggestion_and_buttons_ =
AddChildView(std::make_unique<views::View>());
local_answer_header_and_suggestion_and_buttons_
->SetLayoutManager(std::make_unique<views::BoxLayout>())
->SetOrientation(views::LayoutOrientation::kVertical);
auto* suggestion_and_buttons =
local_answer_header_and_suggestion_and_buttons_->AddChildView(
std::make_unique<views::View>());
suggestion_and_buttons
->SetLayoutManager(std::make_unique<views::FlexLayout>())
->SetCrossAxisAlignment(views::LayoutAlignment::kCenter);
views::View* suggestion_and_button_row =
suggestion_and_buttons->AddChildView(std::make_unique<views::View>());
suggestion_and_button_row->SetLayoutManager(
std::make_unique<views::FlexLayout>());
suggestion_and_button_row->SetProperty(
views::kFlexBehaviorKey,
// FlexSpecification has multiple constructors, and if no direction is
// specified, the settings will be used in both horizontal and vertical
// directions. Therefore, we must specify the horizontal direction.
// Otherwise, the vertical height will be stretched.
views::FlexSpecification(views::LayoutOrientation::kHorizontal,
views::MinimumFlexSizeRule::kScaleToZero,
views::MaximumFlexSizeRule::kUnbounded));
suggestion_view_ = suggestion_and_button_row->AddChildView(
std::make_unique<OmniboxMatchCellView>(this));
suggestion_view_->iph_link_view()->SetCallback(base::BindRepeating(
&OmniboxResultView::OpenIphLink, weak_factory_.GetWeakPtr()));
auto* const iph_link_focus_ring =
views::FocusRing::Get(suggestion_view_->iph_link_view());
iph_link_focus_ring->SetHasFocusPredicate(base::BindRepeating(
[](const OmniboxResultView* result_view, const View* view) {
return view->GetVisible() && result_view->GetMatchSelected() &&
result_view->popup_view_->GetSelection().state ==
OmniboxPopupSelection::FOCUSED_IPH_LINK;
},
base::Unretained(this)));
iph_link_focus_ring->SetColorId(kColorOmniboxResultsFocusIndicator);
// Allocate space for the suggestion text only after accounting
// for the space needed to render the inline action chip row.
suggestion_view_->SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToZero,
views::MaximumFlexSizeRule::kPreferred)
.WithOrder(2));
// TODO(b/345536738): Move the common code for setting up instances of
// OmniboxResultViewButton to the constructor.
thumbs_up_button_ = suggestion_and_buttons->AddChildView(
std::make_unique<OmniboxResultViewButton>(
IDS_ACC_THUMBS_UP_SUGGESTION_BUTTON,
base::BindRepeating(
&OmniboxResultView::ButtonPressed, base::Unretained(this),
OmniboxPopupSelection::FOCUSED_BUTTON_THUMBS_UP)));
thumbs_up_button_->SetProperty(views::kMarginsKey,
gfx::Insets::TLBR(0, 0, 0, 8));
views::InstallCircleHighlightPathGenerator(thumbs_up_button_);
thumbs_up_button_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_OMNIBOX_THUMBS_UP_SUGGESTION));
auto* const thumbs_up_focus_ring = views::FocusRing::Get(thumbs_up_button_);
thumbs_up_focus_ring->SetHasFocusPredicate(base::BindRepeating(
[](const OmniboxResultView* results, const View* view) {
return view->GetVisible() && results->GetMatchSelected() &&
(results->popup_view_->GetSelection().state ==
OmniboxPopupSelection::FOCUSED_BUTTON_THUMBS_UP);
},
base::Unretained(this)));
thumbs_up_focus_ring->SetColorId(kColorOmniboxResultsFocusIndicator);
thumbs_down_button_ = suggestion_and_buttons->AddChildView(
std::make_unique<OmniboxResultViewButton>(
IDS_ACC_THUMBS_DOWN_SUGGESTION_BUTTON,
base::BindRepeating(
&OmniboxResultView::ButtonPressed, base::Unretained(this),
OmniboxPopupSelection::FOCUSED_BUTTON_THUMBS_DOWN)));
thumbs_down_button_->SetProperty(views::kMarginsKey,
gfx::Insets::TLBR(0, 0, 0, 8));
views::InstallCircleHighlightPathGenerator(thumbs_down_button_);
thumbs_down_button_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_OMNIBOX_THUMBS_DOWN_SUGGESTION));
auto* const thumbs_down_focus_ring =
views::FocusRing::Get(thumbs_down_button_);
thumbs_down_focus_ring->SetHasFocusPredicate(base::BindRepeating(
[](const OmniboxResultView* results, const View* view) {
return view->GetVisible() && results->GetMatchSelected() &&
(results->popup_view_->GetSelection().state ==
OmniboxPopupSelection::FOCUSED_BUTTON_THUMBS_DOWN);
},
base::Unretained(this)));
thumbs_down_focus_ring->SetColorId(kColorOmniboxResultsFocusIndicator);
remove_suggestion_button_ = suggestion_and_buttons->AddChildView(
std::make_unique<OmniboxResultViewButton>(
IDS_ACC_REMOVE_SUGGESTION_BUTTON,
base::BindRepeating(
&OmniboxResultView::ButtonPressed, base::Unretained(this),
OmniboxPopupSelection::FOCUSED_BUTTON_REMOVE_SUGGESTION)));
remove_suggestion_button_->SetProperty(views::kMarginsKey,
gfx::Insets::TLBR(0, 0, 0, 16));
views::InstallCircleHighlightPathGenerator(remove_suggestion_button_);
auto* const remove_focus_ring =
views::FocusRing::Get(remove_suggestion_button_);
remove_focus_ring->SetHasFocusPredicate(base::BindRepeating(
[](const OmniboxResultView* results, const View* view) {
return view->GetVisible() && results->GetMatchSelected() &&
(results->popup_view_->GetSelection().state ==
OmniboxPopupSelection::FOCUSED_BUTTON_REMOVE_SUGGESTION);
},
base::Unretained(this)));
remove_focus_ring->SetColorId(kColorOmniboxResultsFocusIndicator);
button_row_ = suggestion_and_button_row->AddChildView(
std::make_unique<OmniboxSuggestionButtonRowView>(popup_view_,
model_index));
// If there's insufficient space for rendering both the suggestion text
// and the action chip row together, then allow the inline action chip row
// to disappear entirely.
button_row_->SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kPreferredSnapToZero,
views::MaximumFlexSizeRule::kPreferred));
mouse_enter_exit_handler_.ObserveMouseEnterExitOn(this);
GetViewAccessibility().SetRole(ax::mojom::Role::kListBoxOption);
UpdateAccessibleName();
GetViewAccessibility().SetPosInSet(model_index_ + 1);
}
OmniboxResultView::~OmniboxResultView() = default;
// static
std::unique_ptr<views::Background> OmniboxResultView::GetPopupCellBackground(
const views::View* view,
OmniboxPartState part_state) {
DCHECK(view);
const bool prefers_contrast =
view->GetNativeTheme() &&
view->GetNativeTheme()->UserHasContrastPreference();
// TODO(tapted): Consider using background()->SetColor() and
// always have a background.
if (part_state == OmniboxPartState::NORMAL && !prefers_contrast) {
return nullptr;
}
if (part_state == OmniboxPartState::IPH) {
return views::CreateRoundedRectBackground(
GetOmniboxBackgroundColorId(part_state),
/*radius=*/kIPHBackgroundBorderRadius,
/*for_border_thickness=*/0);
}
const float half_row_height = OmniboxMatchCellView::kRowHeight / 2;
gfx::RoundedCornersF radii = {0, half_row_height, half_row_height, 0};
return views::CreateRoundedRectBackground(
GetOmniboxBackgroundColorId(part_state), radii);
}
void OmniboxResultView::SetMatch(const AutocompleteMatch& match) {
match_ = match.GetMatchWithContentsAndDescriptionPossiblySwapped();
suggestion_view_->SetProperty(views::kMarginsKey,
gfx::Insets::TLBR(0, 0, 0, 0));
suggestion_view_->OnMatchUpdate(this, match_);
UpdateFeedbackButtonsVisibility();
UpdateRemoveSuggestionVisibility();
if (match_.IsIPHSuggestion()) {
remove_suggestion_button_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_OMNIBOX_CLOSE_IPH_SUGGESTION));
remove_suggestion_button_->GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(IDS_ACC_DISMISS_CHROME_TIP_BUTTON));
} else {
remove_suggestion_button_->SetTooltipText(
l10n_util::GetStringUTF16(IDS_OMNIBOX_REMOVE_SUGGESTION));
remove_suggestion_button_->GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(IDS_ACC_REMOVE_SUGGESTION_BUTTON));
}
button_row_->UpdateFromModel();
if (match_.type == AutocompleteMatchType::Type::HISTORY_EMBEDDINGS_ANSWER) {
if (!local_answer_header_) {
local_answer_header_ =
local_answer_header_and_suggestion_and_buttons_->AddChildViewAt(
std::make_unique<OmniboxLocalAnswerHeaderView>(), 0);
}
local_answer_header_->SetText(match.history_embeddings_answer_header_text);
local_answer_header_->SetVisible(true);
local_answer_header_->SetThrobberVisibility(
match.history_embeddings_answer_header_loading);
} else if (local_answer_header_) {
local_answer_header_->SetVisible(false);
}
ApplyThemeAndRefreshIcons();
InvalidateLayout();
UpdateAccessibleName();
}
void OmniboxResultView::ApplyThemeAndRefreshIcons(bool force_reapply_styles) {
const ui::ColorId icon_color_id = GetMatchSelected()
? kColorOmniboxResultsIconSelected
: kColorOmniboxResultsIcon;
// TODO(b/345536738): Iterate over all the buttons and updates their icons.
views::SetImageFromVectorIconWithColor(
thumbs_up_button_,
match_.feedback_type == FeedbackType::kThumbsUp
? vector_icons::kThumbUpFilledIcon
: vector_icons::kThumbUpIcon,
GetLayoutConstant(LOCATION_BAR_ICON_SIZE),
GetColorProvider()->GetColor(icon_color_id),
/* omnibox buttons are never disabled */
gfx::kPlaceholderColor);
if (thumbs_up_button_->GetVisible()) {
views::FocusRing::Get(thumbs_up_button_)->SchedulePaint();
}
views::SetImageFromVectorIconWithColor(
thumbs_down_button_,
match_.feedback_type == FeedbackType::kThumbsDown
? vector_icons::kThumbDownFilledIcon
: vector_icons::kThumbDownIcon,
GetLayoutConstant(LOCATION_BAR_ICON_SIZE),
GetColorProvider()->GetColor(icon_color_id),
/* omnibox buttons are never disabled */
gfx::kPlaceholderColor);
if (thumbs_down_button_->GetVisible()) {
views::FocusRing::Get(thumbs_down_button_)->SchedulePaint();
}
views::SetImageFromVectorIconWithColor(
remove_suggestion_button_, vector_icons::kCloseRoundedIcon,
GetLayoutConstant(LOCATION_BAR_ICON_SIZE),
GetColorProvider()->GetColor(icon_color_id),
/* omnibox buttons are never disabled */
gfx::kPlaceholderColor);
if (remove_suggestion_button_->GetVisible()) {
views::FocusRing::Get(remove_suggestion_button_)->SchedulePaint();
}
const OmniboxPartState state = GetThemeState();
SetBackground(GetPopupCellBackground(this, state));
// Reapply the dim color to account for the highlight state.
const bool selected = (state == OmniboxPartState::SELECTED);
const ui::ColorId dimmed_id = selected
? kColorOmniboxResultsTextDimmedSelected
: kColorOmniboxResultsTextDimmed;
suggestion_view_->separator()->ApplyTextColor(dimmed_id);
// Recreate the icons in case the color needs to change.
// Note: if this is an extension icon or favicon then this can be done in
// SetMatch() once (rather than repeatedly, as happens here). There may
// be an optimization opportunity here.
// TODO(dschuyler): determine whether to optimize the color changes.
auto icon = GetIcon();
if (icon.IsEmpty()) {
suggestion_view_->ClearIcon();
} else {
suggestion_view_->SetIcon(*icon.ToImageSkia(), match_);
}
// We must reapply colors for all the text fields here. If we don't, we can
// break theme changes for ZeroSuggest. See https://crbug.com/1095205.
//
// TODO(tommycli): We should finish migrating this logic to live entirely
// within OmniboxTextView, which should keep track of its own OmniboxPart.
bool prefers_contrast =
GetNativeTheme() && GetNativeTheme()->UserHasContrastPreference();
if (match_.type == AutocompleteMatchType::NULL_RESULT_MESSAGE) {
suggestion_view_->content()->ApplyTextColor(
match_.IsIPHSuggestion() ? kColorOmniboxResultsTextDimmed
: kColorOmniboxText);
} else if (prefers_contrast || force_reapply_styles) {
// Normally, OmniboxTextView caches its appearance, but in high contrast,
// selected-ness changes the text colors, so the styling of the text part of
// the results needs to be recomputed.
suggestion_view_->content()->ReapplyStyling();
suggestion_view_->description()->ReapplyStyling();
}
button_row_->SetThemeState(GetThemeState());
// The selection indicator indicates when the suggestion is focused. Do not
// show the selection indicator if an auxiliary button is selected.
if (match_.HasInstantKeyword(
popup_view_->controller()->client()->GetTemplateURLService())) {
const OmniboxPopupSelection::LineState line_state =
popup_view_->GetSelection().state;
selection_indicator_->SetVisible(
selected &&
(line_state == OmniboxPopupSelection::LineState::NORMAL ||
line_state == OmniboxPopupSelection::LineState::KEYWORD_MODE));
} else {
selection_indicator_->SetVisible(selected &&
popup_view_->GetSelection().state ==
OmniboxPopupSelection::NORMAL);
}
if (suggestion_view_->iph_link_view()->GetVisible()) {
views::FocusRing::Get(suggestion_view_->iph_link_view())->SchedulePaint();
}
}
void OmniboxResultView::OnSelectionStateChanged() {
UpdateFeedbackButtonsVisibility();
UpdateRemoveSuggestionVisibility();
UpdateAccessibleName();
UpdateAccessibilitySelectedState();
if (GetMatchSelected()) {
const auto selection_state = popup_view_->GetSelection().state;
// The text is also accessible via text/value change events in the omnibox
// but this selection event allows the screen reader to get more details
// about the list and the user's position within it.
// Limit which selection states fire the events, in order to avoid duplicate
// events. Specifically, OmniboxPopupViewViews::ProvideButtonFocusHint()
// already fires the correct events when the user tabs to an attached button
// in the current row.
if (selection_state == OmniboxPopupSelection::NORMAL) {
popup_view_->FireAXEventsForNewActiveDescendant(this);
}
}
ApplyThemeAndRefreshIcons();
button_row_->SelectionStateChanged();
}
bool OmniboxResultView::GetMatchSelected() const {
const auto selection = popup_view_->GetSelection();
return selection.line == model_index_;
}
views::Button* OmniboxResultView::GetActiveAuxiliaryButtonForAccessibility() {
return const_cast<views::Button*>(
std::as_const(*this).GetActiveAuxiliaryButtonForAccessibility());
}
const views::Button*
OmniboxResultView::GetActiveAuxiliaryButtonForAccessibility() const {
if (popup_view_->GetSelection().state ==
OmniboxPopupSelection::FOCUSED_BUTTON_THUMBS_UP) {
return thumbs_up_button_;
} else if (popup_view_->GetSelection().state ==
OmniboxPopupSelection::FOCUSED_BUTTON_THUMBS_DOWN) {
return thumbs_down_button_;
} else if (popup_view_->GetSelection().state ==
OmniboxPopupSelection::FOCUSED_BUTTON_REMOVE_SUGGESTION) {
return remove_suggestion_button_;
}
return button_row_->GetActiveButton();
}
OmniboxPartState OmniboxResultView::GetThemeState() const {
// NULL_RESULT_MESSAGE matches are no-op suggestions that only deliver a
// message. The selected and hovered states imply an action can be taken from
// that suggestion, so do not allow those states for this result.
if (match_.type == AutocompleteMatchType::NULL_RESULT_MESSAGE) {
return match_.IsIPHSuggestion() ? OmniboxPartState::IPH
: OmniboxPartState::NORMAL;
}
if (GetMatchSelected()) {
return OmniboxPartState::SELECTED;
}
// If we don't highlight the whole row when the user has the mouse over the
// remove suggestion button, it's unclear which suggestion is being removed.
return IsMouseHovered() ? OmniboxPartState::HOVERED
: OmniboxPartState::NORMAL;
}
void OmniboxResultView::OnMatchIconUpdated() {
// The new icon will be fetched during ApplyThemeAndRefreshIcons().
ApplyThemeAndRefreshIcons();
}
void OmniboxResultView::SetRichSuggestionImage(const gfx::ImageSkia& image) {
suggestion_view_->SetImage(image, match_);
}
void OmniboxResultView::ButtonPressed(OmniboxPopupSelection::LineState state,
const ui::Event& event) {
popup_view_->model()->OpenSelection(
OmniboxPopupSelection(model_index_, state), event.time_stamp());
if (state == OmniboxPopupSelection::FOCUSED_BUTTON_REMOVE_SUGGESTION) {
// The button could be pressed and the deletion successful, but the match
// may continue to appear with the X button remaining so it looked like it
// didn't delete. There may be a deeper async matches issue involved, but
// this seems to help in at least some cases (pedals + entities, e.g. dino).
UpdateRemoveSuggestionVisibility();
}
}
////////////////////////////////////////////////////////////////////////////////
// OmniboxResultView, views::View overrides:
bool OmniboxResultView::OnMousePressed(const ui::MouseEvent& event) {
if (event.IsOnlyLeftMouseButton()) {
popup_view_->SetSelectedIndex(model_index_);
// Inform the model that a new result is now selected via mouse press.
popup_view_->model()->OnNavigationLikely(
model_index_, omnibox::mojom::NavigationPredictor::kMouseDown);
}
return true;
}
bool OmniboxResultView::OnMouseDragged(const ui::MouseEvent& event) {
if (HitTestPoint(event.location())) {
// When the drag enters or remains within the bounds of this view, either
// set the state to be selected or hovered, depending on the mouse button.
if (event.IsOnlyLeftMouseButton()) {
if (!GetMatchSelected()) {
popup_view_->SetSelectedIndex(model_index_);
}
} else {
UpdateHoverState();
}
return true;
}
// When the drag leaves the bounds of this view, cancel the hover state and
// pass control to the popup view.
UpdateHoverState();
SetMouseAndGestureHandler(popup_view_);
return false;
}
void OmniboxResultView::OnMouseReleased(const ui::MouseEvent& event) {
if (AutocompleteMatch::IsFeaturedSearchType(match_.type)) {
// Featured search matches in the keyword mode refresh are a special case
// that does not commit the omnibox by opening a selected match.
OmniboxEditModel* model = popup_view_->model();
model->ClearKeyword();
model->SetPopupSelection(OmniboxPopupSelection(
model_index_, OmniboxPopupSelection::LineState::KEYWORD_MODE));
model->AcceptKeyword(metrics::OmniboxEventProto::TAB);
return;
}
if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) {
const auto disposition = event.IsOnlyLeftMouseButton()
? WindowOpenDisposition::CURRENT_TAB
: WindowOpenDisposition::NEW_BACKGROUND_TAB;
popup_view_->model()->OpenSelection(OmniboxPopupSelection(model_index_),
event.time_stamp(), disposition);
}
}
void OmniboxResultView::OnMouseEntered(const ui::MouseEvent& event) {
UpdateHoverState();
}
void OmniboxResultView::OnMouseExited(const ui::MouseEvent& event) {
UpdateHoverState();
}
void OmniboxResultView::OnThemeChanged() {
views::View::OnThemeChanged();
ApplyThemeAndRefreshIcons(/*force_reapply_styles=*/true);
}
void OmniboxResultView::UpdateAccessibilityProperties() {
GetViewAccessibility().SetSetSize(
popup_view_->controller()->autocomplete_controller()->result().size());
}
////////////////////////////////////////////////////////////////////////////////
// OmniboxResultView, private:
void OmniboxResultView::OpenIphLink() {
popup_view_->controller()->client()->OpenIphLink(match_.iph_link_url);
}
gfx::Image OmniboxResultView::GetIcon() const {
// Usually, use kColorOmniboxResultsIcon[Selected] for icon color. Except for
// history cluster suggestions which want to stand out. They reuse the
// kColorOmniboxResultsUrl[Selected] color which is intended for the URL text
// in suggestion texts.
ui::ColorId vector_icon_color_id;
if (match_.type == AutocompleteMatchType::STARTER_PACK ||
match_.type == AutocompleteMatchType::FEATURED_ENTERPRISE_SEARCH) {
vector_icon_color_id = kColorOmniboxResultsStarterPackIcon;
} else if (match_.type == AutocompleteMatchType::HISTORY_CLUSTER ||
match_.type == AutocompleteMatchType::PEDAL) {
vector_icon_color_id = kColorOmniboxAnswerIconGM3Foreground;
} else {
vector_icon_color_id = GetMatchSelected() ? kColorOmniboxResultsIconSelected
: kColorOmniboxResultsIcon;
}
return popup_view_->GetMatchIcon(
match_, GetColorProvider()->GetColor(vector_icon_color_id));
}
void OmniboxResultView::UpdateHoverState() {
UpdateFeedbackButtonsVisibility();
UpdateRemoveSuggestionVisibility();
ApplyThemeAndRefreshIcons();
GetViewAccessibility().SetIsHovered(IsMouseHovered());
}
void OmniboxResultView::UpdateFeedbackButtonsVisibility() {
const bool old_visibility = thumbs_up_button_->GetVisible();
const bool new_visibility =
popup_view_->model()->IsPopupControlPresentOnMatch(OmniboxPopupSelection(
model_index_, OmniboxPopupSelection::FOCUSED_BUTTON_THUMBS_UP)) &&
(GetMatchSelected() || IsMouseHovered());
// Same rules apply to both buttons.
thumbs_up_button_->SetVisible(new_visibility);
thumbs_down_button_->SetVisible(new_visibility);
if (old_visibility != new_visibility) {
InvalidateLayout();
}
}
// TODO(b/345536738): Introduce a single UpdateButtonsVisibility() that iterates
// over all the buttons and updates their visibilities.
void OmniboxResultView::UpdateRemoveSuggestionVisibility() {
const bool old_visibility = remove_suggestion_button_->GetVisible();
const bool new_visibility =
popup_view_->model()->IsPopupControlPresentOnMatch(OmniboxPopupSelection(
model_index_,
OmniboxPopupSelection::FOCUSED_BUTTON_REMOVE_SUGGESTION)) &&
(GetMatchSelected() || IsMouseHovered());
remove_suggestion_button_->SetVisible(new_visibility);
if (old_visibility != new_visibility) {
InvalidateLayout();
}
}
void OmniboxResultView::UpdateAccessibilitySelectedState() {
GetViewAccessibility().SetIsSelected(GetMatchSelected());
}
void OmniboxResultView::UpdateAccessibleName() {
// Get the label without the ", n of m" positional text appended.
// The positional info is provided via
// ax::mojom::IntAttribute::kPosInSet/SET_SIZE and providing it via text as
// well would result in duplicate announcements.
const auto* autocomplete_controller =
popup_view_->controller()->autocomplete_controller();
// TODO(tommycli): We re-fetch the original match from the popup model,
// because |match_| already has its contents and description swapped by this
// class, and we don't want that for the bubble. We should improve this.
const bool is_selected = GetMatchSelected();
if (model_index_ < autocomplete_controller->result().size()) {
const auto raw_match =
autocomplete_controller->result().match_at(model_index_);
// The selected match can have a special name, e.g. when is one or more
// buttons that can be tabbed to.
std::u16string label;
if (is_selected) {
// The selected match can have a special name, e.g. when is one or more
// buttons that can be tabbed to.
label =
popup_view_->model()->GetPopupAccessibilityLabelForCurrentSelection(
raw_match.contents, false);
// If the line immediately after the current selection is the
// informational IPH row, append its accessibility label at the end of
// this selection's accessibility label.
label += popup_view_->model()
->MaybeGetPopupAccessibilityLabelForIPHSuggestion();
} else {
label = AutocompleteMatchType::ToAccessibilityLabel(
raw_match,
popup_view_->model()->GetSuggestionGroupHeaderText(
raw_match.suggestion_group_id),
raw_match.contents);
}
GetViewAccessibility().SetName(label);
}
}
////////////////////////////////////////////////////////////////////////////////
// OmniboxResultView, views::View overrides, private:
void OmniboxResultView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
InvalidateLayout();
}
////////////////////////////////////////////////////////////////////////////////
// OmniboxResultView, overrides, private:
DEFINE_ENUM_CONVERTERS(OmniboxPartState,
{OmniboxPartState::NORMAL, u"NORMAL"},
{OmniboxPartState::HOVERED, u"HOVERED"},
{OmniboxPartState::SELECTED, u"SELECTED"})
BEGIN_METADATA(OmniboxResultView)
ADD_READONLY_PROPERTY_METADATA(bool, MatchSelected)
ADD_READONLY_PROPERTY_METADATA(OmniboxPartState, ThemeState)
ADD_READONLY_PROPERTY_METADATA(gfx::Image, Icon)
END_METADATA
|