• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_DRAG_DROP_DRAG_DROP_SPRING_LOADING_STATE_MACHINE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_DRAG_DROP_DRAG_DROP_SPRING_LOADING_STATE_MACHINE_H
18 
19 #include <string_view>
20 #include <unordered_map>
21 
22 #include "ui/base/referenced.h"
23 
24 #include "base/memory/ace_type.h"
25 #include "base/thread/cancelable_callback.h"
26 #include "base/thread/task_executor.h"
27 #include "core/components_ng/base/frame_node.h"
28 #include "core/components_ng/manager/drag_drop/drag_drop_related_configuration.h"
29 #include "core/components_ng/manager/drag_drop/drag_drop_spring_loading/drag_drop_spring_loading_state_base.h"
30 
31 namespace OHOS::Ace::NG {
32 class DragDropSpringLoadingDetector;
33 class DragDropSpringLoadingStateMachine : public AceType {
34     DECLARE_ACE_TYPE(DragDropSpringLoadingStateMachine, AceType);
35 
36 public:
37     DragDropSpringLoadingStateMachine();
38     ~DragDropSpringLoadingStateMachine() = default;
39 
40     template<class T>
RegisterStateHandler(DragDropSpringLoadingState state,const std::vector<DragDropSpringLoadingState> & transitions)41     void RegisterStateHandler(
42         DragDropSpringLoadingState state, const std::vector<DragDropSpringLoadingState>& transitions)
43     {
44         CHECK_NULL_VOID(stateMachinePool_);
45         stateMachinePool_->RegisterStateHandler<T>(state, transitions, AceType::WeakClaim(this));
46     }
47 
SetDetector(const WeakPtr<DragDropSpringLoadingDetector> & detector)48     void SetDetector(const WeakPtr<DragDropSpringLoadingDetector>& detector)
49     {
50         detector_ = detector;
51     }
52 
GetCurrentState()53     DragDropSpringLoadingState GetCurrentState() const
54     {
55         return currentState_;
56     };
57 
58     bool RequestStatusTransition(DragDropSpringLoadingState nextState, std::string_view extraInfo);
59 
60     void ResetMachine();
61 
IsInSpringLoadingState()62     bool IsInSpringLoadingState() const
63     {
64         return currentState_ == DragDropSpringLoadingState::BEGIN ||
65                currentState_ == DragDropSpringLoadingState::UPDATE || currentState_ == DragDropSpringLoadingState::END;
66     }
67 
IsWaitingForIdleFinish()68     bool IsWaitingForIdleFinish() const
69     {
70         return isWaitingForIdleFinish_;
71     }
72 
SetWaitingForIdleFinish(bool isWaiting)73     void SetWaitingForIdleFinish(bool isWaiting)
74     {
75         isWaitingForIdleFinish_ = isWaiting;
76     }
77 
78     void PostDelayedTask(const std::function<void()>&& task, uint32_t delay, const std::string& name);
79 
80     void Begin(std::string_view extraInfo);
81 
IncrementCurrentNotifySequence()82     void IncrementCurrentNotifySequence()
83     {
84         currentNotifySequence_++;
85     }
86 
GetCurrentNotifySequence()87     int32_t GetCurrentNotifySequence()
88     {
89         return currentNotifySequence_;
90     }
91 
IncrementCurrentUpdateNotifyCount()92     void IncrementCurrentUpdateNotifyCount()
93     {
94         currentUpdateNotifyCount_++;
95     }
96 
GetCurrentUpdateNotifyCount()97     int32_t GetCurrentUpdateNotifyCount()
98     {
99         return currentUpdateNotifyCount_;
100     }
101 
102     Point GetPoint();
103 
104     RefPtr<FrameNode> GetFrameNode();
105 
SetUserConfig(const RefPtr<DragSpringLoadingConfiguration> & userConfig)106     void SetUserConfig(const RefPtr<DragSpringLoadingConfiguration>& userConfig)
107     {
108         userConfig_ = userConfig;
109     }
110 
GetUserConfig()111     const RefPtr<DragSpringLoadingConfiguration>& GetUserConfig() const
112     {
113         return userConfig_;
114     }
115 
116 private:
117     class DragDropSpringLoadingStateMachinePool : public AceType {
118         DECLARE_ACE_TYPE(DragDropSpringLoadingStateMachinePool, AceType);
119 
120     public:
121         template<class T>
RegisterStateHandler(DragDropSpringLoadingState state,const std::vector<DragDropSpringLoadingState> & transitions,WeakPtr<DragDropSpringLoadingStateMachine> machine)122         bool RegisterStateHandler(DragDropSpringLoadingState state,
123             const std::vector<DragDropSpringLoadingState>& transitions,
124             WeakPtr<DragDropSpringLoadingStateMachine> machine)
125         {
126             if (states_.find(state) != states_.end()) {
127                 return false;
128             }
129             auto stateHandler = MakeRefPtr<T>(machine);
130             states_[state] = { stateHandler, transitions };
131             return true;
132         }
133 
GetStateHandler(DragDropSpringLoadingState state)134         RefPtr<DragDropSpringLoadingStateBase> GetStateHandler(DragDropSpringLoadingState state) const
135         {
136             auto it = states_.find(state);
137             return it != states_.end() ? it->second.stateHandler : nullptr;
138         }
139 
140         bool IsAllowedTransition(DragDropSpringLoadingState from, DragDropSpringLoadingState to) const;
141 
142     private:
143         struct StateInfo {
144             RefPtr<DragDropSpringLoadingStateBase> stateHandler = nullptr;
145             std::vector<DragDropSpringLoadingState> transitions;
146         };
147         std::unordered_map<DragDropSpringLoadingState, StateInfo> states_;
148     };
149     RefPtr<DragDropSpringLoadingStateMachinePool> stateMachinePool_ =
150         MakeRefPtr<DragDropSpringLoadingStateMachinePool>();
151     CancelableCallback<void()> timer_;
152     std::optional<SingleTaskExecutor> taskExecutor_;
153     DragDropSpringLoadingState currentState_ { DragDropSpringLoadingState::IDLE };
154     bool isWaitingForIdleFinish_ { false };
155     int32_t currentNotifySequence_ = -1;
156     int32_t currentUpdateNotifyCount_ = 0;
157     WeakPtr<DragDropSpringLoadingDetector> detector_;
158     RefPtr<DragSpringLoadingConfiguration> userConfig_ = nullptr;
159 };
160 } // namespace OHOS::Ace::NG
161 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_DRAG_DROP_DRAG_DROP_SPRING_LOADING_STATE_MACHINE_H