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 #include "frameworks/bridge/declarative_frontend/engine/functions/js_common_utils.h"
22
23 namespace OHOS::Ace::Framework {
24 using namespace OHOS::Ace::Framework::CommonUtils;
Execute()25 void JsGestureFunction::Execute()
26 {
27 JsFunction::Execute();
28 }
29
Execute(const GestureEvent & info)30 void JsGestureFunction::Execute(const GestureEvent& info)
31 {
32 JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateGestureEvent(info));
33 JsFunction::ExecuteJS(1, ¶m);
34 }
35
CreateGestureEvent(const GestureEvent & info)36 JSRef<JSObject> JsGestureFunction::CreateGestureEvent(const GestureEvent& info)
37 {
38 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
39 objectTemplate->SetInternalFieldCount(1);
40 JSRef<JSObject> gestureInfoObj = objectTemplate->NewInstance();
41 gestureInfoObj->SetProperty<bool>("repeat", info.GetRepeat());
42 gestureInfoObj->SetProperty<double>("offsetX", PipelineBase::Px2VpWithCurrentDensity(info.GetOffsetX()));
43 gestureInfoObj->SetProperty<double>("offsetY", PipelineBase::Px2VpWithCurrentDensity(info.GetOffsetY()));
44 gestureInfoObj->SetProperty<double>("scale", info.GetScale());
45 gestureInfoObj->SetProperty<double>("angle", info.GetAngle());
46 gestureInfoObj->SetProperty<double>("speed", info.GetSpeed());
47 gestureInfoObj->SetProperty<double>("timestamp", info.GetTimeStamp().time_since_epoch().count());
48 gestureInfoObj->SetProperty<double>(
49 "pinchCenterX", PipelineBase::Px2VpWithCurrentDensity(info.GetPinchCenter().GetX()));
50 gestureInfoObj->SetProperty<double>(
51 "pinchCenterY", PipelineBase::Px2VpWithCurrentDensity(info.GetPinchCenter().GetY()));
52 gestureInfoObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
53 gestureInfoObj->SetProperty<double>("pressure", info.GetForce());
54 gestureInfoObj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
55 gestureInfoObj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
56 gestureInfoObj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
57 gestureInfoObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
58
59 gestureInfoObj->SetProperty<double>(
60 "velocityX", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityX()));
61 gestureInfoObj->SetProperty<double>(
62 "velocityY", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityY()));
63 gestureInfoObj->SetProperty<double>(
64 "velocity", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityValue()));
65 gestureInfoObj->SetPropertyObject(
66 "getModifierKeyState",
67 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
68 gestureInfoObj->SetPropertyObject("fingerList", CreateFingerListArray(info));
69 gestureInfoObj->SetPropertyObject("fingerInfos", CreateFingerInfosArray(info));
70 gestureInfoObj->SetProperty<double>("deviceId", info.GetDeviceId());
71
72 auto target = CreateEventTargetObject(info);
73 gestureInfoObj->SetPropertyObject("target", target);
74 gestureInfoObj->SetProperty<float>("axisVertical", info.GetVerticalAxis());
75 gestureInfoObj->SetProperty<float>("axisHorizontal", info.GetHorizontalAxis());
76 gestureInfoObj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
77 if (info.GetGestureTypeName() == GestureTypeName::TAP_GESTURE) {
78 if (!info.GetFingerList().empty()) {
79 gestureInfoObj->SetPropertyObject("tapLocation", GetTapLocation(info.GetFingerList().back()));
80 }
81 }
82 gestureInfoObj->Wrap<GestureEvent>(const_cast<GestureEvent*> (&info));
83 return gestureInfoObj;
84 }
85
CreateFingerListArray(const GestureEvent & info)86 JSRef<JSArray> JsGestureFunction::CreateFingerListArray(const GestureEvent& info)
87 {
88 JSRef<JSArray> fingerArr = JSRef<JSArray>::New();
89 const std::list<FingerInfo>& fingerList = info.GetFingerList();
90 std::list<FingerInfo> notTouchFingerList;
91 int32_t maxFingerId = -1;
92 for (const FingerInfo& fingerInfo : fingerList) {
93 JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
94 if (fingerInfo.sourceType_ == SourceType::TOUCH && fingerInfo.sourceTool_ == SourceTool::FINGER) {
95 fingerArr->SetValueAt(fingerInfo.fingerId_, element);
96 if (fingerInfo.fingerId_ > maxFingerId) {
97 maxFingerId = fingerInfo.fingerId_;
98 }
99 } else {
100 notTouchFingerList.emplace_back(fingerInfo);
101 }
102 }
103 auto idx = maxFingerId + 1;
104 for (const FingerInfo& fingerInfo : notTouchFingerList) {
105 JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
106 fingerArr->SetValueAt(idx++, element);
107 }
108 return fingerArr;
109 }
110
CreateFingerInfosArray(const GestureEvent & info)111 JSRef<JSArray> JsGestureFunction::CreateFingerInfosArray(const GestureEvent& info)
112 {
113 JSRef<JSArray> fingerArr = JSRef<JSArray>::New();
114 const std::list<FingerInfo>& fingerList = info.GetFingerList();
115 std::list<FingerInfo> notTouchFingerList;
116 std::vector<JSRef<JSObject>> validFingers;
117 for (const FingerInfo& fingerInfo : fingerList) {
118 JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
119 if (fingerInfo.sourceType_ == SourceType::TOUCH && fingerInfo.sourceTool_ == SourceTool::FINGER) {
120 validFingers.emplace_back(element);
121 } else {
122 notTouchFingerList.emplace_back(fingerInfo);
123 }
124 }
125 for (size_t i = 0; i < validFingers.size(); ++i) {
126 fingerArr->SetValueAt(i, validFingers[i]);
127 }
128 auto idx = validFingers.size();
129 for (const FingerInfo& fingerInfo : notTouchFingerList) {
130 JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
131 fingerArr->SetValueAt(idx++, element);
132 }
133 return fingerArr;
134 }
135
CreateFingerInfo(const FingerInfo & fingerInfo)136 JSRef<JSObject> JsGestureFunction::CreateFingerInfo(const FingerInfo& fingerInfo)
137 {
138 JSRef<JSObject> fingerInfoObj = JSRef<JSObject>::New();
139 const OHOS::Ace::Offset& globalLocation = fingerInfo.globalLocation_;
140 const OHOS::Ace::Offset& localLocation = fingerInfo.localLocation_;
141 const OHOS::Ace::Offset& screenLocation = fingerInfo.screenLocation_;
142 const OHOS::Ace::Offset& globalDisplayLocation = fingerInfo.globalDisplayLocation_;
143 fingerInfoObj->SetProperty<int32_t>("id", fingerInfo.fingerId_);
144 fingerInfoObj->SetProperty<int32_t>("hand", fingerInfo.operatingHand_);
145 fingerInfoObj->SetProperty<double>("globalX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
146 fingerInfoObj->SetProperty<double>("globalY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
147 fingerInfoObj->SetProperty<double>("localX", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
148 fingerInfoObj->SetProperty<double>("localY", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
149 fingerInfoObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
150 fingerInfoObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
151 fingerInfoObj->SetProperty<double>(
152 "globalDisplayX", PipelineBase::Px2VpWithCurrentDensity(globalDisplayLocation.GetX()));
153 fingerInfoObj->SetProperty<double>(
154 "globalDisplayY", PipelineBase::Px2VpWithCurrentDensity(globalDisplayLocation.GetY()));
155 return fingerInfoObj;
156 }
157
158 } // namespace OHOS::Ace::Framework
159