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.
5 #include "ui/app_list/views/apps_container_view.h"
10 #include "base/command_line.h"
11 #include "ui/app_list/app_list_constants.h"
12 #include "ui/app_list/app_list_folder_item.h"
13 #include "ui/app_list/app_list_switches.h"
14 #include "ui/app_list/pagination_model.h"
15 #include "ui/app_list/views/app_list_folder_view.h"
16 #include "ui/app_list/views/app_list_item_view.h"
17 #include "ui/app_list/views/app_list_main_view.h"
18 #include "ui/app_list/views/apps_grid_view.h"
19 #include "ui/app_list/views/folder_background_view.h"
20 #include "ui/events/event.h"
24 AppsContainerView::AppsContainerView(AppListMainView* app_list_main_view,
25 PaginationModel* pagination_model,
28 show_state_(SHOW_NONE),
29 top_icon_animation_pending_count_(0) {
30 apps_grid_view_ = new AppsGridView(app_list_main_view, pagination_model);
31 int cols = kPreferredCols;
32 int rows = kPreferredRows;
33 // ShouldCenterWindow also implies that it is wide instead of tall.
34 if (app_list_main_view->ShouldCenterWindow()) {
35 cols = kExperimentalPreferredCols;
36 rows = kExperimentalPreferredRows;
38 apps_grid_view_->SetLayout(kPreferredIconDimension, cols, rows);
39 AddChildView(apps_grid_view_);
41 folder_background_view_ = new FolderBackgroundView();
42 AddChildView(folder_background_view_);
44 app_list_folder_view_ =
45 new AppListFolderView(this, model, app_list_main_view);
46 AddChildView(app_list_folder_view_);
48 apps_grid_view_->SetModel(model_);
49 apps_grid_view_->SetItemList(model_->top_level_item_list());
50 SetShowState(SHOW_APPS,
51 false); /* show apps without animation */
54 AppsContainerView::~AppsContainerView() {
57 void AppsContainerView::ShowActiveFolder(AppListFolderItem* folder_item) {
58 // Prevent new animations from starting if there are currently animations
59 // pending. This fixes crbug.com/357099.
60 if (top_icon_animation_pending_count_)
63 app_list_folder_view_->SetAppListFolderItem(folder_item);
64 SetShowState(SHOW_ACTIVE_FOLDER, false);
66 CreateViewsForFolderTopItemsAnimation(folder_item, true);
69 void AppsContainerView::ShowApps(AppListFolderItem* folder_item) {
70 if (top_icon_animation_pending_count_)
73 PrepareToShowApps(folder_item);
74 SetShowState(SHOW_APPS,
75 true); /* show apps with animation */
78 void AppsContainerView::ResetForShowApps() {
79 SetShowState(SHOW_APPS, false /* show apps without animation */);
80 folder_background_view_->UpdateFolderContainerBubble(
81 FolderBackgroundView::NO_BUBBLE);
84 void AppsContainerView::SetDragAndDropHostOfCurrentAppList(
85 ApplicationDragAndDropHost* drag_and_drop_host) {
86 apps_grid_view()->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
87 app_list_folder_view()->items_grid_view()->
88 SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
91 void AppsContainerView::ReparentFolderItemTransit(
92 AppListFolderItem* folder_item) {
93 if (top_icon_animation_pending_count_)
96 PrepareToShowApps(folder_item);
97 SetShowState(SHOW_ITEM_REPARENT, false);
100 bool AppsContainerView::IsInFolderView() const {
101 return show_state_ == SHOW_ACTIVE_FOLDER;
104 gfx::Size AppsContainerView::GetPreferredSize() {
105 const gfx::Size grid_size = apps_grid_view_->GetPreferredSize();
106 const gfx::Size folder_view_size = app_list_folder_view_->GetPreferredSize();
108 int width = std::max(grid_size.width(), folder_view_size.width());
109 int height = std::max(grid_size.height(), folder_view_size.height());
110 return gfx::Size(width, height);
113 void AppsContainerView::Layout() {
114 gfx::Rect rect(GetContentsBounds());
118 switch (show_state_) {
120 apps_grid_view_->SetBoundsRect(rect);
122 case SHOW_ACTIVE_FOLDER:
123 folder_background_view_->SetBoundsRect(rect);
124 app_list_folder_view_->SetBoundsRect(rect);
126 case SHOW_ITEM_REPARENT:
133 bool AppsContainerView::OnKeyPressed(const ui::KeyEvent& event) {
134 if (show_state_ == SHOW_APPS)
135 return apps_grid_view_->OnKeyPressed(event);
137 return app_list_folder_view_->OnKeyPressed(event);
140 void AppsContainerView::OnTopIconAnimationsComplete() {
141 --top_icon_animation_pending_count_;
143 if (!top_icon_animation_pending_count_) {
144 // Clean up the transitional views used for top item icon animation.
145 top_icon_views_.clear();
147 // Show the folder icon when closing the folder.
148 if ((show_state_ == SHOW_APPS || show_state_ == SHOW_ITEM_REPARENT) &&
149 apps_grid_view_->activated_item_view()) {
150 apps_grid_view_->activated_item_view()->SetVisible(true);
155 void AppsContainerView::SetShowState(ShowState show_state,
156 bool show_apps_with_animation) {
157 if (show_state_ == show_state)
160 show_state_ = show_state;
162 switch (show_state_) {
164 folder_background_view_->SetVisible(false);
165 if (show_apps_with_animation) {
166 app_list_folder_view_->ScheduleShowHideAnimation(false, false);
167 apps_grid_view_->ScheduleShowHideAnimation(true);
169 app_list_folder_view_->HideViewImmediately();
170 apps_grid_view_->ResetForShowApps();
173 case SHOW_ACTIVE_FOLDER:
174 folder_background_view_->SetVisible(true);
175 apps_grid_view_->ScheduleShowHideAnimation(false);
176 app_list_folder_view_->ScheduleShowHideAnimation(true, false);
178 case SHOW_ITEM_REPARENT:
179 folder_background_view_->SetVisible(false);
180 folder_background_view_->UpdateFolderContainerBubble(
181 FolderBackgroundView::NO_BUBBLE);
182 app_list_folder_view_->ScheduleShowHideAnimation(false, true);
183 apps_grid_view_->ScheduleShowHideAnimation(true);
192 Rects AppsContainerView::GetTopItemIconBoundsInActiveFolder() {
193 // Get the active folder's icon bounds relative to AppsContainerView.
194 AppListItemView* folder_item_view = apps_grid_view_->activated_item_view();
195 gfx::Rect to_grid_view = folder_item_view->ConvertRectToParent(
196 folder_item_view->GetIconBounds());
197 gfx::Rect to_container = apps_grid_view_->ConvertRectToParent(to_grid_view);
199 return AppListFolderItem::GetTopIconsBounds(to_container);
202 void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
203 AppListFolderItem* active_folder,
205 top_icon_views_.clear();
206 std::vector<gfx::Rect> top_items_bounds =
207 GetTopItemIconBoundsInActiveFolder();
208 top_icon_animation_pending_count_ =
209 std::min(kNumFolderTopItems, active_folder->item_list()->item_count());
210 for (size_t i = 0; i < top_icon_animation_pending_count_; ++i) {
211 TopIconAnimationView* icon_view = new TopIconAnimationView(
212 active_folder->GetTopIcon(i), top_items_bounds[i], open_folder);
213 icon_view->AddObserver(this);
214 top_icon_views_.push_back(icon_view);
216 // Add the transitional views into child views, and set its bounds to the
217 // same location of the item in the folder list view.
218 AddChildView(top_icon_views_[i]);
219 top_icon_views_[i]->SetBoundsRect(
220 app_list_folder_view_->ConvertRectToParent(
221 app_list_folder_view_->GetItemIconBoundsAt(i)));
222 static_cast<TopIconAnimationView*>(top_icon_views_[i])->TransformView();
226 void AppsContainerView::PrepareToShowApps(AppListFolderItem* folder_item) {
228 CreateViewsForFolderTopItemsAnimation(folder_item, false);
230 // Hide the active folder item until the animation completes.
231 if (apps_grid_view_->activated_item_view())
232 apps_grid_view_->activated_item_view()->SetVisible(false);
235 } // namespace app_list