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/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"
33 // Duration for show/hide animation in milliseconds.
34 const int kAnimationDurationMs = 200;
36 // Offset in pixels to animation away/towards the launcher.
37 const int kAnimationOffset = 8;
39 // The maximum shift in pixels when over-scroll happens.
40 const int kMaxOverScrollShift = 48;
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;
47 ui::Layer* GetLayer(views::Widget* widget) {
48 return widget->GetNativeView()->layer();
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);
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);
72 case SHELF_ALIGNMENT_LEFT:
73 offseted.Offset(-kAnimationOffset, 0);
75 case SHELF_ALIGNMENT_RIGHT:
76 offseted.Offset(kAnimationOffset, 0);
78 case SHELF_ALIGNMENT_TOP:
79 offseted.Offset(0, -kAnimationOffset);
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();
100 std::min(screen_width - kMinimalAnchorPositionOffset - anchor.x(),
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));
113 return gfx::Vector2d();
119 ////////////////////////////////////////////////////////////////////////////////
120 // AppListController, public:
122 AppListController::AppListController()
123 : pagination_model_(new app_list::PaginationModel),
126 should_snap_back_(false) {
127 Shell::GetInstance()->AddShellObserver(this);
128 pagination_model_->AddObserver(this);
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();
137 Shell::GetInstance()->RemoveShellObserver(this);
138 pagination_model_->RemoveObserver(this);
141 void AppListController::SetVisible(bool visible, aura::Window* window) {
142 if (visible == is_visible_)
145 is_visible_ = visible;
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();
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.
159 view_->GetWidget()->Deactivate();
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(
175 applist_button_bounds);
176 view->InitAsBubbleAttachedToAnchor(
178 pagination_model_.get(),
179 Launcher::ForWindow(container)->GetAppListButtonView(),
180 GetAnchorPositionOffsetToShelf(applist_button_bounds,
181 Launcher::ForWindow(container)->GetAppListButtonView()->
183 GetBubbleArrow(container),
184 true /* border_accepts_events */);
185 view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
187 view->InitAsBubbleAttachedToAnchor(
189 pagination_model_.get(),
190 Launcher::ForWindow(container)->GetAppListButtonView(),
192 GetBubbleArrow(container),
193 true /* border_accepts_events */);
196 // By setting us as DnD recipient, the app list knows that we can
198 if (!CommandLine::ForCurrentProcess()->HasSwitch(
199 ash::switches::kAshDisableDragAndDropAppListToLauncher)) {
200 SetDragAndDropHostOfCurrentAppList(
201 Launcher::ForWindow(window)->GetDragAndDropHostForAppList());
204 // Update applist button status when app list visibility is changed.
205 Launcher::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
208 bool AppListController::IsVisible() const {
209 return view_ && view_->GetWidget()->IsVisible();
212 aura::Window* AppListController::GetWindow() {
213 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
216 ////////////////////////////////////////////////////////////////////////////////
217 // AppListController, private:
219 void AppListController::SetDragAndDropHostOfCurrentAppList(
220 app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
221 if (view_ && is_visible_)
222 view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
225 void AppListController::SetView(app_list::AppListView* view) {
226 DCHECK(view_ == NULL);
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);
237 view_->ShowWhenReady();
240 void AppListController::ResetView() {
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);
254 void AppListController::ScheduleAnimation() {
255 // Stop observing previous animation.
256 StopObservingImplicitAnimations();
258 views::Widget* widget = view_->GetWidget();
259 ui::Layer* layer = GetLayer(widget);
260 layer->GetAnimator()->StopAnimating();
262 gfx::Rect target_bounds;
264 target_bounds = widget->GetWindowBoundsInScreen();
265 widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
267 target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
271 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
272 animation.SetTransitionDuration(
273 base::TimeDelta::FromMilliseconds(
274 is_visible_ ? 0 : kAnimationDurationMs));
275 animation.AddObserver(this);
277 layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
278 widget->SetBounds(target_bounds);
281 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
282 // If the event happened on a menu, then the event should not close the app
284 aura::Window* target = static_cast<aura::Window*>(event->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))
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(),
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);
308 void AppListController::UpdateBounds() {
309 if (view_ && is_visible_)
310 view_->UpdateBounds();
313 ////////////////////////////////////////////////////////////////////////////////
314 // AppListController, aura::EventFilter implementation:
316 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
317 if (event->type() == ui::ET_MOUSE_PRESSED)
318 ProcessLocatedEvent(event);
321 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
322 if (event->type() == ui::ET_GESTURE_TAP_DOWN)
323 ProcessLocatedEvent(event);
326 ////////////////////////////////////////////////////////////////////////////////
327 // AppListController, aura::FocusObserver implementation:
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();
335 if (applist_container->Contains(lost_focus) &&
336 (!gained_focus || !applist_container->Contains(gained_focus))) {
337 SetVisible(false, applist_window);
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) {
350 ////////////////////////////////////////////////////////////////////////////////
351 // AppListController, ui::ImplicitAnimationObserver implementation:
353 void AppListController::OnImplicitAnimationsCompleted() {
355 view_->GetWidget()->Activate();
357 view_->GetWidget()->Close();
360 ////////////////////////////////////////////////////////////////////////////////
361 // AppListController, views::WidgetObserver implementation:
363 void AppListController::OnWidgetDestroying(views::Widget* widget) {
364 DCHECK(view_->GetWidget() == widget);
366 SetVisible(false, widget->GetNativeView());
370 ////////////////////////////////////////////////////////////////////////////////
371 // AppListController, ShellObserver implementation:
372 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
374 view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
377 ////////////////////////////////////////////////////////////////////////////////
378 // AppListController, ShelfIconObserver implementation:
380 void AppListController::OnShelfIconPositionsChanged() {
384 ////////////////////////////////////////////////////////////////////////////////
385 // AppListController, PaginationModelObserver implementation:
387 void AppListController::TotalPagesChanged() {
390 void AppListController::SelectedPageChanged(int old_selected,
394 void AppListController::TransitionStarted() {
397 void AppListController::TransitionChanged() {
398 // |view_| could be NULL when app list is closed with a running transition.
402 const app_list::PaginationModel::Transition& transition =
403 pagination_model_->transition();
404 if (pagination_model_->is_valid_page(transition.target_page))
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();
415 const int current_page = pagination_model_->selected_page();
416 const int dir = transition.target_page > current_page ? -1 : 1;
418 const double progress = 1.0 - pow(1.0 - transition.progress, 4);
419 const int shift = kMaxOverScrollShift * progress * dir;
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_);
434 } // namespace internal