blob: 245a48f462993697c872d515355e754b498f7ef7 [file] [log] [blame]
Mitsuru Oshima35b4c2a2019-10-17 17:04:341// Copyright 2019 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/public/cpp/autotest_private_api_utils.h"
6
Mitsuru Oshima9a6378d92019-11-01 00:48:157#include "ash/app_list/app_list_controller_impl.h"
Shengsong Tan3b97bb22019-10-31 02:42:058#include "ash/frame/non_client_frame_view_ash.h"
Mitsuru Oshima9a6378d92019-11-01 00:48:159#include "ash/home_screen/home_screen_controller.h"
Mitsuru Oshima35b4c2a2019-10-17 17:04:3410#include "ash/shell.h"
11#include "ash/wm/mru_window_tracker.h"
12#include "ash/wm/tablet_mode/scoped_skip_user_session_blocked_check.h"
Mitsuru Oshima35b4c2a2019-10-17 17:04:3413
14namespace ash {
Mitsuru Oshima9a6378d92019-11-01 00:48:1515namespace {
16
Toni Barzicd770f6a2019-11-04 20:06:4717class HomeLauncherStateWaiter {
Mitsuru Oshima9a6378d92019-11-01 00:48:1518 public:
19 HomeLauncherStateWaiter(bool target_shown, base::OnceClosure closure)
20 : target_shown_(target_shown), closure_(std::move(closure)) {
21 Shell::Get()
Toni Barzicd770f6a2019-11-04 20:06:4722 ->app_list_controller()
23 ->SetHomeLauncherAnimationCallbackForTesting(base::BindRepeating(
24 &HomeLauncherStateWaiter::OnHomeLauncherAnimationCompleted,
25 base::Unretained(this)));
Mitsuru Oshima9a6378d92019-11-01 00:48:1526 }
Toni Barzicd770f6a2019-11-04 20:06:4727 ~HomeLauncherStateWaiter() {
Mitsuru Oshima9a6378d92019-11-01 00:48:1528 Shell::Get()
Toni Barzicd770f6a2019-11-04 20:06:4729 ->app_list_controller()
30 ->SetHomeLauncherAnimationCallbackForTesting(base::NullCallback());
Mitsuru Oshima9a6378d92019-11-01 00:48:1531 }
32
33 private:
Toni Barzicd770f6a2019-11-04 20:06:4734 // Passed to AppListControllerImpl as a callback to run when home launcher
35 // transition animation is complete.
36 void OnHomeLauncherAnimationCompleted(bool shown) {
Mitsuru Oshima9a6378d92019-11-01 00:48:1537 if (shown == target_shown_) {
38 std::move(closure_).Run();
39 delete this;
40 }
41 }
42
43 bool target_shown_;
44 base::OnceClosure closure_;
45
46 DISALLOW_COPY_AND_ASSIGN(HomeLauncherStateWaiter);
47};
48
49// A waiter that waits until the animation ended with the target state, and
50// execute the callback. This self destruction upon completion.
51class LauncherStateWaiter {
52 public:
53 LauncherStateWaiter(ash::AppListViewState state, base::OnceClosure closure)
54 : target_state_(state), closure_(std::move(closure)) {
Toni Barzicd770f6a2019-11-04 20:06:4755 Shell::Get()
56 ->app_list_controller()
57 ->SetStateTransitionAnimationCallbackForTesting(base::BindRepeating(
58 &LauncherStateWaiter::OnStateChanged, base::Unretained(this)));
Mitsuru Oshima9a6378d92019-11-01 00:48:1559 }
60 ~LauncherStateWaiter() {
Toni Barzicd770f6a2019-11-04 20:06:4761 Shell::Get()
62 ->app_list_controller()
63 ->SetStateTransitionAnimationCallbackForTesting(base::NullCallback());
Mitsuru Oshima9a6378d92019-11-01 00:48:1564 }
65
66 void OnStateChanged(ash::AppListViewState state) {
67 if (target_state_ == state) {
68 std::move(closure_).Run();
69 delete this;
70 }
71 }
72
73 private:
74 ash::AppListViewState target_state_;
75 base::OnceClosure closure_;
76
77 DISALLOW_COPY_AND_ASSIGN(LauncherStateWaiter);
78};
79
80} // namespace
Mitsuru Oshima35b4c2a2019-10-17 17:04:3481
82std::vector<aura::Window*> GetAppWindowList() {
83 ScopedSkipUserSessionBlockedCheck skip_session_blocked;
Kazuki Takise9e5b7a02020-03-04 12:12:4584 return Shell::Get()->mru_window_tracker()->BuildAppWindowList(kAllDesks);
Mitsuru Oshima35b4c2a2019-10-17 17:04:3485}
86
Mitsuru Oshima9a6378d92019-11-01 00:48:1587bool WaitForLauncherState(AppListViewState target_state,
Reilly Grantb6702232019-11-26 22:46:0488 base::OnceClosure closure) {
Mitsuru Oshima9a6378d92019-11-01 00:48:1589 // In the tablet mode, some of the app-list state switching is handled
90 // differently. For open and close, HomeLauncherGestureHandler handles the
91 // gestures and animation. HomeLauncherStateWaiter can wait for such
92 // animation. For switching between the search and apps-grid,
93 // LauncherStateWaiter can wait for the animation.
94 bool should_wait_for_home_launcher = false;
95 if (Shell::Get()->tablet_mode_controller()->InTabletMode() &&
96 target_state != AppListViewState::kFullscreenSearch) {
97 // App-list can't enter into kPeeking or kHalf state. Thus |target_state|
98 // should be either kClosed or kFullscreenAllApps.
99 DCHECK(target_state == AppListViewState::kClosed ||
100 target_state == AppListViewState::kFullscreenAllApps);
101 const AppListViewState current_state =
102 Shell::Get()->app_list_controller()->GetAppListViewState();
103 should_wait_for_home_launcher =
104 (target_state == AppListViewState::kClosed) ||
105 (current_state != AppListViewState::kFullscreenSearch);
106 }
107 if (should_wait_for_home_launcher) {
108 // We don't check if the home launcher is animating to the target visibility
109 // because a) home launcher behavior is deterministic, b) correctly
110 // deteching if the home launcher is animating to visibile/invisible require
111 // some refactoring.
Manu Cornetc3511d902020-01-10 22:44:43112 bool target_visible = target_state != AppListViewState::kClosed;
Reilly Grantb6702232019-11-26 22:46:04113 new HomeLauncherStateWaiter(target_visible, std::move(closure));
Mitsuru Oshima9a6378d92019-11-01 00:48:15114 } else {
115 // Don't wait if the launcher is already in the target state and not
116 // animating.
117 auto* app_list_view =
118 Shell::Get()->app_list_controller()->presenter()->GetView();
119 bool animating =
120 app_list_view &&
121 app_list_view->GetWidget()->GetLayer()->GetAnimator()->is_animating();
122 bool at_target_state =
Manu Cornetc3511d902020-01-10 22:44:43123 (!app_list_view && target_state == AppListViewState::kClosed) ||
Mitsuru Oshima9a6378d92019-11-01 00:48:15124 (app_list_view && app_list_view->app_list_state() == target_state);
125 if (at_target_state && !animating) {
126 std::move(closure).Run();
127 return true;
128 }
Reilly Grantb6702232019-11-26 22:46:04129 new LauncherStateWaiter(target_state, std::move(closure));
Mitsuru Oshima9a6378d92019-11-01 00:48:15130 }
131 return false;
132}
133
134} // namespace ash