Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / apps_container_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/apps_container_view.h"
6
7 #include <algorithm>
8
9 #include "base/command_line.h"
10 #include "ui/app_list/app_list_constants.h"
11 #include "ui/app_list/app_list_folder_item.h"
12 #include "ui/app_list/app_list_switches.h"
13 #include "ui/app_list/pagination_model.h"
14 #include "ui/app_list/views/app_list_folder_view.h"
15 #include "ui/app_list/views/app_list_item_view.h"
16 #include "ui/app_list/views/app_list_main_view.h"
17 #include "ui/app_list/views/apps_grid_view.h"
18 #include "ui/app_list/views/folder_background_view.h"
19 #include "ui/events/event.h"
20
21 namespace app_list {
22
23 AppsContainerView::AppsContainerView(AppListMainView* app_list_main_view,
24                                      PaginationModel* pagination_model,
25                                      AppListModel* model,
26                                      content::WebContents* start_page_contents)
27     : model_(model),
28       show_state_(SHOW_APPS),
29       top_icon_animation_pending_count_(0) {
30   apps_grid_view_ = new AppsGridView(
31       app_list_main_view, pagination_model, start_page_contents);
32   int cols = kPreferredCols;
33   int rows = kPreferredRows;
34   if (CommandLine::ForCurrentProcess()->HasSwitch(
35       app_list::switches::kEnableExperimentalAppList)) {
36     cols = kExperimentalPreferredCols;
37     rows = kExperimentalPreferredRows;
38   }
39   apps_grid_view_->SetLayout(kPreferredIconDimension, cols, rows);
40   AddChildView(apps_grid_view_);
41
42   folder_background_view_ = new FolderBackgroundView();
43   AddChildView(folder_background_view_);
44
45   app_list_folder_view_ = new AppListFolderView(
46       this,
47       model,
48       app_list_main_view,
49       start_page_contents);
50   AddChildView(app_list_folder_view_);
51
52   apps_grid_view_->SetModel(model_);
53   apps_grid_view_->SetItemList(model_->item_list());
54   SetShowState(SHOW_APPS,
55                false);  /* show apps without animation */
56 }
57
58 AppsContainerView::~AppsContainerView() {
59 }
60
61 void AppsContainerView::ShowActiveFolder(AppListFolderItem* folder_item) {
62   app_list_folder_view_->SetAppListFolderItem(folder_item);
63   SetShowState(SHOW_ACTIVE_FOLDER, false);
64
65   CreateViewsForFolderTopItemsAnimation(folder_item, true);
66 }
67
68 void AppsContainerView::ShowApps(AppListFolderItem* folder_item) {
69   PrepareToShowApps(folder_item);
70   SetShowState(SHOW_APPS,
71                true);  /* show apps with animation */
72 }
73
74 void AppsContainerView::SetDragAndDropHostOfCurrentAppList(
75     ApplicationDragAndDropHost* drag_and_drop_host) {
76   apps_grid_view()->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
77   app_list_folder_view()->items_grid_view()->
78       SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
79 }
80
81 void AppsContainerView::ReparentFolderItemTransit(
82     AppListFolderItem* folder_item) {
83   PrepareToShowApps(folder_item);
84   SetShowState(SHOW_ITEM_REPARENT, false);
85 }
86
87 gfx::Size AppsContainerView::GetPreferredSize() {
88   const gfx::Size grid_size = apps_grid_view_->GetPreferredSize();
89   const gfx::Size folder_view_size = app_list_folder_view_->GetPreferredSize();
90
91   int width = std::max(grid_size.width(), folder_view_size.width());
92   int height = std::max(grid_size.height(), folder_view_size.height());
93   return gfx::Size(width, height);
94 }
95
96 void AppsContainerView::Layout() {
97   gfx::Rect rect(GetContentsBounds());
98   if (rect.IsEmpty())
99     return;
100
101   switch (show_state_) {
102     case SHOW_APPS:
103       apps_grid_view_->SetBoundsRect(rect);
104       break;
105     case SHOW_ACTIVE_FOLDER:
106       folder_background_view_->SetBoundsRect(rect);
107       app_list_folder_view_->SetBoundsRect(rect);
108       break;
109     case SHOW_ITEM_REPARENT:
110       break;
111     default:
112       NOTREACHED();
113   }
114 }
115
116 bool AppsContainerView::OnKeyPressed(const ui::KeyEvent& event) {
117   if (show_state_ == SHOW_APPS)
118     return apps_grid_view_->OnKeyPressed(event);
119   else
120     return app_list_folder_view_->OnKeyPressed(event);
121 }
122
123 void AppsContainerView::OnTopIconAnimationsComplete() {
124   --top_icon_animation_pending_count_;
125
126   if (!top_icon_animation_pending_count_) {
127     // Clean up the transitional views used for top item icon animation.
128     top_icon_views_.clear();
129
130     // Show the folder icon when closing the folder.
131     if ((show_state_ == SHOW_APPS || show_state_ == SHOW_ITEM_REPARENT) &&
132         apps_grid_view_->activated_item_view()) {
133       apps_grid_view_->activated_item_view()->SetVisible(true);
134     }
135   }
136 }
137
138 void AppsContainerView::SetShowState(ShowState show_state,
139                                      bool show_apps_with_animation) {
140   if (show_state_ == show_state)
141     return;
142
143   show_state_ = show_state;
144
145   switch (show_state_) {
146     case SHOW_APPS:
147       folder_background_view_->SetVisible(false);
148       if (show_apps_with_animation) {
149         app_list_folder_view_->ScheduleShowHideAnimation(false, false);
150         apps_grid_view_->ScheduleShowHideAnimation(true);
151       } else {
152         app_list_folder_view_->HideViewImmediately();
153         apps_grid_view_->SetVisible(true);
154       }
155       break;
156     case SHOW_ACTIVE_FOLDER:
157       folder_background_view_->SetVisible(true);
158       apps_grid_view_->ScheduleShowHideAnimation(false);
159       app_list_folder_view_->ScheduleShowHideAnimation(true, false);
160       break;
161     case SHOW_ITEM_REPARENT:
162       folder_background_view_->SetVisible(false);
163       folder_background_view_->UpdateFolderContainerBubble(
164           FolderBackgroundView::NO_BUBBLE);
165       app_list_folder_view_->ScheduleShowHideAnimation(false, true);
166       apps_grid_view_->ScheduleShowHideAnimation(true);
167       break;
168     default:
169       NOTREACHED();
170   }
171
172   Layout();
173 }
174
175 Rects AppsContainerView::GetTopItemIconBoundsInActiveFolder() {
176   // Get the active folder's icon bounds relative to AppsContainerView.
177   AppListItemView* folder_item_view = apps_grid_view_->activated_item_view();
178   gfx::Rect to_grid_view = folder_item_view->ConvertRectToParent(
179       folder_item_view->GetIconBounds());
180   gfx::Rect to_container = apps_grid_view_->ConvertRectToParent(to_grid_view);
181
182   return AppListFolderItem::GetTopIconsBounds(to_container);
183 }
184
185 void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
186     AppListFolderItem* active_folder,
187     bool open_folder) {
188   top_icon_views_.clear();
189   std::vector<gfx::Rect> top_items_bounds =
190       GetTopItemIconBoundsInActiveFolder();
191   top_icon_animation_pending_count_ =
192       std::min(kNumFolderTopItems, active_folder->item_list()->item_count());
193   for (size_t i = 0; i < top_icon_animation_pending_count_; ++i) {
194     TopIconAnimationView* icon_view = new TopIconAnimationView(
195         active_folder->GetTopIcon(i), top_items_bounds[i], open_folder);
196     icon_view->AddObserver(this);
197     top_icon_views_.push_back(icon_view);
198
199     // Add the transitional views into child views, and set its bounds to the
200     // same location of the item in the folder list view.
201     AddChildView(top_icon_views_[i]);
202     top_icon_views_[i]->SetBoundsRect(
203         app_list_folder_view_->ConvertRectToParent(
204             app_list_folder_view_->GetItemIconBoundsAt(i)));
205     static_cast<TopIconAnimationView*>(top_icon_views_[i])->TransformView();
206   }
207 }
208
209 void AppsContainerView::PrepareToShowApps(AppListFolderItem* folder_item) {
210   if (folder_item)
211     CreateViewsForFolderTopItemsAnimation(folder_item, false);
212
213   // Hide the active folder item until the animation completes.
214   if (apps_grid_view_->activated_item_view())
215     apps_grid_view_->activated_item_view()->SetVisible(false);
216 }
217
218 }  // namespace app_list