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 "ash/wm/maximize_mode/maximize_mode_controller.h"
16 #include "base/command_line.h"
17 #include "ui/app_list/app_list_constants.h"
18 #include "ui/app_list/app_list_switches.h"
19 #include "ui/app_list/pagination_model.h"
20 #include "ui/app_list/views/app_list_view.h"
21 #include "ui/aura/client/focus_client.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/compositor/layer.h"
25 #include "ui/compositor/scoped_layer_animation_settings.h"
26 #include "ui/events/event.h"
27 #include "ui/gfx/transform_util.h"
28 #include "ui/keyboard/keyboard_controller.h"
29 #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 // The minimal margin (in pixels) around the app list when in centered mode.
49 const int kMinimalCenteredAppListMargin
= 10;
51 ui::Layer
* GetLayer(views::Widget
* widget
) {
52 return widget
->GetNativeView()->layer();
55 // Gets arrow location based on shelf alignment.
56 views::BubbleBorder::Arrow
GetBubbleArrow(aura::Window
* window
) {
57 DCHECK(Shell::HasInstance());
58 return ShelfLayoutManager::ForShelf(window
)->
59 SelectValueForShelfAlignment(
60 views::BubbleBorder::BOTTOM_CENTER
,
61 views::BubbleBorder::LEFT_CENTER
,
62 views::BubbleBorder::RIGHT_CENTER
,
63 views::BubbleBorder::TOP_CENTER
);
66 // Offset given |rect| towards shelf.
67 gfx::Rect
OffsetTowardsShelf(const gfx::Rect
& rect
, views::Widget
* widget
) {
68 DCHECK(Shell::HasInstance());
69 ShelfAlignment shelf_alignment
= Shell::GetInstance()->GetShelfAlignment(
70 widget
->GetNativeView()->GetRootWindow());
71 gfx::Rect
offseted(rect
);
72 switch (shelf_alignment
) {
73 case SHELF_ALIGNMENT_BOTTOM
:
74 offseted
.Offset(0, kAnimationOffset
);
76 case SHELF_ALIGNMENT_LEFT
:
77 offseted
.Offset(-kAnimationOffset
, 0);
79 case SHELF_ALIGNMENT_RIGHT
:
80 offseted
.Offset(kAnimationOffset
, 0);
82 case SHELF_ALIGNMENT_TOP
:
83 offseted
.Offset(0, -kAnimationOffset
);
90 // Using |button_bounds|, determine the anchor offset so that the bubble gets
91 // shown above the shelf (used for the alternate shelf theme).
92 gfx::Vector2d
GetAnchorPositionOffsetToShelf(
93 const gfx::Rect
& button_bounds
, views::Widget
* widget
) {
94 DCHECK(Shell::HasInstance());
95 ShelfAlignment shelf_alignment
= Shell::GetInstance()->GetShelfAlignment(
96 widget
->GetNativeView()->GetRootWindow());
97 gfx::Point
anchor(button_bounds
.CenterPoint());
98 switch (shelf_alignment
) {
99 case SHELF_ALIGNMENT_TOP
:
100 case SHELF_ALIGNMENT_BOTTOM
:
101 if (base::i18n::IsRTL()) {
102 int screen_width
= widget
->GetWorkAreaBoundsInScreen().width();
103 return gfx::Vector2d(
104 std::min(screen_width
- kMinimalAnchorPositionOffset
- anchor
.x(),
107 return gfx::Vector2d(
108 std::max(kMinimalAnchorPositionOffset
- anchor
.x(), 0), 0);
109 case SHELF_ALIGNMENT_LEFT
:
110 return gfx::Vector2d(
111 0, std::max(kMinimalAnchorPositionOffset
- anchor
.y(), 0));
112 case SHELF_ALIGNMENT_RIGHT
:
113 return gfx::Vector2d(
114 0, std::max(kMinimalAnchorPositionOffset
- anchor
.y(), 0));
117 return gfx::Vector2d();
121 // Gets the point at the center of the display that a particular view is on.
122 // This calculation excludes the virtual keyboard area. If the height of the
123 // display area is less than |minimum_height|, its bottom will be extended to
124 // that height (so that the app list never starts above the top of the screen).
125 gfx::Point
GetCenterOfDisplayForView(const views::View
* view
,
126 int minimum_height
) {
127 aura::Window
* window
= view
->GetWidget()->GetNativeView();
128 gfx::Rect bounds
= ScreenUtil::GetShelfDisplayBoundsInScreen(window
);
130 // If the virtual keyboard is active, subtract it from the display bounds, so
131 // that the app list is centered in the non-keyboard area of the display.
132 // (Note that work_area excludes the keyboard, but it doesn't get updated
133 // until after this function is called.)
134 keyboard::KeyboardController
* keyboard_controller
=
135 keyboard::KeyboardController::GetInstance();
136 if (keyboard_controller
&& keyboard_controller
->keyboard_visible())
137 bounds
.Subtract(keyboard_controller
->current_keyboard_bounds());
139 // Apply the |minimum_height|.
140 if (bounds
.height() < minimum_height
)
141 bounds
.set_height(minimum_height
);
143 return bounds
.CenterPoint();
146 // Gets the minimum height of the rectangle to center the app list in.
147 int GetMinimumBoundsHeightForAppList(const app_list::AppListView
* app_list
) {
148 return app_list
->bounds().height() + 2 * kMinimalCenteredAppListMargin
;
151 bool IsFullscreenAppListEnabled() {
152 #if defined(OS_CHROMEOS)
153 return base::CommandLine::ForCurrentProcess()->HasSwitch(
154 switches::kAshEnableFullscreenAppList
) &&
155 app_list::switches::IsExperimentalAppListEnabled();
163 ////////////////////////////////////////////////////////////////////////////////
164 // AppListController, public:
166 AppListController::AppListController()
167 : is_visible_(false),
170 current_apps_page_(-1),
171 should_snap_back_(false) {
172 Shell::GetInstance()->AddShellObserver(this);
175 AppListController::~AppListController() {
176 // Ensures app list view goes before the controller since pagination model
177 // lives in the controller and app list view would access it on destruction.
179 view_
->GetAppsPaginationModel()->RemoveObserver(this);
180 if (view_
->GetWidget())
181 view_
->GetWidget()->CloseNow();
184 Shell::GetInstance()->RemoveShellObserver(this);
187 void AppListController::Show(aura::Window
* window
) {
193 // App list needs to know the new shelf layout in order to calculate its
194 // UI layout when AppListView visibility changes.
195 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
196 UpdateAutoHideState();
201 // Note the AppListViewDelegate outlives the AppListView. For Ash, the view
202 // is destroyed when dismissed.
203 app_list::AppListView
* view
= new app_list::AppListView(
204 Shell::GetInstance()->delegate()->GetAppListViewDelegate());
205 aura::Window
* root_window
= window
->GetRootWindow();
206 aura::Window
* container
= GetRootWindowController(root_window
)->
207 GetContainer(kShellWindowId_AppListContainer
);
208 views::View
* applist_button
=
209 Shelf::ForWindow(container
)->GetAppListButtonView();
210 is_centered_
= view
->ShouldCenterWindow();
211 bool is_fullscreen
= IsFullscreenAppListEnabled() &&
213 ->maximize_mode_controller()
214 ->IsMaximizeModeWindowManagerEnabled();
216 view
->InitAsFramelessWindow(
217 container
, current_apps_page_
,
218 ScreenUtil::GetDisplayWorkAreaBoundsInParent(container
));
219 } else if (is_centered_
) {
220 // Note: We can't center the app list until we have its dimensions, so we
221 // init at (0, 0) and then reset its anchor point.
222 view
->InitAsBubbleAtFixedLocation(container
,
225 views::BubbleBorder::FLOAT
,
226 true /* border_accepts_events */);
227 // The experimental app list is centered over the display of the app list
228 // button that was pressed (if triggered via keyboard, this is the display
229 // with the currently focused window).
230 view
->SetAnchorPoint(GetCenterOfDisplayForView(
231 applist_button
, GetMinimumBoundsHeightForAppList(view
)));
233 gfx::Rect applist_button_bounds
= applist_button
->GetBoundsInScreen();
234 // We need the location of the button within the local screen.
235 applist_button_bounds
= ScreenUtil::ConvertRectFromScreen(
237 applist_button_bounds
);
238 view
->InitAsBubbleAttachedToAnchor(
241 Shelf::ForWindow(container
)->GetAppListButtonView(),
242 GetAnchorPositionOffsetToShelf(
243 applist_button_bounds
,
244 Shelf::ForWindow(container
)->GetAppListButtonView()->GetWidget()),
245 GetBubbleArrow(container
),
246 true /* border_accepts_events */);
247 view
->SetArrowPaintType(views::BubbleBorder::PAINT_NONE
);
250 // By setting us as DnD recipient, the app list knows that we can
252 SetDragAndDropHostOfCurrentAppList(
253 Shelf::ForWindow(window
)->GetDragAndDropHostForAppList());
255 // Update applist button status when app list visibility is changed.
256 Shelf::ForWindow(window
)->GetAppListButtonView()->SchedulePaint();
259 void AppListController::Dismiss() {
263 // If the app list is currently visible, there should be an existing view.
268 // App list needs to know the new shelf layout in order to calculate its
269 // UI layout when AppListView visibility changes.
270 Shell::GetPrimaryRootWindowController()
271 ->GetShelfLayoutManager()
272 ->UpdateAutoHideState();
274 // Our widget is currently active. When the animation completes we'll hide
275 // the widget, changing activation. If a menu is shown before the animation
276 // completes then the activation change triggers the menu to close. By
277 // deactivating now we ensure there is no activation change when the
278 // animation completes and any menus stay open.
279 view_
->GetWidget()->Deactivate();
282 // Update applist button status when app list visibility is changed.
283 Shelf::ForWindow(view_
->GetWidget()->GetNativeView())
284 ->GetAppListButtonView()
288 bool AppListController::IsVisible() const {
289 return view_
&& view_
->GetWidget()->IsVisible();
292 aura::Window
* AppListController::GetWindow() {
293 return is_visible_
&& view_
? view_
->GetWidget()->GetNativeWindow() : NULL
;
296 ////////////////////////////////////////////////////////////////////////////////
297 // AppListController, private:
299 void AppListController::SetDragAndDropHostOfCurrentAppList(
300 app_list::ApplicationDragAndDropHost
* drag_and_drop_host
) {
301 if (view_
&& is_visible_
)
302 view_
->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host
);
305 void AppListController::SetView(app_list::AppListView
* view
) {
306 DCHECK(view_
== NULL
);
310 views::Widget
* widget
= view_
->GetWidget();
311 widget
->AddObserver(this);
312 keyboard::KeyboardController
* keyboard_controller
=
313 keyboard::KeyboardController::GetInstance();
314 if (keyboard_controller
)
315 keyboard_controller
->AddObserver(this);
316 Shell::GetInstance()->AddPreTargetHandler(this);
317 Shelf::ForWindow(widget
->GetNativeWindow())->AddIconObserver(this);
318 widget
->GetNativeView()->GetRootWindow()->AddObserver(this);
319 aura::client::GetFocusClient(widget
->GetNativeView())->AddObserver(this);
321 view_
->GetAppsPaginationModel()->AddObserver(this);
323 view_
->ShowWhenReady();
326 void AppListController::ResetView() {
330 views::Widget
* widget
= view_
->GetWidget();
331 widget
->RemoveObserver(this);
332 GetLayer(widget
)->GetAnimator()->RemoveObserver(this);
333 keyboard::KeyboardController
* keyboard_controller
=
334 keyboard::KeyboardController::GetInstance();
335 if (keyboard_controller
)
336 keyboard_controller
->RemoveObserver(this);
337 Shell::GetInstance()->RemovePreTargetHandler(this);
338 Shelf::ForWindow(widget
->GetNativeWindow())->RemoveIconObserver(this);
339 widget
->GetNativeView()->GetRootWindow()->RemoveObserver(this);
340 aura::client::GetFocusClient(widget
->GetNativeView())->RemoveObserver(this);
342 view_
->GetAppsPaginationModel()->RemoveObserver(this);
347 void AppListController::ScheduleAnimation() {
348 // Stop observing previous animation.
349 StopObservingImplicitAnimations();
351 views::Widget
* widget
= view_
->GetWidget();
352 ui::Layer
* layer
= GetLayer(widget
);
353 layer
->GetAnimator()->StopAnimating();
355 gfx::Rect target_bounds
;
357 target_bounds
= widget
->GetWindowBoundsInScreen();
358 widget
->SetBounds(OffsetTowardsShelf(target_bounds
, widget
));
360 target_bounds
= OffsetTowardsShelf(widget
->GetWindowBoundsInScreen(),
364 ui::ScopedLayerAnimationSettings
animation(layer
->GetAnimator());
365 animation
.SetTransitionDuration(
366 base::TimeDelta::FromMilliseconds(
367 is_visible_
? 0 : kAnimationDurationMs
));
368 animation
.AddObserver(this);
370 layer
->SetOpacity(is_visible_
? 1.0 : 0.0);
371 widget
->SetBounds(target_bounds
);
374 void AppListController::ProcessLocatedEvent(ui::LocatedEvent
* event
) {
375 if (!view_
|| !is_visible_
)
378 // If the event happened on a menu, then the event should not close the app
380 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
382 RootWindowController
* root_controller
=
383 GetRootWindowController(target
->GetRootWindow());
384 if (root_controller
) {
385 aura::Window
* menu_container
=
386 root_controller
->GetContainer(kShellWindowId_MenuContainer
);
387 if (menu_container
->Contains(target
))
389 aura::Window
* keyboard_container
= root_controller
->GetContainer(
390 kShellWindowId_VirtualKeyboardContainer
);
391 if (keyboard_container
->Contains(target
))
396 aura::Window
* window
= view_
->GetWidget()->GetNativeView()->parent();
397 if (!window
->Contains(target
) &&
398 !app_list::switches::ShouldNotDismissOnBlur()) {
403 void AppListController::UpdateBounds() {
404 if (!view_
|| !is_visible_
)
407 view_
->UpdateBounds();
410 view_
->SetAnchorPoint(GetCenterOfDisplayForView(
411 view_
, GetMinimumBoundsHeightForAppList(view_
)));
414 ////////////////////////////////////////////////////////////////////////////////
415 // AppListController, aura::EventFilter implementation:
417 void AppListController::OnMouseEvent(ui::MouseEvent
* event
) {
418 if (event
->type() == ui::ET_MOUSE_PRESSED
)
419 ProcessLocatedEvent(event
);
422 void AppListController::OnGestureEvent(ui::GestureEvent
* event
) {
423 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
)
424 ProcessLocatedEvent(event
);
427 ////////////////////////////////////////////////////////////////////////////////
428 // AppListController, aura::FocusObserver implementation:
430 void AppListController::OnWindowFocused(aura::Window
* gained_focus
,
431 aura::Window
* lost_focus
) {
432 if (view_
&& is_visible_
) {
433 aura::Window
* applist_window
= view_
->GetWidget()->GetNativeView();
434 aura::Window
* applist_container
= applist_window
->parent();
436 if (applist_container
->Contains(lost_focus
) &&
437 (!gained_focus
|| !applist_container
->Contains(gained_focus
)) &&
438 !app_list::switches::ShouldNotDismissOnBlur()) {
444 ////////////////////////////////////////////////////////////////////////////////
445 // AppListController, aura::WindowObserver implementation:
446 void AppListController::OnWindowBoundsChanged(aura::Window
* root
,
447 const gfx::Rect
& old_bounds
,
448 const gfx::Rect
& new_bounds
) {
452 ////////////////////////////////////////////////////////////////////////////////
453 // AppListController, ui::ImplicitAnimationObserver implementation:
455 void AppListController::OnImplicitAnimationsCompleted() {
457 view_
->GetWidget()->Activate();
459 view_
->GetWidget()->Close();
462 ////////////////////////////////////////////////////////////////////////////////
463 // AppListController, views::WidgetObserver implementation:
465 void AppListController::OnWidgetDestroying(views::Widget
* widget
) {
466 DCHECK(view_
->GetWidget() == widget
);
472 ////////////////////////////////////////////////////////////////////////////////
473 // AppListController, keyboard::KeyboardControllerObserver implementation:
475 void AppListController::OnKeyboardBoundsChanging(const gfx::Rect
& new_bounds
) {
479 ////////////////////////////////////////////////////////////////////////////////
480 // AppListController, ShellObserver implementation:
481 void AppListController::OnShelfAlignmentChanged(aura::Window
* root_window
) {
483 view_
->SetBubbleArrow(GetBubbleArrow(view_
->GetWidget()->GetNativeView()));
486 void AppListController::OnMaximizeModeStarted() {
487 // The "fullscreen" app-list is initialized as in a different type of window,
488 // therefore we can't switch between the fullscreen status and the normal
489 // app-list bubble. App-list should be dismissed for the transition between
490 // maximize mode (touch-view mode) and non-maximize mode, otherwise the app
491 // list tries to behave as a bubble which leads to a crash. crbug.com/510062
492 if (IsFullscreenAppListEnabled() && is_visible_
)
496 void AppListController::OnMaximizeModeEnded() {
497 // See the comments of OnMaximizeModeStarted().
498 if (IsFullscreenAppListEnabled() && is_visible_
)
502 ////////////////////////////////////////////////////////////////////////////////
503 // AppListController, ShelfIconObserver implementation:
505 void AppListController::OnShelfIconPositionsChanged() {
509 ////////////////////////////////////////////////////////////////////////////////
510 // AppListController, PaginationModelObserver implementation:
512 void AppListController::TotalPagesChanged() {
515 void AppListController::SelectedPageChanged(int old_selected
,
517 current_apps_page_
= new_selected
;
520 void AppListController::TransitionStarted() {
523 void AppListController::TransitionChanged() {
524 // |view_| could be NULL when app list is closed with a running transition.
528 app_list::PaginationModel
* pagination_model
= view_
->GetAppsPaginationModel();
530 const app_list::PaginationModel::Transition
& transition
=
531 pagination_model
->transition();
532 if (pagination_model
->is_valid_page(transition
.target_page
))
535 views::Widget
* widget
= view_
->GetWidget();
536 ui::LayerAnimator
* widget_animator
= GetLayer(widget
)->GetAnimator();
537 if (!pagination_model
->IsRevertingCurrentTransition()) {
538 // Update cached |view_bounds_| if it is the first over-scroll move and
539 // widget does not have running animations.
540 if (!should_snap_back_
&& !widget_animator
->is_animating())
541 view_bounds_
= widget
->GetWindowBoundsInScreen();
543 const int current_page
= pagination_model
->selected_page();
544 const int dir
= transition
.target_page
> current_page
? -1 : 1;
546 const double progress
= 1.0 - pow(1.0 - transition
.progress
, 4);
547 const int shift
= kMaxOverScrollShift
* progress
* dir
;
549 gfx::Rect
shifted(view_bounds_
);
550 shifted
.set_x(shifted
.x() + shift
);
552 widget
->SetBounds(shifted
);
554 should_snap_back_
= true;
555 } else if (should_snap_back_
) {
556 should_snap_back_
= false;
557 ui::ScopedLayerAnimationSettings
animation(widget_animator
);
558 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
559 app_list::kOverscrollPageTransitionDurationMs
));
560 widget
->SetBounds(view_bounds_
);