- add sources.
[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/launcher/launcher.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/screen_ash.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 launcher.
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::ForLauncher(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 = Launcher::ForWindow(container)->
171           GetAppListButtonView()->GetBoundsInScreen();
172       // We need the location of the button within the local screen.
173       applist_button_bounds = ash::ScreenAsh::ConvertRectFromScreen(
174           root_window,
175           applist_button_bounds);
176       view->InitAsBubbleAttachedToAnchor(
177           container,
178           pagination_model_.get(),
179           Launcher::ForWindow(container)->GetAppListButtonView(),
180           GetAnchorPositionOffsetToShelf(applist_button_bounds,
181               Launcher::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           Launcher::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             ash::switches::kAshDisableDragAndDropAppListToLauncher)) {
200       SetDragAndDropHostOfCurrentAppList(
201           Launcher::ForWindow(window)->GetDragAndDropHostForAppList());
202     }
203   }
204   // Update applist button status when app list visibility is changed.
205   Launcher::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   Launcher::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   Launcher::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           ash::internal::kShellWindowId_MenuContainer);
291       if (menu_container->Contains(target))
292         return;
293     }
294   }
295
296   if (view_ && is_visible_) {
297     aura::Window* window = view_->GetWidget()->GetNativeView();
298     gfx::Point window_local_point(event->root_location());
299     aura::Window::ConvertPointToTarget(window->GetRootWindow(),
300                                        window,
301                                        &window_local_point);
302     // Use HitTest to respect the hit test mask of the bubble.
303     if (!window->HitTest(window_local_point))
304       SetVisible(false, window);
305   }
306 }
307
308 void AppListController::UpdateBounds() {
309   if (view_ && is_visible_)
310     view_->UpdateBounds();
311 }
312
313 ////////////////////////////////////////////////////////////////////////////////
314 // AppListController, aura::EventFilter implementation:
315
316 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
317   if (event->type() == ui::ET_MOUSE_PRESSED)
318     ProcessLocatedEvent(event);
319 }
320
321 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
322   if (event->type() == ui::ET_GESTURE_TAP_DOWN)
323     ProcessLocatedEvent(event);
324 }
325
326 ////////////////////////////////////////////////////////////////////////////////
327 // AppListController,  aura::FocusObserver implementation:
328
329 void AppListController::OnWindowFocused(aura::Window* gained_focus,
330                                         aura::Window* lost_focus) {
331   if (view_ && is_visible_) {
332     aura::Window* applist_window = view_->GetWidget()->GetNativeView();
333     aura::Window* applist_container = applist_window->parent();
334
335     if (applist_container->Contains(lost_focus) &&
336         (!gained_focus || !applist_container->Contains(gained_focus))) {
337       SetVisible(false, applist_window);
338     }
339   }
340 }
341
342 ////////////////////////////////////////////////////////////////////////////////
343 // AppListController,  aura::WindowObserver implementation:
344 void AppListController::OnWindowBoundsChanged(aura::Window* root,
345                                               const gfx::Rect& old_bounds,
346                                               const gfx::Rect& new_bounds) {
347   UpdateBounds();
348 }
349
350 ////////////////////////////////////////////////////////////////////////////////
351 // AppListController, ui::ImplicitAnimationObserver implementation:
352
353 void AppListController::OnImplicitAnimationsCompleted() {
354   if (is_visible_ )
355     view_->GetWidget()->Activate();
356   else
357     view_->GetWidget()->Close();
358 }
359
360 ////////////////////////////////////////////////////////////////////////////////
361 // AppListController, views::WidgetObserver implementation:
362
363 void AppListController::OnWidgetDestroying(views::Widget* widget) {
364   DCHECK(view_->GetWidget() == widget);
365   if (is_visible_)
366     SetVisible(false, widget->GetNativeView());
367   ResetView();
368 }
369
370 ////////////////////////////////////////////////////////////////////////////////
371 // AppListController, ShellObserver implementation:
372 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
373   if (view_)
374     view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
375 }
376
377 ////////////////////////////////////////////////////////////////////////////////
378 // AppListController, ShelfIconObserver implementation:
379
380 void AppListController::OnShelfIconPositionsChanged() {
381   UpdateBounds();
382 }
383
384 ////////////////////////////////////////////////////////////////////////////////
385 // AppListController, PaginationModelObserver implementation:
386
387 void AppListController::TotalPagesChanged() {
388 }
389
390 void AppListController::SelectedPageChanged(int old_selected,
391                                             int new_selected) {
392 }
393
394 void AppListController::TransitionStarted() {
395 }
396
397 void AppListController::TransitionChanged() {
398   // |view_| could be NULL when app list is closed with a running transition.
399   if (!view_)
400     return;
401
402   const app_list::PaginationModel::Transition& transition =
403       pagination_model_->transition();
404   if (pagination_model_->is_valid_page(transition.target_page))
405     return;
406
407   views::Widget* widget = view_->GetWidget();
408   ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
409   if (!pagination_model_->IsRevertingCurrentTransition()) {
410     // Update cached |view_bounds_| if it is the first over-scroll move and
411     // widget does not have running animations.
412     if (!should_snap_back_ && !widget_animator->is_animating())
413       view_bounds_ = widget->GetWindowBoundsInScreen();
414
415     const int current_page = pagination_model_->selected_page();
416     const int dir = transition.target_page > current_page ? -1 : 1;
417
418     const double progress = 1.0 - pow(1.0 - transition.progress, 4);
419     const int shift = kMaxOverScrollShift * progress * dir;
420
421     gfx::Rect shifted(view_bounds_);
422     shifted.set_x(shifted.x() + shift);
423     widget->SetBounds(shifted);
424     should_snap_back_ = true;
425   } else if (should_snap_back_) {
426     should_snap_back_ = false;
427     ui::ScopedLayerAnimationSettings animation(widget_animator);
428     animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
429         app_list::kOverscrollPageTransitionDurationMs));
430     widget->SetBounds(view_bounds_);
431   }
432 }
433
434 }  // namespace internal
435 }  // namespace ash