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