69fa7619879fe0a77987d913b991cdaa118a69b3
[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 <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_item_view.h"
14 #include "ui/app_list/views/app_list_main_view.h"
15 #include "ui/app_list/views/apps_container_view.h"
16 #include "ui/app_list/views/apps_grid_view.h"
17 #include "ui/app_list/views/contents_view.h"
18 #include "ui/app_list/views/folder_header_view.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h"
20 #include "ui/events/event.h"
21 #include "ui/views/view_model.h"
22 #include "ui/views/view_model_utils.h"
23
24 namespace app_list {
25
26 namespace {
27
28 // Indexes of interesting views in ViewModel of AppListFolderView.
29 const int kIndexFolderHeader = 0;
30 const int kIndexChildItems = 1;
31
32 }  // namespace
33
34 AppListFolderView::AppListFolderView(AppsContainerView* container_view,
35                                      AppListModel* model,
36                                      AppListMainView* app_list_main_view,
37                                      content::WebContents* start_page_contents)
38     : container_view_(container_view),
39       folder_header_view_(new FolderHeaderView(this)),
40       view_model_(new views::ViewModel),
41       folder_item_(NULL),
42       pagination_model_(new PaginationModel) {
43   AddChildView(folder_header_view_);
44   view_model_->Add(folder_header_view_, kIndexFolderHeader);
45
46   items_grid_view_ = new AppsGridView(
47       app_list_main_view, pagination_model_.get(), NULL);
48   items_grid_view_->set_is_root_level(false);
49   items_grid_view_->SetLayout(kPreferredIconDimension,
50                               kPreferredCols,
51                               kPreferredRows);
52   items_grid_view_->SetModel(model);
53   AddChildView(items_grid_view_);
54   view_model_->Add(items_grid_view_, kIndexChildItems);
55
56 #if defined(USE_AURA)
57   SetPaintToLayer(true);
58   SetFillsBoundsOpaquely(false);
59 #endif
60 }
61
62 AppListFolderView::~AppListFolderView() {
63   // Make sure |items_grid_view_| is deleted before |pagination_model_|.
64   RemoveAllChildViews(true);
65 }
66
67 void AppListFolderView::SetAppListFolderItem(AppListFolderItem* folder) {
68   folder_item_ = folder;
69   items_grid_view_->SetItemList(folder_item_->item_list());
70   folder_header_view_->SetFolderItem(folder_item_);
71 }
72
73 void AppListFolderView::ScheduleShowHideAnimation(bool show) {
74   // Stop any previous animation.
75   layer()->GetAnimator()->StopAnimating();
76
77   // Hide the top items temporarily if showing the view for opening the folder.
78   if (show)
79     items_grid_view_->SetTopItemViewsVisible(false);
80
81   // Set initial state.
82   SetVisible(true);
83   layer()->SetOpacity(show ? 0.0f : 1.0f);
84
85   ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
86   animation.SetTweenType(gfx::Tween::EASE_IN_2);
87   animation.AddObserver(this);
88   animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
89       show ? kFolderTransitionInDurationMs : kFolderTransitionOutDurationMs));
90
91   layer()->SetOpacity(show ? 1.0f : 0.0f);
92 }
93
94 gfx::Size AppListFolderView::GetPreferredSize() {
95   const gfx::Size header_size = folder_header_view_->GetPreferredSize();
96   const gfx::Size grid_size = items_grid_view_->GetPreferredSize();
97   int width = std::max(header_size.width(), grid_size.width());
98   int height = header_size.height() + grid_size.height();
99   return gfx::Size(width, height);
100 }
101
102 void AppListFolderView::Layout() {
103   CalculateIdealBounds();
104   views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
105 }
106
107 bool AppListFolderView::OnKeyPressed(const ui::KeyEvent& event) {
108   return items_grid_view_->OnKeyPressed(event);
109 }
110
111 void AppListFolderView::OnImplicitAnimationsCompleted() {
112   // Show the top items when the opening folder animation is done.
113   if (layer()->opacity() == 1.0f)
114     items_grid_view_->SetTopItemViewsVisible(true);
115
116   if (layer()->opacity() == 0.0f)
117     SetVisible(false);
118 }
119
120 void AppListFolderView::CalculateIdealBounds() {
121   gfx::Rect rect(GetContentsBounds());
122   if (rect.IsEmpty())
123     return;
124
125   gfx::Rect header_frame(rect);
126   gfx::Size size = folder_header_view_->GetPreferredSize();
127   header_frame.set_height(size.height());
128   view_model_->set_ideal_bounds(kIndexFolderHeader, header_frame);
129
130   gfx::Rect grid_frame(rect);
131   grid_frame.set_y(header_frame.height());
132   view_model_->set_ideal_bounds(kIndexChildItems, grid_frame);
133 }
134
135 gfx::Rect AppListFolderView::GetItemIconBoundsAt(int index) {
136   AppListItemView* item_view = items_grid_view_->GetItemViewAt(index);
137   // Icon bounds relative to AppListItemView.
138   const gfx::Rect icon_bounds = item_view->GetIconBounds();
139   gfx::Rect to_apps_grid_view = item_view->ConvertRectToParent(icon_bounds);
140   gfx::Rect to_folder =
141       items_grid_view_->ConvertRectToParent(to_apps_grid_view);
142
143   // Get the icon image's bound.
144   to_folder.ClampToCenteredSize(
145       gfx::Size(kPreferredIconDimension, kPreferredIconDimension));
146
147   return to_folder;
148 }
149
150 void AppListFolderView::NavigateBack(AppListFolderItem* item,
151                                      const ui::Event& event_flags) {
152   container_view_->ShowApps(item);
153 }
154
155 }  // namespace app_list