Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / ui / app_list / views / apps_container_view.cc
blobff0e355f7cc81c3e3afeedb701f14e7a92fc59d0
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"
7 #include <algorithm>
8 #include <vector>
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"
22 namespace app_list {
24 AppsContainerView::AppsContainerView(AppListMainView* app_list_main_view,
25 PaginationModel* pagination_model,
26 AppListModel* model)
27 : model_(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 // The folder view is initially hidden.
47 app_list_folder_view_->SetVisible(false);
48 AddChildView(app_list_folder_view_);
50 apps_grid_view_->SetModel(model_);
51 apps_grid_view_->SetItemList(model_->top_level_item_list());
52 SetShowState(SHOW_APPS,
53 false); /* show apps without animation */
56 AppsContainerView::~AppsContainerView() {
59 void AppsContainerView::ShowActiveFolder(AppListFolderItem* folder_item) {
60 // Prevent new animations from starting if there are currently animations
61 // pending. This fixes crbug.com/357099.
62 if (top_icon_animation_pending_count_)
63 return;
65 app_list_folder_view_->SetAppListFolderItem(folder_item);
66 SetShowState(SHOW_ACTIVE_FOLDER, false);
68 CreateViewsForFolderTopItemsAnimation(folder_item, true);
71 void AppsContainerView::ShowApps(AppListFolderItem* folder_item) {
72 if (top_icon_animation_pending_count_)
73 return;
75 PrepareToShowApps(folder_item);
76 SetShowState(SHOW_APPS,
77 true); /* show apps with animation */
80 void AppsContainerView::ResetForShowApps() {
81 SetShowState(SHOW_APPS, false /* show apps without animation */);
82 folder_background_view_->UpdateFolderContainerBubble(
83 FolderBackgroundView::NO_BUBBLE);
86 void AppsContainerView::SetDragAndDropHostOfCurrentAppList(
87 ApplicationDragAndDropHost* drag_and_drop_host) {
88 apps_grid_view()->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
89 app_list_folder_view()->items_grid_view()->
90 SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
93 void AppsContainerView::ReparentFolderItemTransit(
94 AppListFolderItem* folder_item) {
95 if (top_icon_animation_pending_count_)
96 return;
98 PrepareToShowApps(folder_item);
99 SetShowState(SHOW_ITEM_REPARENT, false);
102 bool AppsContainerView::IsInFolderView() const {
103 return show_state_ == SHOW_ACTIVE_FOLDER;
106 gfx::Size AppsContainerView::GetPreferredSize() const {
107 const gfx::Size grid_size = apps_grid_view_->GetPreferredSize();
108 const gfx::Size folder_view_size = app_list_folder_view_->GetPreferredSize();
110 int width = std::max(grid_size.width(), folder_view_size.width());
111 int height = std::max(grid_size.height(), folder_view_size.height());
112 return gfx::Size(width, height);
115 void AppsContainerView::Layout() {
116 gfx::Rect rect(GetContentsBounds());
117 if (rect.IsEmpty())
118 return;
120 switch (show_state_) {
121 case SHOW_APPS:
122 apps_grid_view_->SetBoundsRect(rect);
123 break;
124 case SHOW_ACTIVE_FOLDER:
125 folder_background_view_->SetBoundsRect(rect);
126 app_list_folder_view_->SetBoundsRect(rect);
127 break;
128 case SHOW_ITEM_REPARENT:
129 break;
130 default:
131 NOTREACHED();
135 bool AppsContainerView::OnKeyPressed(const ui::KeyEvent& event) {
136 if (show_state_ == SHOW_APPS)
137 return apps_grid_view_->OnKeyPressed(event);
138 else
139 return app_list_folder_view_->OnKeyPressed(event);
142 void AppsContainerView::OnTopIconAnimationsComplete() {
143 --top_icon_animation_pending_count_;
145 if (!top_icon_animation_pending_count_) {
146 // Clean up the transitional views used for top item icon animation.
147 top_icon_views_.clear();
149 // Show the folder icon when closing the folder.
150 if ((show_state_ == SHOW_APPS || show_state_ == SHOW_ITEM_REPARENT) &&
151 apps_grid_view_->activated_folder_item_view()) {
152 apps_grid_view_->activated_folder_item_view()->SetVisible(true);
157 void AppsContainerView::SetShowState(ShowState show_state,
158 bool show_apps_with_animation) {
159 if (show_state_ == show_state)
160 return;
162 show_state_ = show_state;
164 switch (show_state_) {
165 case SHOW_APPS:
166 folder_background_view_->SetVisible(false);
167 if (show_apps_with_animation) {
168 app_list_folder_view_->ScheduleShowHideAnimation(false, false);
169 apps_grid_view_->ScheduleShowHideAnimation(true);
170 } else {
171 app_list_folder_view_->HideViewImmediately();
172 apps_grid_view_->ResetForShowApps();
174 break;
175 case SHOW_ACTIVE_FOLDER:
176 folder_background_view_->SetVisible(true);
177 apps_grid_view_->ScheduleShowHideAnimation(false);
178 app_list_folder_view_->ScheduleShowHideAnimation(true, false);
179 break;
180 case SHOW_ITEM_REPARENT:
181 folder_background_view_->SetVisible(false);
182 folder_background_view_->UpdateFolderContainerBubble(
183 FolderBackgroundView::NO_BUBBLE);
184 app_list_folder_view_->ScheduleShowHideAnimation(false, true);
185 apps_grid_view_->ScheduleShowHideAnimation(true);
186 break;
187 default:
188 NOTREACHED();
191 Layout();
194 Rects AppsContainerView::GetTopItemIconBoundsInActiveFolder() {
195 // Get the active folder's icon bounds relative to AppsContainerView.
196 AppListItemView* folder_item_view =
197 apps_grid_view_->activated_folder_item_view();
198 gfx::Rect to_grid_view = folder_item_view->ConvertRectToParent(
199 folder_item_view->GetIconBounds());
200 gfx::Rect to_container = apps_grid_view_->ConvertRectToParent(to_grid_view);
202 return AppListFolderItem::GetTopIconsBounds(to_container);
205 void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
206 AppListFolderItem* active_folder,
207 bool open_folder) {
208 top_icon_views_.clear();
209 std::vector<gfx::Rect> top_items_bounds =
210 GetTopItemIconBoundsInActiveFolder();
211 top_icon_animation_pending_count_ =
212 std::min(kNumFolderTopItems, active_folder->item_list()->item_count());
213 for (size_t i = 0; i < top_icon_animation_pending_count_; ++i) {
214 if (active_folder->GetTopIcon(i).isNull())
215 continue;
217 TopIconAnimationView* icon_view = new TopIconAnimationView(
218 active_folder->GetTopIcon(i), top_items_bounds[i], open_folder);
219 icon_view->AddObserver(this);
220 top_icon_views_.push_back(icon_view);
222 // Add the transitional views into child views, and set its bounds to the
223 // same location of the item in the folder list view.
224 AddChildView(top_icon_views_[i]);
225 top_icon_views_[i]->SetBoundsRect(
226 app_list_folder_view_->ConvertRectToParent(
227 app_list_folder_view_->GetItemIconBoundsAt(i)));
228 static_cast<TopIconAnimationView*>(top_icon_views_[i])->TransformView();
232 void AppsContainerView::PrepareToShowApps(AppListFolderItem* folder_item) {
233 if (folder_item)
234 CreateViewsForFolderTopItemsAnimation(folder_item, false);
236 // Hide the active folder item until the animation completes.
237 if (apps_grid_view_->activated_folder_item_view())
238 apps_grid_view_->activated_folder_item_view()->SetVisible(false);
241 } // namespace app_list