1 /*
2  * Copyright (c) 2021-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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_GESTURE_REFEREE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_GESTURE_REFEREE_H
18 
19 #include <list>
20 #include <set>
21 #include <unordered_map>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/memory/referenced.h"
25 #include "base/utils/singleton.h"
26 #include "core/event/touch_event.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 class NGGestureRecognizer;
31 
32 enum class GestureDisposal {
33     ACCEPT = 0,
34     REJECT,
35     PENDING,
36     NONE,
37 };
38 
TransGestureDisposal(GestureDisposal disposal)39 inline std::string TransGestureDisposal(GestureDisposal disposal)
40 {
41     const char *str[] = { "ACCEPT", "REJECT", "PENDING", "NONE" };
42     if (disposal >= GestureDisposal::ACCEPT && disposal <= GestureDisposal::NONE) {
43         return str[static_cast<int32_t>(disposal)];
44     }
45     return std::string("Disposal:").append(std::to_string(static_cast<int32_t>(disposal)));
46 }
47 
48 class GestureScope : public AceType {
49     DECLARE_ACE_TYPE(GestureScope, AceType);
50 
51 public:
GestureScope(size_t touchId)52     explicit GestureScope(size_t touchId) : touchId_(touchId) {}
53     ~GestureScope() override = default;
54 
55     void AddMember(const RefPtr<NGGestureRecognizer>& recognizer);
56     void DelMember(const RefPtr<NGGestureRecognizer>& recognizer);
57 
58     void Close(bool isBlocked = false);
59 
60     bool IsPending(size_t touchId);
61 
IsEmpty()62     bool IsEmpty() const
63     {
64         return recognizers_.empty();
65     }
66 
67     bool CheckNeedBlocked(const RefPtr<NGGestureRecognizer>& recognizer);
68 
69     void OnAcceptGesture(const RefPtr<NGGestureRecognizer>& recognizer);
70 
71     RefPtr<NGGestureRecognizer> UnBlockGesture();
72 
IsDelayClosed()73     bool IsDelayClosed() const
74     {
75         return isDelay_;
76     }
77 
SetDelayClose()78     void SetDelayClose()
79     {
80         isDelay_ = true;
81     }
82 
HasGestureAccepted()83     bool HasGestureAccepted() const
84     {
85         return hasGestureAccepted_;
86     }
87 
SetQueryStateFunc(const std::function<void (size_t)> & queryStateFunc)88     void SetQueryStateFunc(const std::function<void(size_t)>& queryStateFunc)
89     {
90         queryStateFunc_ = queryStateFunc;
91     }
92     bool QueryAllDone(size_t touchId);
93     bool CheckRecognizerState();
94 
95     bool IsReady();
96     bool HasFailRecognizer();
97     void ForceCleanGestureScope();
98     void ForceCleanGestureScopeState();
99     void CleanGestureScopeState();
100 private:
101     bool Existed(const RefPtr<NGGestureRecognizer>& recognizer);
102     std::list<WeakPtr<NGGestureRecognizer>> recognizers_;
103 
104     size_t touchId_ = 0;
105     bool isDelay_ = false;
106     bool hasGestureAccepted_ = false;
107 
108     std::function<void(size_t)> queryStateFunc_;
109 };
110 
111 class GestureReferee : public virtual AceType {
112     DECLARE_ACE_TYPE(GestureReferee, AceType);
113 
114 public:
115     GestureReferee() = default;
116     ~GestureReferee() override = default;
117 
118     void AddGestureToScope(size_t touchId, const TouchTestResult& result);
119 
120     // Try to clean gesture scope when receive cancel event.
121     void CleanGestureScope(size_t touchId);
122 
123     // Called by the gesture recognizer when the gesture recognizer has completed the recognition of the gesture (accept
124     // or reject)
125     void Adjudicate(const RefPtr<NGGestureRecognizer>& recognizer, GestureDisposal disposal);
126 
127     bool HasGestureAccepted(size_t touchId) const;
128 
SetQueryStateFunc(std::function<void (size_t)> && queryStateFunc)129     void SetQueryStateFunc(std::function<void(size_t)>&& queryStateFunc)
130     {
131         queryStateFunc_ = queryStateFunc;
132     }
133     bool QueryAllDone(size_t touchId);
134     bool QueryAllDone();
135     bool CheckEventTypeChange(SourceType type, bool isAxis = false) const;
136     bool CheckSourceTypeChange(SourceType type, bool isAxis = false);
137     void CleanAll(bool isBlocked = false);
138     void CleanRedundanceScope();
139 
140     bool IsReady();
141     bool HasFailRecognizer(int32_t touchId);
142     void ForceCleanGestureReferee();
143     void ForceCleanGestureRefereeState();
144     void CleanGestureRefereeState(int32_t touchId);
145 private:
146     void HandleAcceptDisposal(const RefPtr<NGGestureRecognizer>& recognizer);
147     void HandlePendingDisposal(const RefPtr<NGGestureRecognizer>& recognizer);
148     void HandleRejectDisposal(const RefPtr<NGGestureRecognizer>& recognizer);
149 
150     // Stores gesture recognizer collection according to Id.
151     std::unordered_map<size_t, RefPtr<GestureScope>> gestureScopes_;
152 
153     std::function<void(size_t)> queryStateFunc_;
154     SourceType lastSourceType_ = SourceType::NONE;
155     bool lastIsAxis_ = false;
156 };
157 
158 } // namespace OHOS::Ace::NG
159 
160 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_GESTURE_REFEREE_H
161