Update V8 to version 4.4.61.
[chromium-blink-merge.git] / ash / wm / app_list_controller.cc
blob4dbd3af502fc44c97dc8e9d64b16558543c220d4
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"
31 namespace ash {
32 namespace {
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);
75 break;
76 case SHELF_ALIGNMENT_LEFT:
77 offseted.Offset(-kAnimationOffset, 0);
78 break;
79 case SHELF_ALIGNMENT_RIGHT:
80 offseted.Offset(kAnimationOffset, 0);
81 break;
82 case SHELF_ALIGNMENT_TOP:
83 offseted.Offset(0, -kAnimationOffset);
84 break;
87 return offseted;
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(),
105 0), 0);
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));
115 default:
116 NOTREACHED();
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 } // namespace
153 ////////////////////////////////////////////////////////////////////////////////
154 // AppListController, public:
156 AppListController::AppListController()
157 : is_visible_(false),
158 is_centered_(false),
159 view_(NULL),
160 current_apps_page_(-1),
161 should_snap_back_(false) {
162 Shell::GetInstance()->AddShellObserver(this);
165 AppListController::~AppListController() {
166 // Ensures app list view goes before the controller since pagination model
167 // lives in the controller and app list view would access it on destruction.
168 if (view_) {
169 view_->GetAppsPaginationModel()->RemoveObserver(this);
170 if (view_->GetWidget())
171 view_->GetWidget()->CloseNow();
174 Shell::GetInstance()->RemoveShellObserver(this);
177 void AppListController::Show(aura::Window* window) {
178 if (is_visible_)
179 return;
181 is_visible_ = true;
183 // App list needs to know the new shelf layout in order to calculate its
184 // UI layout when AppListView visibility changes.
185 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
186 UpdateAutoHideState();
188 if (view_) {
189 ScheduleAnimation();
190 } else {
191 // Note the AppListViewDelegate outlives the AppListView. For Ash, the view
192 // is destroyed when dismissed.
193 app_list::AppListView* view = new app_list::AppListView(
194 Shell::GetInstance()->delegate()->GetAppListViewDelegate());
195 aura::Window* root_window = window->GetRootWindow();
196 aura::Window* container = GetRootWindowController(root_window)->
197 GetContainer(kShellWindowId_AppListContainer);
198 views::View* applist_button =
199 Shelf::ForWindow(container)->GetAppListButtonView();
200 is_centered_ = view->ShouldCenterWindow();
201 bool is_fullscreen = false;
202 #if defined(OS_CHROMEOS)
203 is_fullscreen = base::CommandLine::ForCurrentProcess()->HasSwitch(
204 switches::kAshEnableFullscreenAppList) &&
205 app_list::switches::IsExperimentalAppListEnabled() &&
206 Shell::GetInstance()
207 ->maximize_mode_controller()
208 ->IsMaximizeModeWindowManagerEnabled();
209 #endif
210 if (is_fullscreen) {
211 view->InitAsFramelessWindow(
212 container, current_apps_page_,
213 ScreenUtil::GetDisplayWorkAreaBoundsInParent(container));
214 } else if (is_centered_) {
215 // Note: We can't center the app list until we have its dimensions, so we
216 // init at (0, 0) and then reset its anchor point.
217 view->InitAsBubbleAtFixedLocation(container,
218 current_apps_page_,
219 gfx::Point(),
220 views::BubbleBorder::FLOAT,
221 true /* border_accepts_events */);
222 // The experimental app list is centered over the display of the app list
223 // button that was pressed (if triggered via keyboard, this is the display
224 // with the currently focused window).
225 view->SetAnchorPoint(GetCenterOfDisplayForView(
226 applist_button, GetMinimumBoundsHeightForAppList(view)));
227 } else {
228 gfx::Rect applist_button_bounds = applist_button->GetBoundsInScreen();
229 // We need the location of the button within the local screen.
230 applist_button_bounds = ScreenUtil::ConvertRectFromScreen(
231 root_window,
232 applist_button_bounds);
233 view->InitAsBubbleAttachedToAnchor(
234 container,
235 current_apps_page_,
236 Shelf::ForWindow(container)->GetAppListButtonView(),
237 GetAnchorPositionOffsetToShelf(
238 applist_button_bounds,
239 Shelf::ForWindow(container)->GetAppListButtonView()->GetWidget()),
240 GetBubbleArrow(container),
241 true /* border_accepts_events */);
242 view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
244 SetView(view);
245 // By setting us as DnD recipient, the app list knows that we can
246 // handle items.
247 SetDragAndDropHostOfCurrentAppList(
248 Shelf::ForWindow(window)->GetDragAndDropHostForAppList());
250 // Update applist button status when app list visibility is changed.
251 Shelf::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
254 void AppListController::Dismiss() {
255 if (!is_visible_)
256 return;
258 // If the app list is currently visible, there should be an existing view.
259 DCHECK(view_);
261 is_visible_ = false;
263 // App list needs to know the new shelf layout in order to calculate its
264 // UI layout when AppListView visibility changes.
265 Shell::GetPrimaryRootWindowController()
266 ->GetShelfLayoutManager()
267 ->UpdateAutoHideState();
269 // Our widget is currently active. When the animation completes we'll hide
270 // the widget, changing activation. If a menu is shown before the animation
271 // completes then the activation change triggers the menu to close. By
272 // deactivating now we ensure there is no activation change when the
273 // animation completes and any menus stay open.
274 view_->GetWidget()->Deactivate();
275 ScheduleAnimation();
277 // Update applist button status when app list visibility is changed.
278 Shelf::ForWindow(view_->GetWidget()->GetNativeView())
279 ->GetAppListButtonView()
280 ->SchedulePaint();
283 bool AppListController::IsVisible() const {
284 return view_ && view_->GetWidget()->IsVisible();
287 aura::Window* AppListController::GetWindow() {
288 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
291 ////////////////////////////////////////////////////////////////////////////////
292 // AppListController, private:
294 void AppListController::SetDragAndDropHostOfCurrentAppList(
295 app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
296 if (view_ && is_visible_)
297 view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
300 void AppListController::SetView(app_list::AppListView* view) {
301 DCHECK(view_ == NULL);
302 DCHECK(is_visible_);
304 view_ = view;
305 views::Widget* widget = view_->GetWidget();
306 widget->AddObserver(this);
307 keyboard::KeyboardController* keyboard_controller =
308 keyboard::KeyboardController::GetInstance();
309 if (keyboard_controller)
310 keyboard_controller->AddObserver(this);
311 Shell::GetInstance()->AddPreTargetHandler(this);
312 Shelf::ForWindow(widget->GetNativeWindow())->AddIconObserver(this);
313 widget->GetNativeView()->GetRootWindow()->AddObserver(this);
314 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this);
316 view_->GetAppsPaginationModel()->AddObserver(this);
318 view_->ShowWhenReady();
321 void AppListController::ResetView() {
322 if (!view_)
323 return;
325 views::Widget* widget = view_->GetWidget();
326 widget->RemoveObserver(this);
327 GetLayer(widget)->GetAnimator()->RemoveObserver(this);
328 keyboard::KeyboardController* keyboard_controller =
329 keyboard::KeyboardController::GetInstance();
330 if (keyboard_controller)
331 keyboard_controller->RemoveObserver(this);
332 Shell::GetInstance()->RemovePreTargetHandler(this);
333 Shelf::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this);
334 widget->GetNativeView()->GetRootWindow()->RemoveObserver(this);
335 aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this);
337 view_->GetAppsPaginationModel()->RemoveObserver(this);
339 view_ = NULL;
342 void AppListController::ScheduleAnimation() {
343 // Stop observing previous animation.
344 StopObservingImplicitAnimations();
346 views::Widget* widget = view_->GetWidget();
347 ui::Layer* layer = GetLayer(widget);
348 layer->GetAnimator()->StopAnimating();
350 gfx::Rect target_bounds;
351 if (is_visible_) {
352 target_bounds = widget->GetWindowBoundsInScreen();
353 widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
354 } else {
355 target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
356 widget);
359 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
360 animation.SetTransitionDuration(
361 base::TimeDelta::FromMilliseconds(
362 is_visible_ ? 0 : kAnimationDurationMs));
363 animation.AddObserver(this);
365 layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
366 widget->SetBounds(target_bounds);
369 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
370 if (!view_ || !is_visible_)
371 return;
373 // If the event happened on a menu, then the event should not close the app
374 // list.
375 aura::Window* target = static_cast<aura::Window*>(event->target());
376 if (target) {
377 RootWindowController* root_controller =
378 GetRootWindowController(target->GetRootWindow());
379 if (root_controller) {
380 aura::Window* menu_container =
381 root_controller->GetContainer(kShellWindowId_MenuContainer);
382 if (menu_container->Contains(target))
383 return;
384 aura::Window* keyboard_container = root_controller->GetContainer(
385 kShellWindowId_VirtualKeyboardContainer);
386 if (keyboard_container->Contains(target))
387 return;
391 aura::Window* window = view_->GetWidget()->GetNativeView()->parent();
392 if (!window->Contains(target) &&
393 !app_list::switches::ShouldNotDismissOnBlur()) {
394 Dismiss();
398 void AppListController::UpdateBounds() {
399 if (!view_ || !is_visible_)
400 return;
402 view_->UpdateBounds();
404 if (is_centered_)
405 view_->SetAnchorPoint(GetCenterOfDisplayForView(
406 view_, GetMinimumBoundsHeightForAppList(view_)));
409 ////////////////////////////////////////////////////////////////////////////////
410 // AppListController, aura::EventFilter implementation:
412 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
413 if (event->type() == ui::ET_MOUSE_PRESSED)
414 ProcessLocatedEvent(event);
417 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
418 if (event->type() == ui::ET_GESTURE_TAP_DOWN)
419 ProcessLocatedEvent(event);
422 ////////////////////////////////////////////////////////////////////////////////
423 // AppListController, aura::FocusObserver implementation:
425 void AppListController::OnWindowFocused(aura::Window* gained_focus,
426 aura::Window* lost_focus) {
427 if (view_ && is_visible_) {
428 aura::Window* applist_window = view_->GetWidget()->GetNativeView();
429 aura::Window* applist_container = applist_window->parent();
431 if (applist_container->Contains(lost_focus) &&
432 (!gained_focus || !applist_container->Contains(gained_focus)) &&
433 !app_list::switches::ShouldNotDismissOnBlur()) {
434 Dismiss();
439 ////////////////////////////////////////////////////////////////////////////////
440 // AppListController, aura::WindowObserver implementation:
441 void AppListController::OnWindowBoundsChanged(aura::Window* root,
442 const gfx::Rect& old_bounds,
443 const gfx::Rect& new_bounds) {
444 UpdateBounds();
447 ////////////////////////////////////////////////////////////////////////////////
448 // AppListController, ui::ImplicitAnimationObserver implementation:
450 void AppListController::OnImplicitAnimationsCompleted() {
451 if (is_visible_ )
452 view_->GetWidget()->Activate();
453 else
454 view_->GetWidget()->Close();
457 ////////////////////////////////////////////////////////////////////////////////
458 // AppListController, views::WidgetObserver implementation:
460 void AppListController::OnWidgetDestroying(views::Widget* widget) {
461 DCHECK(view_->GetWidget() == widget);
462 if (is_visible_)
463 Dismiss();
464 ResetView();
467 ////////////////////////////////////////////////////////////////////////////////
468 // AppListController, keyboard::KeyboardControllerObserver implementation:
470 void AppListController::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) {
471 UpdateBounds();
474 ////////////////////////////////////////////////////////////////////////////////
475 // AppListController, ShellObserver implementation:
476 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
477 if (view_)
478 view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
481 ////////////////////////////////////////////////////////////////////////////////
482 // AppListController, ShelfIconObserver implementation:
484 void AppListController::OnShelfIconPositionsChanged() {
485 UpdateBounds();
488 ////////////////////////////////////////////////////////////////////////////////
489 // AppListController, PaginationModelObserver implementation:
491 void AppListController::TotalPagesChanged() {
494 void AppListController::SelectedPageChanged(int old_selected,
495 int new_selected) {
496 current_apps_page_ = new_selected;
499 void AppListController::TransitionStarted() {
502 void AppListController::TransitionChanged() {
503 // |view_| could be NULL when app list is closed with a running transition.
504 if (!view_)
505 return;
507 app_list::PaginationModel* pagination_model = view_->GetAppsPaginationModel();
509 const app_list::PaginationModel::Transition& transition =
510 pagination_model->transition();
511 if (pagination_model->is_valid_page(transition.target_page))
512 return;
514 views::Widget* widget = view_->GetWidget();
515 ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
516 if (!pagination_model->IsRevertingCurrentTransition()) {
517 // Update cached |view_bounds_| if it is the first over-scroll move and
518 // widget does not have running animations.
519 if (!should_snap_back_ && !widget_animator->is_animating())
520 view_bounds_ = widget->GetWindowBoundsInScreen();
522 const int current_page = pagination_model->selected_page();
523 const int dir = transition.target_page > current_page ? -1 : 1;
525 const double progress = 1.0 - pow(1.0 - transition.progress, 4);
526 const int shift = kMaxOverScrollShift * progress * dir;
528 gfx::Rect shifted(view_bounds_);
529 shifted.set_x(shifted.x() + shift);
531 widget->SetBounds(shifted);
533 should_snap_back_ = true;
534 } else if (should_snap_back_) {
535 should_snap_back_ = false;
536 ui::ScopedLayerAnimationSettings animation(widget_animator);
537 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
538 app_list::kOverscrollPageTransitionDurationMs));
539 widget->SetBounds(view_bounds_);
543 } // namespace ash