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/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"
23 AppsContainerView::AppsContainerView(AppListMainView
* app_list_main_view
,
26 show_state_(SHOW_NONE
),
27 top_icon_animation_pending_count_(0) {
28 apps_grid_view_
= new AppsGridView(app_list_main_view
);
31 if (switches::IsExperimentalAppListEnabled()) {
32 cols
= kExperimentalPreferredCols
;
33 rows
= kExperimentalPreferredRows
;
34 } else if (app_list_main_view
->ShouldCenterWindow()) {
35 cols
= kCenteredPreferredCols
;
36 rows
= kCenteredPreferredRows
;
38 cols
= kPreferredCols
;
39 rows
= kPreferredRows
;
41 apps_grid_view_
->SetLayout(cols
, rows
);
42 AddChildView(apps_grid_view_
);
44 folder_background_view_
= new FolderBackgroundView();
45 AddChildView(folder_background_view_
);
47 app_list_folder_view_
=
48 new AppListFolderView(this, model
, app_list_main_view
);
49 // The folder view is initially hidden.
50 app_list_folder_view_
->SetVisible(false);
51 AddChildView(app_list_folder_view_
);
53 apps_grid_view_
->SetModel(model_
);
54 apps_grid_view_
->SetItemList(model_
->top_level_item_list());
55 SetShowState(SHOW_APPS
,
56 false); /* show apps without animation */
59 AppsContainerView::~AppsContainerView() {
62 void AppsContainerView::ShowActiveFolder(AppListFolderItem
* folder_item
) {
63 // Prevent new animations from starting if there are currently animations
64 // pending. This fixes crbug.com/357099.
65 if (top_icon_animation_pending_count_
)
68 app_list_folder_view_
->SetAppListFolderItem(folder_item
);
69 SetShowState(SHOW_ACTIVE_FOLDER
, false);
71 CreateViewsForFolderTopItemsAnimation(folder_item
, true);
73 apps_grid_view_
->ClearAnySelectedView();
76 void AppsContainerView::ShowApps(AppListFolderItem
* folder_item
) {
77 if (top_icon_animation_pending_count_
)
80 PrepareToShowApps(folder_item
);
81 SetShowState(SHOW_APPS
,
82 true); /* show apps with animation */
85 void AppsContainerView::ResetForShowApps() {
86 SetShowState(SHOW_APPS
, false /* show apps without animation */);
87 folder_background_view_
->UpdateFolderContainerBubble(
88 FolderBackgroundView::NO_BUBBLE
);
91 void AppsContainerView::SetDragAndDropHostOfCurrentAppList(
92 ApplicationDragAndDropHost
* drag_and_drop_host
) {
93 apps_grid_view()->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host
);
94 app_list_folder_view()->items_grid_view()->
95 SetDragAndDropHostOfCurrentAppList(drag_and_drop_host
);
98 void AppsContainerView::ReparentFolderItemTransit(
99 AppListFolderItem
* folder_item
) {
100 if (top_icon_animation_pending_count_
)
103 PrepareToShowApps(folder_item
);
104 SetShowState(SHOW_ITEM_REPARENT
, false);
107 bool AppsContainerView::IsInFolderView() const {
108 return show_state_
== SHOW_ACTIVE_FOLDER
;
111 gfx::Size
AppsContainerView::GetPreferredSize() const {
112 const gfx::Size grid_size
= apps_grid_view_
->GetPreferredSize();
113 const gfx::Size folder_view_size
= app_list_folder_view_
->GetPreferredSize();
115 int width
= std::max(grid_size
.width(), folder_view_size
.width());
116 int height
= std::max(grid_size
.height(), folder_view_size
.height());
117 return gfx::Size(width
, height
);
120 void AppsContainerView::Layout() {
121 gfx::Rect
rect(GetContentsBounds());
125 switch (show_state_
) {
127 apps_grid_view_
->SetBoundsRect(rect
);
129 case SHOW_ACTIVE_FOLDER
:
130 folder_background_view_
->SetBoundsRect(rect
);
131 app_list_folder_view_
->SetBoundsRect(rect
);
133 case SHOW_ITEM_REPARENT
:
140 bool AppsContainerView::OnKeyPressed(const ui::KeyEvent
& event
) {
141 if (show_state_
== SHOW_APPS
)
142 return apps_grid_view_
->OnKeyPressed(event
);
144 return app_list_folder_view_
->OnKeyPressed(event
);
147 void AppsContainerView::OnWillBeShown() {
148 apps_grid_view()->ClearAnySelectedView();
149 app_list_folder_view()->items_grid_view()->ClearAnySelectedView();
152 gfx::Rect
AppsContainerView::GetPageBoundsForState(
153 AppListModel::State state
) const {
154 gfx::Rect onscreen_bounds
= GetDefaultContentsBounds();
155 if (state
== AppListModel::STATE_APPS
)
156 return onscreen_bounds
;
158 return GetBelowContentsOffscreenBounds(onscreen_bounds
.size());
161 void AppsContainerView::OnTopIconAnimationsComplete() {
162 --top_icon_animation_pending_count_
;
164 if (!top_icon_animation_pending_count_
) {
165 // Clean up the transitional views used for top item icon animation.
166 top_icon_views_
.clear();
168 // Show the folder icon when closing the folder.
169 if ((show_state_
== SHOW_APPS
|| show_state_
== SHOW_ITEM_REPARENT
) &&
170 apps_grid_view_
->activated_folder_item_view()) {
171 apps_grid_view_
->activated_folder_item_view()->SetVisible(true);
176 void AppsContainerView::SetShowState(ShowState show_state
,
177 bool show_apps_with_animation
) {
178 if (show_state_
== show_state
)
181 show_state_
= show_state
;
183 switch (show_state_
) {
185 folder_background_view_
->SetVisible(false);
186 if (show_apps_with_animation
) {
187 app_list_folder_view_
->ScheduleShowHideAnimation(false, false);
188 apps_grid_view_
->ScheduleShowHideAnimation(true);
190 app_list_folder_view_
->HideViewImmediately();
191 apps_grid_view_
->ResetForShowApps();
194 case SHOW_ACTIVE_FOLDER
:
195 folder_background_view_
->SetVisible(true);
196 apps_grid_view_
->ScheduleShowHideAnimation(false);
197 app_list_folder_view_
->ScheduleShowHideAnimation(true, false);
199 case SHOW_ITEM_REPARENT
:
200 folder_background_view_
->SetVisible(false);
201 folder_background_view_
->UpdateFolderContainerBubble(
202 FolderBackgroundView::NO_BUBBLE
);
203 app_list_folder_view_
->ScheduleShowHideAnimation(false, true);
204 apps_grid_view_
->ScheduleShowHideAnimation(true);
213 std::vector
<gfx::Rect
> AppsContainerView::GetTopItemIconBoundsInActiveFolder() {
214 // Get the active folder's icon bounds relative to AppsContainerView.
215 AppListItemView
* folder_item_view
=
216 apps_grid_view_
->activated_folder_item_view();
217 gfx::Rect to_grid_view
= folder_item_view
->ConvertRectToParent(
218 folder_item_view
->GetIconBounds());
219 gfx::Rect to_container
= apps_grid_view_
->ConvertRectToParent(to_grid_view
);
221 return FolderImage::GetTopIconsBounds(to_container
);
224 void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
225 AppListFolderItem
* active_folder
,
227 top_icon_views_
.clear();
228 std::vector
<gfx::Rect
> top_items_bounds
=
229 GetTopItemIconBoundsInActiveFolder();
230 top_icon_animation_pending_count_
=
231 std::min(kNumFolderTopItems
, active_folder
->item_list()->item_count());
232 for (size_t i
= 0; i
< top_icon_animation_pending_count_
; ++i
) {
233 if (active_folder
->GetTopIcon(i
).isNull())
236 TopIconAnimationView
* icon_view
= new TopIconAnimationView(
237 active_folder
->GetTopIcon(i
), top_items_bounds
[i
], open_folder
);
238 icon_view
->AddObserver(this);
239 top_icon_views_
.push_back(icon_view
);
241 // Add the transitional views into child views, and set its bounds to the
242 // same location of the item in the folder list view.
243 AddChildView(top_icon_views_
[i
]);
244 top_icon_views_
[i
]->SetBoundsRect(
245 app_list_folder_view_
->ConvertRectToParent(
246 app_list_folder_view_
->GetItemIconBoundsAt(i
)));
247 static_cast<TopIconAnimationView
*>(top_icon_views_
[i
])->TransformView();
251 void AppsContainerView::PrepareToShowApps(AppListFolderItem
* folder_item
) {
253 CreateViewsForFolderTopItemsAnimation(folder_item
, false);
255 // Hide the active folder item until the animation completes.
256 if (apps_grid_view_
->activated_folder_item_view())
257 apps_grid_view_
->activated_folder_item_view()->SetVisible(false);
260 } // namespace app_list