Home | History | Annotate | Download | only in views
      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/app_list_folder_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/app_list_model.h"
     12 #include "ui/app_list/pagination_model.h"
     13 #include "ui/app_list/views/app_list_main_view.h"
     14 #include "ui/app_list/views/apps_container_view.h"
     15 #include "ui/app_list/views/apps_grid_view.h"
     16 #include "ui/app_list/views/contents_view.h"
     17 #include "ui/app_list/views/folder_header_view.h"
     18 #include "ui/events/event.h"
     19 #include "ui/views/view_model.h"
     20 #include "ui/views/view_model_utils.h"
     21 
     22 namespace app_list {
     23 
     24 namespace {
     25 
     26 // Indexes of interesting views in ViewModel of AppListFolderView.
     27 const int kIndexFolderHeader = 0;
     28 const int kIndexChildItems = 1;
     29 
     30 }  // namespace
     31 
     32 AppListFolderView::AppListFolderView(AppsContainerView* container_view,
     33                                      AppListModel* model,
     34                                      AppListMainView* app_list_main_view,
     35                                      content::WebContents* start_page_contents)
     36     : container_view_(container_view),
     37       folder_header_view_(new FolderHeaderView(this)),
     38       view_model_(new views::ViewModel),
     39       folder_item_(NULL),
     40       pagination_model_(new PaginationModel) {
     41   AddChildView(folder_header_view_);
     42   view_model_->Add(folder_header_view_, kIndexFolderHeader);
     43 
     44   items_grid_view_ = new AppsGridView(
     45       app_list_main_view, pagination_model_.get(), NULL);
     46   items_grid_view_->set_is_root_level(false);
     47   items_grid_view_->SetLayout(kPreferredIconDimension,
     48                               kPreferredCols,
     49                               kPreferredRows);
     50   items_grid_view_->SetModel(model);
     51   AddChildView(items_grid_view_);
     52   view_model_->Add(items_grid_view_, kIndexChildItems);
     53 }
     54 
     55 AppListFolderView::~AppListFolderView() {
     56   // Make sure |items_grid_view_| is deleted before |pagination_model_|.
     57   RemoveAllChildViews(true);
     58 }
     59 
     60 void AppListFolderView::SetAppListFolderItem(AppListFolderItem* folder) {
     61   folder_item_ = folder;
     62   items_grid_view_->SetItemList(folder_item_->item_list());
     63   folder_header_view_->SetFolderItem(folder_item_);
     64 }
     65 
     66 gfx::Size AppListFolderView::GetPreferredSize() {
     67   const gfx::Size header_size = folder_header_view_->GetPreferredSize();
     68   const gfx::Size grid_size = items_grid_view_->GetPreferredSize();
     69   int width = std::max(header_size.width(), grid_size.width());
     70   int height = header_size.height() + grid_size.height();
     71   return gfx::Size(width, height);
     72 }
     73 
     74 void AppListFolderView::Layout() {
     75   CalculateIdealBounds();
     76   views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
     77 }
     78 
     79 bool AppListFolderView::OnKeyPressed(const ui::KeyEvent& event) {
     80   return items_grid_view_->OnKeyPressed(event);
     81 }
     82 
     83 void AppListFolderView::CalculateIdealBounds() {
     84   gfx::Rect rect(GetContentsBounds());
     85   if (rect.IsEmpty())
     86     return;
     87 
     88   gfx::Rect header_frame(rect);
     89   gfx::Size size = folder_header_view_->GetPreferredSize();
     90   header_frame.set_height(size.height());
     91   view_model_->set_ideal_bounds(kIndexFolderHeader, header_frame);
     92 
     93   gfx::Rect grid_frame(rect);
     94   grid_frame.set_y(header_frame.height());
     95   view_model_->set_ideal_bounds(kIndexChildItems, grid_frame);
     96 }
     97 
     98 void AppListFolderView::NavigateBack(AppListFolderItem* item,
     99                                      const ui::Event& event_flags) {
    100   container_view_->ShowApps();
    101 }
    102 
    103 }  // namespace app_list
    104