#ifndef ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_SEARCH_FIELD_VIEW_H_
#define ASH_QUICK_INSERT_VIEWS_QUICK_INSERT_SEARCH_FIELD_VIEW_H_
#include <string_view>
#include "ash/ash_export.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"
#include "ui/views/view_tracker.h"
namespace views {
class Textfield;
class ImageButton;
}
namespace ash {
class QuickInsertKeyEventHandler;
class QuickInsertPerformanceMetrics;
class QuickInsertSearchBarTextfield;
class ASH_EXPORT QuickInsertSearchFieldView
: public views::BoxLayoutView,
public views::TextfieldController,
public views::FocusChangeListener {
METADATA_HEADER(QuickInsertSearchFieldView, views::BoxLayoutView)
public:
using SearchCallback =
base::RepeatingCallback<void(const std::u16string& query)>;
using BackCallback = base::RepeatingClosure;
static constexpr base::TimeDelta kNotifyInitialActiveDescendantA11yDelay =
base::Milliseconds(1500);
explicit QuickInsertSearchFieldView(
SearchCallback search_callback,
BackCallback back_callback,
QuickInsertKeyEventHandler* key_event_handler,
QuickInsertPerformanceMetrics* performance_metrics);
QuickInsertSearchFieldView(const QuickInsertSearchFieldView&) = delete;
QuickInsertSearchFieldView& operator=(const QuickInsertSearchFieldView&) =
delete;
~QuickInsertSearchFieldView() override;
void RequestFocus() override;
void AddedToWidget() override;
void RemovedFromWidget() override;
void OnPaint(gfx::Canvas* canvas) override;
void ContentsChanged(views::Textfield* sender,
const std::u16string& new_contents) override;
bool HandleKeyEvent(views::Textfield* sender,
const ui::KeyEvent& key_event) override;
void OnDidChangeFocus(View* focused_before, View* focused_now) override;
void ContentsChangedInternal(std::u16string_view new_contents);
std::u16string_view GetPlaceholderText() const;
void SetPlaceholderText(std::u16string_view new_placeholder_text);
void SetTextfieldActiveDescendant(views::View* view);
std::u16string_view GetQueryText() const;
void SetQueryText(std::u16string text);
void SetBackButtonVisible(bool visible);
void SetShouldShowFocusIndicator(bool should_show_focus_indicator);
views::View* GetViewLeftOf(views::View* view);
views::View* GetViewRightOf(views::View* view);
bool LeftEventShouldMoveCursor(views::View* pseudo_focused_view);
bool RightEventShouldMoveCursor(views::View* pseudo_focused_view);
void OnGainedPseudoFocusFromLeftEvent(views::View* pseudo_focused_view);
void OnGainedPseudoFocusFromRightEvent(views::View* pseudo_focused_view);
QuickInsertSearchBarTextfield* textfield() { return textfield_; }
QuickInsertSearchBarTextfield& textfield_for_testing() { return *textfield_; }
views::ImageButton& back_button_for_testing() { return *back_button_; }
views::ImageButton& clear_button_for_testing() { return *clear_button_; }
private:
void ClearButtonPressed();
void UpdateTextfieldBorder();
void ScheduleNotifyInitialActiveDescendantForA11y();
void NotifyInitialActiveDescendantForA11y();
size_t GetQueryStartIndexForTraversal();
size_t GetQueryEndIndexForTraversal();
bool should_show_focus_indicator_ = false;
SearchCallback search_callback_;
raw_ptr<QuickInsertKeyEventHandler> key_event_handler_ = nullptr;
raw_ptr<QuickInsertPerformanceMetrics> performance_metrics_ = nullptr;
raw_ptr<QuickInsertSearchBarTextfield> textfield_ = nullptr;
raw_ptr<views::ImageButton> back_button_ = nullptr;
raw_ptr<views::ImageButton> clear_button_ = nullptr;
views::ViewTracker active_descendant_tracker_;
base::OneShotTimer notify_initial_active_descendant_timer_;
};
BEGIN_VIEW_BUILDER(ASH_EXPORT, QuickInsertSearchFieldView, views::BoxLayoutView)
VIEW_BUILDER_PROPERTY(std::u16string, PlaceholderText)
END_VIEW_BUILDER
}
DEFINE_VIEW_BUILDER(ASH_EXPORT, ash::QuickInsertSearchFieldView)
#endif