Chromium Code Reviews| Index: ui/app_list/search_result_list_view.cc |
| diff --git a/ui/app_list/search_result_list_view.cc b/ui/app_list/search_result_list_view.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..af121ba9854655a409eb64c2c2bac21372549329 |
| --- /dev/null |
| +++ b/ui/app_list/search_result_list_view.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/app_list/search_result_list_view.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop.h" |
| +#include "ui/app_list/search_result_view.h" |
| +#include "ui/app_list/search_result_list_view_delegate.h" |
| +#include "ui/app_list/view_ids.h" |
| +#include "ui/views/layout/box_layout.h" |
| + |
| +namespace { |
| + |
| +const int kMaxResults = 6; |
| + |
| +} // namespace |
| + |
| +namespace app_list { |
| + |
| +SearchResultListView::SearchResultListView( |
| + SearchResultListViewDelegate* delegate) |
| + : delegate_(delegate), |
| + results_(NULL), |
| + last_visible_index_(0), |
| + selected_index_(-1), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(update_factory_(this)) { |
| + set_id(VIEW_ID_SEARCH_RESULTS); |
| + |
| + SetLayoutManager( |
| + new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
| + |
| + for (int i = 0; i < kMaxResults; ++i) |
| + AddChildView(new SearchResultView(this)); |
|
sky
2012/05/22 22:16:06
What happens if you there isn't enough space to fi
xiyuan
2012/05/23 21:25:22
Hosting AppListView is updated to leave enough ver
|
| +} |
| + |
| +SearchResultListView::~SearchResultListView() { |
| + if (results_) |
| + results_->RemoveObserver(this); |
| +} |
| + |
| +void SearchResultListView::SetResults(AppListModel::SearchResults* results) { |
| + if (results_) |
| + results_->RemoveObserver(this); |
| + |
| + results_ = results; |
| + if (results_) |
| + results_->AddObserver(this); |
| + |
| + Update(); |
| +} |
| + |
| +void SearchResultListView::SetSelectedIndex(int selected_index) { |
| + if (selected_index_ == selected_index) |
| + return; |
| + |
| + if (selected_index_ >= 0) |
| + GetResultViewAt(selected_index_)->SetSelected(false); |
| + |
| + selected_index_ = selected_index; |
| + |
| + if (selected_index_ >= 0) |
| + GetResultViewAt(selected_index_)->SetSelected(true); |
| +} |
| + |
| +SearchResultView* SearchResultListView::GetResultViewAt(int index) { |
| + DCHECK(index >= 0 && index < child_count()); |
| + return static_cast<SearchResultView*>(child_at(index)); |
| +} |
| + |
| +void SearchResultListView::Update() { |
| + last_visible_index_ = 0; |
| + for (size_t i = 0; i < static_cast<size_t>(child_count()); ++i) { |
| + SearchResultView* result_view = GetResultViewAt(i); |
| + if (i < results_->item_count()) { |
| + result_view->SetResult(results_->GetItemAt(i)); |
| + result_view->SetVisible(true); |
| + last_visible_index_ = i; |
| + } else { |
| + result_view->SetResult(NULL); |
| + result_view->SetVisible(false); |
| + } |
| + } |
| + if (selected_index_ > last_visible_index_) |
| + SetSelectedIndex(last_visible_index_); |
| + |
| + Layout(); |
| + update_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +void SearchResultListView::ScheduleUpdate() { |
| + if (!update_factory_.HasWeakPtrs()) { |
|
sky
2012/05/22 22:16:06
Document why this is throttled.
xiyuan
2012/05/23 21:25:22
Done.
|
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&SearchResultListView::Update, |
| + update_factory_.GetWeakPtr())); |
| + } |
| +} |
| + |
| +gfx::Size SearchResultListView::GetPreferredSize() { |
| + if (!parent()) |
| + return gfx::Size(); |
| + |
| + // Result list occupies the same place as grid view + page switcher. |
|
sky
2012/05/22 22:16:06
I don't understand this. The preferred size should
xiyuan
2012/05/23 21:25:22
Removed and explicitly layout those views in paren
|
| + gfx::Rect rect(parent()->GetViewByID(VIEW_ID_APPS_GRID)->bounds()); |
| + rect = rect.Union(parent()->GetViewByID(VIEW_ID_PAGE_SWITCHER)->bounds()); |
| + return rect.size(); |
| +} |
| + |
| +bool SearchResultListView::OnKeyPressed(const views::KeyEvent& event) { |
| + switch (event.key_code()) { |
| + case ui::VKEY_UP: |
| + SetSelectedIndex(std::max(selected_index_ - 1, 0)); |
| + return true; |
| + case ui::VKEY_DOWN: |
| + SetSelectedIndex(std::min(selected_index_ + 1, last_visible_index_)); |
| + return true; |
| + case ui::VKEY_RETURN: |
| + if (selected_index_ >= 0) |
| + ButtonPressed(GetResultViewAt(selected_index_), event); |
| + return true; |
| + default: |
| + break; |
| + } |
| + return false; |
| +} |
| + |
| +void SearchResultListView::ButtonPressed(views::Button* sender, |
| + const views::Event& event) { |
| + if (sender->GetClassName() != SearchResultView::kViewClassName) |
| + return; |
| + |
| + if (delegate_) { |
| + delegate_->OpenResult(static_cast<SearchResultView*>(sender)->result(), |
| + event.flags()); |
| + } |
| +} |
| + |
| +void SearchResultListView::ListItemsAdded(size_t start, size_t count) { |
| + ScheduleUpdate(); |
| +} |
| + |
| +void SearchResultListView::ListItemsRemoved(size_t start, size_t count) { |
| + ScheduleUpdate(); |
| +} |
| + |
| +void SearchResultListView::ListItemsChanged(size_t start, size_t count) { |
| + ScheduleUpdate(); |
| +} |
| + |
| +} // namespace app_list |