search_result_container_view.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (c) 2014 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_CONTAINER_VIEW_H_
  5. #define ASH_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/app_list/app_list_view_delegate.h"
  10. #include "ash/app_list/model/app_list_model.h"
  11. #include "ash/app_list/model/search/search_model.h"
  12. #include "ash/app_list/views/search_result_base_view.h"
  13. #include "ash/ash_export.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/scoped_multi_source_observation.h"
  16. #include "ui/views/view.h"
  17. #include "ui/views/view_observer.h"
  18. namespace ash {
  19. // SearchResultContainerView is a base class for views that contain multiple
  20. // search results. SearchPageView holds these in a list and manages which one is
  21. // selected. There can be one result within one SearchResultContainerView
  22. // selected at a time; moving off the end of one container view selects the
  23. // first element of the next container view, and vice versa
  24. class ASH_EXPORT SearchResultContainerView : public views::View,
  25. public views::ViewObserver,
  26. public ui::ListModelObserver {
  27. public:
  28. class Delegate {
  29. public:
  30. // Called whenever results in the container start changing, i.e. during
  31. // ScheduleUpdate(). It will be followed up with
  32. // OnSearchResultContainerResultsChanged() when the update completes.
  33. virtual void OnSearchResultContainerResultsChanging() = 0;
  34. // Called whenever results in the container change, i.e. during |Update()|.
  35. virtual void OnSearchResultContainerResultsChanged() = 0;
  36. };
  37. explicit SearchResultContainerView(AppListViewDelegate* view_delegate);
  38. SearchResultContainerView(const SearchResultContainerView&) = delete;
  39. SearchResultContainerView& operator=(const SearchResultContainerView&) =
  40. delete;
  41. ~SearchResultContainerView() override;
  42. void set_delegate(Delegate* delegate) { delegate_ = delegate; }
  43. // Sets the search results to listen to.
  44. void SetResults(SearchModel::SearchResults* results);
  45. SearchModel::SearchResults* results() { return results_; }
  46. int num_results() const { return num_results_; }
  47. virtual SearchResultBaseView* GetResultViewAt(size_t index) = 0;
  48. // Information needed to configure search result visibility animations when
  49. // result updates are animated.
  50. struct ResultsAnimationInfo {
  51. // Total number of visible views (either title or result views).
  52. int total_views = 0;
  53. // Total number of visible result views.
  54. int total_result_views = 0;
  55. // The index of the first result view that should be animated.
  56. int first_animated_result_view_index = 0;
  57. // The number of views that are animating (either title or result views).
  58. int animating_views = 0;
  59. // Whether fast search result update animations should be used.
  60. bool use_short_animations = false;
  61. };
  62. // Information needed to determine if a search result shuold have an updated
  63. // animation.
  64. struct SearchResultAimationMetadata {
  65. // The ID of the search result.
  66. std::string result_id;
  67. // Whether animations should be skipped for this search result.
  68. bool skip_animations = false;
  69. };
  70. // Schedules animations for result list updates. Expected to be implemented
  71. // for search result containers that animate result updates.
  72. // `aggregate_animation_info` The aggregated animation information for all
  73. // search result containers that appear in the search results UI before this
  74. // container.
  75. // Returns the animation info for this container.
  76. virtual absl::optional<ResultsAnimationInfo> ScheduleResultAnimations(
  77. const ResultsAnimationInfo& aggregate_animation_info);
  78. // Appends search result IDs of the search results shown by the container
  79. // view into 'result_ids_'
  80. virtual void AppendShownResultMetadata(
  81. std::vector<SearchResultAimationMetadata>* result_metadata_);
  82. // Returns whether the container view has any animating child views.
  83. virtual bool HasAnimatingChildView();
  84. bool horizontally_traversable() const { return horizontally_traversable_; }
  85. // Allows a container to define its traversal behavior
  86. void set_horizontally_traversable(bool horizontally_traversable) {
  87. horizontally_traversable_ = horizontally_traversable;
  88. }
  89. // Called when the result selection controller updates its selected result.
  90. virtual void OnSelectedResultChanged();
  91. // Batching method that actually performs the update and updates layout.
  92. void Update();
  93. // Returns whether an update is currently scheduled for this container.
  94. bool UpdateScheduled();
  95. // Overridden from views::View:
  96. const char* GetClassName() const override;
  97. // Functions to allow derivative classes to add/remove observed result views.
  98. void AddObservedResultView(SearchResultBaseView* result_view);
  99. void RemoveObservedResultView(SearchResultBaseView* result_view);
  100. // Overridden from ui::ListModelObserver:
  101. void ListItemsAdded(size_t start, size_t count) override;
  102. void ListItemsRemoved(size_t start, size_t count) override;
  103. void ListItemMoved(size_t index, size_t target_index) override;
  104. void ListItemsChanged(size_t start, size_t count) override;
  105. // Returns the first result in the container view. Returns nullptr if it does
  106. // not exist.
  107. SearchResultBaseView* GetFirstResultView();
  108. // Called from SearchResultPageView OnShown/OnHidden
  109. void SetShown(bool shown);
  110. bool shown() const { return shown_; }
  111. // Called when SetShowing has changed a result.
  112. virtual void OnShownChanged();
  113. AppListViewDelegate* view_delegate() const { return view_delegate_; }
  114. private:
  115. // Schedules an Update call using |update_factory_|. Do nothing if there is a
  116. // pending call.
  117. void ScheduleUpdate();
  118. // Updates UI with model. Returns the number of visible results.
  119. virtual int DoUpdate() = 0;
  120. Delegate* delegate_ = nullptr;
  121. int num_results_ = 0;
  122. // If true, left/right key events will traverse this container
  123. bool horizontally_traversable_ = false;
  124. SearchModel::SearchResults* results_ = nullptr; // Owned by SearchModel.
  125. // view delegate for notifications.
  126. bool shown_ = false;
  127. AppListViewDelegate* const view_delegate_;
  128. base::ScopedMultiSourceObservation<views::View, views::ViewObserver>
  129. result_view_observations_{this};
  130. // The factory that consolidates multiple Update calls into one.
  131. base::WeakPtrFactory<SearchResultContainerView> update_factory_{this};
  132. };
  133. } // namespace ash
  134. #endif // ASH_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_