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"
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"
18 const int kMaxResults
= 6;
24 SearchResultListView::SearchResultListView(
25 SearchResultListViewDelegate
* delegate
)
26 : delegate_(delegate
),
28 last_visible_index_(0),
30 ALLOW_THIS_IN_INITIALIZER_LIST(update_factory_(this)) {
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() {
40 results_
->RemoveObserver(this);
43 void SearchResultListView::SetResults(AppListModel::SearchResults
* results
) {
45 results_
->RemoveObserver(this);
49 results_
->AddObserver(this);
54 void SearchResultListView::SetSelectedIndex(int selected_index
) {
55 if (selected_index_
== selected_index
)
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)
72 return static_cast<const SearchResultView
*>(child_at(selected_index_
)) ==
76 bool SearchResultListView::OnKeyPressed(const ui::KeyEvent
& event
) {
77 switch (event
.key_code()) {
79 SetSelectedIndex(std::max(selected_index_
- 1, 0));
82 SetSelectedIndex(std::min(selected_index_
+ 1, last_visible_index_
));
85 if (selected_index_
>= 0)
86 SearchResultActivated(GetResultViewAt(selected_index_
), event
);
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
;
108 result_view
->SetResult(NULL
);
109 result_view
->SetVisible(false);
112 if (selected_index_
> last_visible_index_
)
113 SetSelectedIndex(last_visible_index_
);
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(
125 base::Bind(&SearchResultListView::Update
,
126 update_factory_
.GetWeakPtr()));
130 void SearchResultListView::ListItemsAdded(size_t start
, size_t count
) {
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();
142 void SearchResultListView::ListItemMoved(size_t index
, size_t target_index
) {
146 void SearchResultListView::ListItemsChanged(size_t start
, size_t count
) {
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
,
158 const ui::Event
& event
) {
159 if (delegate_
&& view
->result()) {
160 delegate_
->InvokeResultAction(
161 *(view
->result()), action_index
, event
.flags());
165 } // namespace app_list