search_box_view.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_APP_LIST_VIEWS_SEARCH_BOX_VIEW_H_
  5. #define ASH_APP_LIST_VIEWS_SEARCH_BOX_VIEW_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "ash/app_list/app_list_model_provider.h"
  10. #include "ash/app_list/app_list_view_delegate.h"
  11. #include "ash/app_list/model/search/search_box_model.h"
  12. #include "ash/app_list/model/search/search_box_model_observer.h"
  13. #include "ash/ash_export.h"
  14. #include "ash/public/cpp/app_list/app_list_types.h"
  15. #include "ash/search_box/search_box_view_base.h"
  16. #include "base/scoped_observation.h"
  17. #include "base/time/time.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace views {
  20. class Textfield;
  21. class View;
  22. } // namespace views
  23. namespace ash {
  24. class AppListView;
  25. class AppListViewDelegate;
  26. class ContentsView;
  27. class ResultSelectionController;
  28. class SearchBoxViewDelegate;
  29. class SearchResultBaseView;
  30. // Subclass of SearchBoxViewBase. SearchBoxModel is its data model
  31. // that controls what icon to display, what placeholder text to use for
  32. // Textfield. The text and selection model part could be set to change the
  33. // contents and selection model of the Textfield.
  34. class ASH_EXPORT SearchBoxView : public SearchBoxViewBase,
  35. public AppListModelProvider::Observer,
  36. public SearchBoxModelObserver {
  37. public:
  38. enum class PlaceholderTextType {
  39. kShortcuts = 0,
  40. kTabs = 1,
  41. kSettings = 2,
  42. kGames = 3
  43. };
  44. SearchBoxView(SearchBoxViewDelegate* delegate,
  45. AppListViewDelegate* view_delegate,
  46. AppListView* app_list_view = nullptr);
  47. SearchBoxView(const SearchBoxView&) = delete;
  48. SearchBoxView& operator=(const SearchBoxView&) = delete;
  49. ~SearchBoxView() override;
  50. // Initializes the search box style for usage in bubble (clamshell mode)
  51. // launcher.
  52. void InitializeForBubbleLauncher();
  53. // Initializes the search box style for usage in fullscreen (tablet mode)
  54. // launcher.
  55. void InitializeForFullscreenLauncher();
  56. // Must be called before the user interacts with the search box. Cannot be
  57. // part of Init() because the controller isn't available until after Init()
  58. // is called.
  59. void SetResultSelectionController(ResultSelectionController* controller);
  60. // Called when tablet mode starts and ends.
  61. void OnTabletModeChanged(bool started);
  62. // Resets state of SearchBoxView so it can be reshown.
  63. void ResetForShow();
  64. // Returns the total focus ring spacing for use in folders.
  65. static int GetFocusRingSpacing();
  66. // Creates a focus ring layer if the search box is not in the bubble launcher.
  67. void MaybeCreateFocusRing();
  68. // Overridden from SearchBoxViewBase:
  69. void UpdateSearchTextfieldAccessibleNodeData(
  70. ui::AXNodeData* node_data) override;
  71. void HandleSearchBoxEvent(ui::LocatedEvent* located_event) override;
  72. void UpdateKeyboardVisibility() override;
  73. void HandleQueryChange(const std::u16string& query,
  74. bool initiated_by_user) override;
  75. void UpdatePlaceholderTextStyle() override;
  76. void UpdateSearchBoxBorder() override;
  77. void RecordSearchBoxActivationHistogram(ui::EventType event_type) override;
  78. void OnSearchBoxActiveChanged(bool active) override;
  79. void UpdateSearchBoxFocusPaint() override;
  80. // AppListModelProvider::Observer:
  81. void OnActiveAppListModelsChanged(AppListModel* model,
  82. SearchModel* search_model) override;
  83. // Overridden from views::View:
  84. void OnKeyEvent(ui::KeyEvent* event) override;
  85. bool OnMouseWheel(const ui::MouseWheelEvent& event) override;
  86. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  87. void OnPaintBackground(gfx::Canvas* canvas) override;
  88. void OnPaintBorder(gfx::Canvas* canvas) override;
  89. const char* GetClassName() const override;
  90. void OnThemeChanged() override;
  91. void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
  92. // Updates the search box's background corner radius and color based on the
  93. // state of AppListModel.
  94. void UpdateBackground(AppListState target_state);
  95. // Updates the search box's layout based on the state of AppListModel.
  96. void UpdateLayout(AppListState target_state, int target_state_height);
  97. // Returns background border corner radius in the given state.
  98. int GetSearchBoxBorderCornerRadiusForState(AppListState state) const;
  99. // Returns background color for the given state.
  100. SkColor GetBackgroundColorForState(AppListState state) const;
  101. // Shows Zero State suggestions.
  102. void ShowZeroStateSuggestions();
  103. // Called when the wallpaper colors change.
  104. void OnWallpaperColorsChanged();
  105. // Sets the autocomplete text if autocomplete conditions are met.
  106. void ProcessAutocomplete(SearchResultBaseView* first_result_view);
  107. // Sets up prefix match autocomplete. Returns true if successful.
  108. bool ProcessPrefixMatchAutocomplete(SearchResult* search_result,
  109. const std::u16string& user_typed_text);
  110. // Removes all autocomplete text.
  111. void ClearAutocompleteText();
  112. // Updates the search box with |new_query| and starts a new search.
  113. void UpdateQuery(const std::u16string& new_query);
  114. // Clears the search query and de-activate the search box.
  115. void ClearSearchAndDeactivateSearchBox();
  116. // Sets the view accessibility ID of the search box's active descendant.
  117. // The active descendant should be the currently selected result view in the
  118. // search results list.
  119. // `nullopt` indicates no active descendant, i.e. that no result is selected.
  120. void SetA11yActiveDescendant(
  121. const absl::optional<int32_t>& active_descendant);
  122. // Refreshes the placeholder text with a fixed one rather than the one picked
  123. // up randomly
  124. void UseFixedPlaceholderTextForTest();
  125. void set_contents_view(ContentsView* contents_view) {
  126. contents_view_ = contents_view;
  127. }
  128. ContentsView* contents_view() { return contents_view_; }
  129. ResultSelectionController* result_selection_controller_for_test() {
  130. return result_selection_controller_;
  131. }
  132. void set_highlight_range_for_test(const gfx::Range& range) {
  133. highlight_range_ = range;
  134. }
  135. const std::u16string& current_query() const { return current_query_; }
  136. // Update search box view background when result container visibility changes.
  137. void OnResultContainerVisibilityChanged(bool visible);
  138. // Whether the search box has a non-empty, non-whitespace query.
  139. bool HasValidQuery();
  140. // Calculates the correct sizing for search box icons and buttons.
  141. int GetSearchBoxIconSize();
  142. int GetSearchBoxButtonSize();
  143. private:
  144. class FocusRingLayer;
  145. // Called when the close button within the search box gets pressed.
  146. void CloseButtonPressed();
  147. // Called when the assistant button within the search box gets pressed.
  148. void AssistantButtonPressed();
  149. // Updates the icon shown left of the search box texfield.
  150. void UpdateSearchIcon();
  151. // Whether 'autocomplete_text' is a valid candidate for classic highlighted
  152. // autocomplete.
  153. bool IsValidAutocompleteText(const std::u16string& autocomplete_text);
  154. // Updates the text field text color.
  155. void UpdateTextColor();
  156. // Updates the search box placeholder text and accessible name.
  157. void UpdatePlaceholderTextAndAccessibleName();
  158. // Notifies SearchBoxViewDelegate that the autocomplete text is valid.
  159. void AcceptAutocompleteText();
  160. // Returns true if there is currently an autocomplete suggestion in
  161. // search_box().
  162. bool HasAutocompleteText();
  163. // After verifying autocomplete text is valid, sets the current searchbox
  164. // text to the autocomplete text and sets the text highlight.
  165. void SetAutocompleteText(const std::u16string& autocomplete_text);
  166. // Returns the text shown in the text field when there is no text inputs.
  167. SearchBoxView::PlaceholderTextType SelectPlaceholderText() const;
  168. // Overridden from views::TextfieldController:
  169. void OnBeforeUserAction(views::Textfield* sender) override;
  170. bool HandleKeyEvent(views::Textfield* sender,
  171. const ui::KeyEvent& key_event) override;
  172. bool HandleMouseEvent(views::Textfield* sender,
  173. const ui::MouseEvent& mouse_event) override;
  174. bool HandleGestureEvent(views::Textfield* sender,
  175. const ui::GestureEvent& gesture_event) override;
  176. // Overridden from SearchBoxModelObserver:
  177. void SearchEngineChanged() override;
  178. void ShowAssistantChanged() override;
  179. // Updates search_box() for the |selected_result|. Should be called when the
  180. // selected search result changes.
  181. void UpdateSearchBoxForSelectedResult(SearchResult* selected_result);
  182. // Returns true if the event to trigger autocomplete should be handled.
  183. bool ShouldProcessAutocomplete();
  184. // Clear highlight range.
  185. void ResetHighlightRange();
  186. // Tracks whether the search result page view is visible.
  187. bool search_result_page_visible_ = false;
  188. // Tracks the current app list state.
  189. AppListState current_app_list_state_ = AppListState::kStateApps;
  190. std::u16string current_query_;
  191. // The range of highlighted text for autocomplete.
  192. gfx::Range highlight_range_;
  193. // The key most recently pressed.
  194. ui::KeyboardCode last_key_pressed_ = ui::VKEY_UNKNOWN;
  195. SearchBoxViewDelegate* const delegate_;
  196. AppListViewDelegate* const view_delegate_;
  197. // Owned by views hierarchy. May be null for bubble launcher.
  198. AppListView* const app_list_view_;
  199. // Owned by views hierarchy. May be null for bubble launcher.
  200. ContentsView* contents_view_ = nullptr;
  201. // The layer that will draw the focus ring if needed. Could be a nullptr if
  202. // the search box is in the bubble launcher.
  203. std::unique_ptr<FocusRingLayer> focus_ring_layer_;
  204. // Whether the search box is embedded in the bubble launcher.
  205. const bool is_app_list_bubble_;
  206. // Whether tablet mode is active.
  207. bool is_tablet_mode_;
  208. // Whether the search box view should draw a highlight border.
  209. bool should_paint_highlight_border_ = false;
  210. // The corner radius of the search box background.
  211. int corner_radius_ = 0;
  212. // Set by SearchResultPageView when the accessibility selection moves to a
  213. // search result view - the value is the ID of the currently selected result
  214. // view.
  215. absl::optional<int32_t> a11y_active_descendant_;
  216. // Owned by SearchResultPageView (for fullscreen launcher) or
  217. // ProductivityLauncherSearchPage (for bubble launcher).
  218. ResultSelectionController* result_selection_controller_ = nullptr;
  219. // The timestamp taken when the search box model's query is updated by the
  220. // user. Used in metrics. Metrics are only recorded for search model updates
  221. // that occur after a search has been initiated.
  222. base::TimeTicks user_initiated_model_update_time_;
  223. // If true, `SelectPlaceholderText()` always returns a fixed placeholder text
  224. // instead of the one picked randomly.
  225. bool use_fixed_placeholder_text_for_test_ = false;
  226. base::ScopedObservation<SearchBoxModel, SearchBoxModelObserver>
  227. search_box_model_observer_{this};
  228. base::WeakPtrFactory<SearchBoxView> weak_ptr_factory_{this};
  229. };
  230. } // namespace ash
  231. #endif // ASH_APP_LIST_VIEWS_SEARCH_BOX_VIEW_H_