search_result_base_view.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2018 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_RESULT_BASE_VIEW_H_
  5. #define ASH_APP_LIST_VIEWS_SEARCH_RESULT_BASE_VIEW_H_
  6. #include "ash/app_list/model/search/search_result_observer.h"
  7. #include "ash/ash_export.h"
  8. #include "base/time/time.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. #include "ui/views/controls/button/button.h"
  11. namespace ash {
  12. class SearchResult;
  13. class SearchResultActionsView;
  14. // Base class for views that observe and display a search result
  15. class ASH_EXPORT SearchResultBaseView : public views::Button,
  16. public SearchResultObserver {
  17. public:
  18. SearchResultBaseView();
  19. SearchResultBaseView(const SearchResultBaseView&) = delete;
  20. SearchResultBaseView& operator=(const SearchResultBaseView&) = delete;
  21. // Set whether the result is selected. It updates the background highlight,
  22. // and selects the result action associated with the result if
  23. // SearchBoxSelection feature is enabled.
  24. //
  25. // |reverse_tab_order| - Indicates whether the selection was set as part of
  26. // reverse tab traversal. Should be set when selection was changed while
  27. // handling TAB keyboard key. Ignored if |selected| is false.
  28. void SetSelected(bool selected, absl::optional<bool> reverse_tab_order);
  29. // Selects the initial action that should be associated with the result view,
  30. // notifying a11y hierarchy of the selection. If the result view does not
  31. // support result actions (i.e. does not have actions_view_), this will just
  32. // announce the current result view selection.
  33. // |reverse_tab_order| - whether the action was selected in reverse tab order.
  34. virtual void SelectInitialResultAction(bool reverse_tab_order);
  35. // Selects the next result action for the view, if the result supports
  36. // non-default actions (see actions_view_).
  37. // |reverse_tab_order| - whether the action was selected while handling TAB
  38. // key in reverse tab order.
  39. //
  40. // Returns whether the selected result action was changed.
  41. virtual bool SelectNextResultAction(bool reverse_tab_order);
  42. // Returns the view that is currently selected - for example, if the result
  43. // supports action views and an action view is currently selected, this
  44. // should return the action view, otherwise it should return `this`.
  45. virtual views::View* GetSelectedView();
  46. SearchResult* result() const { return result_; }
  47. void SetResult(SearchResult* result);
  48. // Invoked before changing |result_| to |new_result|.
  49. virtual void OnResultChanging(SearchResult* new_result) {}
  50. // Invoked after |result_| is updated.
  51. virtual void OnResultChanged() {}
  52. // Overridden from SearchResultObserver:
  53. void OnResultDestroying() override;
  54. // Computes the button's spoken feedback name.
  55. std::u16string ComputeAccessibleName() const;
  56. // Clears the result without calling |OnResultChanged| or |OnResultChanging|
  57. void ClearResult();
  58. bool selected() const { return selected_; }
  59. int index_in_container() const { return index_in_container_.value(); }
  60. void set_index_in_container(size_t index) { index_in_container_ = index; }
  61. void set_result_display_start_time(base::TimeTicks start_time) {
  62. result_display_start_time_ = start_time;
  63. }
  64. base::TimeTicks result_display_start_time() const {
  65. return result_display_start_time_;
  66. }
  67. void set_is_default_result(bool is_default) {
  68. is_default_result_ = is_default;
  69. }
  70. bool is_default_result() const { return is_default_result_; }
  71. // views::Button:
  72. bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override;
  73. // views::View:
  74. const char* GetClassName() const override;
  75. SearchResultActionsView* actions_view() { return actions_view_; }
  76. protected:
  77. ~SearchResultBaseView() override;
  78. void UpdateAccessibleName();
  79. void set_actions_view(SearchResultActionsView* actions_view) {
  80. actions_view_ = actions_view;
  81. }
  82. private:
  83. // If non-default result action was selected, clears the actions_view_'s
  84. // selection state.
  85. void ClearSelectedResultAction();
  86. // Whether the result is currently selected.
  87. bool selected_ = false;
  88. // Expected to be set by result view implementations that supports
  89. // extra result actions. It points to the view containing result actions
  90. // buttons. Owned by the views hierarchy.
  91. SearchResultActionsView* actions_view_ = nullptr;
  92. // The index of this view within a |SearchResultContainerView| that holds it.
  93. absl::optional<int> index_in_container_;
  94. // The starting time when |result_| is being displayed.
  95. base::TimeTicks result_display_start_time_;
  96. // True if |result_| is selected as the default result which can be
  97. // activated by user by pressing ENTER key.
  98. bool is_default_result_ = false;
  99. SearchResult* result_ = nullptr; // Owned by SearchModel::SearchResults.
  100. };
  101. } // namespace ash
  102. #endif // ASH_APP_LIST_VIEWS_SEARCH_RESULT_BASE_VIEW_H_