Mailbox support for texture layers.
[chromium-blink-merge.git] / ash / wm / app_list_controller.cc
blob1d402ceec8b247ce7184c6495fb121f9f07b9325
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/launcher/launcher.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/shell.h"
10 #include "ash/shell_delegate.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/wm/property_util.h"
13 #include "ash/wm/shelf_layout_manager.h"
14 #include "ui/app_list/app_list_constants.h"
15 #include "ui/app_list/app_list_view.h"
16 #include "ui/app_list/pagination_model.h"
17 #include "ui/aura/client/focus_client.h"
18 #include "ui/aura/root_window.h"
19 #include "ui/aura/window.h"
20 #include "ui/base/events/event.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/compositor/scoped_layer_animation_settings.h"
23 #include "ui/gfx/transform_util.h"
24 #include "ui/views/widget/widget.h"
26 namespace ash {
27 namespace internal {
29 namespace {
31 // Duration for show/hide animation in milliseconds.
32 const int kAnimationDurationMs = 200;
34 // Offset in pixels to animation away/towards the launcher.
35 const int kAnimationOffset = 8;
37 // The maximum shift in pixels when over-scroll happens.
38 const int kMaxOverScrollShift = 48;
40 ui::Layer* GetLayer(views::Widget* widget) {
41 return widget->GetNativeView()->layer();
44 // Gets arrow location based on shelf alignment.
45 views::BubbleBorder::ArrowLocation GetBubbleArrowLocation(
46 aura::Window* window) {
47 DCHECK(Shell::HasInstance());
48 return ShelfLayoutManager::ForLauncher(window)->
49 SelectValueForShelfAlignment(
50 views::BubbleBorder::BOTTOM_CENTER,
51 views::BubbleBorder::LEFT_CENTER,
52 views::BubbleBorder::RIGHT_CENTER);
55 // Offset given |rect| towards shelf.
56 gfx::Rect OffsetTowardsShelf(const gfx::Rect& rect, views::Widget* widget) {
57 DCHECK(Shell::HasInstance());
58 ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment(
59 widget->GetNativeView()->GetRootWindow());
60 gfx::Rect offseted(rect);
61 switch (shelf_alignment) {
62 case SHELF_ALIGNMENT_BOTTOM:
63 offseted.Offset(0, kAnimationOffset);
64 break;
65 case SHELF_ALIGNMENT_LEFT:
66 offseted.Offset(-kAnimationOffset, 0);
67 break;
68 case SHELF_ALIGNMENT_RIGHT:
69 offseted.Offset(kAnimationOffset, 0);
70 break;
71 default:
72 NOTREACHED() << "Unknown shelf alignment " << shelf_alignment;
73 break;
76 return offseted;
79 } // namespace
81 ////////////////////////////////////////////////////////////////////////////////
82 // AppListController, public:
84 AppListController::AppListController()
85 : pagination_model_(new app_list::PaginationModel),
86 is_visible_(false),
87 view_(NULL),
88 should_snap_back_(false) {
89 Shell::GetInstance()->AddShellObserver(this);
90 pagination_model_->AddObserver(this);
93 AppListController::~AppListController() {
94 // Ensures app list view goes before the controller since pagination model
95 // lives in the controller and app list view would access it on destruction.
96 if (view_ && view_->GetWidget())
97 view_->GetWidget()->CloseNow();
99 Shell::GetInstance()->RemoveShellObserver(this);
100 pagination_model_->RemoveObserver(this);
103 void AppListController::SetVisible(bool visible, aura::Window* window) {
104 if (visible == is_visible_)
105 return;
107 is_visible_ = visible;
109 // App list needs to know the new shelf layout in order to calculate its
110 // UI layout when AppListView visibility changes.
111 Shell::GetPrimaryRootWindowController()->shelf()->UpdateAutoHideState();
113 if (view_) {
114 ScheduleAnimation();
115 } else if (is_visible_) {
116 // AppListModel and AppListViewDelegate are owned by AppListView. They
117 // will be released with AppListView on close.
118 app_list::AppListView* view = new app_list::AppListView(
119 Shell::GetInstance()->delegate()->CreateAppListViewDelegate());
120 aura::Window* container = GetRootWindowController(window->GetRootWindow())->
121 GetContainer(kShellWindowId_AppListContainer);
122 view->InitAsBubble(
123 container,
124 pagination_model_.get(),
125 Launcher::ForWindow(container)->GetAppListButtonView(),
126 gfx::Point(),
127 GetBubbleArrowLocation(container));
128 SetView(view);
132 bool AppListController::IsVisible() const {
133 return view_ && view_->GetWidget()->IsVisible();
136 aura::Window* AppListController::GetWindow() {
137 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
140 ////////////////////////////////////////////////////////////////////////////////
141 // AppListController, private:
143 void AppListController::SetView(app_list::AppListView* view) {
144 DCHECK(view_ == NULL);
145 DCHECK(is_visible_);
147 view_ = view;
148 views::Widget* widget = view_->GetWidget();
149 widget->AddObserver(this);
150 Shell::GetInstance()->AddPreTargetHandler(this);
151 Launcher::ForWindow(widget->GetNativeWindow())->AddIconObserver(this);
152 widget->GetNativeView()->GetRootWindow()->AddRootWindowObserver(this);
153 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this);
155 view_->ShowWhenReady();
158 void AppListController::ResetView() {
159 if (!view_)
160 return;
162 views::Widget* widget = view_->GetWidget();
163 widget->RemoveObserver(this);
164 GetLayer(widget)->GetAnimator()->RemoveObserver(this);
165 Shell::GetInstance()->RemovePreTargetHandler(this);
166 Launcher::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this);
167 widget->GetNativeView()->GetRootWindow()->RemoveRootWindowObserver(this);
168 aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this);
169 view_ = NULL;
172 void AppListController::ScheduleAnimation() {
173 // Stop observing previous animation.
174 StopObservingImplicitAnimations();
176 views::Widget* widget = view_->GetWidget();
177 ui::Layer* layer = GetLayer(widget);
178 layer->GetAnimator()->StopAnimating();
180 gfx::Rect target_bounds;
181 if (is_visible_) {
182 target_bounds = widget->GetWindowBoundsInScreen();
183 widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
184 } else {
185 target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
186 widget);
189 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
190 animation.SetTransitionDuration(
191 base::TimeDelta::FromMilliseconds(
192 is_visible_ ? 0 : kAnimationDurationMs));
193 animation.AddObserver(this);
195 layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
196 widget->SetBounds(target_bounds);
199 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
200 // If the event happened on a menu, then the event should not close the app
201 // list.
202 aura::Window* target = static_cast<aura::Window*>(event->target());
203 if (target) {
204 RootWindowController* root_controller =
205 GetRootWindowController(target->GetRootWindow());
206 if (root_controller) {
207 aura::Window* menu_container = root_controller->GetContainer(
208 ash::internal::kShellWindowId_MenuContainer);
209 if (menu_container->Contains(target))
210 return;
214 if (view_ && is_visible_) {
215 aura::Window* window = view_->GetWidget()->GetNativeView();
216 gfx::Point window_local_point(event->root_location());
217 aura::Window::ConvertPointToTarget(window->GetRootWindow(),
218 window,
219 &window_local_point);
220 // Use HitTest to respect the hit test mask of the bubble.
221 if (!window->HitTest(window_local_point))
222 SetVisible(false, window);
226 void AppListController::UpdateBounds() {
227 if (view_ && is_visible_)
228 view_->UpdateBounds();
231 ////////////////////////////////////////////////////////////////////////////////
232 // AppListController, aura::EventFilter implementation:
234 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
235 if (event->type() == ui::ET_MOUSE_PRESSED)
236 ProcessLocatedEvent(event);
239 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
240 if (event->type() == ui::ET_GESTURE_TAP)
241 ProcessLocatedEvent(event);
244 ////////////////////////////////////////////////////////////////////////////////
245 // AppListController, aura::FocusObserver implementation:
247 void AppListController::OnWindowFocused(aura::Window* gained_focus,
248 aura::Window* lost_focus) {
249 if (gained_focus && view_ && is_visible_) {
250 aura::Window* applist_container =
251 GetRootWindowController(gained_focus->GetRootWindow())->GetContainer(
252 kShellWindowId_AppListContainer);
253 if (gained_focus->parent() != applist_container)
254 SetVisible(false, gained_focus);
258 ////////////////////////////////////////////////////////////////////////////////
259 // AppListController, aura::RootWindowObserver implementation:
260 void AppListController::OnRootWindowResized(const aura::RootWindow* root,
261 const gfx::Size& old_size) {
262 UpdateBounds();
265 ////////////////////////////////////////////////////////////////////////////////
266 // AppListController, ui::ImplicitAnimationObserver implementation:
268 void AppListController::OnImplicitAnimationsCompleted() {
269 if (is_visible_ )
270 view_->GetWidget()->Activate();
271 else
272 view_->GetWidget()->Close();
275 ////////////////////////////////////////////////////////////////////////////////
276 // AppListController, views::WidgetObserver implementation:
278 void AppListController::OnWidgetClosing(views::Widget* widget) {
279 DCHECK(view_->GetWidget() == widget);
280 if (is_visible_)
281 SetVisible(false, widget->GetNativeView());
282 ResetView();
285 ////////////////////////////////////////////////////////////////////////////////
286 // AppListController, ShellObserver implementation:
287 void AppListController::OnShelfAlignmentChanged(aura::RootWindow* root_window) {
288 if (view_) {
289 view_->SetBubbleArrowLocation(GetBubbleArrowLocation(
290 view_->GetWidget()->GetNativeView()));
294 ////////////////////////////////////////////////////////////////////////////////
295 // AppListController, LauncherIconObserver implementation:
297 void AppListController::OnLauncherIconPositionsChanged() {
298 UpdateBounds();
301 ////////////////////////////////////////////////////////////////////////////////
302 // AppListController, PaginationModelObserver implementation:
304 void AppListController::TotalPagesChanged() {
307 void AppListController::SelectedPageChanged(int old_selected,
308 int new_selected) {
311 void AppListController::TransitionChanged() {
312 // |view_| could be NULL when app list is closed with a running transition.
313 if (!view_)
314 return;
316 const app_list::PaginationModel::Transition& transition =
317 pagination_model_->transition();
318 if (pagination_model_->is_valid_page(transition.target_page))
319 return;
321 views::Widget* widget = view_->GetWidget();
322 ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
323 if (!pagination_model_->IsRevertingCurrentTransition()) {
324 // Update cached |view_bounds_| if it is the first over-scroll move and
325 // widget does not have running animations.
326 if (!should_snap_back_ && !widget_animator->is_animating())
327 view_bounds_ = widget->GetWindowBoundsInScreen();
329 const int current_page = pagination_model_->selected_page();
330 const int dir = transition.target_page > current_page ? -1 : 1;
332 const double progress = 1.0 - pow(1.0 - transition.progress, 4);
333 const int shift = kMaxOverScrollShift * progress * dir;
335 gfx::Rect shifted(view_bounds_);
336 shifted.set_x(shifted.x() + shift);
337 widget->SetBounds(shifted);
338 should_snap_back_ = true;
339 } else if (should_snap_back_) {
340 should_snap_back_ = false;
341 ui::ScopedLayerAnimationSettings animation(widget_animator);
342 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
343 app_list::kOverscrollPageTransitionDurationMs));
344 widget->SetBounds(view_bounds_);
348 } // namespace internal
349 } // namespace ash