Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ash / wm / app_list_controller.cc
blob6839cc4579ada864f193b4cb486a7ec6cac77906
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/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/keyboard/keyboard_controller.h"
28 #include "ui/views/widget/widget.h"
30 namespace ash {
31 namespace {
33 // Duration for show/hide animation in milliseconds.
34 const int kAnimationDurationMs = 200;
36 // Offset in pixels to animation away/towards the shelf.
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::ForShelf(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);
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;
83 return offseted;
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);
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();
117 // Gets the point at the center of the screen, excluding the virtual keyboard.
118 gfx::Point GetScreenCenter() {
119 gfx::Rect bounds = Shell::GetScreen()->GetPrimaryDisplay().bounds();
121 // If the virtual keyboard is active, subtract it from the display bounds, so
122 // that the app list is centered in the non-keyboard area of the display.
123 // (Note that work_area excludes the keyboard, but it doesn't get updated
124 // until after this function is called.)
125 keyboard::KeyboardController* keyboard_controller =
126 keyboard::KeyboardController::GetInstance();
127 if (keyboard_controller && keyboard_controller->keyboard_visible())
128 bounds.Subtract(keyboard_controller->current_keyboard_bounds());
130 return bounds.CenterPoint();
133 } // namespace
135 ////////////////////////////////////////////////////////////////////////////////
136 // AppListController, public:
138 AppListController::AppListController()
139 : pagination_model_(new app_list::PaginationModel),
140 is_visible_(false),
141 is_centered_(false),
142 view_(NULL),
143 should_snap_back_(false) {
144 Shell::GetInstance()->AddShellObserver(this);
145 pagination_model_->AddObserver(this);
148 AppListController::~AppListController() {
149 // Ensures app list view goes before the controller since pagination model
150 // lives in the controller and app list view would access it on destruction.
151 if (view_ && view_->GetWidget())
152 view_->GetWidget()->CloseNow();
154 Shell::GetInstance()->RemoveShellObserver(this);
155 pagination_model_->RemoveObserver(this);
158 void AppListController::SetVisible(bool visible, aura::Window* window) {
159 if (visible == is_visible_)
160 return;
162 is_visible_ = visible;
164 // App list needs to know the new shelf layout in order to calculate its
165 // UI layout when AppListView visibility changes.
166 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
167 UpdateAutoHideState();
169 if (view_) {
170 // Our widget is currently active. When the animation completes we'll hide
171 // the widget, changing activation. If a menu is shown before the animation
172 // completes then the activation change triggers the menu to close. By
173 // deactivating now we ensure there is no activation change when the
174 // animation completes and any menus stay open.
175 if (!visible)
176 view_->GetWidget()->Deactivate();
177 ScheduleAnimation();
178 } else if (is_visible_) {
179 // AppListModel and AppListViewDelegate are owned by AppListView. They
180 // will be released with AppListView on close.
181 app_list::AppListView* view = new app_list::AppListView(
182 Shell::GetInstance()->delegate()->CreateAppListViewDelegate());
183 aura::Window* root_window = window->GetRootWindow();
184 aura::Window* container = GetRootWindowController(root_window)->
185 GetContainer(kShellWindowId_AppListContainer);
186 is_centered_ = app_list::switches::IsCenteredAppListEnabled();
187 if (is_centered_) {
188 // The experimental app list is centered over the primary display.
189 view->InitAsBubbleAtFixedLocation(
190 NULL,
191 pagination_model_.get(),
192 GetScreenCenter(),
193 views::BubbleBorder::FLOAT,
194 true /* border_accepts_events */);
195 } else {
196 gfx::Rect applist_button_bounds = Shelf::ForWindow(container)->
197 GetAppListButtonView()->GetBoundsInScreen();
198 // We need the location of the button within the local screen.
199 applist_button_bounds = ScreenUtil::ConvertRectFromScreen(
200 root_window,
201 applist_button_bounds);
202 view->InitAsBubbleAttachedToAnchor(
203 container,
204 pagination_model_.get(),
205 Shelf::ForWindow(container)->GetAppListButtonView(),
206 GetAnchorPositionOffsetToShelf(applist_button_bounds,
207 Shelf::ForWindow(container)->GetAppListButtonView()->
208 GetWidget()),
209 GetBubbleArrow(container),
210 true /* border_accepts_events */);
211 view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
213 SetView(view);
214 // By setting us as DnD recipient, the app list knows that we can
215 // handle items.
216 SetDragAndDropHostOfCurrentAppList(
217 Shelf::ForWindow(window)->GetDragAndDropHostForAppList());
219 // Update applist button status when app list visibility is changed.
220 Shelf::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
223 bool AppListController::IsVisible() const {
224 return view_ && view_->GetWidget()->IsVisible();
227 aura::Window* AppListController::GetWindow() {
228 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
231 ////////////////////////////////////////////////////////////////////////////////
232 // AppListController, private:
234 void AppListController::SetDragAndDropHostOfCurrentAppList(
235 app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
236 if (view_ && is_visible_)
237 view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
240 void AppListController::SetView(app_list::AppListView* view) {
241 DCHECK(view_ == NULL);
242 DCHECK(is_visible_);
244 view_ = view;
245 views::Widget* widget = view_->GetWidget();
246 widget->AddObserver(this);
247 keyboard::KeyboardController* keyboard_controller =
248 keyboard::KeyboardController::GetInstance();
249 if (keyboard_controller)
250 keyboard_controller->AddObserver(this);
251 Shell::GetInstance()->AddPreTargetHandler(this);
252 Shelf::ForWindow(widget->GetNativeWindow())->AddIconObserver(this);
253 widget->GetNativeView()->GetRootWindow()->AddObserver(this);
254 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this);
256 view_->ShowWhenReady();
259 void AppListController::ResetView() {
260 if (!view_)
261 return;
263 views::Widget* widget = view_->GetWidget();
264 widget->RemoveObserver(this);
265 GetLayer(widget)->GetAnimator()->RemoveObserver(this);
266 keyboard::KeyboardController* keyboard_controller =
267 keyboard::KeyboardController::GetInstance();
268 if (keyboard_controller)
269 keyboard_controller->RemoveObserver(this);
270 Shell::GetInstance()->RemovePreTargetHandler(this);
271 Shelf::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this);
272 widget->GetNativeView()->GetRootWindow()->RemoveObserver(this);
273 aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this);
274 view_ = NULL;
277 void AppListController::ScheduleAnimation() {
278 // Stop observing previous animation.
279 StopObservingImplicitAnimations();
281 views::Widget* widget = view_->GetWidget();
282 ui::Layer* layer = GetLayer(widget);
283 layer->GetAnimator()->StopAnimating();
285 gfx::Rect target_bounds;
286 if (is_visible_) {
287 target_bounds = widget->GetWindowBoundsInScreen();
288 widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
289 } else {
290 target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
291 widget);
294 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
295 animation.SetTransitionDuration(
296 base::TimeDelta::FromMilliseconds(
297 is_visible_ ? 0 : kAnimationDurationMs));
298 animation.AddObserver(this);
300 layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
301 widget->SetBounds(target_bounds);
304 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
305 if (!view_ || !is_visible_)
306 return;
308 // If the event happened on a menu, then the event should not close the app
309 // list.
310 aura::Window* target = static_cast<aura::Window*>(event->target());
311 if (target) {
312 RootWindowController* root_controller =
313 GetRootWindowController(target->GetRootWindow());
314 if (root_controller) {
315 aura::Window* menu_container =
316 root_controller->GetContainer(kShellWindowId_MenuContainer);
317 if (menu_container->Contains(target))
318 return;
319 aura::Window* keyboard_container = root_controller->GetContainer(
320 kShellWindowId_VirtualKeyboardContainer);
321 if (keyboard_container->Contains(target))
322 return;
326 aura::Window* window = view_->GetWidget()->GetNativeView()->parent();
327 if (!window->Contains(target))
328 SetVisible(false, window);
331 void AppListController::UpdateBounds() {
332 if (!view_ || !is_visible_)
333 return;
335 view_->UpdateBounds();
337 if (is_centered_)
338 view_->SetAnchorPoint(GetScreenCenter());
341 ////////////////////////////////////////////////////////////////////////////////
342 // AppListController, aura::EventFilter implementation:
344 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
345 if (event->type() == ui::ET_MOUSE_PRESSED)
346 ProcessLocatedEvent(event);
349 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
350 if (event->type() == ui::ET_GESTURE_TAP_DOWN)
351 ProcessLocatedEvent(event);
354 ////////////////////////////////////////////////////////////////////////////////
355 // AppListController, aura::FocusObserver implementation:
357 void AppListController::OnWindowFocused(aura::Window* gained_focus,
358 aura::Window* lost_focus) {
359 if (view_ && is_visible_) {
360 aura::Window* applist_window = view_->GetWidget()->GetNativeView();
361 aura::Window* applist_container = applist_window->parent();
363 if (applist_container->Contains(lost_focus) &&
364 (!gained_focus || !applist_container->Contains(gained_focus))) {
365 SetVisible(false, applist_window);
370 ////////////////////////////////////////////////////////////////////////////////
371 // AppListController, aura::WindowObserver implementation:
372 void AppListController::OnWindowBoundsChanged(aura::Window* root,
373 const gfx::Rect& old_bounds,
374 const gfx::Rect& new_bounds) {
375 UpdateBounds();
378 ////////////////////////////////////////////////////////////////////////////////
379 // AppListController, ui::ImplicitAnimationObserver implementation:
381 void AppListController::OnImplicitAnimationsCompleted() {
382 if (is_visible_ )
383 view_->GetWidget()->Activate();
384 else
385 view_->GetWidget()->Close();
388 ////////////////////////////////////////////////////////////////////////////////
389 // AppListController, views::WidgetObserver implementation:
391 void AppListController::OnWidgetDestroying(views::Widget* widget) {
392 DCHECK(view_->GetWidget() == widget);
393 if (is_visible_)
394 SetVisible(false, widget->GetNativeView());
395 ResetView();
398 ////////////////////////////////////////////////////////////////////////////////
399 // AppListController, keyboard::KeyboardControllerObserver implementation:
401 void AppListController::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) {
402 UpdateBounds();
405 ////////////////////////////////////////////////////////////////////////////////
406 // AppListController, ShellObserver implementation:
407 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
408 if (view_)
409 view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
412 ////////////////////////////////////////////////////////////////////////////////
413 // AppListController, ShelfIconObserver implementation:
415 void AppListController::OnShelfIconPositionsChanged() {
416 UpdateBounds();
419 ////////////////////////////////////////////////////////////////////////////////
420 // AppListController, PaginationModelObserver implementation:
422 void AppListController::TotalPagesChanged() {
425 void AppListController::SelectedPageChanged(int old_selected,
426 int new_selected) {
429 void AppListController::TransitionStarted() {
432 void AppListController::TransitionChanged() {
433 // |view_| could be NULL when app list is closed with a running transition.
434 if (!view_)
435 return;
437 const app_list::PaginationModel::Transition& transition =
438 pagination_model_->transition();
439 if (pagination_model_->is_valid_page(transition.target_page))
440 return;
442 views::Widget* widget = view_->GetWidget();
443 ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
444 if (!pagination_model_->IsRevertingCurrentTransition()) {
445 // Update cached |view_bounds_| if it is the first over-scroll move and
446 // widget does not have running animations.
447 if (!should_snap_back_ && !widget_animator->is_animating())
448 view_bounds_ = widget->GetWindowBoundsInScreen();
450 const int current_page = pagination_model_->selected_page();
451 const int dir = transition.target_page > current_page ? -1 : 1;
453 const double progress = 1.0 - pow(1.0 - transition.progress, 4);
454 const int shift = kMaxOverScrollShift * progress * dir;
456 gfx::Rect shifted(view_bounds_);
457 shifted.set_x(shifted.x() + shift);
458 widget->SetBounds(shifted);
459 should_snap_back_ = true;
460 } else if (should_snap_back_) {
461 should_snap_back_ = false;
462 ui::ScopedLayerAnimationSettings animation(widget_animator);
463 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
464 app_list::kOverscrollPageTransitionDurationMs));
465 widget->SetBounds(view_bounds_);
469 } // namespace ash