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_GESTURES_GESTURE_REFEREE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_REFEREE_H
18 
19 #include <functional>
20 #include <list>
21 #include <unordered_map>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/utils/singleton.h"
25 #include "core/event/touch_event.h"
26 
27 namespace OHOS::Ace {
28 
29 class GestureRecognizer;
30 
31 enum class GestureDisposal {
32     ACCEPT = 0,
33     REJECT,
34     PENDING,
35 };
36 
37 class GestureScope {
38 public:
GestureScope(size_t touchId)39     explicit GestureScope(size_t touchId) : touchId_(touchId) {}
40     ~GestureScope() = default;
41 
42     void AddMember(const RefPtr<GestureRecognizer>& recognizer);
43     void DelMember(const RefPtr<GestureRecognizer>& recognizer);
44 
45     void ForceClose();
46 
47     void HandleGestureDisposal(const RefPtr<GestureRecognizer>& recognizer, GestureDisposal disposal);
48 
IsEmpty()49     bool IsEmpty() const
50     {
51         return highRecognizers_.empty() && lowRecognizers_.empty() && parallelRecognizers_.empty();
52     }
53 
54     bool IsPending() const;
55 
SetQueryStateFunc(const std::function<bool (size_t)> & queryStateFunc)56     void SetQueryStateFunc(const std::function<bool(size_t)>& queryStateFunc)
57     {
58         queryStateFunc_ = queryStateFunc;
59     }
60 
GetHighRecognizers()61     const std::list<WeakPtr<GestureRecognizer>>& GetHighRecognizers() const
62     {
63         return highRecognizers_;
64     }
65 
GetLowRecognizers()66     const std::list<WeakPtr<GestureRecognizer>>& GetLowRecognizers() const
67     {
68         return lowRecognizers_;
69     }
70 
GetParallelRecognizers()71     const std::list<WeakPtr<GestureRecognizer>>& GetParallelRecognizers() const
72     {
73         return parallelRecognizers_;
74     }
75 
76 private:
77     bool Existed(const RefPtr<GestureRecognizer>& recognizer);
78     const std::list<WeakPtr<GestureRecognizer>>& GetMembersByRecognizer(const RefPtr<GestureRecognizer>& recognizer);
79     bool CheckNeedBlocked(const RefPtr<GestureRecognizer>& recognizer);
80     void AcceptGesture(const RefPtr<GestureRecognizer>& recognizer);
81     void UnBlockGesture(std::list<WeakPtr<GestureRecognizer>>& members);
82     void HandleParallelDisposal(const RefPtr<GestureRecognizer>& recognizer, GestureDisposal disposal);
83     void HandleAcceptDisposal(const RefPtr<GestureRecognizer>& recognizer);
84     void HandlePendingDisposal(const RefPtr<GestureRecognizer>& recognizer);
85     void HandleRejectDisposal(const RefPtr<GestureRecognizer>& recognizer);
86     void RemoveAndUnBlockGesture(bool isPrevPending, const WeakPtr<GestureRecognizer>& recognizer);
87 
88     size_t touchId_ = 0;
89 
90     std::list<WeakPtr<GestureRecognizer>> highRecognizers_;
91     std::list<WeakPtr<GestureRecognizer>> lowRecognizers_;
92     std::list<WeakPtr<GestureRecognizer>> parallelRecognizers_;
93 
94     std::function<bool(size_t)> queryStateFunc_;
95 };
96 
97 class GestureReferee : public virtual AceType {
98     DECLARE_ACE_TYPE(GestureReferee, AceType);
99 
100 public:
101     GestureReferee() = default;
102     ~GestureReferee() override = default;
103 
104     // Each gesture recognizer should add itself to the gesture scope at the beginning of the gesture sequence
105     // (touch down event) for gesture adjudicating.
106     void AddGestureRecognizer(size_t touchId, const RefPtr<GestureRecognizer>& recognizer);
107 
108     // In multi finger recognizers, touch up one finger does not result to the whole gesture failed. Recognizer should
109     // remove the recognizer out of the referee.
110     void DelGestureRecognizer(size_t touchId, const RefPtr<GestureRecognizer>& recognizer);
111 
112     // Try to clean gesture scope when receive cancel event.
113     void CleanGestureScope(size_t touchId);
114 
115     // Called by the gesture recognizer when the gesture recognizer has completed the recognition of the gesture (accept
116     // or reject)
117     void Adjudicate(size_t touchId, const RefPtr<GestureRecognizer>& recognizer, GestureDisposal disposal);
118 
SetQueryStateFunc(std::function<bool (size_t)> && queryStateFunc)119     void SetQueryStateFunc(std::function<bool(size_t)>&& queryStateFunc)
120     {
121         queryStateFunc_ = queryStateFunc;
122     }
123 
GetGestureScope()124     const std::unordered_map<size_t, GestureScope>& GetGestureScope() const
125     {
126         return gestureScopes_;
127     }
128 
129 private:
130     // Stores gesture recognizer collection according to Id.
131     std::unordered_map<size_t, GestureScope> gestureScopes_;
132 
133     std::function<bool(size_t)> queryStateFunc_;
134 };
135 
136 } // namespace OHOS::Ace
137 
138 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_REFEREE_H
139