• 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_GESTURE_RECOGNIZER_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_GESTURE_RECOGNIZER_H
18 
19 #include <cstdint>
20 
21 #include "base/memory/referenced.h"
22 #include "bridge/declarative_frontend/jsview/js_gesture.h"
23 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
24 #include "core/components_ng/pattern/scrollable/scrollable_pattern.h"
25 #include "core/components_ng/pattern/swiper/swiper_pattern.h"
26 #include "core/gestures/gesture_info.h"
27 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
28 #include "frameworks/core/components_ng/gestures/recognizers/pinch_recognizer.h"
29 #include "frameworks/core/components_ng/gestures/recognizers/rotation_recognizer.h"
30 #include "frameworks/core/components_ng/gestures/recognizers/swipe_recognizer.h"
31 
32 namespace OHOS::Ace::Framework {
33 enum class GestureRecognizerState {
34     UNKNOWN = -1,
35     READY = 0,
36     DETECTING = 1,
37     PENDING = 2,
38     BLOCKED = 3,
39     SUCCEED = 4,
40     FAIL = 5,
41 };
42 
43 class JSEventTargetInfo : public Referenced {
44 public:
45     static void JSBind(BindingTarget globalObj);
46 
SetInspectorId(const std::string & inspectorId)47     void SetInspectorId(const std::string& inspectorId)
48     {
49         inspectorId_ = inspectorId;
50     }
51 
GetInspectorId(const JSCallbackInfo & args)52     void GetInspectorId(const JSCallbackInfo& args)
53     {
54         args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(inspectorId_)));
55     }
56 
57 private:
Constructor(const JSCallbackInfo & args)58     static void Constructor(const JSCallbackInfo& args)
59     {
60         auto jsEventTarget = Referenced::MakeRefPtr<JSEventTargetInfo>();
61         jsEventTarget->IncRefCount();
62         args.SetReturnValue(Referenced::RawPtr(jsEventTarget));
63     }
64 
Destructor(JSEventTargetInfo * jsEventTarget)65     static void Destructor(JSEventTargetInfo* jsEventTarget)
66     {
67         if (jsEventTarget != nullptr) {
68             jsEventTarget->DecRefCount();
69         }
70     }
71 
72     std::string inspectorId_;
73 };
74 
75 class JSScrollableTargetInfo : public JSEventTargetInfo {
76 public:
77     static void JSBind(BindingTarget globalObj);
78 
SetPattern(const WeakPtr<NG::Pattern> & pattern)79     void SetPattern(const WeakPtr<NG::Pattern>& pattern)
80     {
81         pattern_ = pattern;
82     }
83 
84     // use for ArkTs1.2 interop
GetPatternPointer()85     int64_t GetPatternPointer()
86     {
87         auto pattern = pattern_.Upgrade();
88         CHECK_NULL_RETURN(pattern, 0);
89         return reinterpret_cast<int64_t>(AceType::RawPtr(pattern));
90     }
91 
92     void IsBegin(const JSCallbackInfo& args);
93 
94     void IsEnd(const JSCallbackInfo& args);
95 
96 private:
Constructor(const JSCallbackInfo & args)97     static void Constructor(const JSCallbackInfo& args)
98     {
99         auto jsScrollableTargetInfo = Referenced::MakeRefPtr<JSScrollableTargetInfo>();
100         jsScrollableTargetInfo->IncRefCount();
101         args.SetReturnValue(Referenced::RawPtr(jsScrollableTargetInfo));
102     }
103 
Destructor(JSScrollableTargetInfo * jsScrollableTargetInfo)104     static void Destructor(JSScrollableTargetInfo* jsScrollableTargetInfo)
105     {
106         if (jsScrollableTargetInfo != nullptr) {
107             jsScrollableTargetInfo->DecRefCount();
108         }
109     }
110 
111     WeakPtr<NG::Pattern> pattern_;
112 };
113 
114 class JSGestureRecognizer : public Referenced {
115 public:
116     static void JSBind(BindingTarget globalObj);
117 
Update(const RefPtr<NG::NGGestureRecognizer> & recognizer)118     void Update(const RefPtr<NG::NGGestureRecognizer>& recognizer)
119     {
120         recognizer_ = recognizer;
121     }
122 
GetRecognizer()123     WeakPtr<NG::NGGestureRecognizer> GetRecognizer()
124     {
125         return recognizer_;
126     }
127 
128     void GetTag(const JSCallbackInfo& args);
129 
130     void GetType(const JSCallbackInfo& args);
131 
132     void GetFingers(const JSCallbackInfo& args);
133 
134     void IsFingerCountLimit(const JSCallbackInfo& args);
135 
136     void IsBuiltInRecognizer(const JSCallbackInfo& args);
137 
138     void SetEnabled(const JSCallbackInfo& args);
139 
140     void PreventBegin(const JSCallbackInfo& args);
141 
142     void IsEnabled(const JSCallbackInfo& args);
143 
144     void GetEventTargetInfo(const JSCallbackInfo& args);
145 
146     void GetRefereeState(const JSCallbackInfo& args);
147 
148     void IsValid(const JSCallbackInfo& args);
149 
ConvertRefereeState(NG::RefereeState state)150     static GestureRecognizerState ConvertRefereeState(NG::RefereeState state)
151     {
152         switch (state) {
153             case NG::RefereeState::READY:
154                 return GestureRecognizerState::READY;
155             case NG::RefereeState::DETECTING:
156                 return GestureRecognizerState::DETECTING;
157             case NG::RefereeState::PENDING:
158                 return GestureRecognizerState::PENDING;
159             case NG::RefereeState::PENDING_BLOCKED:
160             case NG::RefereeState::SUCCEED_BLOCKED:
161                 return GestureRecognizerState::BLOCKED;
162             case NG::RefereeState::SUCCEED:
163                 return GestureRecognizerState::SUCCEED;
164             case NG::RefereeState::FAIL:
165                 return GestureRecognizerState::FAIL;
166             default:
167                 return GestureRecognizerState::UNKNOWN;
168         }
169     }
170 
171 private:
Constructor(const JSCallbackInfo & args)172     static void Constructor(const JSCallbackInfo& args)
173     {
174         auto jsGestureRecognizer = Referenced::MakeRefPtr<JSGestureRecognizer>();
175         jsGestureRecognizer->IncRefCount();
176         args.SetReturnValue(Referenced::RawPtr(jsGestureRecognizer));
177     }
178 
Destructor(JSGestureRecognizer * jsGestureRecognizer)179     static void Destructor(JSGestureRecognizer* jsGestureRecognizer)
180     {
181         if (jsGestureRecognizer != nullptr) {
182             jsGestureRecognizer->DecRefCount();
183         }
184     }
185 
186     WeakPtr<NG::NGGestureRecognizer> recognizer_;
187 
188 protected:
189     bool isLimitFingerCount_ = false;
190     int fingers_ = 1;
191 };
192 
193 class JSMultiFingerRecognizer : public JSGestureRecognizer {
194 public:
Update(const RefPtr<NG::MultiFingersRecognizer> & recognizer)195     void Update(const RefPtr<NG::MultiFingersRecognizer>& recognizer)
196     {
197         JSGestureRecognizer::Update(recognizer);
198         isLimitFingerCount_ = recognizer->GetLimitFingerCount();
199         fingers_ = recognizer->GetFingers();
200     }
201 };
202 
203 class JSPanRecognizer : public JSMultiFingerRecognizer {
204 public:
205     static void JSBind(BindingTarget globalObj);
206 
Update(const RefPtr<NG::PanRecognizer> & recognizer)207     void Update(const RefPtr<NG::PanRecognizer>& recognizer)
208     {
209         JSMultiFingerRecognizer::Update(recognizer);
210         SetPanGestureOptions(
211             recognizer->GetFingers(), recognizer->GetDistance(), recognizer->GetDirection());
212         direction_ = recognizer->GetDirection();
213         distance_ = recognizer->GetDistance();
214     }
215 
GetPanGestureOptions(const JSCallbackInfo & args)216     void GetPanGestureOptions(const JSCallbackInfo& args)
217     {
218         JSRef<JSObject> panGestureOptionObj = JSClass<JSPanGestureOption>::NewInstance();
219         auto panGestureOption = Referenced::Claim(panGestureOptionObj->Unwrap<JSPanGestureOption>());
220         panGestureOption->SetPanGestureOption(panGestureOption_);
221         args.SetReturnValue(panGestureOptionObj);
222     }
223 
224     void GetDirection(const JSCallbackInfo& args);
225 
226     void GetPanDistance(const JSCallbackInfo& args);
227 
228     void GetPanDistanceMap(const JSCallbackInfo& args);
229 private:
Constructor(const JSCallbackInfo & args)230     static void Constructor(const JSCallbackInfo& args)
231     {
232         auto jsPanRecognizer = Referenced::MakeRefPtr<JSPanRecognizer>();
233         jsPanRecognizer->IncRefCount();
234         args.SetReturnValue(Referenced::RawPtr(jsPanRecognizer));
235     }
236 
Destructor(JSPanRecognizer * jsPanRecognizer)237     static void Destructor(JSPanRecognizer* jsPanRecognizer)
238     {
239         if (jsPanRecognizer != nullptr) {
240             jsPanRecognizer->DecRefCount();
241         }
242     }
243 
SetPanGestureOptions(int32_t fingers,double distance,PanDirection direction)244     void SetPanGestureOptions(int32_t fingers, double distance, PanDirection direction)
245     {
246         panGestureOption_ = AceType::MakeRefPtr<PanGestureOption>();
247         panGestureOption_->SetFingers(fingers);
248         panGestureOption_->SetDistance(distance);
249         panGestureOption_->SetDirection(direction);
250     }
251     RefPtr<PanGestureOption> panGestureOption_;
252     PanDirection direction_;
253     double distance_;
254 };
255 
256 class JSPinchRecognizer : public JSMultiFingerRecognizer {
257 public:
258     static void JSBind(BindingTarget globalObj);
259 
Update(const RefPtr<NG::PinchRecognizer> & recognizer)260     void Update(const RefPtr<NG::PinchRecognizer>& recognizer)
261     {
262         JSMultiFingerRecognizer::Update(recognizer);
263         distance_ = recognizer->GetDistance();
264     }
265 
266     void GetDistance(const JSCallbackInfo& args);
267 
268 private:
Constructor(const JSCallbackInfo & args)269     static void Constructor(const JSCallbackInfo& args)
270     {
271         auto jsPinchRecognizer = Referenced::MakeRefPtr<JSPinchRecognizer>();
272         jsPinchRecognizer->IncRefCount();
273         args.SetReturnValue(Referenced::RawPtr(jsPinchRecognizer));
274     }
275 
Destructor(JSPinchRecognizer * jsPinchRecognizer)276     static void Destructor(JSPinchRecognizer* jsPinchRecognizer)
277     {
278         if (jsPinchRecognizer != nullptr) {
279             jsPinchRecognizer->DecRefCount();
280         }
281     }
282 
283     double distance_ = 5;
284 };
285 
286 class JSTapRecognizer : public JSMultiFingerRecognizer {
287 public:
288     static void JSBind(BindingTarget globalObj);
289 
Update(const RefPtr<NG::ClickRecognizer> & recognizer)290     void Update(const RefPtr<NG::ClickRecognizer>& recognizer)
291     {
292         JSMultiFingerRecognizer::Update(recognizer);
293         count_ = recognizer->GetCount();
294     }
295 
296     void GetCount(const JSCallbackInfo& args);
297 
298 private:
Constructor(const JSCallbackInfo & args)299     static void Constructor(const JSCallbackInfo& args)
300     {
301         auto jsTapRecognizer = Referenced::MakeRefPtr<JSTapRecognizer>();
302         jsTapRecognizer->IncRefCount();
303         args.SetReturnValue(Referenced::RawPtr(jsTapRecognizer));
304     }
305 
Destructor(JSTapRecognizer * jsTapRecognizer)306     static void Destructor(JSTapRecognizer* jsTapRecognizer)
307     {
308         if (jsTapRecognizer != nullptr) {
309             jsTapRecognizer->DecRefCount();
310         }
311     }
312 
313     int32_t count_ = 1;
314 };
315 
316 class JSLongPressRecognizer : public JSMultiFingerRecognizer {
317 public:
318     static void JSBind(BindingTarget globalObj);
319 
Update(const RefPtr<NG::LongPressRecognizer> & recognizer)320     void Update(const RefPtr<NG::LongPressRecognizer>& recognizer)
321     {
322         JSMultiFingerRecognizer::Update(recognizer);
323         duration_ = recognizer->GetDuration();
324         repeat_ = recognizer->GetIsRepeat();
325     }
326 
327     void GetDuration(const JSCallbackInfo& args);
328     void GetRepeat(const JSCallbackInfo& args);
329     void SetRepeat(bool repeat);
330 
331 private:
Constructor(const JSCallbackInfo & args)332     static void Constructor(const JSCallbackInfo& args)
333     {
334         auto jsLongPressRecognizer = Referenced::MakeRefPtr<JSLongPressRecognizer>();
335         jsLongPressRecognizer->IncRefCount();
336         args.SetReturnValue(Referenced::RawPtr(jsLongPressRecognizer));
337     }
338 
Destructor(JSLongPressRecognizer * jsLongPressRecognizer)339     static void Destructor(JSLongPressRecognizer* jsLongPressRecognizer)
340     {
341         if (jsLongPressRecognizer != nullptr) {
342             jsLongPressRecognizer->DecRefCount();
343         }
344     }
345 
346     int32_t duration_ = 500;
347     bool repeat_ = false;
348 };
349 
350 class JSRotationRecognizer : public JSMultiFingerRecognizer {
351 public:
352     static void JSBind(BindingTarget globalObj);
353 
Update(const RefPtr<NG::RotationRecognizer> & recognizer)354     void Update(const RefPtr<NG::RotationRecognizer>& recognizer)
355     {
356         JSMultiFingerRecognizer::Update(recognizer);
357         angle_ = recognizer->GetAngle();
358     }
359 
360     void GetAngle(const JSCallbackInfo& args);
361 
362 private:
Constructor(const JSCallbackInfo & args)363     static void Constructor(const JSCallbackInfo& args)
364     {
365         auto jsRotationRecognizer = Referenced::MakeRefPtr<JSRotationRecognizer>();
366         jsRotationRecognizer->IncRefCount();
367         args.SetReturnValue(Referenced::RawPtr(jsRotationRecognizer));
368     }
369 
Destructor(JSRotationRecognizer * jsRotationRecognizer)370     static void Destructor(JSRotationRecognizer* jsRotationRecognizer)
371     {
372         if (jsRotationRecognizer != nullptr) {
373             jsRotationRecognizer->DecRefCount();
374         }
375     }
376 
377     double angle_ = 1.0;
378 };
379 
380 class JSSwipeRecognizer : public JSMultiFingerRecognizer {
381 public:
382     static void JSBind(BindingTarget globalObj);
383 
Update(const RefPtr<NG::SwipeRecognizer> & recognizer)384     void Update(const RefPtr<NG::SwipeRecognizer>& recognizer)
385     {
386         JSMultiFingerRecognizer::Update(recognizer);
387         direction_ = recognizer->GetDirection();
388         speed_ = recognizer->GetSpeed();
389     }
390 
391     void GetDirection(const JSCallbackInfo& args);
392     void GetSpeed(const JSCallbackInfo& args);
393 
394 private:
Constructor(const JSCallbackInfo & args)395     static void Constructor(const JSCallbackInfo& args)
396     {
397         auto jsSwipeRecognizer = Referenced::MakeRefPtr<JSSwipeRecognizer>();
398         jsSwipeRecognizer->IncRefCount();
399         args.SetReturnValue(Referenced::RawPtr(jsSwipeRecognizer));
400     }
401 
Destructor(JSSwipeRecognizer * jsSwipeRecognizer)402     static void Destructor(JSSwipeRecognizer* jsSwipeRecognizer)
403     {
404         if (jsSwipeRecognizer != nullptr) {
405             jsSwipeRecognizer->DecRefCount();
406         }
407     }
408 
409     SwipeDirection direction_;
410     double speed_ = 100.0;
411 };
412 
413 class JSTouchRecognizer : public Referenced {
414 public:
415     static void JSBind(BindingTarget globalObj);
416 
417     void GetEventTargetInfo(const JSCallbackInfo& args);
418     void CancelTouch(const JSCallbackInfo& args);
SetTouchData(const WeakPtr<TouchEventTarget> & target,const std::unordered_set<int32_t> & fingerIds)419     void SetTouchData(const WeakPtr<TouchEventTarget>& target, const std::unordered_set<int32_t>& fingerIds)
420     {
421         target_ = std::move(target);
422         fingerIds_ = std::move(fingerIds);
423     }
424 
425 private:
Constructor(const JSCallbackInfo & args)426     static void Constructor(const JSCallbackInfo& args)
427     {
428         auto jsTouchRecognizer = Referenced::MakeRefPtr<JSTouchRecognizer>();
429         jsTouchRecognizer->IncRefCount();
430         args.SetReturnValue(Referenced::RawPtr(jsTouchRecognizer));
431     }
432 
Destructor(JSTouchRecognizer * jsTouchRecognizer)433     static void Destructor(JSTouchRecognizer* jsTouchRecognizer)
434     {
435         if (jsTouchRecognizer != nullptr) {
436             jsTouchRecognizer->DecRefCount();
437         }
438     }
439 
440     WeakPtr<TouchEventTarget> target_;
441     std::unordered_set<int32_t> fingerIds_;
442 };
443 } // namespace OHOS::Ace::Framework
444 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_GESTURE_RECOGNIZER_H