Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 1 | // 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 Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 7 | #include "ash/app_list/app_list_controller_impl.h" |
Shengsong Tan | 3b97bb2 | 2019-10-31 02:42:05 | [diff] [blame] | 8 | #include "ash/frame/non_client_frame_view_ash.h" |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 9 | #include "ash/home_screen/home_screen_controller.h" |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 10 | #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" |
Gabriel Charette | 9f60dd1 | 2020-03-06 20:48:04 | [diff] [blame^] | 13 | #include "base/bind_helpers.h" |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 14 | |
| 15 | namespace ash { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 16 | namespace { |
| 17 | |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 18 | class HomeLauncherStateWaiter { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 19 | public: |
| 20 | HomeLauncherStateWaiter(bool target_shown, base::OnceClosure closure) |
| 21 | : target_shown_(target_shown), closure_(std::move(closure)) { |
| 22 | Shell::Get() |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 23 | ->app_list_controller() |
| 24 | ->SetHomeLauncherAnimationCallbackForTesting(base::BindRepeating( |
| 25 | &HomeLauncherStateWaiter::OnHomeLauncherAnimationCompleted, |
| 26 | base::Unretained(this))); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 27 | } |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 28 | ~HomeLauncherStateWaiter() { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 29 | Shell::Get() |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 30 | ->app_list_controller() |
| 31 | ->SetHomeLauncherAnimationCallbackForTesting(base::NullCallback()); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | private: |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 35 | // Passed to AppListControllerImpl as a callback to run when home launcher |
| 36 | // transition animation is complete. |
| 37 | void OnHomeLauncherAnimationCompleted(bool shown) { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 38 | if (shown == target_shown_) { |
| 39 | std::move(closure_).Run(); |
| 40 | delete this; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bool target_shown_; |
| 45 | base::OnceClosure closure_; |
| 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(HomeLauncherStateWaiter); |
| 48 | }; |
| 49 | |
| 50 | // A waiter that waits until the animation ended with the target state, and |
| 51 | // execute the callback. This self destruction upon completion. |
| 52 | class LauncherStateWaiter { |
| 53 | public: |
| 54 | LauncherStateWaiter(ash::AppListViewState state, base::OnceClosure closure) |
| 55 | : target_state_(state), closure_(std::move(closure)) { |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 56 | Shell::Get() |
| 57 | ->app_list_controller() |
| 58 | ->SetStateTransitionAnimationCallbackForTesting(base::BindRepeating( |
| 59 | &LauncherStateWaiter::OnStateChanged, base::Unretained(this))); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 60 | } |
| 61 | ~LauncherStateWaiter() { |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 62 | Shell::Get() |
| 63 | ->app_list_controller() |
| 64 | ->SetStateTransitionAnimationCallbackForTesting(base::NullCallback()); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void OnStateChanged(ash::AppListViewState state) { |
| 68 | if (target_state_ == state) { |
| 69 | std::move(closure_).Run(); |
| 70 | delete this; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | ash::AppListViewState target_state_; |
| 76 | base::OnceClosure closure_; |
| 77 | |
| 78 | DISALLOW_COPY_AND_ASSIGN(LauncherStateWaiter); |
| 79 | }; |
| 80 | |
| 81 | } // namespace |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 82 | |
| 83 | std::vector<aura::Window*> GetAppWindowList() { |
| 84 | ScopedSkipUserSessionBlockedCheck skip_session_blocked; |
Kazuki Takise | 9e5b7a0 | 2020-03-04 12:12:45 | [diff] [blame] | 85 | return Shell::Get()->mru_window_tracker()->BuildAppWindowList(kAllDesks); |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 86 | } |
| 87 | |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 88 | bool WaitForLauncherState(AppListViewState target_state, |
Reilly Grant | b670223 | 2019-11-26 22:46:04 | [diff] [blame] | 89 | base::OnceClosure closure) { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 90 | // In the tablet mode, some of the app-list state switching is handled |
| 91 | // differently. For open and close, HomeLauncherGestureHandler handles the |
| 92 | // gestures and animation. HomeLauncherStateWaiter can wait for such |
| 93 | // animation. For switching between the search and apps-grid, |
| 94 | // LauncherStateWaiter can wait for the animation. |
| 95 | bool should_wait_for_home_launcher = false; |
| 96 | if (Shell::Get()->tablet_mode_controller()->InTabletMode() && |
| 97 | target_state != AppListViewState::kFullscreenSearch) { |
| 98 | // App-list can't enter into kPeeking or kHalf state. Thus |target_state| |
| 99 | // should be either kClosed or kFullscreenAllApps. |
| 100 | DCHECK(target_state == AppListViewState::kClosed || |
| 101 | target_state == AppListViewState::kFullscreenAllApps); |
| 102 | const AppListViewState current_state = |
| 103 | Shell::Get()->app_list_controller()->GetAppListViewState(); |
| 104 | should_wait_for_home_launcher = |
| 105 | (target_state == AppListViewState::kClosed) || |
| 106 | (current_state != AppListViewState::kFullscreenSearch); |
| 107 | } |
| 108 | if (should_wait_for_home_launcher) { |
| 109 | // We don't check if the home launcher is animating to the target visibility |
| 110 | // because a) home launcher behavior is deterministic, b) correctly |
| 111 | // deteching if the home launcher is animating to visibile/invisible require |
| 112 | // some refactoring. |
Manu Cornet | c3511d90 | 2020-01-10 22:44:43 | [diff] [blame] | 113 | bool target_visible = target_state != AppListViewState::kClosed; |
Reilly Grant | b670223 | 2019-11-26 22:46:04 | [diff] [blame] | 114 | new HomeLauncherStateWaiter(target_visible, std::move(closure)); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 115 | } else { |
| 116 | // Don't wait if the launcher is already in the target state and not |
| 117 | // animating. |
| 118 | auto* app_list_view = |
| 119 | Shell::Get()->app_list_controller()->presenter()->GetView(); |
| 120 | bool animating = |
| 121 | app_list_view && |
| 122 | app_list_view->GetWidget()->GetLayer()->GetAnimator()->is_animating(); |
| 123 | bool at_target_state = |
Manu Cornet | c3511d90 | 2020-01-10 22:44:43 | [diff] [blame] | 124 | (!app_list_view && target_state == AppListViewState::kClosed) || |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 125 | (app_list_view && app_list_view->app_list_state() == target_state); |
| 126 | if (at_target_state && !animating) { |
| 127 | std::move(closure).Run(); |
| 128 | return true; |
| 129 | } |
Reilly Grant | b670223 | 2019-11-26 22:46:04 | [diff] [blame] | 130 | new LauncherStateWaiter(target_state, std::move(closure)); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | } // namespace ash |