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()
13 : selected_index_(-1),
16 update_factory_(this) {
19 SearchResultContainerView::~SearchResultContainerView() {
21 results_
->RemoveObserver(this);
24 void SearchResultContainerView::SetResults(
25 AppListModel::SearchResults
* results
) {
27 results_
->RemoveObserver(this);
31 results_
->AddObserver(this);
36 void SearchResultContainerView::SetSelectedIndex(int selected_index
) {
37 DCHECK(IsValidSelectionIndex(selected_index
));
38 int old_selected
= selected_index_
;
39 selected_index_
= selected_index
;
40 UpdateSelectedIndex(old_selected
, selected_index_
);
43 void SearchResultContainerView::ClearSelectedIndex() {
44 int old_selected
= selected_index_
;
46 UpdateSelectedIndex(old_selected
, selected_index_
);
49 bool SearchResultContainerView::IsValidSelectionIndex(int index
) const {
50 return index
>= 0 && index
<= num_results() - 1;
53 void SearchResultContainerView::ScheduleUpdate() {
54 // When search results are added one by one, each addition generates an update
55 // request. Consolidates those update requests into one Update call.
56 if (!update_factory_
.HasWeakPtrs()) {
57 base::MessageLoop::current()->PostTask(
59 base::Bind(&SearchResultContainerView::DoUpdate
,
60 update_factory_
.GetWeakPtr()));
64 void SearchResultContainerView::ListItemsAdded(size_t start
, size_t count
) {
68 void SearchResultContainerView::ListItemsRemoved(size_t start
, size_t count
) {
72 void SearchResultContainerView::ListItemMoved(size_t index
,
73 size_t target_index
) {
77 void SearchResultContainerView::ListItemsChanged(size_t start
, size_t count
) {
81 void SearchResultContainerView::DoUpdate() {
82 num_results_
= Update();
84 PreferredSizeChanged();
85 update_factory_
.InvalidateWeakPtrs();
88 } // namespace app_list