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/ash_switches.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/screen_util.h"
10 #include "ash/shelf/shelf.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shell.h"
13 #include "ash/shell_delegate.h"
14 #include "ash/shell_window_ids.h"
15 #include "base/command_line.h"
16 #include "ui/app_list/app_list_constants.h"
17 #include "ui/app_list/app_list_switches.h"
18 #include "ui/app_list/pagination_model.h"
19 #include "ui/app_list/views/app_list_view.h"
20 #include "ui/aura/client/focus_client.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_event_dispatcher.h"
23 #include "ui/compositor/layer.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/events/event.h"
26 #include "ui/gfx/transform_util.h"
27 #include "ui/views/widget/widget.h"
34 // Duration for show/hide animation in milliseconds.
35 const int kAnimationDurationMs
= 200;
37 // Offset in pixels to animation away/towards the shelf.
38 const int kAnimationOffset
= 8;
40 // The maximum shift in pixels when over-scroll happens.
41 const int kMaxOverScrollShift
= 48;
43 // The minimal anchor position offset to make sure that the bubble is still on
44 // the screen with 8 pixels spacing on the left / right. This constant is a
45 // result of minimal bubble arrow sizes and offsets.
46 const int kMinimalAnchorPositionOffset
= 57;
48 ui::Layer
* GetLayer(views::Widget
* widget
) {
49 return widget
->GetNativeView()->layer();
52 // Gets arrow location based on shelf alignment.
53 views::BubbleBorder::Arrow
GetBubbleArrow(aura::Window
* window
) {
54 DCHECK(Shell::HasInstance());
55 return ShelfLayoutManager::ForShelf(window
)->
56 SelectValueForShelfAlignment(
57 views::BubbleBorder::BOTTOM_CENTER
,
58 views::BubbleBorder::LEFT_CENTER
,
59 views::BubbleBorder::RIGHT_CENTER
,
60 views::BubbleBorder::TOP_CENTER
);
63 // Offset given |rect| towards shelf.
64 gfx::Rect
OffsetTowardsShelf(const gfx::Rect
& rect
, views::Widget
* widget
) {
65 DCHECK(Shell::HasInstance());
66 ShelfAlignment shelf_alignment
= Shell::GetInstance()->GetShelfAlignment(
67 widget
->GetNativeView()->GetRootWindow());
68 gfx::Rect
offseted(rect
);
69 switch (shelf_alignment
) {
70 case SHELF_ALIGNMENT_BOTTOM
:
71 offseted
.Offset(0, kAnimationOffset
);
73 case SHELF_ALIGNMENT_LEFT
:
74 offseted
.Offset(-kAnimationOffset
, 0);
76 case SHELF_ALIGNMENT_RIGHT
:
77 offseted
.Offset(kAnimationOffset
, 0);
79 case SHELF_ALIGNMENT_TOP
:
80 offseted
.Offset(0, -kAnimationOffset
);
87 // Using |button_bounds|, determine the anchor offset so that the bubble gets
88 // shown above the shelf (used for the alternate shelf theme).
89 gfx::Vector2d
GetAnchorPositionOffsetToShelf(
90 const gfx::Rect
& button_bounds
, views::Widget
* widget
) {
91 DCHECK(Shell::HasInstance());
92 ShelfAlignment shelf_alignment
= Shell::GetInstance()->GetShelfAlignment(
93 widget
->GetNativeView()->GetRootWindow());
94 gfx::Point
anchor(button_bounds
.CenterPoint());
95 switch (shelf_alignment
) {
96 case SHELF_ALIGNMENT_TOP
:
97 case SHELF_ALIGNMENT_BOTTOM
:
98 if (base::i18n::IsRTL()) {
99 int screen_width
= widget
->GetWorkAreaBoundsInScreen().width();
100 return gfx::Vector2d(
101 std::min(screen_width
- kMinimalAnchorPositionOffset
- anchor
.x(),
104 return gfx::Vector2d(
105 std::max(kMinimalAnchorPositionOffset
- anchor
.x(), 0), 0);
106 case SHELF_ALIGNMENT_LEFT
:
107 return gfx::Vector2d(
108 0, std::max(kMinimalAnchorPositionOffset
- anchor
.y(), 0));
109 case SHELF_ALIGNMENT_RIGHT
:
110 return gfx::Vector2d(
111 0, std::max(kMinimalAnchorPositionOffset
- anchor
.y(), 0));
114 return gfx::Vector2d();
120 ////////////////////////////////////////////////////////////////////////////////
121 // AppListController, public:
123 AppListController::AppListController()
124 : pagination_model_(new app_list::PaginationModel
),
127 should_snap_back_(false) {
128 Shell::GetInstance()->AddShellObserver(this);
129 pagination_model_
->AddObserver(this);
132 AppListController::~AppListController() {
133 // Ensures app list view goes before the controller since pagination model
134 // lives in the controller and app list view would access it on destruction.
135 if (view_
&& view_
->GetWidget())
136 view_
->GetWidget()->CloseNow();
138 Shell::GetInstance()->RemoveShellObserver(this);
139 pagination_model_
->RemoveObserver(this);
142 void AppListController::SetVisible(bool visible
, aura::Window
* window
) {
143 if (visible
== is_visible_
)
146 is_visible_
= visible
;
148 // App list needs to know the new shelf layout in order to calculate its
149 // UI layout when AppListView visibility changes.
150 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
151 UpdateAutoHideState();
154 // Our widget is currently active. When the animation completes we'll hide
155 // the widget, changing activation. If a menu is shown before the animation
156 // completes then the activation change triggers the menu to close. By
157 // deactivating now we ensure there is no activation change when the
158 // animation completes and any menus stay open.
160 view_
->GetWidget()->Deactivate();
162 } else if (is_visible_
) {
163 // AppListModel and AppListViewDelegate are owned by AppListView. They
164 // will be released with AppListView on close.
165 app_list::AppListView
* view
= new app_list::AppListView(
166 Shell::GetInstance()->delegate()->CreateAppListViewDelegate());
167 aura::Window
* root_window
= window
->GetRootWindow();
168 aura::Window
* container
= GetRootWindowController(root_window
)->
169 GetContainer(kShellWindowId_AppListContainer
);
170 if (CommandLine::ForCurrentProcess()->HasSwitch(
171 app_list::switches::kEnableExperimentalAppList
)) {
172 // The experimental app list is centered over the primary display.
173 view
->InitAsBubbleAtFixedLocation(
175 pagination_model_
.get(),
176 Shell::GetScreen()->GetPrimaryDisplay().bounds().CenterPoint(),
177 views::BubbleBorder::FLOAT
,
178 true /* border_accepts_events */);
179 } else if (ash::switches::UseAlternateShelfLayout()) {
180 gfx::Rect applist_button_bounds
= Shelf::ForWindow(container
)->
181 GetAppListButtonView()->GetBoundsInScreen();
182 // We need the location of the button within the local screen.
183 applist_button_bounds
= ScreenUtil::ConvertRectFromScreen(
185 applist_button_bounds
);
186 view
->InitAsBubbleAttachedToAnchor(
188 pagination_model_
.get(),
189 Shelf::ForWindow(container
)->GetAppListButtonView(),
190 GetAnchorPositionOffsetToShelf(applist_button_bounds
,
191 Shelf::ForWindow(container
)->GetAppListButtonView()->
193 GetBubbleArrow(container
),
194 true /* border_accepts_events */);
195 view
->SetArrowPaintType(views::BubbleBorder::PAINT_NONE
);
197 view
->InitAsBubbleAttachedToAnchor(
199 pagination_model_
.get(),
200 Shelf::ForWindow(container
)->GetAppListButtonView(),
202 GetBubbleArrow(container
),
203 true /* border_accepts_events */);
206 // By setting us as DnD recipient, the app list knows that we can
208 if (!CommandLine::ForCurrentProcess()->HasSwitch(
209 switches::kAshDisableDragAndDropAppListToLauncher
)) {
210 SetDragAndDropHostOfCurrentAppList(
211 Shelf::ForWindow(window
)->GetDragAndDropHostForAppList());
214 // Update applist button status when app list visibility is changed.
215 Shelf::ForWindow(window
)->GetAppListButtonView()->SchedulePaint();
218 bool AppListController::IsVisible() const {
219 return view_
&& view_
->GetWidget()->IsVisible();
222 aura::Window
* AppListController::GetWindow() {
223 return is_visible_
&& view_
? view_
->GetWidget()->GetNativeWindow() : NULL
;
226 ////////////////////////////////////////////////////////////////////////////////
227 // AppListController, private:
229 void AppListController::SetDragAndDropHostOfCurrentAppList(
230 app_list::ApplicationDragAndDropHost
* drag_and_drop_host
) {
231 if (view_
&& is_visible_
)
232 view_
->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host
);
235 void AppListController::SetView(app_list::AppListView
* view
) {
236 DCHECK(view_
== NULL
);
240 views::Widget
* widget
= view_
->GetWidget();
241 widget
->AddObserver(this);
242 Shell::GetInstance()->AddPreTargetHandler(this);
243 Shelf::ForWindow(widget
->GetNativeWindow())->AddIconObserver(this);
244 widget
->GetNativeView()->GetRootWindow()->AddObserver(this);
245 aura::client::GetFocusClient(widget
->GetNativeView())->AddObserver(this);
247 view_
->ShowWhenReady();
250 void AppListController::ResetView() {
254 views::Widget
* widget
= view_
->GetWidget();
255 widget
->RemoveObserver(this);
256 GetLayer(widget
)->GetAnimator()->RemoveObserver(this);
257 Shell::GetInstance()->RemovePreTargetHandler(this);
258 Shelf::ForWindow(widget
->GetNativeWindow())->RemoveIconObserver(this);
259 widget
->GetNativeView()->GetRootWindow()->RemoveObserver(this);
260 aura::client::GetFocusClient(widget
->GetNativeView())->RemoveObserver(this);
264 void AppListController::ScheduleAnimation() {
265 // Stop observing previous animation.
266 StopObservingImplicitAnimations();
268 views::Widget
* widget
= view_
->GetWidget();
269 ui::Layer
* layer
= GetLayer(widget
);
270 layer
->GetAnimator()->StopAnimating();
272 gfx::Rect target_bounds
;
274 target_bounds
= widget
->GetWindowBoundsInScreen();
275 widget
->SetBounds(OffsetTowardsShelf(target_bounds
, widget
));
277 target_bounds
= OffsetTowardsShelf(widget
->GetWindowBoundsInScreen(),
281 ui::ScopedLayerAnimationSettings
animation(layer
->GetAnimator());
282 animation
.SetTransitionDuration(
283 base::TimeDelta::FromMilliseconds(
284 is_visible_
? 0 : kAnimationDurationMs
));
285 animation
.AddObserver(this);
287 layer
->SetOpacity(is_visible_
? 1.0 : 0.0);
288 widget
->SetBounds(target_bounds
);
291 void AppListController::ProcessLocatedEvent(ui::LocatedEvent
* event
) {
292 if (!view_
|| !is_visible_
)
295 // If the event happened on a menu, then the event should not close the app
297 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
299 RootWindowController
* root_controller
=
300 GetRootWindowController(target
->GetRootWindow());
301 if (root_controller
) {
302 aura::Window
* menu_container
= root_controller
->GetContainer(
303 internal::kShellWindowId_MenuContainer
);
304 if (menu_container
->Contains(target
))
306 aura::Window
* keyboard_container
= root_controller
->GetContainer(
307 internal::kShellWindowId_VirtualKeyboardContainer
);
308 if (keyboard_container
->Contains(target
))
313 aura::Window
* window
= view_
->GetWidget()->GetNativeView();
314 if (!window
->Contains(target
))
315 SetVisible(false, window
);
318 void AppListController::UpdateBounds() {
319 if (view_
&& is_visible_
)
320 view_
->UpdateBounds();
323 ////////////////////////////////////////////////////////////////////////////////
324 // AppListController, aura::EventFilter implementation:
326 void AppListController::OnMouseEvent(ui::MouseEvent
* event
) {
327 if (event
->type() == ui::ET_MOUSE_PRESSED
)
328 ProcessLocatedEvent(event
);
331 void AppListController::OnGestureEvent(ui::GestureEvent
* event
) {
332 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
)
333 ProcessLocatedEvent(event
);
336 ////////////////////////////////////////////////////////////////////////////////
337 // AppListController, aura::FocusObserver implementation:
339 void AppListController::OnWindowFocused(aura::Window
* gained_focus
,
340 aura::Window
* lost_focus
) {
341 if (view_
&& is_visible_
) {
342 aura::Window
* applist_window
= view_
->GetWidget()->GetNativeView();
343 aura::Window
* applist_container
= applist_window
->parent();
345 if (applist_container
->Contains(lost_focus
) &&
346 (!gained_focus
|| !applist_container
->Contains(gained_focus
))) {
347 SetVisible(false, applist_window
);
352 ////////////////////////////////////////////////////////////////////////////////
353 // AppListController, aura::WindowObserver implementation:
354 void AppListController::OnWindowBoundsChanged(aura::Window
* root
,
355 const gfx::Rect
& old_bounds
,
356 const gfx::Rect
& new_bounds
) {
360 ////////////////////////////////////////////////////////////////////////////////
361 // AppListController, ui::ImplicitAnimationObserver implementation:
363 void AppListController::OnImplicitAnimationsCompleted() {
365 view_
->GetWidget()->Activate();
367 view_
->GetWidget()->Close();
370 ////////////////////////////////////////////////////////////////////////////////
371 // AppListController, views::WidgetObserver implementation:
373 void AppListController::OnWidgetDestroying(views::Widget
* widget
) {
374 DCHECK(view_
->GetWidget() == widget
);
376 SetVisible(false, widget
->GetNativeView());
380 ////////////////////////////////////////////////////////////////////////////////
381 // AppListController, ShellObserver implementation:
382 void AppListController::OnShelfAlignmentChanged(aura::Window
* root_window
) {
384 view_
->SetBubbleArrow(GetBubbleArrow(view_
->GetWidget()->GetNativeView()));
387 ////////////////////////////////////////////////////////////////////////////////
388 // AppListController, ShelfIconObserver implementation:
390 void AppListController::OnShelfIconPositionsChanged() {
394 ////////////////////////////////////////////////////////////////////////////////
395 // AppListController, PaginationModelObserver implementation:
397 void AppListController::TotalPagesChanged() {
400 void AppListController::SelectedPageChanged(int old_selected
,
404 void AppListController::TransitionStarted() {
407 void AppListController::TransitionChanged() {
408 // |view_| could be NULL when app list is closed with a running transition.
412 const app_list::PaginationModel::Transition
& transition
=
413 pagination_model_
->transition();
414 if (pagination_model_
->is_valid_page(transition
.target_page
))
417 views::Widget
* widget
= view_
->GetWidget();
418 ui::LayerAnimator
* widget_animator
= GetLayer(widget
)->GetAnimator();
419 if (!pagination_model_
->IsRevertingCurrentTransition()) {
420 // Update cached |view_bounds_| if it is the first over-scroll move and
421 // widget does not have running animations.
422 if (!should_snap_back_
&& !widget_animator
->is_animating())
423 view_bounds_
= widget
->GetWindowBoundsInScreen();
425 const int current_page
= pagination_model_
->selected_page();
426 const int dir
= transition
.target_page
> current_page
? -1 : 1;
428 const double progress
= 1.0 - pow(1.0 - transition
.progress
, 4);
429 const int shift
= kMaxOverScrollShift
* progress
* dir
;
431 gfx::Rect
shifted(view_bounds_
);
432 shifted
.set_x(shifted
.x() + shift
);
433 widget
->SetBounds(shifted
);
434 should_snap_back_
= true;
435 } else if (should_snap_back_
) {
436 should_snap_back_
= false;
437 ui::ScopedLayerAnimationSettings
animation(widget_animator
);
438 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
439 app_list::kOverscrollPageTransitionDurationMs
));
440 widget
->SetBounds(view_bounds_
);
444 } // namespace internal