WebKit roll 130275:130304
[chromium-blink-merge.git] / ui / app_list / search_result_list_view.cc
blobb3f86c695c28df00b0ce0236d4762709d9b35dab
1 // Copyright (c) 2012 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/search_result_list_view.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/message_loop.h"
11 #include "ui/app_list/search_result_list_view_delegate.h"
12 #include "ui/app_list/search_result_view.h"
13 #include "ui/base/events/event.h"
14 #include "ui/views/layout/box_layout.h"
16 namespace {
18 const int kMaxResults = 6;
20 } // namespace
22 namespace app_list {
24 SearchResultListView::SearchResultListView(
25 SearchResultListViewDelegate* delegate)
26 : delegate_(delegate),
27 results_(NULL),
28 last_visible_index_(0),
29 selected_index_(-1),
30 ALLOW_THIS_IN_INITIALIZER_LIST(update_factory_(this)) {
31 SetLayoutManager(
32 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
34 for (int i = 0; i < kMaxResults; ++i)
35 AddChildView(new SearchResultView(this, this));
38 SearchResultListView::~SearchResultListView() {
39 if (results_)
40 results_->RemoveObserver(this);
43 void SearchResultListView::SetResults(AppListModel::SearchResults* results) {
44 if (results_)
45 results_->RemoveObserver(this);
47 results_ = results;
48 if (results_)
49 results_->AddObserver(this);
51 Update();
54 void SearchResultListView::SetSelectedIndex(int selected_index) {
55 if (selected_index_ == selected_index)
56 return;
58 if (selected_index_ >= 0)
59 GetResultViewAt(selected_index_)->SchedulePaint();
61 selected_index_ = selected_index;
63 if (selected_index_ >= 0)
64 GetResultViewAt(selected_index_)->SchedulePaint();
67 bool SearchResultListView::IsResultViewSelected(
68 const SearchResultView* result_view) const {
69 if (selected_index_ < 0)
70 return false;
72 return static_cast<const SearchResultView*>(child_at(selected_index_)) ==
73 result_view;
76 bool SearchResultListView::OnKeyPressed(const ui::KeyEvent& event) {
77 switch (event.key_code()) {
78 case ui::VKEY_UP:
79 SetSelectedIndex(std::max(selected_index_ - 1, 0));
80 return true;
81 case ui::VKEY_DOWN:
82 SetSelectedIndex(std::min(selected_index_ + 1, last_visible_index_));
83 return true;
84 case ui::VKEY_RETURN:
85 if (selected_index_ >= 0)
86 SearchResultActivated(GetResultViewAt(selected_index_), event);
87 return true;
88 default:
89 break;
91 return false;
94 SearchResultView* SearchResultListView::GetResultViewAt(int index) {
95 DCHECK(index >= 0 && index < child_count());
96 return static_cast<SearchResultView*>(child_at(index));
99 void SearchResultListView::Update() {
100 last_visible_index_ = 0;
101 for (size_t i = 0; i < static_cast<size_t>(child_count()); ++i) {
102 SearchResultView* result_view = GetResultViewAt(i);
103 if (i < results_->item_count()) {
104 result_view->SetResult(results_->GetItemAt(i));
105 result_view->SetVisible(true);
106 last_visible_index_ = i;
107 } else {
108 result_view->SetResult(NULL);
109 result_view->SetVisible(false);
112 if (selected_index_ > last_visible_index_)
113 SetSelectedIndex(last_visible_index_);
115 Layout();
116 update_factory_.InvalidateWeakPtrs();
119 void SearchResultListView::ScheduleUpdate() {
120 // When search results are added one by one, each addition generates an update
121 // request. Consolidates those update requests into one Update call.
122 if (!update_factory_.HasWeakPtrs()) {
123 MessageLoop::current()->PostTask(
124 FROM_HERE,
125 base::Bind(&SearchResultListView::Update,
126 update_factory_.GetWeakPtr()));
130 void SearchResultListView::ListItemsAdded(size_t start, size_t count) {
131 ScheduleUpdate();
134 void SearchResultListView::ListItemsRemoved(size_t start, size_t count) {
135 size_t last = std::min(start + count, static_cast<size_t>(child_count()));
136 for (size_t i = start; i < last; ++i)
137 GetResultViewAt(i)->ClearResultNoRepaint();
139 ScheduleUpdate();
142 void SearchResultListView::ListItemMoved(size_t index, size_t target_index) {
143 NOTREACHED();
146 void SearchResultListView::ListItemsChanged(size_t start, size_t count) {
147 ScheduleUpdate();
150 void SearchResultListView::SearchResultActivated(SearchResultView* view,
151 const ui::Event& event) {
152 if (delegate_ && view->result())
153 delegate_->OpenResult(*(view->result()), event.flags());
156 void SearchResultListView::SearchResultActionActivated(SearchResultView* view,
157 int action_index,
158 const ui::Event& event) {
159 if (delegate_ && view->result()) {
160 delegate_->InvokeResultAction(
161 *(view->result()), action_index, event.flags());
165 } // namespace app_list