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
|
// 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/autofill/popup/popup_search_bar_view.h"
#include <memory>
#include <string>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/flex_layout.h"
namespace autofill {
PopupSearchBarView::PopupSearchBarView(const std::u16string& placeholder,
Delegate& delegate)
: delegate_(delegate) {
ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
SetLayoutManager(std::make_unique<views::FlexLayout>())
->SetOrientation(views::LayoutOrientation::kHorizontal)
.SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
.SetCollapseMargins(true)
.SetDefault(
views::kMarginsKey,
gfx::Insets::VH(0, layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL)));
AddChildView(
std::make_unique<views::ImageView>(ui::ImageModel::FromVectorIcon(
vector_icons::kSearchChromeRefreshIcon, ui::kColorIcon,
layout_provider->GetDistanceMetric(
views::DISTANCE_BUBBLE_HEADER_VECTOR_ICON_SIZE))));
input_ = AddChildView(
views::Builder<views::Textfield>()
.SetPlaceholderText(placeholder)
.SetController(this)
.SetBorder(nullptr)
.SetAccessibleName(placeholder)
.SetProperty(views::kElementIdentifierKey, kInputField)
.SetProperty(views::kFlexBehaviorKey,
views::FlexSpecification(views::FlexSpecification(
views::LayoutOrientation::kHorizontal,
views::MinimumFlexSizeRule::kPreferred,
views::MaximumFlexSizeRule::kUnbounded)))
.Build());
input_changed_subscription_ =
input_->AddTextChangedCallback(base::BindRepeating(
&PopupSearchBarView::OnInputChanged, base::Unretained(this)));
// TODO(crbug.com/325246516): Clarify whether the clear button should be
// rendered on top of the input field and rework the layout (probably with a
// custom LayoutManager).
clear_ = AddChildView(
views::Builder<views::ImageButton>(
views::CreateVectorImageButtonWithNativeTheme(
base::BindRepeating(&PopupSearchBarView::OnClearPressed,
base::Unretained(this)),
vector_icons::kCloseChromeRefreshIcon))
// Reset the border set by `CreateVectorImageButtonWithNativeTheme()`
// as it sets an unnecessary padding to the highlighting circle.
.SetBorder(nullptr)
.SetAccessibleName(l10n_util::GetStringUTF16(
IDS_AUTOFILL_POPUP_SEARCH_BAR_CLEAR_SEARCH_BUTTON_A11Y_NAME))
.Build());
clear_->SetFocusBehavior(FocusBehavior::ALWAYS);
views::InstallCircleHighlightPathGenerator(clear_);
}
void PopupSearchBarView::AddedToWidget() {
GetFocusManager()->AddFocusChangeListener(this);
}
void PopupSearchBarView::RemovedFromWidget() {
GetFocusManager()->RemoveFocusChangeListener(this);
}
void PopupSearchBarView::OnDidChangeFocus(views::View* focused_before,
views::View* focused_now) {
if (focused_now != input_ && focused_now != clear_) {
delegate_->SearchBarOnFocusLost();
}
}
bool PopupSearchBarView::HandleKeyEvent(views::Textfield* sender,
const ui::KeyEvent& key_event) {
if (key_event.type() == ui::EventType::kKeyPressed) {
return delegate_->SearchBarHandleKeyPressed(key_event);
}
return false;
}
void PopupSearchBarView::Focus() {
input_->RequestFocus();
}
void PopupSearchBarView::SetInputTextForTesting(const std::u16string& text) {
input_->SetText(text);
}
gfx::Point PopupSearchBarView::GetClearButtonScreenCenterPointForTesting()
const {
return clear_->GetBoundsInScreen().CenterPoint();
}
PopupSearchBarView::~PopupSearchBarView() = default;
void PopupSearchBarView::OnInputChanged() {
input_change_notification_timer_.Start(
FROM_HERE, kInputChangeCallbackDelay,
// `delegate_` is expected to outlive `this`, the timer will either be
// triggered when it is alive or canceled.
base::BindOnce(&Delegate::SearchBarOnInputChanged,
base::Unretained(delegate_), input_->GetText()));
}
void PopupSearchBarView::OnClearPressed() {
input_->SetText({});
}
BEGIN_METADATA(PopupSearchBarView)
END_METADATA
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(PopupSearchBarView, kInputField);
} // namespace autofill
|