Chromium Code Reviews| 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_view.h" | |
| 12 #include "ui/app_list/search_result_list_view_delegate.h" | |
| 13 #include "ui/app_list/view_ids.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 set_id(VIEW_ID_SEARCH_RESULTS); | |
| 32 | |
| 33 SetLayoutManager( | |
| 34 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | |
| 35 | |
| 36 for (int i = 0; i < kMaxResults; ++i) | |
| 37 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
| |
| 38 } | |
| 39 | |
| 40 SearchResultListView::~SearchResultListView() { | |
| 41 if (results_) | |
| 42 results_->RemoveObserver(this); | |
| 43 } | |
| 44 | |
| 45 void SearchResultListView::SetResults(AppListModel::SearchResults* results) { | |
| 46 if (results_) | |
| 47 results_->RemoveObserver(this); | |
| 48 | |
| 49 results_ = results; | |
| 50 if (results_) | |
| 51 results_->AddObserver(this); | |
| 52 | |
| 53 Update(); | |
| 54 } | |
| 55 | |
| 56 void SearchResultListView::SetSelectedIndex(int selected_index) { | |
| 57 if (selected_index_ == selected_index) | |
| 58 return; | |
| 59 | |
| 60 if (selected_index_ >= 0) | |
| 61 GetResultViewAt(selected_index_)->SetSelected(false); | |
| 62 | |
| 63 selected_index_ = selected_index; | |
| 64 | |
| 65 if (selected_index_ >= 0) | |
| 66 GetResultViewAt(selected_index_)->SetSelected(true); | |
| 67 } | |
| 68 | |
| 69 SearchResultView* SearchResultListView::GetResultViewAt(int index) { | |
| 70 DCHECK(index >= 0 && index < child_count()); | |
| 71 return static_cast<SearchResultView*>(child_at(index)); | |
| 72 } | |
| 73 | |
| 74 void SearchResultListView::Update() { | |
| 75 last_visible_index_ = 0; | |
| 76 for (size_t i = 0; i < static_cast<size_t>(child_count()); ++i) { | |
| 77 SearchResultView* result_view = GetResultViewAt(i); | |
| 78 if (i < results_->item_count()) { | |
| 79 result_view->SetResult(results_->GetItemAt(i)); | |
| 80 result_view->SetVisible(true); | |
| 81 last_visible_index_ = i; | |
| 82 } else { | |
| 83 result_view->SetResult(NULL); | |
| 84 result_view->SetVisible(false); | |
| 85 } | |
| 86 } | |
| 87 if (selected_index_ > last_visible_index_) | |
| 88 SetSelectedIndex(last_visible_index_); | |
| 89 | |
| 90 Layout(); | |
| 91 update_factory_.InvalidateWeakPtrs(); | |
| 92 } | |
| 93 | |
| 94 void SearchResultListView::ScheduleUpdate() { | |
| 95 if (!update_factory_.HasWeakPtrs()) { | |
|
sky
2012/05/22 22:16:06
Document why this is throttled.
xiyuan
2012/05/23 21:25:22
Done.
| |
| 96 MessageLoop::current()->PostTask( | |
| 97 FROM_HERE, | |
| 98 base::Bind(&SearchResultListView::Update, | |
| 99 update_factory_.GetWeakPtr())); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 gfx::Size SearchResultListView::GetPreferredSize() { | |
| 104 if (!parent()) | |
| 105 return gfx::Size(); | |
| 106 | |
| 107 // 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
| |
| 108 gfx::Rect rect(parent()->GetViewByID(VIEW_ID_APPS_GRID)->bounds()); | |
| 109 rect = rect.Union(parent()->GetViewByID(VIEW_ID_PAGE_SWITCHER)->bounds()); | |
| 110 return rect.size(); | |
| 111 } | |
| 112 | |
| 113 bool SearchResultListView::OnKeyPressed(const views::KeyEvent& event) { | |
| 114 switch (event.key_code()) { | |
| 115 case ui::VKEY_UP: | |
| 116 SetSelectedIndex(std::max(selected_index_ - 1, 0)); | |
| 117 return true; | |
| 118 case ui::VKEY_DOWN: | |
| 119 SetSelectedIndex(std::min(selected_index_ + 1, last_visible_index_)); | |
| 120 return true; | |
| 121 case ui::VKEY_RETURN: | |
| 122 if (selected_index_ >= 0) | |
| 123 ButtonPressed(GetResultViewAt(selected_index_), event); | |
| 124 return true; | |
| 125 default: | |
| 126 break; | |
| 127 } | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 void SearchResultListView::ButtonPressed(views::Button* sender, | |
| 132 const views::Event& event) { | |
| 133 if (sender->GetClassName() != SearchResultView::kViewClassName) | |
| 134 return; | |
| 135 | |
| 136 if (delegate_) { | |
| 137 delegate_->OpenResult(static_cast<SearchResultView*>(sender)->result(), | |
| 138 event.flags()); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void SearchResultListView::ListItemsAdded(size_t start, size_t count) { | |
| 143 ScheduleUpdate(); | |
| 144 } | |
| 145 | |
| 146 void SearchResultListView::ListItemsRemoved(size_t start, size_t count) { | |
| 147 ScheduleUpdate(); | |
| 148 } | |
| 149 | |
| 150 void SearchResultListView::ListItemsChanged(size_t start, size_t count) { | |
| 151 ScheduleUpdate(); | |
| 152 } | |
| 153 | |
| 154 } // namespace app_list | |
| OLD | NEW |