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 #include "frameworks/bridge/declarative_frontend/engine/functions/js_gesture_function.h"
17 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
18 
19 #include "base/log/log.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
21 
22 namespace OHOS::Ace::Framework {
23 
Execute()24 void JsGestureFunction::Execute()
25 {
26     JsFunction::Execute();
27 }
28 
Execute(const GestureEvent & info)29 void JsGestureFunction::Execute(const GestureEvent& info)
30 {
31     JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateGestureEvent(info));
32     JsFunction::ExecuteJS(1, &param);
33 }
34 
CreateGestureEvent(const GestureEvent & info)35 JSRef<JSObject> JsGestureFunction::CreateGestureEvent(const GestureEvent& info)
36 {
37     JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
38     objectTemplate->SetInternalFieldCount(1);
39     JSRef<JSObject> gestureInfoObj = objectTemplate->NewInstance();
40     gestureInfoObj->SetProperty<bool>("repeat", info.GetRepeat());
41     gestureInfoObj->SetProperty<double>("offsetX", PipelineBase::Px2VpWithCurrentDensity(info.GetOffsetX()));
42     gestureInfoObj->SetProperty<double>("offsetY", PipelineBase::Px2VpWithCurrentDensity(info.GetOffsetY()));
43     gestureInfoObj->SetProperty<double>("scale", info.GetScale());
44     gestureInfoObj->SetProperty<double>("angle", info.GetAngle());
45     gestureInfoObj->SetProperty<double>("speed", info.GetSpeed());
46     gestureInfoObj->SetProperty<double>("timestamp", info.GetTimeStamp().time_since_epoch().count());
47     gestureInfoObj->SetProperty<double>(
48         "pinchCenterX", PipelineBase::Px2VpWithCurrentDensity(info.GetPinchCenter().GetX()));
49     gestureInfoObj->SetProperty<double>(
50         "pinchCenterY", PipelineBase::Px2VpWithCurrentDensity(info.GetPinchCenter().GetY()));
51     gestureInfoObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
52     gestureInfoObj->SetProperty<double>("pressure", info.GetForce());
53     gestureInfoObj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
54     gestureInfoObj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
55     gestureInfoObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
56 
57     gestureInfoObj->SetProperty<double>(
58         "velocityX", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityX()));
59     gestureInfoObj->SetProperty<double>(
60         "velocityY", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityY()));
61     gestureInfoObj->SetProperty<double>(
62         "velocity", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityValue()));
63     gestureInfoObj->SetPropertyObject(
64         "getModifierKeyState",
65         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
66     gestureInfoObj->SetPropertyObject("fingerList", CreateFingerListArray(info));
67     gestureInfoObj->SetProperty<double>("deviceId", info.GetDeviceId());
68 
69     auto target = CreateEventTargetObject(info);
70     gestureInfoObj->SetPropertyObject("target", target);
71     gestureInfoObj->SetProperty<float>("axisVertical", info.GetVerticalAxis());
72     gestureInfoObj->SetProperty<float>("axisHorizontal", info.GetHorizontalAxis());
73     gestureInfoObj->Wrap<GestureEvent>(const_cast<GestureEvent*> (&info));
74     return gestureInfoObj;
75 }
76 
CreateFingerListArray(const GestureEvent & info)77 JSRef<JSArray> JsGestureFunction::CreateFingerListArray(const GestureEvent& info)
78 {
79     JSRef<JSArray> fingerArr = JSRef<JSArray>::New();
80     const std::list<FingerInfo>& fingerList = info.GetFingerList();
81     std::list<FingerInfo> notTouchFingerList;
82     int32_t maxFingerId = -1;
83     for (const FingerInfo& fingerInfo : fingerList) {
84         JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
85         if (fingerInfo.sourceType_ == SourceType::TOUCH && fingerInfo.sourceTool_ == SourceTool::FINGER) {
86             fingerArr->SetValueAt(fingerInfo.fingerId_, element);
87             if (fingerInfo.fingerId_ > maxFingerId) {
88                 maxFingerId = fingerInfo.fingerId_;
89             }
90         } else {
91             notTouchFingerList.emplace_back(fingerInfo);
92         }
93     }
94     auto idx = maxFingerId + 1;
95     for (const FingerInfo& fingerInfo : notTouchFingerList) {
96         JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
97         fingerArr->SetValueAt(idx++, element);
98     }
99     return fingerArr;
100 }
101 
CreateFingerInfo(const FingerInfo & fingerInfo)102 JSRef<JSObject> JsGestureFunction::CreateFingerInfo(const FingerInfo& fingerInfo)
103 {
104     JSRef<JSObject> fingerInfoObj = JSRef<JSObject>::New();
105     const OHOS::Ace::Offset& globalLocation = fingerInfo.globalLocation_;
106     const OHOS::Ace::Offset& localLocation = fingerInfo.localLocation_;
107     const OHOS::Ace::Offset& screenLocation  = fingerInfo.screenLocation_;
108     fingerInfoObj->SetProperty<int32_t>("id", fingerInfo.fingerId_);
109     fingerInfoObj->SetProperty<double>("globalX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
110     fingerInfoObj->SetProperty<double>("globalY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
111     fingerInfoObj->SetProperty<double>("localX", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
112     fingerInfoObj->SetProperty<double>("localY", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
113     fingerInfoObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
114     fingerInfoObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
115     return fingerInfoObj;
116 }
117 
118 } // namespace OHOS::Ace::Framework
119