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/app_list_folder_view.h"
9 #include "ui/app_list/app_list_constants.h"
10 #include "ui/app_list/app_list_folder_item.h"
11 #include "ui/app_list/app_list_model.h"
12 #include "ui/app_list/pagination_model.h"
13 #include "ui/app_list/views/app_list_item_view.h"
14 #include "ui/app_list/views/app_list_main_view.h"
15 #include "ui/app_list/views/apps_container_view.h"
16 #include "ui/app_list/views/apps_grid_view.h"
17 #include "ui/app_list/views/contents_view.h"
18 #include "ui/app_list/views/folder_header_view.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h"
20 #include "ui/events/event.h"
21 #include "ui/views/view_model.h"
22 #include "ui/views/view_model_utils.h"
28 // Indexes of interesting views in ViewModel of AppListFolderView.
29 const int kIndexFolderHeader = 0;
30 const int kIndexChildItems = 1;
34 AppListFolderView::AppListFolderView(AppsContainerView* container_view,
36 AppListMainView* app_list_main_view,
37 content::WebContents* start_page_contents)
38 : container_view_(container_view),
39 folder_header_view_(new FolderHeaderView(this)),
40 view_model_(new views::ViewModel),
42 pagination_model_(new PaginationModel) {
43 AddChildView(folder_header_view_);
44 view_model_->Add(folder_header_view_, kIndexFolderHeader);
46 items_grid_view_ = new AppsGridView(
47 app_list_main_view, pagination_model_.get(), NULL);
48 items_grid_view_->set_is_root_level(false);
49 items_grid_view_->SetLayout(kPreferredIconDimension,
52 items_grid_view_->SetModel(model);
53 AddChildView(items_grid_view_);
54 view_model_->Add(items_grid_view_, kIndexChildItems);
57 SetPaintToLayer(true);
58 SetFillsBoundsOpaquely(false);
62 AppListFolderView::~AppListFolderView() {
63 // Make sure |items_grid_view_| is deleted before |pagination_model_|.
64 RemoveAllChildViews(true);
67 void AppListFolderView::SetAppListFolderItem(AppListFolderItem* folder) {
68 folder_item_ = folder;
69 items_grid_view_->SetItemList(folder_item_->item_list());
70 folder_header_view_->SetFolderItem(folder_item_);
73 void AppListFolderView::ScheduleShowHideAnimation(bool show) {
74 // Stop any previous animation.
75 layer()->GetAnimator()->StopAnimating();
77 // Hide the top items temporarily if showing the view for opening the folder.
79 items_grid_view_->SetTopItemViewsVisible(false);
83 layer()->SetOpacity(show ? 0.0f : 1.0f);
85 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
86 animation.SetTweenType(gfx::Tween::EASE_IN_2);
87 animation.AddObserver(this);
88 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
89 show ? kFolderTransitionInDurationMs : kFolderTransitionOutDurationMs));
91 layer()->SetOpacity(show ? 1.0f : 0.0f);
94 gfx::Size AppListFolderView::GetPreferredSize() {
95 const gfx::Size header_size = folder_header_view_->GetPreferredSize();
96 const gfx::Size grid_size = items_grid_view_->GetPreferredSize();
97 int width = std::max(header_size.width(), grid_size.width());
98 int height = header_size.height() + grid_size.height();
99 return gfx::Size(width, height);
102 void AppListFolderView::Layout() {
103 CalculateIdealBounds();
104 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
107 bool AppListFolderView::OnKeyPressed(const ui::KeyEvent& event) {
108 return items_grid_view_->OnKeyPressed(event);
111 void AppListFolderView::OnImplicitAnimationsCompleted() {
112 // Show the top items when the opening folder animation is done.
113 if (layer()->opacity() == 1.0f)
114 items_grid_view_->SetTopItemViewsVisible(true);
116 if (layer()->opacity() == 0.0f)
120 void AppListFolderView::CalculateIdealBounds() {
121 gfx::Rect rect(GetContentsBounds());
125 gfx::Rect header_frame(rect);
126 gfx::Size size = folder_header_view_->GetPreferredSize();
127 header_frame.set_height(size.height());
128 view_model_->set_ideal_bounds(kIndexFolderHeader, header_frame);
130 gfx::Rect grid_frame(rect);
131 grid_frame.set_y(header_frame.height());
132 view_model_->set_ideal_bounds(kIndexChildItems, grid_frame);
135 gfx::Rect AppListFolderView::GetItemIconBoundsAt(int index) {
136 AppListItemView* item_view = items_grid_view_->GetItemViewAt(index);
137 // Icon bounds relative to AppListItemView.
138 const gfx::Rect icon_bounds = item_view->GetIconBounds();
139 gfx::Rect to_apps_grid_view = item_view->ConvertRectToParent(icon_bounds);
140 gfx::Rect to_folder =
141 items_grid_view_->ConvertRectToParent(to_apps_grid_view);
143 // Get the icon image's bound.
144 to_folder.ClampToCenteredSize(
145 gfx::Size(kPreferredIconDimension, kPreferredIconDimension));
150 void AppListFolderView::NavigateBack(AppListFolderItem* item,
151 const ui::Event& event_flags) {
152 container_view_->ShowApps(item);
155 } // namespace app_list