blob: 0c1b7665f0a91abdc1c2c0e5b29a6593ef387c97 [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"
Gabriel Charette9f60dd12020-03-06 20:48:0413#include "base/bind_helpers.h"
Mitsuru Oshima35b4c2a2019-10-17 17:04:3414
15namespace ash {
Mitsuru Oshima9a6378d92019-11-01 00:48:1516namespace {
17
Toni Barzicd770f6a2019-11-04 20:06:4718class HomeLauncherStateWaiter {
Mitsuru Oshima9a6378d92019-11-01 00:48:1519 public:
20 HomeLauncherStateWaiter(bool target_shown, base::OnceClosure closure)
21 : target_shown_(target_shown), closure_(std::move(closure)) {
22 Shell::Get()
Toni Barzicd770f6a2019-11-04 20:06:4723 ->app_list_controller()
24 ->SetHomeLauncherAnimationCallbackForTesting(base::BindRepeating(
25 &HomeLauncherStateWaiter::OnHomeLauncherAnimationCompleted,
26 base::Unretained(this)));
Mitsuru Oshima9a6378d92019-11-01 00:48:1527 }
Toni Barzicd770f6a2019-11-04 20:06:4728 ~HomeLauncherStateWaiter() {
Mitsuru Oshima9a6378d92019-11-01 00:48:1529 Shell::Get()
Toni Barzicd770f6a2019-11-04 20:06:4730 ->app_list_controller()
31 ->SetHomeLauncherAnimationCallbackForTesting(base::NullCallback());
Mitsuru Oshima9a6378d92019-11-01 00:48:1532 }
33
34 private:
Toni Barzicd770f6a2019-11-04 20:06:4735 // Passed to AppListControllerImpl as a callback to run when home launcher
36 // transition animation is complete.
37 void OnHomeLauncherAnimationCompleted(bool shown) {
Mitsuru Oshima9a6378d92019-11-01 00:48:1538 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.
52class LauncherStateWaiter {
53 public:
54 LauncherStateWaiter(ash::AppListViewState state, base::OnceClosure closure)
55 : target_state_(state), closure_(std::move(closure)) {
Toni Barzicd770f6a2019-11-04 20:06:4756 Shell::Get()
57 ->app_list_controller()
58 ->SetStateTransitionAnimationCallbackForTesting(base::BindRepeating(
59 &LauncherStateWaiter::OnStateChanged, base::Unretained(this)));
Mitsuru Oshima9a6378d92019-11-01 00:48:1560 }
61 ~LauncherStateWaiter() {
Toni Barzicd770f6a2019-11-04 20:06:4762 Shell::Get()
63 ->app_list_controller()
64 ->SetStateTransitionAnimationCallbackForTesting(base::NullCallback());
Mitsuru Oshima9a6378d92019-11-01 00:48:1565 }
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 Oshima35b4c2a2019-10-17 17:04:3482
83std::vector<aura::Window*> GetAppWindowList() {
84 ScopedSkipUserSessionBlockedCheck skip_session_blocked;
Kazuki Takise9e5b7a02020-03-04 12:12:4585 return Shell::Get()->mru_window_tracker()->BuildAppWindowList(kAllDesks);
Mitsuru Oshima35b4c2a2019-10-17 17:04:3486}
87
Mitsuru Oshima9a6378d92019-11-01 00:48:1588bool WaitForLauncherState(AppListViewState target_state,
Reilly Grantb6702232019-11-26 22:46:0489 base::OnceClosure closure) {
Mitsuru Oshima9a6378d92019-11-01 00:48:1590 // 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 Cornetc3511d902020-01-10 22:44:43113 bool target_visible = target_state != AppListViewState::kClosed;
Reilly Grantb6702232019-11-26 22:46:04114 new HomeLauncherStateWaiter(target_visible, std::move(closure));
Mitsuru Oshima9a6378d92019-11-01 00:48:15115 } 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 Cornetc3511d902020-01-10 22:44:43124 (!app_list_view && target_state == AppListViewState::kClosed) ||
Mitsuru Oshima9a6378d92019-11-01 00:48:15125 (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 Grantb6702232019-11-26 22:46:04130 new LauncherStateWaiter(target_state, std::move(closure));
Mitsuru Oshima9a6378d92019-11-01 00:48:15131 }
132 return false;
133}
134
135} // namespace ash