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->SetRecognizer(panRecognizer);
55         currentRecognizer->SetPanGestureOptions(
56             panRecognizer->GetFingers(), panRecognizer->GetDistance(), panRecognizer->GetDirection());
57         return recognizerObj;
58     }
59     JSRef<JSObject> recognizerObj = JSClass<JSGestureRecognizer>::NewInstance();
60     auto currentRecognizer = Referenced::Claim(recognizerObj->Unwrap<JSGestureRecognizer>());
61     currentRecognizer->SetRecognizer(target);
62     return recognizerObj;
63 }
64 
JSBind(BindingTarget globalObj)65 void JSEventTargetInfo::JSBind(BindingTarget globalObj)
66 {
67     JSClass<JSEventTargetInfo>::Declare("EventTargetInfo");
68     JSClass<JSEventTargetInfo>::CustomMethod("getId", &JSEventTargetInfo::GetInspectorId);
69     JSClass<JSEventTargetInfo>::Bind(globalObj, &JSEventTargetInfo::Constructor, &JSEventTargetInfo::Destructor);
70 }
71 
JSBind(BindingTarget globalObj)72 void JSScrollableTargetInfo::JSBind(BindingTarget globalObj)
73 {
74     JSClass<JSScrollableTargetInfo>::Declare("ScrollableTargetInfo");
75     JSClass<JSScrollableTargetInfo>::CustomMethod("isBegin", &JSScrollableTargetInfo::IsBegin);
76     JSClass<JSScrollableTargetInfo>::CustomMethod("isEnd", &JSScrollableTargetInfo::IsEnd);
77     JSClass<JSScrollableTargetInfo>::CustomMethod("getId", &JSEventTargetInfo::GetInspectorId);
78     JSClass<JSScrollableTargetInfo>::Inherit<JSEventTargetInfo>();
79     JSClass<JSScrollableTargetInfo>::Bind(
80         globalObj, &JSScrollableTargetInfo::Constructor, &JSScrollableTargetInfo::Destructor);
81 }
82 
JSBind(BindingTarget globalObj)83 void JSGestureRecognizer::JSBind(BindingTarget globalObj)
84 {
85     JSClass<JSGestureRecognizer>::Declare("GestureRecognizer");
86     JSClass<JSGestureRecognizer>::CustomMethod("getTag", &JSGestureRecognizer::GetTag);
87     JSClass<JSGestureRecognizer>::CustomMethod("getType", &JSGestureRecognizer::GetType);
88     JSClass<JSGestureRecognizer>::CustomMethod("isBuiltIn", &JSGestureRecognizer::IsBuiltInRecognizer);
89     JSClass<JSGestureRecognizer>::CustomMethod("setEnabled", &JSGestureRecognizer::SetEnabled);
90     JSClass<JSGestureRecognizer>::CustomMethod("isEnabled", &JSGestureRecognizer::IsEnabled);
91     JSClass<JSGestureRecognizer>::CustomMethod("getEventTargetInfo", &JSGestureRecognizer::GetEventTargetInfo);
92     JSClass<JSGestureRecognizer>::CustomMethod("getState", &JSGestureRecognizer::GetRefereeState);
93     JSClass<JSGestureRecognizer>::CustomMethod("isValid", &JSGestureRecognizer::IsValid);
94     JSClass<JSGestureRecognizer>::Bind(
95         globalObj, &JSGestureRecognizer::Constructor, &JSGestureRecognizer::Destructor);
96 }
97 
JSBind(BindingTarget globalObj)98 void JSPanRecognizer::JSBind(BindingTarget globalObj)
99 {
100     JSClass<JSPanRecognizer>::Declare("PanRecognizer");
101     JSClass<JSPanRecognizer>::CustomMethod("getTag", &JSGestureRecognizer::GetTag);
102     JSClass<JSPanRecognizer>::CustomMethod("getType", &JSGestureRecognizer::GetType);
103     JSClass<JSPanRecognizer>::CustomMethod("isBuiltIn", &JSGestureRecognizer::IsBuiltInRecognizer);
104     JSClass<JSPanRecognizer>::CustomMethod("setEnabled", &JSGestureRecognizer::SetEnabled);
105     JSClass<JSPanRecognizer>::CustomMethod("isEnabled", &JSGestureRecognizer::IsEnabled);
106     JSClass<JSPanRecognizer>::CustomMethod("getEventTargetInfo", &JSGestureRecognizer::GetEventTargetInfo);
107     JSClass<JSPanRecognizer>::CustomMethod("getState", &JSGestureRecognizer::GetRefereeState);
108     JSClass<JSPanRecognizer>::CustomMethod("getPanGestureOptions", &JSPanRecognizer::GetPanGestureOptions);
109     JSClass<JSPanRecognizer>::CustomMethod("isValid", &JSGestureRecognizer::IsValid);
110     JSClass<JSPanRecognizer>::Inherit<JSGestureRecognizer>();
111     JSClass<JSPanRecognizer>::Bind(globalObj, &JSPanRecognizer::Constructor, &JSPanRecognizer::Destructor);
112 }
113 
114 } // namespace OHOS::Ace::Framework
115