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, ¶m);
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>("rollAngle", info.GetRollAngle().value_or(0.0f));
56 gestureInfoObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
57
58 gestureInfoObj->SetProperty<double>(
59 "velocityX", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityX()));
60 gestureInfoObj->SetProperty<double>(
61 "velocityY", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityY()));
62 gestureInfoObj->SetProperty<double>(
63 "velocity", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityValue()));
64 gestureInfoObj->SetPropertyObject(
65 "getModifierKeyState",
66 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
67 gestureInfoObj->SetPropertyObject("fingerList", CreateFingerListArray(info));
68 gestureInfoObj->SetProperty<double>("deviceId", info.GetDeviceId());
69
70 auto target = CreateEventTargetObject(info);
71 gestureInfoObj->SetPropertyObject("target", target);
72 gestureInfoObj->SetProperty<float>("axisVertical", info.GetVerticalAxis());
73 gestureInfoObj->SetProperty<float>("axisHorizontal", info.GetHorizontalAxis());
74 gestureInfoObj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
75 gestureInfoObj->Wrap<GestureEvent>(const_cast<GestureEvent*> (&info));
76 return gestureInfoObj;
77 }
78
CreateFingerListArray(const GestureEvent & info)79 JSRef<JSArray> JsGestureFunction::CreateFingerListArray(const GestureEvent& info)
80 {
81 JSRef<JSArray> fingerArr = JSRef<JSArray>::New();
82 const std::list<FingerInfo>& fingerList = info.GetFingerList();
83 std::list<FingerInfo> notTouchFingerList;
84 int32_t maxFingerId = -1;
85 for (const FingerInfo& fingerInfo : fingerList) {
86 JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
87 if (fingerInfo.sourceType_ == SourceType::TOUCH && fingerInfo.sourceTool_ == SourceTool::FINGER) {
88 fingerArr->SetValueAt(fingerInfo.fingerId_, element);
89 if (fingerInfo.fingerId_ > maxFingerId) {
90 maxFingerId = fingerInfo.fingerId_;
91 }
92 } else {
93 notTouchFingerList.emplace_back(fingerInfo);
94 }
95 }
96 auto idx = maxFingerId + 1;
97 for (const FingerInfo& fingerInfo : notTouchFingerList) {
98 JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
99 fingerArr->SetValueAt(idx++, element);
100 }
101 return fingerArr;
102 }
103
CreateFingerInfo(const FingerInfo & fingerInfo)104 JSRef<JSObject> JsGestureFunction::CreateFingerInfo(const FingerInfo& fingerInfo)
105 {
106 JSRef<JSObject> fingerInfoObj = JSRef<JSObject>::New();
107 const OHOS::Ace::Offset& globalLocation = fingerInfo.globalLocation_;
108 const OHOS::Ace::Offset& localLocation = fingerInfo.localLocation_;
109 const OHOS::Ace::Offset& screenLocation = fingerInfo.screenLocation_;
110 fingerInfoObj->SetProperty<int32_t>("id", fingerInfo.fingerId_);
111 fingerInfoObj->SetProperty<int32_t>("hand", fingerInfo.operatingHand_);
112 fingerInfoObj->SetProperty<double>("globalX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
113 fingerInfoObj->SetProperty<double>("globalY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
114 fingerInfoObj->SetProperty<double>("localX", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
115 fingerInfoObj->SetProperty<double>("localY", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
116 fingerInfoObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
117 fingerInfoObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
118 return fingerInfoObj;
119 }
120
121 } // namespace OHOS::Ace::Framework
122