Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: ash/wm/app_list_controller.cc

Issue 1770993002: wip: Refactoring Ash's AppListController, moving the bulk of the logic to chrome/browser/ui/ash/app… Base URL: https://chromium.googlesource.com/chromium/src.git@small_5_apps
Patch Set: Added a comment for PostTask in AppListServiceAsh. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "ash/wm/maximize_mode/maximize_mode_controller.h"
16 #include "base/command_line.h"
17 #include "ui/app_list/app_list_constants.h"
18 #include "ui/app_list/app_list_switches.h"
19 #include "ui/app_list/pagination_model.h"
20 #include "ui/app_list/views/app_list_view.h"
21 #include "ui/aura/client/focus_client.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/compositor/layer.h"
25 #include "ui/compositor/scoped_layer_animation_settings.h"
26 #include "ui/events/event.h"
27 #include "ui/gfx/transform_util.h"
28 #include "ui/keyboard/keyboard_controller.h"
29 #include "ui/views/widget/widget.h"
30
31 namespace ash {
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 // The minimal margin (in pixels) around the app list when in centered mode.
49 const int kMinimalCenteredAppListMargin = 10;
50
51 ui::Layer* GetLayer(views::Widget* widget) {
52 return widget->GetNativeView()->layer();
53 }
54
55 // Gets arrow location based on shelf alignment.
56 views::BubbleBorder::Arrow GetBubbleArrow(aura::Window* window) {
57 DCHECK(Shell::HasInstance());
58 return ShelfLayoutManager::ForShelf(window)->
59 SelectValueForShelfAlignment(
60 views::BubbleBorder::BOTTOM_CENTER,
61 views::BubbleBorder::LEFT_CENTER,
62 views::BubbleBorder::RIGHT_CENTER,
63 views::BubbleBorder::TOP_CENTER);
64 }
65
66 // Offset given |rect| towards shelf.
67 gfx::Rect OffsetTowardsShelf(const gfx::Rect& rect, views::Widget* widget) {
68 DCHECK(Shell::HasInstance());
69 ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment(
70 widget->GetNativeView()->GetRootWindow());
71 gfx::Rect offseted(rect);
72 switch (shelf_alignment) {
73 case SHELF_ALIGNMENT_BOTTOM:
74 offseted.Offset(0, kAnimationOffset);
75 break;
76 case SHELF_ALIGNMENT_LEFT:
77 offseted.Offset(-kAnimationOffset, 0);
78 break;
79 case SHELF_ALIGNMENT_RIGHT:
80 offseted.Offset(kAnimationOffset, 0);
81 break;
82 case SHELF_ALIGNMENT_TOP:
83 offseted.Offset(0, -kAnimationOffset);
84 break;
85 }
86
87 return offseted;
88 }
89
90 // Using |button_bounds|, determine the anchor offset so that the bubble gets
91 // shown above the shelf (used for the alternate shelf theme).
92 gfx::Vector2d GetAnchorPositionOffsetToShelf(
93 const gfx::Rect& button_bounds, views::Widget* widget) {
94 DCHECK(Shell::HasInstance());
95 ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment(
96 widget->GetNativeView()->GetRootWindow());
97 gfx::Point anchor(button_bounds.CenterPoint());
98 switch (shelf_alignment) {
99 case SHELF_ALIGNMENT_TOP:
100 case SHELF_ALIGNMENT_BOTTOM:
101 if (base::i18n::IsRTL()) {
102 int screen_width = widget->GetWorkAreaBoundsInScreen().width();
103 return gfx::Vector2d(
104 std::min(screen_width - kMinimalAnchorPositionOffset - anchor.x(),
105 0), 0);
106 }
107 return gfx::Vector2d(
108 std::max(kMinimalAnchorPositionOffset - anchor.x(), 0), 0);
109 case SHELF_ALIGNMENT_LEFT:
110 return gfx::Vector2d(
111 0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0));
112 case SHELF_ALIGNMENT_RIGHT:
113 return gfx::Vector2d(
114 0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0));
115 default:
116 NOTREACHED();
117 return gfx::Vector2d();
118 }
119 }
120
121 // Gets the point at the center of the display that a particular view is on.
122 // This calculation excludes the virtual keyboard area. If the height of the
123 // display area is less than |minimum_height|, its bottom will be extended to
124 // that height (so that the app list never starts above the top of the screen).
125 gfx::Point GetCenterOfDisplayForView(const views::View* view,
126 int minimum_height) {
127 aura::Window* window = view->GetWidget()->GetNativeView();
128 gfx::Rect bounds = ScreenUtil::GetShelfDisplayBoundsInRoot(window);
129 bounds = ScreenUtil::ConvertRectToScreen(window->GetRootWindow(), bounds);
130
131 // If the virtual keyboard is active, subtract it from the display bounds, so
132 // that the app list is centered in the non-keyboard area of the display.
133 // (Note that work_area excludes the keyboard, but it doesn't get updated
134 // until after this function is called.)
135 keyboard::KeyboardController* keyboard_controller =
136 keyboard::KeyboardController::GetInstance();
137 if (keyboard_controller && keyboard_controller->keyboard_visible())
138 bounds.Subtract(keyboard_controller->current_keyboard_bounds());
139
140 // Apply the |minimum_height|.
141 if (bounds.height() < minimum_height)
142 bounds.set_height(minimum_height);
143
144 return bounds.CenterPoint();
145 }
146
147 // Gets the minimum height of the rectangle to center the app list in.
148 int GetMinimumBoundsHeightForAppList(const app_list::AppListView* app_list) {
149 return app_list->bounds().height() + 2 * kMinimalCenteredAppListMargin;
150 }
151
152 bool IsFullscreenAppListEnabled() {
153 #if defined(OS_CHROMEOS)
154 return base::CommandLine::ForCurrentProcess()->HasSwitch(
155 switches::kAshEnableFullscreenAppList) &&
156 app_list::switches::IsExperimentalAppListEnabled();
157 #else
158 return false;
159 #endif
160 }
161
162 } // namespace
163
164 ////////////////////////////////////////////////////////////////////////////////
165 // AppListController, public:
166
167 AppListController::AppListController()
168 : is_visible_(false),
169 is_centered_(false),
170 view_(NULL),
171 current_apps_page_(-1),
172 should_snap_back_(false) {
173 Shell::GetInstance()->AddShellObserver(this);
174 }
175
176 AppListController::~AppListController() {
177 // Ensures app list view goes before the controller since pagination model
178 // lives in the controller and app list view would access it on destruction.
179 if (view_) {
180 view_->GetAppsPaginationModel()->RemoveObserver(this);
181 if (view_->GetWidget())
182 view_->GetWidget()->CloseNow();
183 }
184
185 Shell::GetInstance()->RemoveShellObserver(this);
186 }
187
188 void AppListController::Show(aura::Window* window) {
189 if (is_visible_)
190 return;
191
192 is_visible_ = true;
193
194 // App list needs to know the new shelf layout in order to calculate its
195 // UI layout when AppListView visibility changes.
196 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()->
197 UpdateAutoHideState();
198
199 if (view_) {
200 ScheduleAnimation();
201 } else {
202 // Note the AppListViewDelegate outlives the AppListView. For Ash, the view
203 // is destroyed when dismissed.
204 app_list::AppListView* view = new app_list::AppListView(
205 Shell::GetInstance()->delegate()->GetAppListViewDelegate());
206 aura::Window* root_window = window->GetRootWindow();
207 aura::Window* container = GetRootWindowController(root_window)->
208 GetContainer(kShellWindowId_AppListContainer);
209 views::View* applist_button =
210 Shelf::ForWindow(container)->GetAppListButtonView();
211 is_centered_ = view->ShouldCenterWindow();
212 bool is_fullscreen = IsFullscreenAppListEnabled() &&
213 Shell::GetInstance()
214 ->maximize_mode_controller()
215 ->IsMaximizeModeWindowManagerEnabled();
216 if (is_fullscreen) {
217 view->InitAsFramelessWindow(
218 container, current_apps_page_,
219 ScreenUtil::GetDisplayWorkAreaBoundsInParent(container));
220 } else if (is_centered_) {
221 // Note: We can't center the app list until we have its dimensions, so we
222 // init at (0, 0) and then reset its anchor point.
223 view->InitAsBubbleAtFixedLocation(container,
224 current_apps_page_,
225 gfx::Point(),
226 views::BubbleBorder::FLOAT,
227 true /* border_accepts_events */);
228 // The experimental app list is centered over the display of the app list
229 // button that was pressed (if triggered via keyboard, this is the display
230 // with the currently focused window).
231 view->SetAnchorPoint(GetCenterOfDisplayForView(
232 applist_button, GetMinimumBoundsHeightForAppList(view)));
233 } else {
234 gfx::Rect applist_button_bounds = applist_button->GetBoundsInScreen();
235 // We need the location of the button within the local screen.
236 applist_button_bounds = ScreenUtil::ConvertRectFromScreen(
237 root_window,
238 applist_button_bounds);
239 view->InitAsBubbleAttachedToAnchor(
240 container,
241 current_apps_page_,
242 Shelf::ForWindow(container)->GetAppListButtonView(),
243 GetAnchorPositionOffsetToShelf(
244 applist_button_bounds,
245 Shelf::ForWindow(container)->GetAppListButtonView()->GetWidget()),
246 GetBubbleArrow(container),
247 true /* border_accepts_events */);
248 view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
249 }
250 SetView(view);
251 // By setting us as DnD recipient, the app list knows that we can
252 // handle items.
253 SetDragAndDropHostOfCurrentAppList(
254 Shelf::ForWindow(window)->GetDragAndDropHostForAppList());
255 }
256 // Update applist button status when app list visibility is changed.
257 Shelf::ForWindow(window)->GetAppListButtonView()->SchedulePaint();
258 }
259
260 void AppListController::Dismiss() {
261 if (!is_visible_)
262 return;
263
264 // If the app list is currently visible, there should be an existing view.
265 DCHECK(view_);
266
267 is_visible_ = false;
268
269 // App list needs to know the new shelf layout in order to calculate its
270 // UI layout when AppListView visibility changes.
271 Shell::GetPrimaryRootWindowController()
272 ->GetShelfLayoutManager()
273 ->UpdateAutoHideState();
274
275 // Our widget is currently active. When the animation completes we'll hide
276 // the widget, changing activation. If a menu is shown before the animation
277 // completes then the activation change triggers the menu to close. By
278 // deactivating now we ensure there is no activation change when the
279 // animation completes and any menus stay open.
280 view_->GetWidget()->Deactivate();
281 ScheduleAnimation();
282
283 // Update applist button status when app list visibility is changed.
284 Shelf::ForWindow(view_->GetWidget()->GetNativeView())
285 ->GetAppListButtonView()
286 ->SchedulePaint();
287 }
288
289 bool AppListController::IsVisible() const {
290 return view_ && view_->GetWidget()->IsVisible();
291 }
292
293 aura::Window* AppListController::GetWindow() {
294 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL;
295 }
296
297 ////////////////////////////////////////////////////////////////////////////////
298 // AppListController, private:
299
300 void AppListController::SetDragAndDropHostOfCurrentAppList(
301 app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
302 if (view_ && is_visible_)
303 view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
304 }
305
306 void AppListController::SetView(app_list::AppListView* view) {
307 DCHECK(view_ == NULL);
308 DCHECK(is_visible_);
309
310 view_ = view;
311 views::Widget* widget = view_->GetWidget();
312 widget->AddObserver(this);
313 keyboard::KeyboardController* keyboard_controller =
314 keyboard::KeyboardController::GetInstance();
315 if (keyboard_controller)
316 keyboard_controller->AddObserver(this);
317 Shell::GetInstance()->AddPreTargetHandler(this);
318 Shelf::ForWindow(widget->GetNativeWindow())->AddIconObserver(this);
319 widget->GetNativeView()->GetRootWindow()->AddObserver(this);
320 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this);
321
322 view_->GetAppsPaginationModel()->AddObserver(this);
323
324 view_->ShowWhenReady();
325 }
326
327 void AppListController::ResetView() {
328 if (!view_)
329 return;
330
331 views::Widget* widget = view_->GetWidget();
332 widget->RemoveObserver(this);
333 GetLayer(widget)->GetAnimator()->RemoveObserver(this);
334 keyboard::KeyboardController* keyboard_controller =
335 keyboard::KeyboardController::GetInstance();
336 if (keyboard_controller)
337 keyboard_controller->RemoveObserver(this);
338 Shell::GetInstance()->RemovePreTargetHandler(this);
339 Shelf::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this);
340 widget->GetNativeView()->GetRootWindow()->RemoveObserver(this);
341 aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this);
342
343 view_->GetAppsPaginationModel()->RemoveObserver(this);
344
345 view_ = NULL;
346 }
347
348 void AppListController::ScheduleAnimation() {
349 // Stop observing previous animation.
350 StopObservingImplicitAnimations();
351
352 views::Widget* widget = view_->GetWidget();
353 ui::Layer* layer = GetLayer(widget);
354 layer->GetAnimator()->StopAnimating();
355
356 gfx::Rect target_bounds;
357 if (is_visible_) {
358 target_bounds = widget->GetWindowBoundsInScreen();
359 widget->SetBounds(OffsetTowardsShelf(target_bounds, widget));
360 } else {
361 target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(),
362 widget);
363 }
364
365 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator());
366 animation.SetTransitionDuration(
367 base::TimeDelta::FromMilliseconds(
368 is_visible_ ? 0 : kAnimationDurationMs));
369 animation.AddObserver(this);
370
371 layer->SetOpacity(is_visible_ ? 1.0 : 0.0);
372 widget->SetBounds(target_bounds);
373 }
374
375 void AppListController::ProcessLocatedEvent(ui::LocatedEvent* event) {
376 if (!view_ || !is_visible_)
377 return;
378
379 // If the event happened on a menu, then the event should not close the app
380 // list.
381 aura::Window* target = static_cast<aura::Window*>(event->target());
382 if (target) {
383 RootWindowController* root_controller =
384 GetRootWindowController(target->GetRootWindow());
385 if (root_controller) {
386 aura::Window* menu_container =
387 root_controller->GetContainer(kShellWindowId_MenuContainer);
388 if (menu_container->Contains(target))
389 return;
390 aura::Window* keyboard_container = root_controller->GetContainer(
391 kShellWindowId_VirtualKeyboardContainer);
392 if (keyboard_container->Contains(target))
393 return;
394 }
395 }
396
397 aura::Window* window = view_->GetWidget()->GetNativeView()->parent();
398 if (!window->Contains(target) &&
399 !app_list::switches::ShouldNotDismissOnBlur()) {
400 Dismiss();
401 }
402 }
403
404 void AppListController::UpdateBounds() {
405 if (!view_ || !is_visible_)
406 return;
407
408 view_->UpdateBounds();
409
410 if (is_centered_)
411 view_->SetAnchorPoint(GetCenterOfDisplayForView(
412 view_, GetMinimumBoundsHeightForAppList(view_)));
413 }
414
415 ////////////////////////////////////////////////////////////////////////////////
416 // AppListController, aura::EventFilter implementation:
417
418 void AppListController::OnMouseEvent(ui::MouseEvent* event) {
419 if (event->type() == ui::ET_MOUSE_PRESSED)
420 ProcessLocatedEvent(event);
421 }
422
423 void AppListController::OnGestureEvent(ui::GestureEvent* event) {
424 if (event->type() == ui::ET_GESTURE_TAP_DOWN)
425 ProcessLocatedEvent(event);
426 }
427
428 ////////////////////////////////////////////////////////////////////////////////
429 // AppListController, aura::FocusObserver implementation:
430
431 void AppListController::OnWindowFocused(aura::Window* gained_focus,
432 aura::Window* lost_focus) {
433 if (view_ && is_visible_) {
434 aura::Window* applist_window = view_->GetWidget()->GetNativeView();
435 aura::Window* applist_container = applist_window->parent();
436
437 if (applist_container->Contains(lost_focus) &&
438 (!gained_focus || !applist_container->Contains(gained_focus)) &&
439 !app_list::switches::ShouldNotDismissOnBlur()) {
440 Dismiss();
441 }
442 }
443 }
444
445 ////////////////////////////////////////////////////////////////////////////////
446 // AppListController, aura::WindowObserver implementation:
447 void AppListController::OnWindowBoundsChanged(aura::Window* root,
448 const gfx::Rect& old_bounds,
449 const gfx::Rect& new_bounds) {
450 UpdateBounds();
451 }
452
453 ////////////////////////////////////////////////////////////////////////////////
454 // AppListController, ui::ImplicitAnimationObserver implementation:
455
456 void AppListController::OnImplicitAnimationsCompleted() {
457 if (is_visible_ )
458 view_->GetWidget()->Activate();
459 else
460 view_->GetWidget()->Close();
461 }
462
463 ////////////////////////////////////////////////////////////////////////////////
464 // AppListController, views::WidgetObserver implementation:
465
466 void AppListController::OnWidgetDestroying(views::Widget* widget) {
467 DCHECK(view_->GetWidget() == widget);
468 if (is_visible_)
469 Dismiss();
470 ResetView();
471 }
472
473 ////////////////////////////////////////////////////////////////////////////////
474 // AppListController, keyboard::KeyboardControllerObserver implementation:
475
476 void AppListController::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) {
477 UpdateBounds();
478 }
479
480 ////////////////////////////////////////////////////////////////////////////////
481 // AppListController, ShellObserver implementation:
482 void AppListController::OnShelfAlignmentChanged(aura::Window* root_window) {
483 if (view_)
484 view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView()));
485 }
486
487 void AppListController::OnMaximizeModeStarted() {
488 // The "fullscreen" app-list is initialized as in a different type of window,
489 // therefore we can't switch between the fullscreen status and the normal
490 // app-list bubble. App-list should be dismissed for the transition between
491 // maximize mode (touch-view mode) and non-maximize mode, otherwise the app
492 // list tries to behave as a bubble which leads to a crash. crbug.com/510062
493 if (IsFullscreenAppListEnabled() && is_visible_)
494 Dismiss();
495 }
496
497 void AppListController::OnMaximizeModeEnded() {
498 // See the comments of OnMaximizeModeStarted().
499 if (IsFullscreenAppListEnabled() && is_visible_)
500 Dismiss();
501 }
502
503 ////////////////////////////////////////////////////////////////////////////////
504 // AppListController, ShelfIconObserver implementation:
505
506 void AppListController::OnShelfIconPositionsChanged() {
507 UpdateBounds();
508 }
509
510 ////////////////////////////////////////////////////////////////////////////////
511 // AppListController, PaginationModelObserver implementation:
512
513 void AppListController::TotalPagesChanged() {
514 }
515
516 void AppListController::SelectedPageChanged(int old_selected,
517 int new_selected) {
518 current_apps_page_ = new_selected;
519 }
520
521 void AppListController::TransitionStarted() {
522 }
523
524 void AppListController::TransitionChanged() {
525 // |view_| could be NULL when app list is closed with a running transition.
526 if (!view_)
527 return;
528
529 app_list::PaginationModel* pagination_model = view_->GetAppsPaginationModel();
530
531 const app_list::PaginationModel::Transition& transition =
532 pagination_model->transition();
533 if (pagination_model->is_valid_page(transition.target_page))
534 return;
535
536 views::Widget* widget = view_->GetWidget();
537 ui::LayerAnimator* widget_animator = GetLayer(widget)->GetAnimator();
538 if (!pagination_model->IsRevertingCurrentTransition()) {
539 // Update cached |view_bounds_| if it is the first over-scroll move and
540 // widget does not have running animations.
541 if (!should_snap_back_ && !widget_animator->is_animating())
542 view_bounds_ = widget->GetWindowBoundsInScreen();
543
544 const int current_page = pagination_model->selected_page();
545 const int dir = transition.target_page > current_page ? -1 : 1;
546
547 const double progress = 1.0 - pow(1.0 - transition.progress, 4);
548 const int shift = kMaxOverScrollShift * progress * dir;
549
550 gfx::Rect shifted(view_bounds_);
551 shifted.set_x(shifted.x() + shift);
552
553 widget->SetBounds(shifted);
554
555 should_snap_back_ = true;
556 } else if (should_snap_back_) {
557 should_snap_back_ = false;
558 ui::ScopedLayerAnimationSettings animation(widget_animator);
559 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
560 app_list::kOverscrollPageTransitionDurationMs));
561 widget->SetBounds(view_bounds_);
562 }
563 }
564
565 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698