| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/app_list/views/start_page_view.h" | 5 #include "ui/app_list/views/start_page_view.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "ui/app_list/app_list_constants.h" | 8 #include "ui/app_list/app_list_constants.h" |
| 9 #include "ui/app_list/app_list_item.h" | 9 #include "ui/app_list/app_list_item.h" |
| 10 #include "ui/app_list/app_list_model.h" | 10 #include "ui/app_list/app_list_model.h" |
| 11 #include "ui/app_list/app_list_view_delegate.h" | 11 #include "ui/app_list/app_list_view_delegate.h" |
| 12 #include "ui/app_list/search_result.h" |
| 12 #include "ui/app_list/views/app_list_main_view.h" | 13 #include "ui/app_list/views/app_list_main_view.h" |
| 13 #include "ui/app_list/views/search_box_view.h" | 14 #include "ui/app_list/views/search_box_view.h" |
| 14 #include "ui/app_list/views/search_result_list_view.h" | 15 #include "ui/app_list/views/search_result_list_view.h" |
| 15 #include "ui/app_list/views/tile_item_view.h" | 16 #include "ui/app_list/views/tile_item_view.h" |
| 16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
| 17 #include "ui/views/background.h" | 18 #include "ui/views/background.h" |
| 18 #include "ui/views/controls/image_view.h" | 19 #include "ui/views/controls/image_view.h" |
| 19 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
| 20 #include "ui/views/controls/textfield/textfield.h" | 21 #include "ui/views/controls/textfield/textfield.h" |
| 21 #include "ui/views/layout/box_layout.h" | 22 #include "ui/views/layout/box_layout.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 89 |
| 89 private: | 90 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(DummySearchBoxView); | 91 DISALLOW_COPY_AND_ASSIGN(DummySearchBoxView); |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 } // namespace | 94 } // namespace |
| 94 | 95 |
| 95 StartPageView::StartPageView(AppListMainView* app_list_main_view, | 96 StartPageView::StartPageView(AppListMainView* app_list_main_view, |
| 96 AppListViewDelegate* view_delegate) | 97 AppListViewDelegate* view_delegate) |
| 97 : app_list_main_view_(app_list_main_view), | 98 : app_list_main_view_(app_list_main_view), |
| 98 model_(NULL), | 99 search_results_model_(NULL), |
| 99 view_delegate_(view_delegate), | 100 view_delegate_(view_delegate), |
| 100 search_box_view_(new DummySearchBoxView(this, view_delegate_)), | 101 search_box_view_(new DummySearchBoxView(this, view_delegate_)), |
| 101 results_view_( | 102 results_view_( |
| 102 new SearchResultListView(app_list_main_view, view_delegate)), | 103 new SearchResultListView(app_list_main_view, view_delegate)), |
| 103 instant_container_(new views::View), | 104 instant_container_(new views::View), |
| 104 tiles_container_(new views::View), | 105 tiles_container_(new views::View), |
| 105 show_state_(SHOW_START_PAGE) { | 106 show_state_(SHOW_START_PAGE), |
| 107 update_factory_(this) { |
| 106 // The view containing the start page WebContents and DummySearchBoxView. | 108 // The view containing the start page WebContents and DummySearchBoxView. |
| 107 InitInstantContainer(); | 109 InitInstantContainer(); |
| 108 AddChildView(instant_container_); | 110 AddChildView(instant_container_); |
| 109 | 111 |
| 110 // The view containing the search results. | 112 // The view containing the search results. |
| 111 AddChildView(results_view_); | 113 AddChildView(results_view_); |
| 112 | 114 |
| 113 // The view containing the start page tiles. | 115 // The view containing the start page tiles. |
| 114 InitTilesContainer(); | 116 InitTilesContainer(); |
| 115 AddChildView(tiles_container_); | 117 AddChildView(tiles_container_); |
| 116 | 118 |
| 117 SetModel(view_delegate_->GetModel()); | 119 SetModel(view_delegate_->GetModel()); |
| 118 view_delegate_->AddObserver(this); | 120 view_delegate_->AddObserver(this); |
| 119 } | 121 } |
| 120 | 122 |
| 121 StartPageView::~StartPageView() { | 123 StartPageView::~StartPageView() { |
| 122 view_delegate_->RemoveObserver(this); | 124 view_delegate_->RemoveObserver(this); |
| 123 if (model_) | 125 if (search_results_model_) |
| 124 model_->RemoveObserver(this); | 126 search_results_model_->RemoveObserver(this); |
| 125 } | 127 } |
| 126 | 128 |
| 127 void StartPageView::InitInstantContainer() { | 129 void StartPageView::InitInstantContainer() { |
| 128 views::BoxLayout* instant_layout_manager = new views::BoxLayout( | 130 views::BoxLayout* instant_layout_manager = new views::BoxLayout( |
| 129 views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing); | 131 views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing); |
| 130 instant_layout_manager->set_inside_border_insets( | 132 instant_layout_manager->set_inside_border_insets( |
| 131 gfx::Insets(kTopMargin, 0, kInstantContainerSpacing, 0)); | 133 gfx::Insets(kTopMargin, 0, kInstantContainerSpacing, 0)); |
| 132 instant_layout_manager->set_main_axis_alignment( | 134 instant_layout_manager->set_main_axis_alignment( |
| 133 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); | 135 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
| 134 instant_layout_manager->set_cross_axis_alignment( | 136 instant_layout_manager->set_cross_axis_alignment( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 161 tiles_container_->SetLayoutManager(tiles_layout_manager); | 163 tiles_container_->SetLayoutManager(tiles_layout_manager); |
| 162 for (size_t i = 0; i < kNumStartPageTiles; ++i) { | 164 for (size_t i = 0; i < kNumStartPageTiles; ++i) { |
| 163 TileItemView* tile_item = new TileItemView(); | 165 TileItemView* tile_item = new TileItemView(); |
| 164 tiles_container_->AddChildView(tile_item); | 166 tiles_container_->AddChildView(tile_item); |
| 165 tile_views_.push_back(tile_item); | 167 tile_views_.push_back(tile_item); |
| 166 } | 168 } |
| 167 } | 169 } |
| 168 | 170 |
| 169 void StartPageView::SetModel(AppListModel* model) { | 171 void StartPageView::SetModel(AppListModel* model) { |
| 170 DCHECK(model); | 172 DCHECK(model); |
| 171 if (model_) | 173 if (search_results_model_) |
| 172 model_->RemoveObserver(this); | 174 search_results_model_->RemoveObserver(this); |
| 173 model_ = model; | 175 search_results_model_ = model->results(); |
| 174 model_->AddObserver(this); | 176 search_results_model_->AddObserver(this); |
| 175 results_view_->SetResults(model_->results()); | 177 results_view_->SetResults(search_results_model_); |
| 176 Reset(); | 178 Reset(); |
| 177 } | 179 } |
| 178 | 180 |
| 179 void StartPageView::Reset() { | 181 void StartPageView::Reset() { |
| 180 SetShowState(SHOW_START_PAGE); | 182 SetShowState(SHOW_START_PAGE); |
| 181 if (!model_ || !model_->top_level_item_list()) | 183 Update(); |
| 182 return; | |
| 183 | |
| 184 for (size_t i = 0; i < kNumStartPageTiles; ++i) { | |
| 185 AppListItem* item = NULL; | |
| 186 if (i < model_->top_level_item_list()->item_count()) | |
| 187 item = model_->top_level_item_list()->item_at(i); | |
| 188 tile_views_[i]->SetAppListItem(item); | |
| 189 } | |
| 190 } | 184 } |
| 191 | 185 |
| 192 void StartPageView::ShowSearchResults() { | 186 void StartPageView::ShowSearchResults() { |
| 193 SetShowState(SHOW_SEARCH_RESULTS); | 187 SetShowState(SHOW_SEARCH_RESULTS); |
| 188 Update(); |
| 194 } | 189 } |
| 195 | 190 |
| 196 void StartPageView::SetShowState(ShowState show_state) { | 191 void StartPageView::SetShowState(ShowState show_state) { |
| 197 instant_container_->SetVisible(show_state == SHOW_START_PAGE); | 192 instant_container_->SetVisible(show_state == SHOW_START_PAGE); |
| 198 results_view_->SetVisible(show_state == SHOW_SEARCH_RESULTS); | 193 results_view_->SetVisible(show_state == SHOW_SEARCH_RESULTS); |
| 199 | 194 |
| 200 // This can be called when the app list is closing (widget is invisible). In | 195 // This can be called when the app list is closing (widget is invisible). In |
| 201 // that case, do not steal focus from other elements. | 196 // that case, do not steal focus from other elements. |
| 202 if (show_state == SHOW_START_PAGE && GetWidget() && GetWidget()->IsVisible()) | 197 if (show_state == SHOW_START_PAGE && GetWidget() && GetWidget()->IsVisible()) |
| 203 search_box_view_->search_box()->RequestFocus(); | 198 search_box_view_->search_box()->RequestFocus(); |
| 204 | 199 |
| 205 if (show_state_ == show_state) | 200 if (show_state_ == show_state) |
| 206 return; | 201 return; |
| 207 | 202 |
| 208 show_state_ = show_state; | 203 show_state_ = show_state; |
| 209 | 204 |
| 205 if (show_state_ == SHOW_START_PAGE) |
| 206 search_box_view_->ClearSearch(); |
| 207 |
| 210 results_view_->UpdateAutoLaunchState(); | 208 results_view_->UpdateAutoLaunchState(); |
| 211 if (show_state == SHOW_SEARCH_RESULTS) | 209 if (show_state == SHOW_SEARCH_RESULTS) |
| 212 results_view_->SetSelectedIndex(0); | 210 results_view_->SetSelectedIndex(0); |
| 213 } | 211 } |
| 214 | 212 |
| 215 bool StartPageView::IsShowingSearchResults() const { | 213 bool StartPageView::IsShowingSearchResults() const { |
| 216 return show_state_ == SHOW_SEARCH_RESULTS; | 214 return show_state_ == SHOW_SEARCH_RESULTS; |
| 217 } | 215 } |
| 218 | 216 |
| 217 void StartPageView::UpdateForTesting() { |
| 218 Update(); |
| 219 } |
| 220 |
| 219 bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) { | 221 bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) { |
| 220 if (show_state_ == SHOW_SEARCH_RESULTS) | 222 if (show_state_ == SHOW_SEARCH_RESULTS) |
| 221 return results_view_->OnKeyPressed(event); | 223 return results_view_->OnKeyPressed(event); |
| 222 | 224 |
| 223 return false; | 225 return false; |
| 224 } | 226 } |
| 225 | 227 |
| 226 void StartPageView::Layout() { | 228 void StartPageView::Layout() { |
| 227 // Instant and search results take up the height of the instant container. | 229 // Instant and search results take up the height of the instant container. |
| 228 gfx::Rect bounds(GetContentsBounds()); | 230 gfx::Rect bounds(GetContentsBounds()); |
| 229 bounds.set_height(instant_container_->GetHeightForWidth(bounds.width())); | 231 bounds.set_height(instant_container_->GetHeightForWidth(bounds.width())); |
| 230 instant_container_->SetBoundsRect(bounds); | 232 instant_container_->SetBoundsRect(bounds); |
| 231 results_view_->SetBoundsRect(bounds); | 233 results_view_->SetBoundsRect(bounds); |
| 232 | 234 |
| 233 // Tiles begin where the instant container ends. | 235 // Tiles begin where the instant container ends. |
| 234 bounds.set_y(bounds.bottom()); | 236 bounds.set_y(bounds.bottom()); |
| 235 bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width())); | 237 bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width())); |
| 236 tiles_container_->SetBoundsRect(bounds); | 238 tiles_container_->SetBoundsRect(bounds); |
| 237 } | 239 } |
| 238 | 240 |
| 241 void StartPageView::Update() { |
| 242 std::vector<SearchResult*> display_results = |
| 243 AppListModel::FilterSearchResultsByDisplayType(search_results_model_, |
| 244 SearchResult::DISPLAY_TILE, |
| 245 kNumStartPageTiles); |
| 246 for (size_t i = 0; i < kNumStartPageTiles; ++i) { |
| 247 SearchResult* item = NULL; |
| 248 if (i < display_results.size()) |
| 249 item = display_results[i]; |
| 250 tile_views_[i]->SetSearchResult(item); |
| 251 } |
| 252 tiles_container_->Layout(); |
| 253 Layout(); |
| 254 update_factory_.InvalidateWeakPtrs(); |
| 255 } |
| 256 |
| 257 void StartPageView::ScheduleUpdate() { |
| 258 // When search results are added one by one, each addition generates an update |
| 259 // request. Consolidates those update requests into one Update call. |
| 260 if (!update_factory_.HasWeakPtrs()) { |
| 261 base::MessageLoop::current()->PostTask( |
| 262 FROM_HERE, |
| 263 base::Bind(&StartPageView::Update, update_factory_.GetWeakPtr())); |
| 264 } |
| 265 } |
| 266 |
| 239 void StartPageView::QueryChanged(SearchBoxView* sender) { | 267 void StartPageView::QueryChanged(SearchBoxView* sender) { |
| 240 // Forward the search terms on to the real search box and clear the dummy | 268 // Forward the search terms on to the real search box and clear the dummy |
| 241 // search box. | 269 // search box. |
| 242 app_list_main_view_->OnStartPageSearchTextfieldChanged( | 270 app_list_main_view_->OnStartPageSearchTextfieldChanged( |
| 243 sender->search_box()->text()); | 271 sender->search_box()->text()); |
| 244 sender->search_box()->SetText(base::string16()); | 272 sender->search_box()->SetText(base::string16()); |
| 245 } | 273 } |
| 246 | 274 |
| 247 void StartPageView::OnProfilesChanged() { | 275 void StartPageView::OnProfilesChanged() { |
| 248 SetModel(view_delegate_->GetModel()); | 276 SetModel(view_delegate_->GetModel()); |
| 249 } | 277 } |
| 250 | 278 |
| 251 void StartPageView::OnAppListModelStatusChanged() { | 279 void StartPageView::ListItemsAdded(size_t start, size_t count) { |
| 252 Reset(); | 280 ScheduleUpdate(); |
| 253 } | 281 } |
| 254 | 282 |
| 255 void StartPageView::OnAppListItemAdded(AppListItem* item) { | 283 void StartPageView::ListItemsRemoved(size_t start, size_t count) { |
| 256 Reset(); | 284 ScheduleUpdate(); |
| 257 } | 285 } |
| 258 | 286 |
| 259 void StartPageView::OnAppListItemDeleted() { | 287 void StartPageView::ListItemMoved(size_t index, size_t target_index) { |
| 260 Reset(); | 288 ScheduleUpdate(); |
| 261 } | 289 } |
| 262 | 290 |
| 263 void StartPageView::OnAppListItemUpdated(AppListItem* item) { | 291 void StartPageView::ListItemsChanged(size_t start, size_t count) { |
| 264 Reset(); | 292 ScheduleUpdate(); |
| 265 } | 293 } |
| 266 | 294 |
| 267 } // namespace app_list | 295 } // namespace app_list |
| OLD | NEW |