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
),
43 pagination_model_(new PaginationModel
) {
44 AddChildView(folder_header_view_
);
45 view_model_
->Add(folder_header_view_
, kIndexFolderHeader
);
47 items_grid_view_
= new AppsGridView(
48 app_list_main_view
, pagination_model_
.get(), NULL
);
49 items_grid_view_
->set_is_root_level(false);
50 items_grid_view_
->SetLayout(kPreferredIconDimension
,
53 items_grid_view_
->SetModel(model
);
54 AddChildView(items_grid_view_
);
55 view_model_
->Add(items_grid_view_
, kIndexChildItems
);
58 SetPaintToLayer(true);
59 SetFillsBoundsOpaquely(false);
62 model_
->item_list()->AddObserver(this);
65 AppListFolderView::~AppListFolderView() {
66 model_
->item_list()->RemoveObserver(this);
67 // Make sure |items_grid_view_| is deleted before |pagination_model_|.
68 RemoveAllChildViews(true);
71 void AppListFolderView::SetAppListFolderItem(AppListFolderItem
* folder
) {
72 folder_item_
= folder
;
73 items_grid_view_
->SetItemList(folder_item_
->item_list());
74 folder_header_view_
->SetFolderItem(folder_item_
);
77 void AppListFolderView::ScheduleShowHideAnimation(bool show
) {
78 // Stop any previous animation.
79 layer()->GetAnimator()->StopAnimating();
81 // Hide the top items temporarily if showing the view for opening the folder.
83 items_grid_view_
->SetTopItemViewsVisible(false);
87 layer()->SetOpacity(show
? 0.0f
: 1.0f
);
89 ui::ScopedLayerAnimationSettings
animation(layer()->GetAnimator());
90 animation
.SetTweenType(gfx::Tween::EASE_IN_2
);
91 animation
.AddObserver(this);
92 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
93 show
? kFolderTransitionInDurationMs
: kFolderTransitionOutDurationMs
));
95 layer()->SetOpacity(show
? 1.0f
: 0.0f
);
98 gfx::Size
AppListFolderView::GetPreferredSize() {
99 const gfx::Size header_size
= folder_header_view_
->GetPreferredSize();
100 const gfx::Size grid_size
= items_grid_view_
->GetPreferredSize();
101 int width
= std::max(header_size
.width(), grid_size
.width());
102 int height
= header_size
.height() + grid_size
.height();
103 return gfx::Size(width
, height
);
106 void AppListFolderView::Layout() {
107 CalculateIdealBounds();
108 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_
);
111 bool AppListFolderView::OnKeyPressed(const ui::KeyEvent
& event
) {
112 return items_grid_view_
->OnKeyPressed(event
);
115 void AppListFolderView::OnListItemRemoved(size_t index
, AppListItem
* item
) {
116 // If the folder item associated with this view is removed from the model,
117 // (e.g. the last item in the folder was deleted), reset the view and signal
118 // the container view to show the app list instead.
119 if (item
== folder_item_
) {
120 items_grid_view_
->SetItemList(NULL
);
121 folder_header_view_
->SetFolderItem(NULL
);
123 // Pass NULL to ShowApps() to avoid triggering animation from the deleted
125 container_view_
->ShowApps(NULL
);
129 void AppListFolderView::OnImplicitAnimationsCompleted() {
130 // Show the top items when the opening folder animation is done.
131 if (layer()->opacity() == 1.0f
)
132 items_grid_view_
->SetTopItemViewsVisible(true);
134 if (layer()->opacity() == 0.0f
)
138 void AppListFolderView::CalculateIdealBounds() {
139 gfx::Rect
rect(GetContentsBounds());
143 gfx::Rect
header_frame(rect
);
144 gfx::Size size
= folder_header_view_
->GetPreferredSize();
145 header_frame
.set_height(size
.height());
146 view_model_
->set_ideal_bounds(kIndexFolderHeader
, header_frame
);
148 gfx::Rect
grid_frame(rect
);
149 grid_frame
.set_y(header_frame
.height());
150 view_model_
->set_ideal_bounds(kIndexChildItems
, grid_frame
);
153 gfx::Rect
AppListFolderView::GetItemIconBoundsAt(int index
) {
154 AppListItemView
* item_view
= items_grid_view_
->GetItemViewAt(index
);
155 // Icon bounds relative to AppListItemView.
156 const gfx::Rect icon_bounds
= item_view
->GetIconBounds();
157 gfx::Rect to_apps_grid_view
= item_view
->ConvertRectToParent(icon_bounds
);
158 gfx::Rect to_folder
=
159 items_grid_view_
->ConvertRectToParent(to_apps_grid_view
);
161 // Get the icon image's bound.
162 to_folder
.ClampToCenteredSize(
163 gfx::Size(kPreferredIconDimension
, kPreferredIconDimension
));
168 void AppListFolderView::NavigateBack(AppListFolderItem
* item
,
169 const ui::Event
& event_flags
) {
170 container_view_
->ShowApps(item
);
173 } // namespace app_list