• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CheckGestureScopeState();
96     void ForceCleanGestureScope();
97     void CleanGestureScopeState();
98 private:
99     bool Existed(const RefPtr<NGGestureRecognizer>& recognizer);
100     std::list<WeakPtr<NGGestureRecognizer>> recognizers_;
101 
102     size_t touchId_ = 0;
103     bool isDelay_ = false;
104     bool hasGestureAccepted_ = false;
105 
106     std::function<void(size_t)> queryStateFunc_;
107 };
108 
109 class GestureReferee : public virtual AceType {
110     DECLARE_ACE_TYPE(GestureReferee, AceType);
111 
112 public:
113     GestureReferee() = default;
114     ~GestureReferee() override = default;
115 
116     void AddGestureToScope(size_t touchId, const TouchTestResult& result);
117 
118     // Try to clean gesture scope when receive cancel event.
119     void CleanGestureScope(size_t touchId);
120 
121     // Called by the gesture recognizer when the gesture recognizer has completed the recognition of the gesture (accept
122     // or reject)
123     void Adjudicate(const RefPtr<NGGestureRecognizer>& recognizer, GestureDisposal disposal);
124 
125     bool HasGestureAccepted(size_t touchId) const;
126 
SetQueryStateFunc(std::function<void (size_t)> && queryStateFunc)127     void SetQueryStateFunc(std::function<void(size_t)>&& queryStateFunc)
128     {
129         queryStateFunc_ = queryStateFunc;
130     }
131     bool QueryAllDone(size_t touchId);
132     bool QueryAllDone();
133     bool CheckSourceTypeChange(SourceType type, bool isAxis = false);
134     void CleanAll(bool isBlocked = false);
135     void CleanRedundanceScope();
136 
137     bool CheckGestureRefereeState();
138     void ForceCleanGestureReferee();
139     void CleanGestureRefereeState(int32_t touchId);
140 private:
141     void HandleAcceptDisposal(const RefPtr<NGGestureRecognizer>& recognizer);
142     void HandlePendingDisposal(const RefPtr<NGGestureRecognizer>& recognizer);
143     void HandleRejectDisposal(const RefPtr<NGGestureRecognizer>& recognizer);
144 
145     // Stores gesture recognizer collection according to Id.
146     std::unordered_map<size_t, RefPtr<GestureScope>> gestureScopes_;
147 
148     std::function<void(size_t)> queryStateFunc_;
149     SourceType lastSourceType_ = SourceType::NONE;
150     bool lastIsAxis_ = false;
151 };
152 
153 } // namespace OHOS::Ace::NG
154 
155 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_GESTURE_REFEREE_H
156