1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ROSEN_WINDOW_NODE_STATE_MACHINE_H
17 #define OHOS_ROSEN_WINDOW_NODE_STATE_MACHINE_H
18 
19 #include <atomic>
20 #include <deque>
21 #include <functional>
22 #include <mutex>
23 #include <vector>
24 #include "wm_common.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 using StateTask = std::function<void()>;
30 }
31 enum class WindowNodeState : uint32_t {
32     INITIAL,
33     STARTING_CREATED,
34     SHOW_ANIMATION_PLAYING,
35     SHOW_ANIMATION_DONE,
36     HIDE_ANIMATION_PLAYING,
37     HIDE_ANIMATION_DONE,
38     SHOWN,
39     HIDDEN,
40     DESTROYED
41 };
42 
43 class WindowNodeStateMachine {
44 public:
45     WindowNodeStateMachine();
46 
47     ~WindowNodeStateMachine();
48 
49     void TransitionTo(WindowNodeState state);
50 
51     void UpdateAnimationTaskCount(bool isAdd);
52 
53     int32_t GetAnimationCount();
54 
55     bool IsWindowNodeShownOrShowing();
56 
57     bool IsRemoteAnimationPlaying();
58 
59     bool IsWindowNodeHiddenOrHiding();
60 
61     WindowNodeState GetCurrentState();
62 
63     bool IsShowAnimationPlaying();
64 
65     bool IsHideAnimationPlaying();
66 
67     void SetDestroyTaskParam(bool onlySelf);
68 
69     bool GetDestroyTaskParam();
70 
71     void SetDestroyTask(StateTask task);
72 
73     bool GetDestroyTask(StateTask& task);
74 
75     void ResetAnimationTaskCount(int32_t taskCount);
76 
77     std::string GenStateMachineInfo();
78     // test
SetWindowId(uint32_t id)79     void SetWindowId(uint32_t id)
80     {
81         windowId_ = id;
82     }
83 
SetWindowType(WindowType type)84     void SetWindowType(WindowType type)
85     {
86         type_ = type;
87     }
88 
89 private:
90     std::deque<std::pair<std::string, StateTask>> stateTaskQ_;
91     WindowNodeState currState_ = WindowNodeState::INITIAL;
92     int32_t taskCount_ = 0;
93     std::recursive_mutex mutex_;
94     bool destroyOnlySelf_ = true;
95     // just for test
96     uint32_t windowId_ = 0;
97     WindowType type_ { WindowType::WINDOW_TYPE_APP_MAIN_WINDOW };
98     uint32_t count1 = 0;
99     uint32_t count2 = 0;
100     StateTask destroyTask_;
101 };
102 } // namespace Rosen
103 } // namespace OHOS
104 #endif