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.
5 #include "ui/app_list/views/search_result_container_view.h"
8 #include "base/message_loop/message_loop.h"
12 SearchResultContainerView::SearchResultContainerView()
17 update_factory_(this) {
20 SearchResultContainerView::~SearchResultContainerView() {
22 results_
->RemoveObserver(this);
25 void SearchResultContainerView::SetResults(
26 AppListModel::SearchResults
* results
) {
28 results_
->RemoveObserver(this);
32 results_
->AddObserver(this);
37 void SearchResultContainerView::SetSelectedIndex(int selected_index
) {
38 DCHECK(IsValidSelectionIndex(selected_index
));
39 int old_selected
= selected_index_
;
40 selected_index_
= selected_index
;
41 UpdateSelectedIndex(old_selected
, selected_index_
);
44 void SearchResultContainerView::ClearSelectedIndex() {
45 int old_selected
= selected_index_
;
47 UpdateSelectedIndex(old_selected
, selected_index_
);
50 bool SearchResultContainerView::IsValidSelectionIndex(int index
) const {
51 return index
>= 0 && index
<= num_results() - 1;
54 void SearchResultContainerView::ScheduleUpdate() {
55 // When search results are added one by one, each addition generates an update
56 // request. Consolidates those update requests into one Update call.
57 if (!update_factory_
.HasWeakPtrs()) {
58 base::MessageLoop::current()->PostTask(
60 base::Bind(&SearchResultContainerView::DoUpdate
,
61 update_factory_
.GetWeakPtr()));
65 bool SearchResultContainerView::UpdateScheduled() {
66 return update_factory_
.HasWeakPtrs();
69 void SearchResultContainerView::ListItemsAdded(size_t start
, size_t count
) {
73 void SearchResultContainerView::ListItemsRemoved(size_t start
, size_t count
) {
77 void SearchResultContainerView::ListItemMoved(size_t index
,
78 size_t target_index
) {
82 void SearchResultContainerView::ListItemsChanged(size_t start
, size_t count
) {
86 void SearchResultContainerView::DoUpdate() {
87 update_factory_
.InvalidateWeakPtrs();
88 num_results_
= Update();
91 delegate_
->OnSearchResultContainerResultsChanged();
94 } // namespace app_list