1 // Copyright 2013 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/views/apps_container_view.h" 6 7 #include <algorithm> 8 9 #include "ui/app_list/app_list_constants.h" 10 #include "ui/app_list/app_list_folder_item.h" 11 #include "ui/app_list/pagination_model.h" 12 #include "ui/app_list/views/app_list_folder_view.h" 13 #include "ui/app_list/views/app_list_main_view.h" 14 #include "ui/app_list/views/apps_grid_view.h" 15 #include "ui/events/event.h" 16 17 namespace app_list { 18 19 AppsContainerView::AppsContainerView(AppListMainView* app_list_main_view, 20 PaginationModel* pagination_model, 21 AppListModel* model, 22 content::WebContents* start_page_contents) 23 : model_(model), 24 show_state_(SHOW_APPS) { 25 apps_grid_view_ = new AppsGridView( 26 app_list_main_view, pagination_model, start_page_contents); 27 apps_grid_view_->SetLayout(kPreferredIconDimension, 28 kPreferredCols, 29 kPreferredRows); 30 AddChildView(apps_grid_view_); 31 32 app_list_folder_view_ = new AppListFolderView( 33 this, 34 model, 35 app_list_main_view, 36 start_page_contents); 37 AddChildView(app_list_folder_view_); 38 39 apps_grid_view_->SetModel(model_); 40 apps_grid_view_->SetItemList(model_->item_list()); 41 } 42 43 AppsContainerView::~AppsContainerView() { 44 } 45 46 void AppsContainerView::ShowActiveFolder(AppListFolderItem* folder_item) { 47 app_list_folder_view_->SetAppListFolderItem(folder_item); 48 SetShowState(SHOW_ACTIVE_FOLDER); 49 } 50 51 void AppsContainerView::ShowApps() { 52 SetShowState(SHOW_APPS); 53 } 54 55 gfx::Size AppsContainerView::GetPreferredSize() { 56 const gfx::Size grid_size = apps_grid_view_->GetPreferredSize(); 57 const gfx::Size folder_view_size = app_list_folder_view_->GetPreferredSize(); 58 59 int width = std::max(grid_size.width(), folder_view_size.width()); 60 int height = std::max(grid_size.height(), folder_view_size.height()); 61 return gfx::Size(width, height); 62 } 63 64 void AppsContainerView::Layout() { 65 gfx::Rect rect(GetContentsBounds()); 66 if (rect.IsEmpty()) 67 return; 68 69 switch (show_state_) { 70 case SHOW_APPS: 71 app_list_folder_view_->SetVisible(false); 72 apps_grid_view_->SetBoundsRect(rect); 73 apps_grid_view_->SetVisible(true); 74 break; 75 case SHOW_ACTIVE_FOLDER: 76 apps_grid_view_->SetVisible(false); 77 app_list_folder_view_->SetBoundsRect(rect); 78 app_list_folder_view_->SetVisible(true); 79 break; 80 default: 81 NOTREACHED(); 82 } 83 } 84 85 bool AppsContainerView::OnKeyPressed(const ui::KeyEvent& event) { 86 if (show_state_ == SHOW_APPS) 87 return apps_grid_view_->OnKeyPressed(event); 88 else 89 return app_list_folder_view_->OnKeyPressed(event); 90 } 91 92 void AppsContainerView::SetShowState(ShowState show_state) { 93 if (show_state_ == show_state) 94 return; 95 96 show_state_ = show_state; 97 Layout(); 98 } 99 100 } // namespace app_list 101