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::OnTopIconAnimationsComplete() {
148 --top_icon_animation_pending_count_
;
150 if (!top_icon_animation_pending_count_
) {
151 // Clean up the transitional views used for top item icon animation.
152 top_icon_views_
.clear();
154 // Show the folder icon when closing the folder.
155 if ((show_state_
== SHOW_APPS
|| show_state_
== SHOW_ITEM_REPARENT
) &&
156 apps_grid_view_
->activated_folder_item_view()) {
157 apps_grid_view_
->activated_folder_item_view()->SetVisible(true);
162 void AppsContainerView::SetShowState(ShowState show_state
,
163 bool show_apps_with_animation
) {
164 if (show_state_
== show_state
)
167 show_state_
= show_state
;
169 switch (show_state_
) {
171 folder_background_view_
->SetVisible(false);
172 if (show_apps_with_animation
) {
173 app_list_folder_view_
->ScheduleShowHideAnimation(false, false);
174 apps_grid_view_
->ScheduleShowHideAnimation(true);
176 app_list_folder_view_
->HideViewImmediately();
177 apps_grid_view_
->ResetForShowApps();
180 case SHOW_ACTIVE_FOLDER
:
181 folder_background_view_
->SetVisible(true);
182 apps_grid_view_
->ScheduleShowHideAnimation(false);
183 app_list_folder_view_
->ScheduleShowHideAnimation(true, false);
185 case SHOW_ITEM_REPARENT
:
186 folder_background_view_
->SetVisible(false);
187 folder_background_view_
->UpdateFolderContainerBubble(
188 FolderBackgroundView::NO_BUBBLE
);
189 app_list_folder_view_
->ScheduleShowHideAnimation(false, true);
190 apps_grid_view_
->ScheduleShowHideAnimation(true);
199 std::vector
<gfx::Rect
> AppsContainerView::GetTopItemIconBoundsInActiveFolder() {
200 // Get the active folder's icon bounds relative to AppsContainerView.
201 AppListItemView
* folder_item_view
=
202 apps_grid_view_
->activated_folder_item_view();
203 gfx::Rect to_grid_view
= folder_item_view
->ConvertRectToParent(
204 folder_item_view
->GetIconBounds());
205 gfx::Rect to_container
= apps_grid_view_
->ConvertRectToParent(to_grid_view
);
207 return FolderImage::GetTopIconsBounds(to_container
);
210 void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
211 AppListFolderItem
* active_folder
,
213 top_icon_views_
.clear();
214 std::vector
<gfx::Rect
> top_items_bounds
=
215 GetTopItemIconBoundsInActiveFolder();
216 top_icon_animation_pending_count_
=
217 std::min(kNumFolderTopItems
, active_folder
->item_list()->item_count());
218 for (size_t i
= 0; i
< top_icon_animation_pending_count_
; ++i
) {
219 if (active_folder
->GetTopIcon(i
).isNull())
222 TopIconAnimationView
* icon_view
= new TopIconAnimationView(
223 active_folder
->GetTopIcon(i
), top_items_bounds
[i
], open_folder
);
224 icon_view
->AddObserver(this);
225 top_icon_views_
.push_back(icon_view
);
227 // Add the transitional views into child views, and set its bounds to the
228 // same location of the item in the folder list view.
229 AddChildView(top_icon_views_
[i
]);
230 top_icon_views_
[i
]->SetBoundsRect(
231 app_list_folder_view_
->ConvertRectToParent(
232 app_list_folder_view_
->GetItemIconBoundsAt(i
)));
233 static_cast<TopIconAnimationView
*>(top_icon_views_
[i
])->TransformView();
237 void AppsContainerView::PrepareToShowApps(AppListFolderItem
* folder_item
) {
239 CreateViewsForFolderTopItemsAnimation(folder_item
, false);
241 // Hide the active folder item until the animation completes.
242 if (apps_grid_view_
->activated_folder_item_view())
243 apps_grid_view_
->activated_folder_item_view()->SetVisible(false);
246 } // namespace app_list