File: search_box_view.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (338 lines) | stat: -rw-r--r-- 12,685 bytes parent folder | download
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
// 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.

#ifndef ASH_APP_LIST_VIEWS_SEARCH_BOX_VIEW_H_
#define ASH_APP_LIST_VIEWS_SEARCH_BOX_VIEW_H_

#include <stdint.h>

#include <memory>
#include <vector>

#include "ash/app_list/app_list_model_provider.h"
#include "ash/app_list/app_list_view_delegate.h"
#include "ash/app_list/model/search/search_box_model.h"
#include "ash/app_list/model/search/search_box_model_observer.h"
#include "ash/ash_export.h"
#include "ash/assistant/ui/main_stage/launcher_search_iph_view.h"
#include "ash/public/cpp/app_list/app_list_types.h"
#include "ash/search_box/search_box_view_base.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace views {
class MenuItemView;
class Textfield;
class View;
}  // namespace views

namespace ash {

class AppListViewDelegate;
class FilterMenuAdapter;
class ResultSelectionController;
class SearchBoxViewDelegate;
class SearchResultBaseView;

// Subclass of SearchBoxViewBase. SearchBoxModel is its data model
// that controls what icon to display, what placeholder text to use for
// Textfield. The text and selection model part could be set to change the
// contents and selection model of the Textfield.
class ASH_EXPORT SearchBoxView : public SearchBoxViewBase,
                                 public AppListModelProvider::Observer,
                                 public SearchBoxModelObserver,
                                 public LauncherSearchIphView::Delegate {
 public:
  enum class PlaceholderTextType {
    kShortcuts = 0,
    kTabs = 1,
    kSettings = 2,
    kGames = 3,
    kImages = 4
  };

  SearchBoxView(SearchBoxViewDelegate* delegate,
                AppListViewDelegate* view_delegate,
                bool is_app_list_bubble);

  SearchBoxView(const SearchBoxView&) = delete;
  SearchBoxView& operator=(const SearchBoxView&) = delete;

  ~SearchBoxView() override;

  // Initializes the search box style for usage in bubble (clamshell mode)
  // launcher.
  void InitializeForBubbleLauncher();

  // Initializes the search box style for usage in fullscreen (tablet mode)
  // launcher.
  void InitializeForFullscreenLauncher();

  // Must be called before the user interacts with the search box. Cannot be
  // part of Init() because the controller isn't available until after Init()
  // is called.
  void SetResultSelectionController(ResultSelectionController* controller);

  // Resets state of SearchBoxView so it can be reshown.
  void ResetForShow();

  // Returns the total focus ring spacing for use in folders.
  static int GetFocusRingSpacing();

  // Overridden from SearchBoxViewBase:
  void UpdateSearchTextfieldAccessibleNodeData(
      ui::AXNodeData* node_data) override;
  void UpdateKeyboardVisibility() override;
  void HandleQueryChange(const std::u16string& query,
                         bool initiated_by_user) override;
  void UpdatePlaceholderTextStyle() override;
  void UpdateSearchBoxBorder() override;
  void OnSearchBoxActiveChanged(bool active) override;
  void UpdateSearchBoxFocusPaint() override;
  void OnAfterUserAction(views::Textfield* sender) override;

  // AppListModelProvider::Observer:
  void OnActiveAppListModelsChanged(AppListModel* model,
                                    SearchModel* search_model) override;

  // Overridden from views::View:
  void OnKeyEvent(ui::KeyEvent* event) override;
  void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  void OnPaintBackground(gfx::Canvas* canvas) override;
  void OnPaintBorder(gfx::Canvas* canvas) override;
  const char* GetClassName() const override;
  void OnThemeChanged() override;
  void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
  void AddedToWidget() override;

  // LauncherSearchIphView::Delegate:
  void RunLauncherSearchQuery(const std::u16string& query) override;
  void OpenAssistantPage() override;

  // Shows the category filter menu that allows users to enable/disable specific
  // search categories.
  void ShowFilterMenu();

  // Called when the category filter menu is closed.
  void OnFilterMenuClosed();

  // Returns the menu item view in the category filter menu that indicates the
  // `category` button. This should only be called when `filter_button_` exists
  // and the menu is opened.
  views::MenuItemView* GetFilterMenuItemByCategory(
      AppListSearchControlCategory category);

  // Returns true if the category filter menu is opened. This should only be
  // called when `filter_button_` exists.
  bool IsFilterMenuOpen();

  // Updates the search box's background corner radius and color based on the
  // state of AppListModel.
  void UpdateBackground(AppListState target_state);

  // Updates the search box's layout based on the state of AppListModel.
  void UpdateLayout(AppListState target_state, int target_state_height);

  // Returns background border corner radius in the given state.
  int GetSearchBoxBorderCornerRadiusForState(AppListState state) const;

  // Returns background color for the given state.
  SkColor GetBackgroundColorForState(AppListState state) const;

  // Sets the autocomplete text if autocomplete conditions are met.
  void ProcessAutocomplete(SearchResultBaseView* first_result_view);

  // Sets up prefix match autocomplete. Returns true if successful.
  bool ProcessPrefixMatchAutocomplete(SearchResult* search_result,
                                      const std::u16string& user_typed_text);

  // Removes all autocomplete text.
  void ClearAutocompleteText();

  // Updates the search box with |new_query| and starts a new search.
  void UpdateQuery(const std::u16string& new_query);

  // Moves the focus back to search box and find a search result to select.
  void EnterSearchResultSelection(const ui::KeyEvent& event);

  // Clears the search query and de-activate the search box.
  void ClearSearchAndDeactivateSearchBox();

  // Sets the view accessibility ID of the search box's active descendant.
  // The active descendant should be the currently selected result view in the
  // search results list.
  // `nullopt` indicates no active descendant, i.e. that no result is selected.
  void SetA11yActiveDescendant(
      const absl::optional<int32_t>& active_descendant);

  // Refreshes the placeholder text with a fixed one rather than the one picked
  // up randomly
  void UseFixedPlaceholderTextForTest();

  ResultSelectionController* result_selection_controller_for_test() {
    return result_selection_controller_;
  }
  void set_highlight_range_for_test(const gfx::Range& range) {
    highlight_range_ = range;
  }

  const std::u16string& current_query() const { return current_query_; }

  // Update search box view background when result container visibility changes.
  void OnResultContainerVisibilityChanged(bool visible);

  // Whether the search box has a non-empty, non-whitespace query.
  bool HasValidQuery();

  // Calculates the correct sizing for search box icons and buttons.
  int GetSearchBoxIconSize();
  int GetSearchBoxButtonSize();

  // Sets whether an IPH can be shown now or not.
  void SetIsIphAllowed(bool iph_allowed);

 private:
  class FocusRingLayer;

  // Called when the close button within the search box gets pressed.
  void CloseButtonPressed();

  // Called when the assistant button within the search box gets pressed.
  void AssistantButtonPressed();

  // Updates the icon shown left of the search box texfield.
  void UpdateSearchIcon();

  // Whether 'autocomplete_text' is a valid candidate for classic highlighted
  // autocomplete.
  bool IsValidAutocompleteText(const std::u16string& autocomplete_text);

  // Updates the text field text color.
  void UpdateTextColor();

  // Updates the search box placeholder text and accessible name.
  void UpdatePlaceholderTextAndAccessibleName();

  // Updates the visibility of an IPH view.
  void UpdateIphViewVisibility();

  // Notifies SearchBoxViewDelegate that the autocomplete text is valid.
  void AcceptAutocompleteText();

  // Returns true if there is currently an autocomplete suggestion in
  // search_box().
  bool HasAutocompleteText();

  // After verifying autocomplete text is valid, sets the current searchbox
  // text to the autocomplete text and sets the text highlight.
  void SetAutocompleteText(const std::u16string& autocomplete_text);

  // Returns the text shown in the text field when there is no text inputs.
  SearchBoxView::PlaceholderTextType SelectPlaceholderText() const;

  // Overridden from views::TextfieldController:
  void OnBeforeUserAction(views::Textfield* sender) override;
  bool HandleKeyEvent(views::Textfield* sender,
                      const ui::KeyEvent& key_event) override;
  bool HandleMouseEvent(views::Textfield* sender,
                        const ui::MouseEvent& mouse_event) override;
  bool HandleGestureEvent(views::Textfield* sender,
                          const ui::GestureEvent& gesture_event) override;

  // Overridden from SearchBoxModelObserver:
  void SearchEngineChanged() override;
  void ShowAssistantChanged() override;
  void OnWouldTriggerIphChanged() override;

  // Updates search_box() for the |selected_result|. Should be called when the
  // selected search result changes.
  void UpdateSearchBoxForSelectedResult(SearchResult* selected_result);

  // Returns true if the event to trigger autocomplete should be handled.
  bool ShouldProcessAutocomplete();

  // Clear highlight range.
  void ResetHighlightRange();

  // Builds the menu model for the category filter menu. This returns a vector
  // of AppListSearchControlCategory that is shown in the filter menu.
  ui::SimpleMenuModel* BuildFilterMenuModel();

  // Returns the search categories that are available for users to choose if
  // they want to have the results in the categories displayed in launcher
  // search. These category will be listed in the filter menu for users to
  // toggle.
  std::vector<AppListSearchControlCategory> GetToggleableCategories();

  // Tracks whether the search result page view is visible.
  bool search_result_page_visible_ = false;

  // Tracks the current app list state.
  AppListState current_app_list_state_ = AppListState::kStateApps;

  std::u16string current_query_;

  // The range of highlighted text for autocomplete.
  gfx::Range highlight_range_;

  // The key most recently pressed.
  ui::KeyboardCode last_key_pressed_ = ui::VKEY_UNKNOWN;

  const raw_ptr<SearchBoxViewDelegate, DanglingUntriaged | ExperimentalAsh>
      delegate_;
  const raw_ptr<AppListViewDelegate, ExperimentalAsh> view_delegate_;

  // The layer that will draw the focus ring if needed. Could be a nullptr if
  // the search box is in the bubble launcher.
  std::unique_ptr<FocusRingLayer> focus_ring_layer_;

  // Whether the search box is embedded in the bubble launcher.
  const bool is_app_list_bubble_;

  // Whether the search box view should draw a highlight border.
  bool should_paint_highlight_border_ = false;

  // The corner radius of the search box background.
  int corner_radius_ = 0;

  // Whether an IPH is allowed to be shown or not.
  bool is_iph_allowed_ = false;

  // The category filter menu adapter and model that handles the menu life cycle
  // and command execution.
  std::unique_ptr<ui::SimpleMenuModel> filter_menu_model_;
  std::unique_ptr<FilterMenuAdapter> filter_menu_adapter_;

  // Set by SearchResultPageView when the accessibility selection moves to a
  // search result view - the value is the ID of the currently selected result
  // view.
  absl::optional<int32_t> a11y_active_descendant_;

  // Owned by SearchResultPageView (for fullscreen launcher) or
  // ProductivityLauncherSearchPage (for bubble launcher).
  raw_ptr<ResultSelectionController, DanglingUntriaged | ExperimentalAsh>
      result_selection_controller_ = nullptr;

  // The timestamp taken when the search box model's query is updated by the
  // user. Used in metrics. Metrics are only recorded for search model updates
  // that occur after a search has been initiated.
  base::TimeTicks user_initiated_model_update_time_;

  // If true, `SelectPlaceholderText()` always returns a fixed placeholder text
  // instead of the one picked randomly.
  bool use_fixed_placeholder_text_for_test_ = false;

  const bool is_jelly_enabled_ = false;

  base::ScopedObservation<SearchBoxModel, SearchBoxModelObserver>
      search_box_model_observer_{this};

  base::WeakPtrFactory<SearchBoxView> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_APP_LIST_VIEWS_SEARCH_BOX_VIEW_H_