Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / app_list / views / search_result_container_view.cc
blobb024fde5cb90add62efabc4d21d8223b3d5d3ea5
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"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
10 namespace app_list {
12 SearchResultContainerView::SearchResultContainerView()
13 : delegate_(nullptr),
14 selected_index_(-1),
15 num_results_(0),
16 results_(NULL),
17 update_factory_(this) {
20 SearchResultContainerView::~SearchResultContainerView() {
21 if (results_)
22 results_->RemoveObserver(this);
25 void SearchResultContainerView::SetResults(
26 AppListModel::SearchResults* results) {
27 if (results_)
28 results_->RemoveObserver(this);
30 results_ = results;
31 if (results_)
32 results_->AddObserver(this);
34 DoUpdate();
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_;
46 selected_index_ = -1;
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(
59 FROM_HERE,
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) {
70 ScheduleUpdate();
73 void SearchResultContainerView::ListItemsRemoved(size_t start, size_t count) {
74 ScheduleUpdate();
77 void SearchResultContainerView::ListItemMoved(size_t index,
78 size_t target_index) {
79 ScheduleUpdate();
82 void SearchResultContainerView::ListItemsChanged(size_t start, size_t count) {
83 ScheduleUpdate();
86 void SearchResultContainerView::DoUpdate() {
87 update_factory_.InvalidateWeakPtrs();
88 num_results_ = Update();
89 Layout();
90 if (delegate_)
91 delegate_->OnSearchResultContainerResultsChanged();
94 } // namespace app_list