1 // Copyright (c) 2012 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 "ash/wm/app_list_controller.h"
7 #include "ash/launcher/launcher.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shell.h"
11 #include "ash/shell_delegate.h"
12 #include "ash/shell_window_ids.h"
13 #include "ash/wm/property_util.h"
14 #include "ui/app_list/app_list_constants.h"
15 #include "ui/app_list/pagination_model.h"
16 #include "ui/app_list/views/app_list_view.h"
17 #include "ui/aura/client/focus_client.h"
18 #include "ui/aura/root_window.h"
19 #include "ui/aura/window.h"
20 #include "ui/base/events/event.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/compositor/scoped_layer_animation_settings.h"
23 #include "ui/gfx/transform_util.h"
24 #include "ui/views/widget/widget.h"
31 // Duration for show/hide animation in milliseconds.
32 const int kAnimationDurationMs
= 200;
34 // Offset in pixels to animation away/towards the launcher.
35 const int kAnimationOffset
= 8;
37 // The maximum shift in pixels when over-scroll happens.
38 const int kMaxOverScrollShift
= 48;
40 ui::Layer
* GetLayer(views::Widget
* widget
) {
41 return widget
->GetNativeView()->layer();
44 // Gets arrow location based on shelf alignment.
45 views::BubbleBorder::ArrowLocation
GetBubbleArrowLocation(
46 aura::Window
* window
) {
47 DCHECK(Shell::HasInstance());
48 return ShelfLayoutManager::ForLauncher(window
)->
49 SelectValueForShelfAlignment(
50 views::BubbleBorder::BOTTOM_CENTER
,
51 views::BubbleBorder::LEFT_CENTER
,
52 views::BubbleBorder::RIGHT_CENTER
,
53 views::BubbleBorder::TOP_CENTER
);
56 // Offset given |rect| towards shelf.
57 gfx::Rect
OffsetTowardsShelf(const gfx::Rect
& rect
, views::Widget
* widget
) {
58 DCHECK(Shell::HasInstance());
59 ShelfAlignment shelf_alignment
= Shell::GetInstance()->GetShelfAlignment(
60 widget
->GetNativeView()->GetRootWindow());
61 gfx::Rect
offseted(rect
);
62 switch (shelf_alignment
) {
63 case SHELF_ALIGNMENT_BOTTOM
:
64 offseted
.Offset(0, kAnimationOffset
);
66 case SHELF_ALIGNMENT_LEFT
:
67 offseted
.Offset(-kAnimationOffset
, 0);
69 case SHELF_ALIGNMENT_RIGHT
:
70 offseted
.Offset(kAnimationOffset
, 0);
72 case SHELF_ALIGNMENT_TOP
:
73 offseted
.Offset(0, -kAnimationOffset
);
82 ////////////////////////////////////////////////////////////////////////////////
83 // AppListController, public:
85 AppListController::AppListController()
86 : pagination_model_(new app_list::PaginationModel
),
89 should_snap_back_(false) {
90 Shell::GetInstance()->AddShellObserver(this);
91 pagination_model_
->AddObserver(this);
94 AppListController::~AppListController() {
95 // Ensures app list view goes before the controller since pagination model
96 // lives in the controller and app list view would access it on destruction.
97 if (view_
&& view_
->GetWidget())
98 view_
->GetWidget()->CloseNow();
100 Shell::GetInstance()->RemoveShellObserver(this);
101 pagination_model_
->RemoveObserver(this);
104 void AppListController::SetVisible(bool visible
, aura::Window
* window
) {
105 if (visible
== is_visible_
)
108 is_visible_
= visible
;
110 // App list needs to know the new shelf layout in order to calculate its
111 // UI layout when AppListView visibility changes.
112 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
113 UpdateAutoHideState();
117 } else if (is_visible_
) {
118 // AppListModel and AppListViewDelegate are owned by AppListView. They
119 // will be released with AppListView on close.
120 app_list::AppListView
* view
= new app_list::AppListView(
121 Shell::GetInstance()->delegate()->CreateAppListViewDelegate());
122 aura::Window
* container
= GetRootWindowController(window
->GetRootWindow())->
123 GetContainer(kShellWindowId_AppListContainer
);
126 pagination_model_
.get(),
127 Launcher::ForWindow(container
)->GetAppListButtonView(),
129 GetBubbleArrowLocation(container
),
130 true /* border_accepts_events */);
135 bool AppListController::IsVisible() const {
136 return view_
&& view_
->GetWidget()->IsVisible();
139 aura::Window
* AppListController::GetWindow() {
140 return is_visible_
&& view_
? view_
->GetWidget()->GetNativeWindow() : NULL
;
143 ////////////////////////////////////////////////////////////////////////////////
144 // AppListController, private:
146 void AppListController::SetView(app_list::AppListView
* view
) {
147 DCHECK(view_
== NULL
);
151 views::Widget
* widget
= view_
->GetWidget();
152 widget
->AddObserver(this);
153 Shell::GetInstance()->AddPreTargetHandler(this);
154 Launcher::ForWindow(widget
->GetNativeWindow())->AddIconObserver(this);
155 widget
->GetNativeView()->GetRootWindow()->AddRootWindowObserver(this);
156 aura::client::GetFocusClient(widget
->GetNativeView())->AddObserver(this);
158 view_
->ShowWhenReady();
161 void AppListController::ResetView() {
165 views::Widget
* widget
= view_
->GetWidget();
166 widget
->RemoveObserver(this);
167 GetLayer(widget
)->GetAnimator()->RemoveObserver(this);
168 Shell::GetInstance()->RemovePreTargetHandler(this);
169 Launcher::ForWindow(widget
->GetNativeWindow())->RemoveIconObserver(this);
170 widget
->GetNativeView()->GetRootWindow()->RemoveRootWindowObserver(this);
171 aura::client::GetFocusClient(widget
->GetNativeView())->RemoveObserver(this);
175 void AppListController::ScheduleAnimation() {
176 // Stop observing previous animation.
177 StopObservingImplicitAnimations();
179 views::Widget
* widget
= view_
->GetWidget();
180 ui::Layer
* layer
= GetLayer(widget
);
181 layer
->GetAnimator()->StopAnimating();
183 gfx::Rect target_bounds
;
185 target_bounds
= widget
->GetWindowBoundsInScreen();
186 widget
->SetBounds(OffsetTowardsShelf(target_bounds
, widget
));
188 target_bounds
= OffsetTowardsShelf(widget
->GetWindowBoundsInScreen(),
192 ui::ScopedLayerAnimationSettings
animation(layer
->GetAnimator());
193 animation
.SetTransitionDuration(
194 base::TimeDelta::FromMilliseconds(
195 is_visible_
? 0 : kAnimationDurationMs
));
196 animation
.AddObserver(this);
198 layer
->SetOpacity(is_visible_
? 1.0 : 0.0);
199 widget
->SetBounds(target_bounds
);
202 void AppListController::ProcessLocatedEvent(ui::LocatedEvent
* event
) {
203 // If the event happened on a menu, then the event should not close the app
205 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
207 RootWindowController
* root_controller
=
208 GetRootWindowController(target
->GetRootWindow());
209 if (root_controller
) {
210 aura::Window
* menu_container
= root_controller
->GetContainer(
211 ash::internal::kShellWindowId_MenuContainer
);
212 if (menu_container
->Contains(target
))
217 if (view_
&& is_visible_
) {
218 aura::Window
* window
= view_
->GetWidget()->GetNativeView();
219 gfx::Point
window_local_point(event
->root_location());
220 aura::Window::ConvertPointToTarget(window
->GetRootWindow(),
222 &window_local_point
);
223 // Use HitTest to respect the hit test mask of the bubble.
224 if (!window
->HitTest(window_local_point
))
225 SetVisible(false, window
);
229 void AppListController::UpdateBounds() {
230 if (view_
&& is_visible_
)
231 view_
->UpdateBounds();
234 ////////////////////////////////////////////////////////////////////////////////
235 // AppListController, aura::EventFilter implementation:
237 void AppListController::OnMouseEvent(ui::MouseEvent
* event
) {
238 if (event
->type() == ui::ET_MOUSE_PRESSED
)
239 ProcessLocatedEvent(event
);
242 void AppListController::OnGestureEvent(ui::GestureEvent
* event
) {
243 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
)
244 ProcessLocatedEvent(event
);
247 ////////////////////////////////////////////////////////////////////////////////
248 // AppListController, aura::FocusObserver implementation:
250 void AppListController::OnWindowFocused(aura::Window
* gained_focus
,
251 aura::Window
* lost_focus
) {
252 if (gained_focus
&& view_
&& is_visible_
) {
253 aura::Window
* applist_container
=
254 GetRootWindowController(gained_focus
->GetRootWindow())->GetContainer(
255 kShellWindowId_AppListContainer
);
256 if (gained_focus
->parent() != applist_container
)
257 SetVisible(false, gained_focus
);
261 ////////////////////////////////////////////////////////////////////////////////
262 // AppListController, aura::RootWindowObserver implementation:
263 void AppListController::OnRootWindowResized(const aura::RootWindow
* root
,
264 const gfx::Size
& old_size
) {
268 ////////////////////////////////////////////////////////////////////////////////
269 // AppListController, ui::ImplicitAnimationObserver implementation:
271 void AppListController::OnImplicitAnimationsCompleted() {
273 view_
->GetWidget()->Activate();
275 view_
->GetWidget()->Close();
278 ////////////////////////////////////////////////////////////////////////////////
279 // AppListController, views::WidgetObserver implementation:
281 void AppListController::OnWidgetDestroying(views::Widget
* widget
) {
282 DCHECK(view_
->GetWidget() == widget
);
284 SetVisible(false, widget
->GetNativeView());
288 ////////////////////////////////////////////////////////////////////////////////
289 // AppListController, ShellObserver implementation:
290 void AppListController::OnShelfAlignmentChanged(aura::RootWindow
* root_window
) {
292 view_
->SetBubbleArrowLocation(GetBubbleArrowLocation(
293 view_
->GetWidget()->GetNativeView()));
297 ////////////////////////////////////////////////////////////////////////////////
298 // AppListController, LauncherIconObserver implementation:
300 void AppListController::OnLauncherIconPositionsChanged() {
304 ////////////////////////////////////////////////////////////////////////////////
305 // AppListController, PaginationModelObserver implementation:
307 void AppListController::TotalPagesChanged() {
310 void AppListController::SelectedPageChanged(int old_selected
,
314 void AppListController::TransitionChanged() {
315 // |view_| could be NULL when app list is closed with a running transition.
319 const app_list::PaginationModel::Transition
& transition
=
320 pagination_model_
->transition();
321 if (pagination_model_
->is_valid_page(transition
.target_page
))
324 views::Widget
* widget
= view_
->GetWidget();
325 ui::LayerAnimator
* widget_animator
= GetLayer(widget
)->GetAnimator();
326 if (!pagination_model_
->IsRevertingCurrentTransition()) {
327 // Update cached |view_bounds_| if it is the first over-scroll move and
328 // widget does not have running animations.
329 if (!should_snap_back_
&& !widget_animator
->is_animating())
330 view_bounds_
= widget
->GetWindowBoundsInScreen();
332 const int current_page
= pagination_model_
->selected_page();
333 const int dir
= transition
.target_page
> current_page
? -1 : 1;
335 const double progress
= 1.0 - pow(1.0 - transition
.progress
, 4);
336 const int shift
= kMaxOverScrollShift
* progress
* dir
;
338 gfx::Rect
shifted(view_bounds_
);
339 shifted
.set_x(shifted
.x() + shift
);
340 widget
->SetBounds(shifted
);
341 should_snap_back_
= true;
342 } else if (should_snap_back_
) {
343 should_snap_back_
= false;
344 ui::ScopedLayerAnimationSettings
animation(widget_animator
);
345 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
346 app_list::kOverscrollPageTransitionDurationMs
));
347 widget
->SetBounds(view_bounds_
);
351 } // namespace internal