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

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

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

Powered by Google App Engine
This is Rietveld 408576698