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"
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"
23 AppsContainerView::AppsContainerView(AppListMainView* app_list_main_view,
24 PaginationModel* pagination_model,
26 content::WebContents* start_page_contents)
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;
39 apps_grid_view_->SetLayout(kPreferredIconDimension, cols, rows);
40 AddChildView(apps_grid_view_);
42 folder_background_view_ = new FolderBackgroundView();
43 AddChildView(folder_background_view_);
45 app_list_folder_view_ = new AppListFolderView(
50 AddChildView(app_list_folder_view_);
52 apps_grid_view_->SetModel(model_);
53 apps_grid_view_->SetItemList(model_->item_list());
54 SetShowState(SHOW_APPS,
55 false); /* show apps without animation */
58 AppsContainerView::~AppsContainerView() {
61 void AppsContainerView::ShowActiveFolder(AppListFolderItem* folder_item) {
62 app_list_folder_view_->SetAppListFolderItem(folder_item);
63 SetShowState(SHOW_ACTIVE_FOLDER, false);
65 CreateViewsForFolderTopItemsAnimation(folder_item, true);
68 void AppsContainerView::ShowApps(AppListFolderItem* folder_item) {
69 PrepareToShowApps(folder_item);
70 SetShowState(SHOW_APPS,
71 true); /* show apps with animation */
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);
81 void AppsContainerView::ReparentFolderItemTransit(
82 AppListFolderItem* folder_item) {
83 PrepareToShowApps(folder_item);
84 SetShowState(SHOW_ITEM_REPARENT, false);
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();
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);
96 void AppsContainerView::Layout() {
97 gfx::Rect rect(GetContentsBounds());
101 switch (show_state_) {
103 apps_grid_view_->SetBoundsRect(rect);
105 case SHOW_ACTIVE_FOLDER:
106 folder_background_view_->SetBoundsRect(rect);
107 app_list_folder_view_->SetBoundsRect(rect);
109 case SHOW_ITEM_REPARENT:
116 bool AppsContainerView::OnKeyPressed(const ui::KeyEvent& event) {
117 if (show_state_ == SHOW_APPS)
118 return apps_grid_view_->OnKeyPressed(event);
120 return app_list_folder_view_->OnKeyPressed(event);
123 void AppsContainerView::OnTopIconAnimationsComplete() {
124 --top_icon_animation_pending_count_;
126 if (!top_icon_animation_pending_count_) {
127 // Clean up the transitional views used for top item icon animation.
128 top_icon_views_.clear();
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);
138 void AppsContainerView::SetShowState(ShowState show_state,
139 bool show_apps_with_animation) {
140 if (show_state_ == show_state)
143 show_state_ = show_state;
145 switch (show_state_) {
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);
152 app_list_folder_view_->HideViewImmediately();
153 apps_grid_view_->SetVisible(true);
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);
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);
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);
182 return AppListFolderItem::GetTopIconsBounds(to_container);
185 void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
186 AppListFolderItem* active_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);
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();
209 void AppsContainerView::PrepareToShowApps(AppListFolderItem* folder_item) {
211 CreateViewsForFolderTopItemsAnimation(folder_item, false);
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);
218 } // namespace app_list