search_result_container_view.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include "ash/app_list/views/search_result_container_view.h"
  5. #include "base/bind.h"
  6. #include "base/location.h"
  7. #include "base/task/single_thread_task_runner.h"
  8. #include "base/threading/thread_task_runner_handle.h"
  9. namespace ash {
  10. SearchResultContainerView::SearchResultContainerView(
  11. AppListViewDelegate* view_delegate)
  12. : view_delegate_(view_delegate) {
  13. DCHECK(view_delegate);
  14. }
  15. SearchResultContainerView::~SearchResultContainerView() {
  16. if (results_)
  17. results_->RemoveObserver(this);
  18. }
  19. void SearchResultContainerView::SetResults(
  20. SearchModel::SearchResults* results) {
  21. if (results_)
  22. results_->RemoveObserver(this);
  23. results_ = results;
  24. if (results_)
  25. results_->AddObserver(this);
  26. Update();
  27. }
  28. absl::optional<SearchResultContainerView::ResultsAnimationInfo>
  29. SearchResultContainerView::ScheduleResultAnimations(
  30. const ResultsAnimationInfo& aggregate_animation_info) {
  31. NOTREACHED();
  32. return absl::nullopt;
  33. }
  34. void SearchResultContainerView::AppendShownResultMetadata(
  35. std::vector<SearchResultAimationMetadata>* result_metadata_) {
  36. NOTREACHED();
  37. }
  38. bool SearchResultContainerView::HasAnimatingChildView() {
  39. NOTREACHED();
  40. return false;
  41. }
  42. void SearchResultContainerView::OnSelectedResultChanged() {}
  43. void SearchResultContainerView::Update() {
  44. update_factory_.InvalidateWeakPtrs();
  45. num_results_ = DoUpdate();
  46. Layout();
  47. if (delegate_)
  48. delegate_->OnSearchResultContainerResultsChanged();
  49. }
  50. bool SearchResultContainerView::UpdateScheduled() {
  51. return update_factory_.HasWeakPtrs();
  52. }
  53. const char* SearchResultContainerView::GetClassName() const {
  54. return "SearchResultContainerView";
  55. }
  56. void SearchResultContainerView::AddObservedResultView(
  57. SearchResultBaseView* result_view) {
  58. result_view_observations_.AddObservation(result_view);
  59. }
  60. void SearchResultContainerView::RemoveObservedResultView(
  61. SearchResultBaseView* result_view) {
  62. result_view_observations_.RemoveObservation(result_view);
  63. }
  64. void SearchResultContainerView::ListItemsAdded(size_t /*start*/,
  65. size_t /*count*/) {
  66. ScheduleUpdate();
  67. }
  68. void SearchResultContainerView::ListItemsRemoved(size_t /*start*/,
  69. size_t /*count*/) {
  70. ScheduleUpdate();
  71. }
  72. void SearchResultContainerView::ListItemMoved(size_t /*index*/,
  73. size_t /*target_index*/) {
  74. ScheduleUpdate();
  75. }
  76. void SearchResultContainerView::ListItemsChanged(size_t /*start*/,
  77. size_t /*count*/) {
  78. ScheduleUpdate();
  79. }
  80. SearchResultBaseView* SearchResultContainerView::GetFirstResultView() {
  81. return num_results_ <= 0 ? nullptr : GetResultViewAt(0);
  82. }
  83. void SearchResultContainerView::SetShown(bool shown) {
  84. if (shown_ == shown) {
  85. return;
  86. }
  87. shown_ = shown;
  88. OnShownChanged();
  89. }
  90. void SearchResultContainerView::OnShownChanged() {}
  91. void SearchResultContainerView::ScheduleUpdate() {
  92. // When search results are added one by one, each addition generates an update
  93. // request. Consolidates those update requests into one Update call.
  94. if (!update_factory_.HasWeakPtrs()) {
  95. if (delegate_)
  96. delegate_->OnSearchResultContainerResultsChanging();
  97. base::ThreadTaskRunnerHandle::Get()->PostTask(
  98. FROM_HERE, base::BindOnce(&SearchResultContainerView::Update,
  99. update_factory_.GetWeakPtr()));
  100. }
  101. }
  102. } // namespace ash