Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 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 | |
Arthur Sonzogni | a98e443 | 2023-11-28 18:15:01 | [diff] [blame] | 7 | #include <optional> |
| 8 | |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 9 | #include "ash/app_list/app_list_controller_impl.h" |
James Cook | 4f4c361 | 2021-05-27 21:45:22 | [diff] [blame] | 10 | #include "ash/app_list/app_list_presenter_impl.h" |
Shengsong Tan | 3b97bb2 | 2019-10-31 02:42:05 | [diff] [blame] | 11 | #include "ash/frame/non_client_frame_view_ash.h" |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 12 | #include "ash/shell.h" |
| 13 | #include "ash/wm/mru_window_tracker.h" |
| 14 | #include "ash/wm/tablet_mode/scoped_skip_user_session_blocked_check.h" |
Avi Drissman | 4de6dab | 2023-01-06 23:17:35 | [diff] [blame] | 15 | #include "base/functional/bind.h" |
| 16 | #include "base/functional/callback_helpers.h" |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 17 | #include "base/memory/raw_ptr.h" |
Jun Mukai | d13d7807 | 2021-01-22 20:08:46 | [diff] [blame] | 18 | #include "base/scoped_observation.h" |
Lei Zhang | 0c04332 | 2021-04-27 16:43:05 | [diff] [blame] | 19 | #include "ui/compositor/layer.h" |
Jun Mukai | d13d7807 | 2021-01-22 20:08:46 | [diff] [blame] | 20 | #include "ui/compositor/layer_animation_observer.h" |
David Sanders | 0a524ec | 2022-03-20 07:31:28 | [diff] [blame] | 21 | #include "ui/compositor/layer_animator.h" |
Eriko Kurimoto | c6b96538 | 2023-12-13 03:55:13 | [diff] [blame] | 22 | #include "ui/display/screen.h" |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 23 | |
| 24 | namespace ash { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 25 | namespace { |
| 26 | |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 27 | class HomeLauncherStateWaiter { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 28 | public: |
| 29 | HomeLauncherStateWaiter(bool target_shown, base::OnceClosure closure) |
| 30 | : target_shown_(target_shown), closure_(std::move(closure)) { |
| 31 | Shell::Get() |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 32 | ->app_list_controller() |
| 33 | ->SetHomeLauncherAnimationCallbackForTesting(base::BindRepeating( |
| 34 | &HomeLauncherStateWaiter::OnHomeLauncherAnimationCompleted, |
| 35 | base::Unretained(this))); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 36 | } |
Peter Boström | ec31a04 | 2021-09-16 23:37:34 | [diff] [blame] | 37 | |
| 38 | HomeLauncherStateWaiter(const HomeLauncherStateWaiter&) = delete; |
| 39 | HomeLauncherStateWaiter& operator=(const HomeLauncherStateWaiter&) = delete; |
| 40 | |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 41 | ~HomeLauncherStateWaiter() { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 42 | Shell::Get() |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 43 | ->app_list_controller() |
| 44 | ->SetHomeLauncherAnimationCallbackForTesting(base::NullCallback()); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | private: |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 48 | // Passed to AppListControllerImpl as a callback to run when home launcher |
| 49 | // transition animation is complete. |
| 50 | void OnHomeLauncherAnimationCompleted(bool shown) { |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 51 | if (shown == target_shown_) { |
| 52 | std::move(closure_).Run(); |
| 53 | delete this; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | bool target_shown_; |
| 58 | base::OnceClosure closure_; |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | // A waiter that waits until the animation ended with the target state, and |
| 62 | // execute the callback. This self destruction upon completion. |
| 63 | class LauncherStateWaiter { |
| 64 | public: |
| 65 | LauncherStateWaiter(ash::AppListViewState state, base::OnceClosure closure) |
| 66 | : target_state_(state), closure_(std::move(closure)) { |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 67 | Shell::Get() |
| 68 | ->app_list_controller() |
| 69 | ->SetStateTransitionAnimationCallbackForTesting(base::BindRepeating( |
| 70 | &LauncherStateWaiter::OnStateChanged, base::Unretained(this))); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 71 | } |
Peter Boström | ec31a04 | 2021-09-16 23:37:34 | [diff] [blame] | 72 | |
| 73 | LauncherStateWaiter(const LauncherStateWaiter&) = delete; |
| 74 | LauncherStateWaiter& operator=(const LauncherStateWaiter&) = delete; |
| 75 | |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 76 | ~LauncherStateWaiter() { |
Toni Barzic | d770f6a | 2019-11-04 20:06:47 | [diff] [blame] | 77 | Shell::Get() |
| 78 | ->app_list_controller() |
| 79 | ->SetStateTransitionAnimationCallbackForTesting(base::NullCallback()); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void OnStateChanged(ash::AppListViewState state) { |
| 83 | if (target_state_ == state) { |
| 84 | std::move(closure_).Run(); |
| 85 | delete this; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | ash::AppListViewState target_state_; |
| 91 | base::OnceClosure closure_; |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 92 | }; |
| 93 | |
Jun Mukai | d13d7807 | 2021-01-22 20:08:46 | [diff] [blame] | 94 | class LauncherAnimationWaiter : public ui::LayerAnimationObserver { |
| 95 | public: |
| 96 | LauncherAnimationWaiter(AppListView* view, base::OnceClosure closure) |
| 97 | : closure_(std::move(closure)) { |
| 98 | observation_.Observe(view->GetWidget()->GetLayer()->GetAnimator()); |
| 99 | } |
| 100 | ~LauncherAnimationWaiter() override = default; |
| 101 | LauncherAnimationWaiter(const LauncherAnimationWaiter&) = delete; |
| 102 | LauncherAnimationWaiter& operator=(const LauncherAnimationWaiter&) = delete; |
| 103 | |
| 104 | private: |
| 105 | // ui::LayerAnimationObserver: |
| 106 | void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override { |
| 107 | std::move(closure_).Run(); |
| 108 | delete this; |
| 109 | } |
| 110 | void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override { |
| 111 | OnLayerAnimationEnded(sequence); |
| 112 | } |
| 113 | void OnLayerAnimationScheduled( |
| 114 | ui::LayerAnimationSequence* sequence) override {} |
| 115 | |
| 116 | base::OnceClosure closure_; |
| 117 | base::ScopedObservation<ui::LayerAnimator, ui::LayerAnimationObserver> |
| 118 | observation_{this}; |
| 119 | }; |
| 120 | |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 121 | bool WaitForHomeLauncherState(bool target_visible, base::OnceClosure closure) { |
| 122 | if (Shell::Get()->app_list_controller()->IsVisible( |
Arthur Sonzogni | a98e443 | 2023-11-28 18:15:01 | [diff] [blame] | 123 | /*display_id=*/std::nullopt) == target_visible) { |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 124 | std::move(closure).Run(); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | new HomeLauncherStateWaiter(target_visible, std::move(closure)); |
| 129 | return false; |
| 130 | } |
| 131 | |
Jun Mukai | d13d7807 | 2021-01-22 20:08:46 | [diff] [blame] | 132 | bool WaitForLauncherAnimation(base::OnceClosure closure) { |
| 133 | auto* app_list_view = |
James Cook | 26dd002 | 2022-01-10 23:28:28 | [diff] [blame] | 134 | Shell::Get()->app_list_controller()->fullscreen_presenter()->GetView(); |
Jun Mukai | d13d7807 | 2021-01-22 20:08:46 | [diff] [blame] | 135 | if (!app_list_view) { |
| 136 | std::move(closure).Run(); |
| 137 | return true; |
| 138 | } |
| 139 | bool animating = |
| 140 | app_list_view->GetWidget()->GetLayer()->GetAnimator()->is_animating(); |
| 141 | if (!animating) { |
| 142 | std::move(closure).Run(); |
| 143 | return true; |
| 144 | } |
| 145 | new LauncherAnimationWaiter(app_list_view, std::move(closure)); |
| 146 | return false; |
| 147 | } |
| 148 | |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 149 | } // namespace |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 150 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 151 | std::vector<raw_ptr<aura::Window, VectorExperimental>> GetAppWindowList() { |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 152 | ScopedSkipUserSessionBlockedCheck skip_session_blocked; |
Kazuki Takise | 9e5b7a0 | 2020-03-04 12:12:45 | [diff] [blame] | 153 | return Shell::Get()->mru_window_tracker()->BuildAppWindowList(kAllDesks); |
Mitsuru Oshima | 35b4c2a | 2019-10-17 17:04:34 | [diff] [blame] | 154 | } |
| 155 | |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 156 | bool WaitForLauncherState(AppListViewState target_state, |
Reilly Grant | b670223 | 2019-11-26 22:46:04 | [diff] [blame] | 157 | base::OnceClosure closure) { |
Eriko Kurimoto | c6b96538 | 2023-12-13 03:55:13 | [diff] [blame] | 158 | const bool in_tablet_mode = display::Screen::GetScreen()->InTabletMode(); |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 159 | if (in_tablet_mode) { |
| 160 | // App-list can't enter kPeeking or kHalf state in tablet mode. Thus |
| 161 | // |target_state| should be either kClosed, kFullscreenAllApps or |
| 162 | // kFullscreenSearch. |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 163 | DCHECK(target_state == AppListViewState::kClosed || |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 164 | target_state == AppListViewState::kFullscreenAllApps || |
| 165 | target_state == AppListViewState::kFullscreenSearch); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 166 | } |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 167 | |
| 168 | // In the tablet mode, home launcher visibility state needs special handling, |
| 169 | // as app list view visibility does not match home launcher visibility. The |
| 170 | // app list view is always visible, but the home launcher may be obscured by |
| 171 | // app windows. The waiter interprets waits for kClosed state as waits |
| 172 | // "home launcher not visible" state - note that the app list view |
| 173 | // is actually expected to be in a visible state. |
| 174 | AppListViewState effective_target_state = |
| 175 | in_tablet_mode && target_state == AppListViewState::kClosed |
| 176 | ? AppListViewState::kFullscreenAllApps |
| 177 | : target_state; |
| 178 | |
Arthur Sonzogni | a98e443 | 2023-11-28 18:15:01 | [diff] [blame] | 179 | std::optional<bool> target_home_launcher_visibility; |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 180 | if (in_tablet_mode) |
| 181 | target_home_launcher_visibility = target_state != AppListViewState::kClosed; |
| 182 | |
| 183 | // Don't wait if the launcher is already in the target state and not |
| 184 | // animating. |
| 185 | auto* app_list_view = |
James Cook | 26dd002 | 2022-01-10 23:28:28 | [diff] [blame] | 186 | Shell::Get()->app_list_controller()->fullscreen_presenter()->GetView(); |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 187 | bool animating = |
| 188 | app_list_view && |
| 189 | app_list_view->GetWidget()->GetLayer()->GetAnimator()->is_animating(); |
| 190 | bool at_target_state = |
| 191 | (!app_list_view && effective_target_state == AppListViewState::kClosed) || |
| 192 | (app_list_view && |
| 193 | app_list_view->app_list_state() == effective_target_state); |
Jun Mukai | d13d7807 | 2021-01-22 20:08:46 | [diff] [blame] | 194 | |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 195 | if (at_target_state && !animating) { |
| 196 | // In tablet mode, ensure that the home launcher is in the expected state. |
| 197 | if (target_home_launcher_visibility.has_value()) { |
| 198 | return WaitForHomeLauncherState(*target_home_launcher_visibility, |
| 199 | std::move(closure)); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 200 | } |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 201 | std::move(closure).Run(); |
| 202 | return true; |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 203 | } |
Toni Barzic | 0c29533 | 2020-11-12 06:20:24 | [diff] [blame] | 204 | |
| 205 | // In tablet mode, ensure that the home launcher is in the expected state. |
| 206 | base::OnceClosure callback = |
| 207 | target_home_launcher_visibility.has_value() |
| 208 | ? base::BindOnce(base::IgnoreResult(&WaitForHomeLauncherState), |
| 209 | *target_home_launcher_visibility, std::move(closure)) |
| 210 | : std::move(closure); |
Jun Mukai | d13d7807 | 2021-01-22 20:08:46 | [diff] [blame] | 211 | if (at_target_state) |
| 212 | return WaitForLauncherAnimation(std::move(callback)); |
| 213 | new LauncherStateWaiter( |
| 214 | target_state, |
| 215 | base::BindOnce(base::IgnoreResult(&WaitForLauncherAnimation), |
| 216 | std::move(callback))); |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
| 220 | } // namespace ash |