Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / wm / app_list_controller.cc
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.
4
5 #include "ash/wm/app_list_controller.h"
6
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"
29
30 namespace ash {
31 namespace {
32
33 // Duration for show/hide animation in milliseconds.
34 const int kAnimationDurationMs = 200;
35
36 // Offset in pixels to animation away/towards the shelf.
37 const int kAnimationOffset = 8;
38
39 // The maximum shift in pixels when over-scroll happens.
40 const int kMaxOverScrollShift = 48;
41
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;
46
47 ui::Layer* GetLayer(views::Widget* widget) {
48   return widget->GetNativeView()->layer();
49 }
50
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);
60 }
61
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);
71       break;
72     case SHELF_ALIGNMENT_LEFT:
73       offseted.Offset(-kAnimationOffset, 0);
74       break;
75     case SHELF_ALIGNMENT_RIGHT:
76       offseted.Offset(kAnimationOffset, 0);
77       break;
78     case SHELF_ALIGNMENT_TOP:
79       offseted.Offset(0, -kAnimationOffset);
80       break;
81   }
82
83   return offseted;
84 }
85
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();
99         return gfx::Vector2d(
100             std::min(screen_width - kMinimalAnchorPositionOffset - anchor.x(),
101                      0), 0);
102       }
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));
111     default:
112       NOTREACHED();
113       return gfx::Vector2d();
114   }
115 }
116
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();
122
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());
131
132   return bounds.CenterPoint();
133 }
134
135 }  // namespace
136
137 ////////////////////////////////////////////////////////////////////////////////
138 // AppListController, public:
139
140 AppListController::AppListController()
141     : pagination_model_(new app_list::PaginationModel),
142       is_visible_(false),
143       is_centered_(false),
144       view_(NULL),
145       should_snap_back_(false) {
146   Shell::GetInstance()->AddShellObserver(this);
147   pagination_model_->AddObserver(this);
148 }
149
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();
155
156   Shell::GetInstance()->RemoveShellObserver(this);
157   pagination_model_->RemoveObserver(this);
158 }
159
160 void AppListController::SetVisible(bool visible, aura::Window* window) {
161   if (visible == is_visible_)
162     return;
163
164   is_visible_ = visible;
165
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();
170
171   if (view_) {
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.
177     if (!visible)
178       view_->GetWidget()->Deactivate();
179     ScheduleAnimation();
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();
191     if (is_centered_) {
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(
196           container,
197           pagination_model_.get(),
198           GetCenterOfDisplayForView(applist_button),
199           views::BubbleBorder::FLOAT,
200           true /* border_accepts_events */);
201     } else {
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(
205           root_window,
206           applist_button_bounds);
207       view->InitAsBubbleAttachedToAnchor(
208           container,
209           pagination_model_.get(),
210           Shelf::ForWindow(container)->GetAppListButtonView(),
211           GetAnchorPositionOffsetToShelf(applist_button_bounds,
212               Shelf::ForWindow(container)->GetAppListButtonView()->
213                   GetWidget()),
214           GetBubbleArrow(container),
215           true /* border_accepts_events */);
216       view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
217     }
218     SetView(view);
219     // By setting us as DnD recipient, the app list knows that we can
220     // handle items.
221     SetDragAndDropHostOfCurrentAppList(
222         Shelf::ForWindow(window)->GetDragAndDropHostForAppList());
223   }
224   // Update applist button status when app list visibility is changed.
225   Shelf::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
226 }
227
228 bool AppListController::IsVisible() const {
229   return view_ && view_->GetWidget()->IsVisible();
230 }
231
232 aura::Window* AppListController::GetWindow() {
233   return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
234 }
235
236 ////////////////////////////////////////////////////////////////////////////////
237 // AppListController, private:
238
239 void AppListController::SetDragAndDropHostOfCurrentAppList(
240     app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
241   if (view_ && is_visible_)
242     view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
243 }
244
245 void AppListController::SetView(app_list::AppListView* view) {
246   DCHECK(view_ == NULL);
247   DCHECK(is_visible_);
248
249   view_ = view;
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);
260
261   view_->ShowWhenReady();
262 }
263
264 void AppListController::ResetView() {
265   if (!view_)
266     return;
267
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);
279   view_ = NULL;
280 }
281
282 void AppListController::ScheduleAnimation() {
283   // Stop observing previous animation.
284   StopObservingImplicitAnimations();
285
286   views::Widget* widget = view_->GetWidget();
287   ui::Layer* layer = GetLayer(widget);
288   layer->GetAnimator()->StopAnimating();
289
290   gfx::Rect target_bounds;
291   if (is_visible_) {
292     target_bounds = widget->GetWindowBoundsInScreen();
293     widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
294   } else {
295     target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
296                                        widget);
297   }
298
299   ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
300   animation.SetTransitionDuration(
301       base::TimeDelta::FromMilliseconds(
302           is_visible_ ? 0 : kAnimationDurationMs));
303   animation.AddObserver(this);
304
305   layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
306   widget->SetBounds(target_bounds);
307 }
308
309 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
310   if (!view_ || !is_visible_)
311     return;
312
313   // If the event happened on a menu, then the event should not close the app
314   // list.
315   aura::Window* target = static_cast<aura::Window*>(event->target());
316   if (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))
323         return;
324       aura::Window* keyboard_container = root_controller->GetContainer(
325           kShellWindowId_VirtualKeyboardContainer);
326       if (keyboard_container->Contains(target))
327         return;
328     }
329   }
330
331   aura::Window* window = view_->GetWidget()->GetNativeView()->parent();
332   if (!window->Contains(target))
333     SetVisible(false, window);
334 }
335
336 void AppListController::UpdateBounds() {
337   if (!view_ || !is_visible_)
338     return;
339
340   view_->UpdateBounds();
341
342   if (is_centered_)
343     view_->SetAnchorPoint(GetCenterOfDisplayForView(view_));
344 }
345
346 ////////////////////////////////////////////////////////////////////////////////
347 // AppListController, aura::EventFilter implementation:
348
349 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
350   if (event->type() == ui::ET_MOUSE_PRESSED)
351     ProcessLocatedEvent(event);
352 }
353
354 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
355   if (event->type() == ui::ET_GESTURE_TAP_DOWN)
356     ProcessLocatedEvent(event);
357 }
358
359 ////////////////////////////////////////////////////////////////////////////////
360 // AppListController,  aura::FocusObserver implementation:
361
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();
367
368     if (applist_container->Contains(lost_focus) &&
369         (!gained_focus || !applist_container->Contains(gained_focus))) {
370       SetVisible(false, applist_window);
371     }
372   }
373 }
374
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) {
380   UpdateBounds();
381 }
382
383 ////////////////////////////////////////////////////////////////////////////////
384 // AppListController, ui::ImplicitAnimationObserver implementation:
385
386 void AppListController::OnImplicitAnimationsCompleted() {
387   if (is_visible_ )
388     view_->GetWidget()->Activate();
389   else
390     view_->GetWidget()->Close();
391 }
392
393 ////////////////////////////////////////////////////////////////////////////////
394 // AppListController, views::WidgetObserver implementation:
395
396 void AppListController::OnWidgetDestroying(views::Widget* widget) {
397   DCHECK(view_->GetWidget() == widget);
398   if (is_visible_)
399     SetVisible(false, widget->GetNativeView());
400   ResetView();
401 }
402
403 ////////////////////////////////////////////////////////////////////////////////
404 // AppListController, keyboard::KeyboardControllerObserver implementation:
405
406 void AppListController::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) {
407   UpdateBounds();
408 }
409
410 ////////////////////////////////////////////////////////////////////////////////
411 // AppListController, ShellObserver implementation:
412 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
413   if (view_)
414     view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
415 }
416
417 ////////////////////////////////////////////////////////////////////////////////
418 // AppListController, ShelfIconObserver implementation:
419
420 void AppListController::OnShelfIconPositionsChanged() {
421   UpdateBounds();
422 }
423
424 ////////////////////////////////////////////////////////////////////////////////
425 // AppListController, PaginationModelObserver implementation:
426
427 void AppListController::TotalPagesChanged() {
428 }
429
430 void AppListController::SelectedPageChanged(int old_selected,
431                                             int new_selected) {
432 }
433
434 void AppListController::TransitionStarted() {
435 }
436
437 void AppListController::TransitionChanged() {
438   // |view_| could be NULL when app list is closed with a running transition.
439   if (!view_)
440     return;
441
442   const app_list::PaginationModel::Transition& transition =
443       pagination_model_->transition();
444   if (pagination_model_->is_valid_page(transition.target_page))
445     return;
446
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();
454
455     const int current_page = pagination_model_->selected_page();
456     const int dir = transition.target_page > current_page ? -1 : 1;
457
458     const double progress = 1.0 - pow(1.0 - transition.progress, 4);
459     const int shift = kMaxOverScrollShift * progress * dir;
460
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_);
471   }
472 }
473
474 }  // namespace ash