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/keyboard/keyboard_controller.h"
28 #include "ui/views/widget/widget.h"
33 // Duration for show/hide animation in milliseconds.
34 const int kAnimationDurationMs = 200;
36 // Offset in pixels to animation away/towards the shelf.
37 const int kAnimationOffset = 8;
39 // The maximum shift in pixels when over-scroll happens.
40 const int kMaxOverScrollShift = 48;
42 // The minimal anchor position offset to make sure that the bubble is still on
43 // the screen with 8 pixels spacing on the left / right. This constant is a
44 // result of minimal bubble arrow sizes and offsets.
45 const int kMinimalAnchorPositionOffset = 57;
47 ui::Layer* GetLayer(views::Widget* widget) {
48 return widget->GetNativeView()->layer();
51 // Gets arrow location based on shelf alignment.
52 views::BubbleBorder::Arrow GetBubbleArrow(aura::Window* window) {
53 DCHECK(Shell::HasInstance());
54 return ShelfLayoutManager::ForShelf(window)->
55 SelectValueForShelfAlignment(
56 views::BubbleBorder::BOTTOM_CENTER,
57 views::BubbleBorder::LEFT_CENTER,
58 views::BubbleBorder::RIGHT_CENTER,
59 views::BubbleBorder::TOP_CENTER);
62 // Offset given |rect| towards shelf.
63 gfx::Rect OffsetTowardsShelf(const gfx::Rect& rect, views::Widget* widget) {
64 DCHECK(Shell::HasInstance());
65 ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment(
66 widget->GetNativeView()->GetRootWindow());
67 gfx::Rect offseted(rect);
68 switch (shelf_alignment) {
69 case SHELF_ALIGNMENT_BOTTOM:
70 offseted.Offset(0, kAnimationOffset);
72 case SHELF_ALIGNMENT_LEFT:
73 offseted.Offset(-kAnimationOffset, 0);
75 case SHELF_ALIGNMENT_RIGHT:
76 offseted.Offset(kAnimationOffset, 0);
78 case SHELF_ALIGNMENT_TOP:
79 offseted.Offset(0, -kAnimationOffset);
86 // Using |button_bounds|, determine the anchor offset so that the bubble gets
87 // shown above the shelf (used for the alternate shelf theme).
88 gfx::Vector2d GetAnchorPositionOffsetToShelf(
89 const gfx::Rect& button_bounds, views::Widget* widget) {
90 DCHECK(Shell::HasInstance());
91 ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment(
92 widget->GetNativeView()->GetRootWindow());
93 gfx::Point anchor(button_bounds.CenterPoint());
94 switch (shelf_alignment) {
95 case SHELF_ALIGNMENT_TOP:
96 case SHELF_ALIGNMENT_BOTTOM:
97 if (base::i18n::IsRTL()) {
98 int screen_width = widget->GetWorkAreaBoundsInScreen().width();
100 std::min(screen_width - kMinimalAnchorPositionOffset - anchor.x(),
103 return gfx::Vector2d(
104 std::max(kMinimalAnchorPositionOffset - anchor.x(), 0), 0);
105 case SHELF_ALIGNMENT_LEFT:
106 return gfx::Vector2d(
107 0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0));
108 case SHELF_ALIGNMENT_RIGHT:
109 return gfx::Vector2d(
110 0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0));
113 return gfx::Vector2d();
117 // Gets the point at the center of the display that a particular view is on.
118 // This calculation excludes the virtual keyboard area.
119 gfx::Point GetCenterOfDisplayForView(const views::View* view) {
120 gfx::Rect bounds = Shell::GetScreen()->GetDisplayNearestWindow(
121 view->GetWidget()->GetNativeView()).bounds();
123 // If the virtual keyboard is active, subtract it from the display bounds, so
124 // that the app list is centered in the non-keyboard area of the display.
125 // (Note that work_area excludes the keyboard, but it doesn't get updated
126 // until after this function is called.)
127 keyboard::KeyboardController* keyboard_controller =
128 keyboard::KeyboardController::GetInstance();
129 if (keyboard_controller && keyboard_controller->keyboard_visible())
130 bounds.Subtract(keyboard_controller->current_keyboard_bounds());
132 return bounds.CenterPoint();
137 ////////////////////////////////////////////////////////////////////////////////
138 // AppListController, public:
140 AppListController::AppListController()
141 : pagination_model_(new app_list::PaginationModel),
145 should_snap_back_(false) {
146 Shell::GetInstance()->AddShellObserver(this);
147 pagination_model_->AddObserver(this);
150 AppListController::~AppListController() {
151 // Ensures app list view goes before the controller since pagination model
152 // lives in the controller and app list view would access it on destruction.
153 if (view_ && view_->GetWidget())
154 view_->GetWidget()->CloseNow();
156 Shell::GetInstance()->RemoveShellObserver(this);
157 pagination_model_->RemoveObserver(this);
160 void AppListController::SetVisible(bool visible, aura::Window* window) {
161 if (visible == is_visible_)
164 is_visible_ = visible;
166 // App list needs to know the new shelf layout in order to calculate its
167 // UI layout when AppListView visibility changes.
168 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
169 UpdateAutoHideState();
172 // Our widget is currently active. When the animation completes we'll hide
173 // the widget, changing activation. If a menu is shown before the animation
174 // completes then the activation change triggers the menu to close. By
175 // deactivating now we ensure there is no activation change when the
176 // animation completes and any menus stay open.
178 view_->GetWidget()->Deactivate();
180 } else if (is_visible_) {
181 // AppListModel and AppListViewDelegate are owned by AppListView. They
182 // will be released with AppListView on close.
183 app_list::AppListView* view = new app_list::AppListView(
184 Shell::GetInstance()->delegate()->CreateAppListViewDelegate());
185 aura::Window* root_window = window->GetRootWindow();
186 aura::Window* container = GetRootWindowController(root_window)->
187 GetContainer(kShellWindowId_AppListContainer);
188 views::View* applist_button =
189 Shelf::ForWindow(container)->GetAppListButtonView();
190 is_centered_ = view->ShouldCenterWindow();
192 // The experimental app list is centered over the display of the app list
193 // button that was pressed (if triggered via keyboard, this is the display
194 // with the currently focused window).
195 view->InitAsBubbleAtFixedLocation(
197 pagination_model_.get(),
198 GetCenterOfDisplayForView(applist_button),
199 views::BubbleBorder::FLOAT,
200 true /* border_accepts_events */);
202 gfx::Rect applist_button_bounds = applist_button->GetBoundsInScreen();
203 // We need the location of the button within the local screen.
204 applist_button_bounds = ScreenUtil::ConvertRectFromScreen(
206 applist_button_bounds);
207 view->InitAsBubbleAttachedToAnchor(
209 pagination_model_.get(),
210 Shelf::ForWindow(container)->GetAppListButtonView(),
211 GetAnchorPositionOffsetToShelf(applist_button_bounds,
212 Shelf::ForWindow(container)->GetAppListButtonView()->
214 GetBubbleArrow(container),
215 true /* border_accepts_events */);
216 view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
219 // By setting us as DnD recipient, the app list knows that we can
221 SetDragAndDropHostOfCurrentAppList(
222 Shelf::ForWindow(window)->GetDragAndDropHostForAppList());
224 // Update applist button status when app list visibility is changed.
225 Shelf::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
228 bool AppListController::IsVisible() const {
229 return view_ && view_->GetWidget()->IsVisible();
232 aura::Window* AppListController::GetWindow() {
233 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
236 ////////////////////////////////////////////////////////////////////////////////
237 // AppListController, private:
239 void AppListController::SetDragAndDropHostOfCurrentAppList(
240 app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
241 if (view_ && is_visible_)
242 view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
245 void AppListController::SetView(app_list::AppListView* view) {
246 DCHECK(view_ == NULL);
250 views::Widget* widget = view_->GetWidget();
251 widget->AddObserver(this);
252 keyboard::KeyboardController* keyboard_controller =
253 keyboard::KeyboardController::GetInstance();
254 if (keyboard_controller)
255 keyboard_controller->AddObserver(this);
256 Shell::GetInstance()->AddPreTargetHandler(this);
257 Shelf::ForWindow(widget->GetNativeWindow())->AddIconObserver(this);
258 widget->GetNativeView()->GetRootWindow()->AddObserver(this);
259 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this);
261 view_->ShowWhenReady();
264 void AppListController::ResetView() {
268 views::Widget* widget = view_->GetWidget();
269 widget->RemoveObserver(this);
270 GetLayer(widget)->GetAnimator()->RemoveObserver(this);
271 keyboard::KeyboardController* keyboard_controller =
272 keyboard::KeyboardController::GetInstance();
273 if (keyboard_controller)
274 keyboard_controller->RemoveObserver(this);
275 Shell::GetInstance()->RemovePreTargetHandler(this);
276 Shelf::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this);
277 widget->GetNativeView()->GetRootWindow()->RemoveObserver(this);
278 aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this);
282 void AppListController::ScheduleAnimation() {
283 // Stop observing previous animation.
284 StopObservingImplicitAnimations();
286 views::Widget* widget = view_->GetWidget();
287 ui::Layer* layer = GetLayer(widget);
288 layer->GetAnimator()->StopAnimating();
290 gfx::Rect target_bounds;
292 target_bounds = widget->GetWindowBoundsInScreen();
293 widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
295 target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
299 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
300 animation.SetTransitionDuration(
301 base::TimeDelta::FromMilliseconds(
302 is_visible_ ? 0 : kAnimationDurationMs));
303 animation.AddObserver(this);
305 layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
306 widget->SetBounds(target_bounds);
309 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
310 if (!view_ || !is_visible_)
313 // If the event happened on a menu, then the event should not close the app
315 aura::Window* target = static_cast<aura::Window*>(event->target());
317 RootWindowController* root_controller =
318 GetRootWindowController(target->GetRootWindow());
319 if (root_controller) {
320 aura::Window* menu_container =
321 root_controller->GetContainer(kShellWindowId_MenuContainer);
322 if (menu_container->Contains(target))
324 aura::Window* keyboard_container = root_controller->GetContainer(
325 kShellWindowId_VirtualKeyboardContainer);
326 if (keyboard_container->Contains(target))
331 aura::Window* window = view_->GetWidget()->GetNativeView()->parent();
332 if (!window->Contains(target))
333 SetVisible(false, window);
336 void AppListController::UpdateBounds() {
337 if (!view_ || !is_visible_)
340 view_->UpdateBounds();
343 view_->SetAnchorPoint(GetCenterOfDisplayForView(view_));
346 ////////////////////////////////////////////////////////////////////////////////
347 // AppListController, aura::EventFilter implementation:
349 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
350 if (event->type() == ui::ET_MOUSE_PRESSED)
351 ProcessLocatedEvent(event);
354 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
355 if (event->type() == ui::ET_GESTURE_TAP_DOWN)
356 ProcessLocatedEvent(event);
359 ////////////////////////////////////////////////////////////////////////////////
360 // AppListController, aura::FocusObserver implementation:
362 void AppListController::OnWindowFocused(aura::Window* gained_focus,
363 aura::Window* lost_focus) {
364 if (view_ && is_visible_) {
365 aura::Window* applist_window = view_->GetWidget()->GetNativeView();
366 aura::Window* applist_container = applist_window->parent();
368 if (applist_container->Contains(lost_focus) &&
369 (!gained_focus || !applist_container->Contains(gained_focus))) {
370 SetVisible(false, applist_window);
375 ////////////////////////////////////////////////////////////////////////////////
376 // AppListController, aura::WindowObserver implementation:
377 void AppListController::OnWindowBoundsChanged(aura::Window* root,
378 const gfx::Rect& old_bounds,
379 const gfx::Rect& new_bounds) {
383 ////////////////////////////////////////////////////////////////////////////////
384 // AppListController, ui::ImplicitAnimationObserver implementation:
386 void AppListController::OnImplicitAnimationsCompleted() {
388 view_->GetWidget()->Activate();
390 view_->GetWidget()->Close();
393 ////////////////////////////////////////////////////////////////////////////////
394 // AppListController, views::WidgetObserver implementation:
396 void AppListController::OnWidgetDestroying(views::Widget* widget) {
397 DCHECK(view_->GetWidget() == widget);
399 SetVisible(false, widget->GetNativeView());
403 ////////////////////////////////////////////////////////////////////////////////
404 // AppListController, keyboard::KeyboardControllerObserver implementation:
406 void AppListController::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) {
410 ////////////////////////////////////////////////////////////////////////////////
411 // AppListController, ShellObserver implementation:
412 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
414 view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
417 ////////////////////////////////////////////////////////////////////////////////
418 // AppListController, ShelfIconObserver implementation:
420 void AppListController::OnShelfIconPositionsChanged() {
424 ////////////////////////////////////////////////////////////////////////////////
425 // AppListController, PaginationModelObserver implementation:
427 void AppListController::TotalPagesChanged() {
430 void AppListController::SelectedPageChanged(int old_selected,
434 void AppListController::TransitionStarted() {
437 void AppListController::TransitionChanged() {
438 // |view_| could be NULL when app list is closed with a running transition.
442 const app_list::PaginationModel::Transition& transition =
443 pagination_model_->transition();
444 if (pagination_model_->is_valid_page(transition.target_page))
447 views::Widget* widget = view_->GetWidget();
448 ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
449 if (!pagination_model_->IsRevertingCurrentTransition()) {
450 // Update cached |view_bounds_| if it is the first over-scroll move and
451 // widget does not have running animations.
452 if (!should_snap_back_ && !widget_animator->is_animating())
453 view_bounds_ = widget->GetWindowBoundsInScreen();
455 const int current_page = pagination_model_->selected_page();
456 const int dir = transition.target_page > current_page ? -1 : 1;
458 const double progress = 1.0 - pow(1.0 - transition.progress, 4);
459 const int shift = kMaxOverScrollShift * progress * dir;
461 gfx::Rect shifted(view_bounds_);
462 shifted.set_x(shifted.x() + shift);
463 widget->SetBounds(shifted);
464 should_snap_back_ = true;
465 } else if (should_snap_back_) {
466 should_snap_back_ = false;
467 ui::ScopedLayerAnimationSettings animation(widget_animator);
468 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
469 app_list::kOverscrollPageTransitionDurationMs));
470 widget->SetBounds(view_bounds_);