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 void AppsContainerView::ReparentDragEnded() {
112 DCHECK_EQ(SHOW_ITEM_REPARENT
, show_state_
);
113 show_state_
= AppsContainerView::SHOW_APPS
;
116 gfx::Size
AppsContainerView::GetPreferredSize() const {
117 const gfx::Size grid_size
= apps_grid_view_
->GetPreferredSize();
118 const gfx::Size folder_view_size
= app_list_folder_view_
->GetPreferredSize();
120 int width
= std::max(grid_size
.width(), folder_view_size
.width());
121 int height
= std::max(grid_size
.height(), folder_view_size
.height());
122 return gfx::Size(width
, height
);
125 void AppsContainerView::Layout() {
126 gfx::Rect
rect(GetContentsBounds());
130 switch (show_state_
) {
132 apps_grid_view_
->SetBoundsRect(rect
);
134 case SHOW_ACTIVE_FOLDER
:
135 folder_background_view_
->SetBoundsRect(rect
);
136 app_list_folder_view_
->SetBoundsRect(rect
);
138 case SHOW_ITEM_REPARENT
:
145 bool AppsContainerView::OnKeyPressed(const ui::KeyEvent
& event
) {
146 if (show_state_
== SHOW_APPS
)
147 return apps_grid_view_
->OnKeyPressed(event
);
149 return app_list_folder_view_
->OnKeyPressed(event
);
152 void AppsContainerView::OnWillBeShown() {
153 apps_grid_view()->ClearAnySelectedView();
154 app_list_folder_view()->items_grid_view()->ClearAnySelectedView();
157 gfx::Rect
AppsContainerView::GetPageBoundsForState(
158 AppListModel::State state
) const {
159 gfx::Rect onscreen_bounds
= GetDefaultContentsBounds();
160 if (state
== AppListModel::STATE_APPS
)
161 return onscreen_bounds
;
163 return GetBelowContentsOffscreenBounds(onscreen_bounds
.size());
166 void AppsContainerView::OnTopIconAnimationsComplete() {
167 --top_icon_animation_pending_count_
;
169 if (!top_icon_animation_pending_count_
) {
170 // Clean up the transitional views used for top item icon animation.
171 top_icon_views_
.clear();
173 // Show the folder icon when closing the folder.
174 if ((show_state_
== SHOW_APPS
|| show_state_
== SHOW_ITEM_REPARENT
) &&
175 apps_grid_view_
->activated_folder_item_view()) {
176 apps_grid_view_
->activated_folder_item_view()->SetVisible(true);
181 void AppsContainerView::SetShowState(ShowState show_state
,
182 bool show_apps_with_animation
) {
183 if (show_state_
== show_state
)
186 show_state_
= show_state
;
188 switch (show_state_
) {
190 folder_background_view_
->SetVisible(false);
191 if (show_apps_with_animation
) {
192 app_list_folder_view_
->ScheduleShowHideAnimation(false, false);
193 apps_grid_view_
->ScheduleShowHideAnimation(true);
195 app_list_folder_view_
->HideViewImmediately();
196 apps_grid_view_
->ResetForShowApps();
199 case SHOW_ACTIVE_FOLDER
:
200 folder_background_view_
->SetVisible(true);
201 apps_grid_view_
->ScheduleShowHideAnimation(false);
202 app_list_folder_view_
->ScheduleShowHideAnimation(true, false);
204 case SHOW_ITEM_REPARENT
:
205 folder_background_view_
->SetVisible(false);
206 folder_background_view_
->UpdateFolderContainerBubble(
207 FolderBackgroundView::NO_BUBBLE
);
208 app_list_folder_view_
->ScheduleShowHideAnimation(false, true);
209 apps_grid_view_
->ScheduleShowHideAnimation(true);
218 std::vector
<gfx::Rect
> AppsContainerView::GetTopItemIconBoundsInActiveFolder() {
219 // Get the active folder's icon bounds relative to AppsContainerView.
220 AppListItemView
* folder_item_view
=
221 apps_grid_view_
->activated_folder_item_view();
222 gfx::Rect to_grid_view
= folder_item_view
->ConvertRectToParent(
223 folder_item_view
->GetIconBounds());
224 gfx::Rect to_container
= apps_grid_view_
->ConvertRectToParent(to_grid_view
);
226 return FolderImage::GetTopIconsBounds(to_container
);
229 void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
230 AppListFolderItem
* active_folder
,
232 top_icon_views_
.clear();
233 std::vector
<gfx::Rect
> top_items_bounds
=
234 GetTopItemIconBoundsInActiveFolder();
235 top_icon_animation_pending_count_
=
236 std::min(kNumFolderTopItems
, active_folder
->item_list()->item_count());
237 for (size_t i
= 0; i
< top_icon_animation_pending_count_
; ++i
) {
238 if (active_folder
->GetTopIcon(i
).isNull())
241 TopIconAnimationView
* icon_view
= new TopIconAnimationView(
242 active_folder
->GetTopIcon(i
), top_items_bounds
[i
], open_folder
);
243 icon_view
->AddObserver(this);
244 top_icon_views_
.push_back(icon_view
);
246 // Add the transitional views into child views, and set its bounds to the
247 // same location of the item in the folder list view.
248 AddChildView(top_icon_views_
[i
]);
249 top_icon_views_
[i
]->SetBoundsRect(
250 app_list_folder_view_
->ConvertRectToParent(
251 app_list_folder_view_
->GetItemIconBoundsAt(i
)));
252 static_cast<TopIconAnimationView
*>(top_icon_views_
[i
])->TransformView();
256 void AppsContainerView::PrepareToShowApps(AppListFolderItem
* folder_item
) {
258 CreateViewsForFolderTopItemsAnimation(folder_item
, false);
260 // Hide the active folder item until the animation completes.
261 if (apps_grid_view_
->activated_folder_item_view())
262 apps_grid_view_
->activated_folder_item_view()->SetVisible(false);
265 } // namespace app_list