Upstream version 7.35.139.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/views/widget/widget.h"
28
29 namespace ash {
30 namespace internal {
31
32 namespace {
33
34 // Duration for show/hide animation in milliseconds.
35 const int kAnimationDurationMs = 200;
36
37 // Offset in pixels to animation away/towards the shelf.
38 const int kAnimationOffset = 8;
39
40 // The maximum shift in pixels when over-scroll happens.
41 const int kMaxOverScrollShift = 48;
42
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;
47
48 ui::Layer* GetLayer(views::Widget* widget) {
49   return widget->GetNativeView()->layer();
50 }
51
52 // Gets arrow location based on shelf alignment.
53 views::BubbleBorder::Arrow GetBubbleArrow(aura::Window* window) {
54   DCHECK(Shell::HasInstance());
55   return ShelfLayoutManager::ForShelf(window)->
56       SelectValueForShelfAlignment(
57           views::BubbleBorder::BOTTOM_CENTER,
58           views::BubbleBorder::LEFT_CENTER,
59           views::BubbleBorder::RIGHT_CENTER,
60           views::BubbleBorder::TOP_CENTER);
61 }
62
63 // Offset given |rect| towards shelf.
64 gfx::Rect OffsetTowardsShelf(const gfx::Rect& rect, views::Widget* widget) {
65   DCHECK(Shell::HasInstance());
66   ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment(
67       widget->GetNativeView()->GetRootWindow());
68   gfx::Rect offseted(rect);
69   switch (shelf_alignment) {
70     case SHELF_ALIGNMENT_BOTTOM:
71       offseted.Offset(0, kAnimationOffset);
72       break;
73     case SHELF_ALIGNMENT_LEFT:
74       offseted.Offset(-kAnimationOffset, 0);
75       break;
76     case SHELF_ALIGNMENT_RIGHT:
77       offseted.Offset(kAnimationOffset, 0);
78       break;
79     case SHELF_ALIGNMENT_TOP:
80       offseted.Offset(0, -kAnimationOffset);
81       break;
82   }
83
84   return offseted;
85 }
86
87 // Using |button_bounds|, determine the anchor offset so that the bubble gets
88 // shown above the shelf (used for the alternate shelf theme).
89 gfx::Vector2d GetAnchorPositionOffsetToShelf(
90     const gfx::Rect& button_bounds, views::Widget* widget) {
91   DCHECK(Shell::HasInstance());
92   ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment(
93       widget->GetNativeView()->GetRootWindow());
94   gfx::Point anchor(button_bounds.CenterPoint());
95   switch (shelf_alignment) {
96     case SHELF_ALIGNMENT_TOP:
97     case SHELF_ALIGNMENT_BOTTOM:
98       if (base::i18n::IsRTL()) {
99         int screen_width = widget->GetWorkAreaBoundsInScreen().width();
100         return gfx::Vector2d(
101             std::min(screen_width - kMinimalAnchorPositionOffset - anchor.x(),
102                      0), 0);
103       }
104       return gfx::Vector2d(
105           std::max(kMinimalAnchorPositionOffset - anchor.x(), 0), 0);
106     case SHELF_ALIGNMENT_LEFT:
107       return gfx::Vector2d(
108           0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0));
109     case SHELF_ALIGNMENT_RIGHT:
110       return gfx::Vector2d(
111           0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0));
112     default:
113       NOTREACHED();
114       return gfx::Vector2d();
115   }
116 }
117
118 }  // namespace
119
120 ////////////////////////////////////////////////////////////////////////////////
121 // AppListController, public:
122
123 AppListController::AppListController()
124     : pagination_model_(new app_list::PaginationModel),
125       is_visible_(false),
126       view_(NULL),
127       should_snap_back_(false) {
128   Shell::GetInstance()->AddShellObserver(this);
129   pagination_model_->AddObserver(this);
130 }
131
132 AppListController::~AppListController() {
133   // Ensures app list view goes before the controller since pagination model
134   // lives in the controller and app list view would access it on destruction.
135   if (view_ && view_->GetWidget())
136     view_->GetWidget()->CloseNow();
137
138   Shell::GetInstance()->RemoveShellObserver(this);
139   pagination_model_->RemoveObserver(this);
140 }
141
142 void AppListController::SetVisible(bool visible, aura::Window* window) {
143   if (visible == is_visible_)
144     return;
145
146   is_visible_ = visible;
147
148   // App list needs to know the new shelf layout in order to calculate its
149   // UI layout when AppListView visibility changes.
150   Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
151       UpdateAutoHideState();
152
153   if (view_) {
154     // Our widget is currently active. When the animation completes we'll hide
155     // the widget, changing activation. If a menu is shown before the animation
156     // completes then the activation change triggers the menu to close. By
157     // deactivating now we ensure there is no activation change when the
158     // animation completes and any menus stay open.
159     if (!visible)
160       view_->GetWidget()->Deactivate();
161     ScheduleAnimation();
162   } else if (is_visible_) {
163     // AppListModel and AppListViewDelegate are owned by AppListView. They
164     // will be released with AppListView on close.
165     app_list::AppListView* view = new app_list::AppListView(
166         Shell::GetInstance()->delegate()->CreateAppListViewDelegate());
167     aura::Window* root_window = window->GetRootWindow();
168     aura::Window* container = GetRootWindowController(root_window)->
169         GetContainer(kShellWindowId_AppListContainer);
170     if (app_list::switches::IsExperimentalAppListPositionEnabled()) {
171       // The experimental app list is centered over the primary display.
172       view->InitAsBubbleAtFixedLocation(
173           NULL,
174           pagination_model_.get(),
175           Shell::GetScreen()->GetPrimaryDisplay().bounds().CenterPoint(),
176           views::BubbleBorder::FLOAT,
177           true /* border_accepts_events */);
178     } else if (ash::switches::UseAlternateShelfLayout()) {
179       gfx::Rect applist_button_bounds = Shelf::ForWindow(container)->
180           GetAppListButtonView()->GetBoundsInScreen();
181       // We need the location of the button within the local screen.
182       applist_button_bounds = ScreenUtil::ConvertRectFromScreen(
183           root_window,
184           applist_button_bounds);
185       view->InitAsBubbleAttachedToAnchor(
186           container,
187           pagination_model_.get(),
188           Shelf::ForWindow(container)->GetAppListButtonView(),
189           GetAnchorPositionOffsetToShelf(applist_button_bounds,
190               Shelf::ForWindow(container)->GetAppListButtonView()->
191                   GetWidget()),
192           GetBubbleArrow(container),
193           true /* border_accepts_events */);
194       view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
195     } else {
196       view->InitAsBubbleAttachedToAnchor(
197           container,
198           pagination_model_.get(),
199           Shelf::ForWindow(container)->GetAppListButtonView(),
200           gfx::Vector2d(),
201           GetBubbleArrow(container),
202           true /* border_accepts_events */);
203     }
204     SetView(view);
205     // By setting us as DnD recipient, the app list knows that we can
206     // handle items.
207     SetDragAndDropHostOfCurrentAppList(
208         Shelf::ForWindow(window)->GetDragAndDropHostForAppList());
209   }
210   // Update applist button status when app list visibility is changed.
211   Shelf::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
212 }
213
214 bool AppListController::IsVisible() const {
215   return view_ && view_->GetWidget()->IsVisible();
216 }
217
218 aura::Window* AppListController::GetWindow() {
219   return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
220 }
221
222 ////////////////////////////////////////////////////////////////////////////////
223 // AppListController, private:
224
225 void AppListController::SetDragAndDropHostOfCurrentAppList(
226     app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
227   if (view_ && is_visible_)
228     view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
229 }
230
231 void AppListController::SetView(app_list::AppListView* view) {
232   DCHECK(view_ == NULL);
233   DCHECK(is_visible_);
234
235   view_ = view;
236   views::Widget* widget = view_->GetWidget();
237   widget->AddObserver(this);
238   Shell::GetInstance()->AddPreTargetHandler(this);
239   Shelf::ForWindow(widget->GetNativeWindow())->AddIconObserver(this);
240   widget->GetNativeView()->GetRootWindow()->AddObserver(this);
241   aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this);
242
243   view_->ShowWhenReady();
244 }
245
246 void AppListController::ResetView() {
247   if (!view_)
248     return;
249
250   views::Widget* widget = view_->GetWidget();
251   widget->RemoveObserver(this);
252   GetLayer(widget)->GetAnimator()->RemoveObserver(this);
253   Shell::GetInstance()->RemovePreTargetHandler(this);
254   Shelf::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this);
255   widget->GetNativeView()->GetRootWindow()->RemoveObserver(this);
256   aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this);
257   view_ = NULL;
258 }
259
260 void AppListController::ScheduleAnimation() {
261   // Stop observing previous animation.
262   StopObservingImplicitAnimations();
263
264   views::Widget* widget = view_->GetWidget();
265   ui::Layer* layer = GetLayer(widget);
266   layer->GetAnimator()->StopAnimating();
267
268   gfx::Rect target_bounds;
269   if (is_visible_) {
270     target_bounds = widget->GetWindowBoundsInScreen();
271     widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
272   } else {
273     target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
274                                        widget);
275   }
276
277   ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
278   animation.SetTransitionDuration(
279       base::TimeDelta::FromMilliseconds(
280           is_visible_ ? 0 : kAnimationDurationMs));
281   animation.AddObserver(this);
282
283   layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
284   widget->SetBounds(target_bounds);
285 }
286
287 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
288   if (!view_ || !is_visible_)
289     return;
290
291   // If the event happened on a menu, then the event should not close the app
292   // list.
293   aura::Window* target = static_cast<aura::Window*>(event->target());
294   if (target) {
295     RootWindowController* root_controller =
296         GetRootWindowController(target->GetRootWindow());
297     if (root_controller) {
298       aura::Window* menu_container = root_controller->GetContainer(
299           internal::kShellWindowId_MenuContainer);
300       if (menu_container->Contains(target))
301         return;
302       aura::Window* keyboard_container = root_controller->GetContainer(
303           internal::kShellWindowId_VirtualKeyboardContainer);
304       if (keyboard_container->Contains(target))
305         return;
306     }
307   }
308
309   aura::Window* window = view_->GetWidget()->GetNativeView()->parent();
310   if (!window->Contains(target))
311     SetVisible(false, window);
312 }
313
314 void AppListController::UpdateBounds() {
315   if (view_ && is_visible_)
316     view_->UpdateBounds();
317 }
318
319 ////////////////////////////////////////////////////////////////////////////////
320 // AppListController, aura::EventFilter implementation:
321
322 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
323   if (event->type() == ui::ET_MOUSE_PRESSED)
324     ProcessLocatedEvent(event);
325 }
326
327 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
328   if (event->type() == ui::ET_GESTURE_TAP_DOWN)
329     ProcessLocatedEvent(event);
330 }
331
332 ////////////////////////////////////////////////////////////////////////////////
333 // AppListController,  aura::FocusObserver implementation:
334
335 void AppListController::OnWindowFocused(aura::Window* gained_focus,
336                                         aura::Window* lost_focus) {
337   if (view_ && is_visible_) {
338     aura::Window* applist_window = view_->GetWidget()->GetNativeView();
339     aura::Window* applist_container = applist_window->parent();
340
341     if (applist_container->Contains(lost_focus) &&
342         (!gained_focus || !applist_container->Contains(gained_focus))) {
343       SetVisible(false, applist_window);
344     }
345   }
346 }
347
348 ////////////////////////////////////////////////////////////////////////////////
349 // AppListController,  aura::WindowObserver implementation:
350 void AppListController::OnWindowBoundsChanged(aura::Window* root,
351                                               const gfx::Rect& old_bounds,
352                                               const gfx::Rect& new_bounds) {
353   UpdateBounds();
354 }
355
356 ////////////////////////////////////////////////////////////////////////////////
357 // AppListController, ui::ImplicitAnimationObserver implementation:
358
359 void AppListController::OnImplicitAnimationsCompleted() {
360   if (is_visible_ )
361     view_->GetWidget()->Activate();
362   else
363     view_->GetWidget()->Close();
364 }
365
366 ////////////////////////////////////////////////////////////////////////////////
367 // AppListController, views::WidgetObserver implementation:
368
369 void AppListController::OnWidgetDestroying(views::Widget* widget) {
370   DCHECK(view_->GetWidget() == widget);
371   if (is_visible_)
372     SetVisible(false, widget->GetNativeView());
373   ResetView();
374 }
375
376 ////////////////////////////////////////////////////////////////////////////////
377 // AppListController, ShellObserver implementation:
378 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
379   if (view_)
380     view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
381 }
382
383 ////////////////////////////////////////////////////////////////////////////////
384 // AppListController, ShelfIconObserver implementation:
385
386 void AppListController::OnShelfIconPositionsChanged() {
387   UpdateBounds();
388 }
389
390 ////////////////////////////////////////////////////////////////////////////////
391 // AppListController, PaginationModelObserver implementation:
392
393 void AppListController::TotalPagesChanged() {
394 }
395
396 void AppListController::SelectedPageChanged(int old_selected,
397                                             int new_selected) {
398 }
399
400 void AppListController::TransitionStarted() {
401 }
402
403 void AppListController::TransitionChanged() {
404   // |view_| could be NULL when app list is closed with a running transition.
405   if (!view_)
406     return;
407
408   const app_list::PaginationModel::Transition& transition =
409       pagination_model_->transition();
410   if (pagination_model_->is_valid_page(transition.target_page))
411     return;
412
413   views::Widget* widget = view_->GetWidget();
414   ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
415   if (!pagination_model_->IsRevertingCurrentTransition()) {
416     // Update cached |view_bounds_| if it is the first over-scroll move and
417     // widget does not have running animations.
418     if (!should_snap_back_ && !widget_animator->is_animating())
419       view_bounds_ = widget->GetWindowBoundsInScreen();
420
421     const int current_page = pagination_model_->selected_page();
422     const int dir = transition.target_page > current_page ? -1 : 1;
423
424     const double progress = 1.0 - pow(1.0 - transition.progress, 4);
425     const int shift = kMaxOverScrollShift * progress * dir;
426
427     gfx::Rect shifted(view_bounds_);
428     shifted.set_x(shifted.x() + shift);
429     widget->SetBounds(shifted);
430     should_snap_back_ = true;
431   } else if (should_snap_back_) {
432     should_snap_back_ = false;
433     ui::ScopedLayerAnimationSettings animation(widget_animator);
434     animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
435         app_list::kOverscrollPageTransitionDurationMs));
436     widget->SetBounds(view_bounds_);
437   }
438 }
439
440 }  // namespace internal
441 }  // namespace ash