Upstream version 5.34.92.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/pagination_model.h"
18 #include "ui/app_list/views/app_list_view.h"
19 #include "ui/aura/client/focus_client.h"
20 #include "ui/aura/root_window.h"
21 #include "ui/aura/window.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/compositor/scoped_layer_animation_settings.h"
24 #include "ui/events/event.h"
25 #include "ui/gfx/transform_util.h"
26 #include "ui/views/widget/widget.h"
27
28 namespace ash {
29 namespace internal {
30
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 }  // namespace
118
119 ////////////////////////////////////////////////////////////////////////////////
120 // AppListController, public:
121
122 AppListController::AppListController()
123     : pagination_model_(new app_list::PaginationModel),
124       is_visible_(false),
125       view_(NULL),
126       should_snap_back_(false) {
127   Shell::GetInstance()->AddShellObserver(this);
128   pagination_model_->AddObserver(this);
129 }
130
131 AppListController::~AppListController() {
132   // Ensures app list view goes before the controller since pagination model
133   // lives in the controller and app list view would access it on destruction.
134   if (view_ && view_->GetWidget())
135     view_->GetWidget()->CloseNow();
136
137   Shell::GetInstance()->RemoveShellObserver(this);
138   pagination_model_->RemoveObserver(this);
139 }
140
141 void AppListController::SetVisible(bool visible, aura::Window* window) {
142   if (visible == is_visible_)
143     return;
144
145   is_visible_ = visible;
146
147   // App list needs to know the new shelf layout in order to calculate its
148   // UI layout when AppListView visibility changes.
149   Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
150       UpdateAutoHideState();
151
152   if (view_) {
153     // Our widget is currently active. When the animation completes we'll hide
154     // the widget, changing activation. If a menu is shown before the animation
155     // completes then the activation change triggers the menu to close. By
156     // deactivating now we ensure there is no activation change when the
157     // animation completes and any menus stay open.
158     if (!visible)
159       view_->GetWidget()->Deactivate();
160     ScheduleAnimation();
161   } else if (is_visible_) {
162     // AppListModel and AppListViewDelegate are owned by AppListView. They
163     // will be released with AppListView on close.
164     app_list::AppListView* view = new app_list::AppListView(
165         Shell::GetInstance()->delegate()->CreateAppListViewDelegate());
166     aura::Window* root_window = window->GetRootWindow();
167     aura::Window* container = GetRootWindowController(root_window)->
168         GetContainer(kShellWindowId_AppListContainer);
169     if (ash::switches::UseAlternateShelfLayout()) {
170       gfx::Rect applist_button_bounds = Shelf::ForWindow(container)->
171           GetAppListButtonView()->GetBoundsInScreen();
172       // We need the location of the button within the local screen.
173       applist_button_bounds = ScreenUtil::ConvertRectFromScreen(
174           root_window,
175           applist_button_bounds);
176       view->InitAsBubbleAttachedToAnchor(
177           container,
178           pagination_model_.get(),
179           Shelf::ForWindow(container)->GetAppListButtonView(),
180           GetAnchorPositionOffsetToShelf(applist_button_bounds,
181               Shelf::ForWindow(container)->GetAppListButtonView()->
182                   GetWidget()),
183           GetBubbleArrow(container),
184           true /* border_accepts_events */);
185       view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
186     } else {
187       view->InitAsBubbleAttachedToAnchor(
188           container,
189           pagination_model_.get(),
190           Shelf::ForWindow(container)->GetAppListButtonView(),
191           gfx::Vector2d(),
192           GetBubbleArrow(container),
193           true /* border_accepts_events */);
194     }
195     SetView(view);
196     // By setting us as DnD recipient, the app list knows that we can
197     // handle items.
198     if (!CommandLine::ForCurrentProcess()->HasSwitch(
199             switches::kAshDisableDragAndDropAppListToLauncher)) {
200       SetDragAndDropHostOfCurrentAppList(
201           Shelf::ForWindow(window)->GetDragAndDropHostForAppList());
202     }
203   }
204   // Update applist button status when app list visibility is changed.
205   Shelf::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
206 }
207
208 bool AppListController::IsVisible() const {
209   return view_ && view_->GetWidget()->IsVisible();
210 }
211
212 aura::Window* AppListController::GetWindow() {
213   return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
214 }
215
216 ////////////////////////////////////////////////////////////////////////////////
217 // AppListController, private:
218
219 void AppListController::SetDragAndDropHostOfCurrentAppList(
220     app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
221   if (view_ && is_visible_)
222     view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
223 }
224
225 void AppListController::SetView(app_list::AppListView* view) {
226   DCHECK(view_ == NULL);
227   DCHECK(is_visible_);
228
229   view_ = view;
230   views::Widget* widget = view_->GetWidget();
231   widget->AddObserver(this);
232   Shell::GetInstance()->AddPreTargetHandler(this);
233   Shelf::ForWindow(widget->GetNativeWindow())->AddIconObserver(this);
234   widget->GetNativeView()->GetRootWindow()->AddObserver(this);
235   aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this);
236
237   view_->ShowWhenReady();
238 }
239
240 void AppListController::ResetView() {
241   if (!view_)
242     return;
243
244   views::Widget* widget = view_->GetWidget();
245   widget->RemoveObserver(this);
246   GetLayer(widget)->GetAnimator()->RemoveObserver(this);
247   Shell::GetInstance()->RemovePreTargetHandler(this);
248   Shelf::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this);
249   widget->GetNativeView()->GetRootWindow()->RemoveObserver(this);
250   aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this);
251   view_ = NULL;
252 }
253
254 void AppListController::ScheduleAnimation() {
255   // Stop observing previous animation.
256   StopObservingImplicitAnimations();
257
258   views::Widget* widget = view_->GetWidget();
259   ui::Layer* layer = GetLayer(widget);
260   layer->GetAnimator()->StopAnimating();
261
262   gfx::Rect target_bounds;
263   if (is_visible_) {
264     target_bounds = widget->GetWindowBoundsInScreen();
265     widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
266   } else {
267     target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
268                                        widget);
269   }
270
271   ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
272   animation.SetTransitionDuration(
273       base::TimeDelta::FromMilliseconds(
274           is_visible_ ? 0 : kAnimationDurationMs));
275   animation.AddObserver(this);
276
277   layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
278   widget->SetBounds(target_bounds);
279 }
280
281 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
282   // If the event happened on a menu, then the event should not close the app
283   // list.
284   aura::Window* target = static_cast<aura::Window*>(event->target());
285   if (target) {
286     RootWindowController* root_controller =
287         GetRootWindowController(target->GetRootWindow());
288     if (root_controller) {
289       aura::Window* menu_container = root_controller->GetContainer(
290           internal::kShellWindowId_MenuContainer);
291       if (menu_container->Contains(target))
292         return;
293       aura::Window* keyboard_container = root_controller->GetContainer(
294           internal::kShellWindowId_VirtualKeyboardContainer);
295       if (keyboard_container->Contains(target))
296         return;
297     }
298   }
299
300   if (view_ && is_visible_) {
301     aura::Window* window = view_->GetWidget()->GetNativeView();
302     gfx::Point window_local_point(event->root_location());
303     aura::Window::ConvertPointToTarget(window->GetRootWindow(),
304                                        window,
305                                        &window_local_point);
306     // Use HitTest to respect the hit test mask of the bubble.
307     if (!window->HitTest(window_local_point))
308       SetVisible(false, window);
309   }
310 }
311
312 void AppListController::UpdateBounds() {
313   if (view_ && is_visible_)
314     view_->UpdateBounds();
315 }
316
317 ////////////////////////////////////////////////////////////////////////////////
318 // AppListController, aura::EventFilter implementation:
319
320 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
321   if (event->type() == ui::ET_MOUSE_PRESSED)
322     ProcessLocatedEvent(event);
323 }
324
325 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
326   if (event->type() == ui::ET_GESTURE_TAP_DOWN)
327     ProcessLocatedEvent(event);
328 }
329
330 ////////////////////////////////////////////////////////////////////////////////
331 // AppListController,  aura::FocusObserver implementation:
332
333 void AppListController::OnWindowFocused(aura::Window* gained_focus,
334                                         aura::Window* lost_focus) {
335   if (view_ && is_visible_) {
336     aura::Window* applist_window = view_->GetWidget()->GetNativeView();
337     aura::Window* applist_container = applist_window->parent();
338
339     if (applist_container->Contains(lost_focus) &&
340         (!gained_focus || !applist_container->Contains(gained_focus))) {
341       SetVisible(false, applist_window);
342     }
343   }
344 }
345
346 ////////////////////////////////////////////////////////////////////////////////
347 // AppListController,  aura::WindowObserver implementation:
348 void AppListController::OnWindowBoundsChanged(aura::Window* root,
349                                               const gfx::Rect& old_bounds,
350                                               const gfx::Rect& new_bounds) {
351   UpdateBounds();
352 }
353
354 ////////////////////////////////////////////////////////////////////////////////
355 // AppListController, ui::ImplicitAnimationObserver implementation:
356
357 void AppListController::OnImplicitAnimationsCompleted() {
358   if (is_visible_ )
359     view_->GetWidget()->Activate();
360   else
361     view_->GetWidget()->Close();
362 }
363
364 ////////////////////////////////////////////////////////////////////////////////
365 // AppListController, views::WidgetObserver implementation:
366
367 void AppListController::OnWidgetDestroying(views::Widget* widget) {
368   DCHECK(view_->GetWidget() == widget);
369   if (is_visible_)
370     SetVisible(false, widget->GetNativeView());
371   ResetView();
372 }
373
374 ////////////////////////////////////////////////////////////////////////////////
375 // AppListController, ShellObserver implementation:
376 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
377   if (view_)
378     view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
379 }
380
381 ////////////////////////////////////////////////////////////////////////////////
382 // AppListController, ShelfIconObserver implementation:
383
384 void AppListController::OnShelfIconPositionsChanged() {
385   UpdateBounds();
386 }
387
388 ////////////////////////////////////////////////////////////////////////////////
389 // AppListController, PaginationModelObserver implementation:
390
391 void AppListController::TotalPagesChanged() {
392 }
393
394 void AppListController::SelectedPageChanged(int old_selected,
395                                             int new_selected) {
396 }
397
398 void AppListController::TransitionStarted() {
399 }
400
401 void AppListController::TransitionChanged() {
402   // |view_| could be NULL when app list is closed with a running transition.
403   if (!view_)
404     return;
405
406   const app_list::PaginationModel::Transition& transition =
407       pagination_model_->transition();
408   if (pagination_model_->is_valid_page(transition.target_page))
409     return;
410
411   views::Widget* widget = view_->GetWidget();
412   ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
413   if (!pagination_model_->IsRevertingCurrentTransition()) {
414     // Update cached |view_bounds_| if it is the first over-scroll move and
415     // widget does not have running animations.
416     if (!should_snap_back_ && !widget_animator->is_animating())
417       view_bounds_ = widget->GetWindowBoundsInScreen();
418
419     const int current_page = pagination_model_->selected_page();
420     const int dir = transition.target_page > current_page ? -1 : 1;
421
422     const double progress = 1.0 - pow(1.0 - transition.progress, 4);
423     const int shift = kMaxOverScrollShift * progress * dir;
424
425     gfx::Rect shifted(view_bounds_);
426     shifted.set_x(shifted.x() + shift);
427     widget->SetBounds(shifted);
428     should_snap_back_ = true;
429   } else if (should_snap_back_) {
430     should_snap_back_ = false;
431     ui::ScopedLayerAnimationSettings animation(widget_animator);
432     animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
433         app_list::kOverscrollPageTransitionDurationMs));
434     widget->SetBounds(view_bounds_);
435   }
436 }
437
438 }  // namespace internal
439 }  // namespace ash