1 /*
2 * Copyright (c) 2024 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 #include "frameworks/bridge/declarative_frontend/engine/functions/js_should_built_in_recognizer_parallel_with_function.h"
17
18 #include "core/components_ng/gestures/recognizers/pan_recognizer.h"
19
20 namespace OHOS::Ace::Framework {
21
Execute(const RefPtr<NG::NGGestureRecognizer> & current,const std::vector<RefPtr<NG::NGGestureRecognizer>> & others)22 RefPtr<NG::NGGestureRecognizer> JsShouldBuiltInRecognizerParallelWithFunction::Execute(
23 const RefPtr<NG::NGGestureRecognizer>& current, const std::vector<RefPtr<NG::NGGestureRecognizer>>& others)
24 {
25 CHECK_NULL_RETURN(current, nullptr);
26 auto currentObj = CreateRecognizerObject(current);
27 JSRef<JSArray> othersArr = JSRef<JSArray>::New();
28 uint32_t idx = 0;
29 for (const auto& item : others) {
30 auto othersObj = CreateRecognizerObject(item);
31 othersArr->SetValueAt(idx++, othersObj);
32 }
33 int32_t paramCount = 2;
34 JSRef<JSVal> params[paramCount];
35 params[0] = currentObj;
36 params[1] = othersArr;
37 auto jsValue = JsFunction::ExecuteJS(paramCount, params);
38 if (!jsValue->IsObject()) {
39 return nullptr;
40 }
41 RefPtr<NG::NGGestureRecognizer> returnValue = nullptr;
42 auto jsObj = JSRef<JSObject>::Cast(jsValue);
43 returnValue = Referenced::Claim(jsObj->Unwrap<JSGestureRecognizer>())->GetRecognizer().Upgrade();
44 return returnValue;
45 }
46
CreateRecognizerObject(const RefPtr<NG::NGGestureRecognizer> & target)47 JSRef<JSObject> JsShouldBuiltInRecognizerParallelWithFunction::CreateRecognizerObject(
48 const RefPtr<NG::NGGestureRecognizer>& target)
49 {
50 auto panRecognizer = AceType::DynamicCast<NG::PanRecognizer>(target);
51 if (panRecognizer) {
52 JSRef<JSObject> recognizerObj = JSClass<JSPanRecognizer>::NewInstance();
53 auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSPanRecognizer>());
54 currentRecognizer->Update(panRecognizer);
55 return recognizerObj;
56 }
57 auto pinchRecognizer = AceType::DynamicCast<NG::PinchRecognizer>(target);
58 if (pinchRecognizer) {
59 JSRef<JSObject> recognizerObj = JSClass<JSPinchRecognizer>::NewInstance();
60 auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSPinchRecognizer>());
61 currentRecognizer->Update(pinchRecognizer);
62 return recognizerObj;
63 }
64 auto tapRecognizer = AceType::DynamicCast<NG::ClickRecognizer>(target);
65 if (tapRecognizer) {
66 JSRef<JSObject> recognizerObj = JSClass<JSTapRecognizer>::NewInstance();
67 auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSTapRecognizer>());
68 currentRecognizer->Update(tapRecognizer);
69 return recognizerObj;
70 }
71 auto longPressRecognizer = AceType::DynamicCast<NG::LongPressRecognizer>(target);
72 if (longPressRecognizer) {
73 JSRef<JSObject> recognizerObj = JSClass<JSLongPressRecognizer>::NewInstance();
74 auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSLongPressRecognizer>());
75 currentRecognizer->Update(longPressRecognizer);
76 return recognizerObj;
77 }
78 auto rotationRecognizer = AceType::DynamicCast<NG::RotationRecognizer>(target);
79 if (rotationRecognizer) {
80 JSRef<JSObject> recognizerObj = JSClass<JSRotationRecognizer>::NewInstance();
81 auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSRotationRecognizer>());
82 currentRecognizer->Update(rotationRecognizer);
83 return recognizerObj;
84 }
85 auto swipeRecognizer = AceType::DynamicCast<NG::SwipeRecognizer>(target);
86 if (swipeRecognizer) {
87 JSRef<JSObject> recognizerObj = JSClass<JSSwipeRecognizer>::NewInstance();
88 auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSSwipeRecognizer>());
89 currentRecognizer->Update(swipeRecognizer);
90 return recognizerObj;
91 }
92 JSRef<JSObject> recognizerObj = JSClass<JSGestureRecognizer>::NewInstance();
93 auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSGestureRecognizer>());
94 currentRecognizer->Update(target);
95 return recognizerObj;
96 }
97 } // namespace OHOS::Ace::Framework
98