OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ui/app_list/search_result_list_view.h" | |
6 | |
7 #include <algorithm> | |
8 | |
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" | |
15 | |
16 namespace { | |
17 | |
18 const int kMaxResults = 6; | |
19 | |
20 } // namespace | |
21 | |
22 namespace app_list { | |
23 | |
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)); | |
33 | |
34 for (int i = 0; i < kMaxResults; ++i) | |
35 AddChildView(new SearchResultView(this, this)); | |
36 } | |
37 | |
38 SearchResultListView::~SearchResultListView() { | |
39 if (results_) | |
40 results_->RemoveObserver(this); | |
41 } | |
42 | |
43 void SearchResultListView::SetResults(AppListModel::SearchResults* results) { | |
44 if (results_) | |
45 results_->RemoveObserver(this); | |
46 | |
47 results_ = results; | |
48 if (results_) | |
49 results_->AddObserver(this); | |
50 | |
51 Update(); | |
52 } | |
53 | |
54 void SearchResultListView::SetSelectedIndex(int selected_index) { | |
55 if (selected_index_ == selected_index) | |
56 return; | |
57 | |
58 if (selected_index_ >= 0) | |
59 GetResultViewAt(selected_index_)->SchedulePaint(); | |
60 | |
61 selected_index_ = selected_index; | |
62 | |
63 if (selected_index_ >= 0) | |
64 GetResultViewAt(selected_index_)->SchedulePaint(); | |
65 } | |
66 | |
67 bool SearchResultListView::IsResultViewSelected( | |
68 const SearchResultView* result_view) const { | |
69 if (selected_index_ < 0) | |
70 return false; | |
71 | |
72 return static_cast<const SearchResultView*>(child_at(selected_index_)) == | |
73 result_view; | |
74 } | |
75 | |
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; | |
90 } | |
91 return false; | |
92 } | |
93 | |
94 SearchResultView* SearchResultListView::GetResultViewAt(int index) { | |
95 DCHECK(index >= 0 && index < child_count()); | |
96 return static_cast<SearchResultView*>(child_at(index)); | |
97 } | |
98 | |
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); | |
110 } | |
111 } | |
112 if (selected_index_ > last_visible_index_) | |
113 SetSelectedIndex(last_visible_index_); | |
114 | |
115 Layout(); | |
116 update_factory_.InvalidateWeakPtrs(); | |
117 } | |
118 | |
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())); | |
127 } | |
128 } | |
129 | |
130 void SearchResultListView::ListItemsAdded(size_t start, size_t count) { | |
131 ScheduleUpdate(); | |
132 } | |
133 | |
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(); | |
138 | |
139 ScheduleUpdate(); | |
140 } | |
141 | |
142 void SearchResultListView::ListItemMoved(size_t index, size_t target_index) { | |
143 NOTREACHED(); | |
144 } | |
145 | |
146 void SearchResultListView::ListItemsChanged(size_t start, size_t count) { | |
147 ScheduleUpdate(); | |
148 } | |
149 | |
150 void SearchResultListView::SearchResultActivated(SearchResultView* view, | |
151 const ui::Event& event) { | |
152 if (delegate_ && view->result()) | |
153 delegate_->OpenResult(*(view->result()), event.flags()); | |
154 } | |
155 | |
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()); | |
162 } | |
163 } | |
164 | |
165 } // namespace app_list | |
OLD | NEW |