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

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