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_click_function.h"
17 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
18
19 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
20
21 namespace OHOS::Ace::Framework {
22
Execute()23 void JsClickFunction::Execute()
24 {
25 JsFunction::ExecuteJS();
26
27 // This is required to request for new frame, which eventually will call
28 // FlushBuild, FlushLayout and FlushRender on the dirty elements
29 #ifdef USE_ARK_ENGINE
30 JsiDeclarativeEngineInstance::TriggerPageUpdate(JsiDeclarativeEngineInstance::GetCurrentRuntime());
31 #endif
32 }
33
Execute(const ClickInfo & info)34 void JsClickFunction::Execute(const ClickInfo& info)
35 {
36 JSRef<JSObject> obj = JSRef<JSObject>::New();
37 Offset globalOffset = info.GetGlobalLocation();
38 Offset localOffset = info.GetLocalLocation();
39 Offset screenOffset = info.GetScreenLocation();
40 obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
41 obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
42 obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
43 obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
44 obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
45 obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
46 obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
47 obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
48 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
49 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
50 obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
51 obj->SetPropertyObject("getModifierKeyState",
52 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
53 auto target = CreateEventTargetObject(info);
54 obj->SetPropertyObject("target", target);
55 obj->SetProperty<double>("pressure", info.GetForce());
56 obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
57 obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
58 obj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
59 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
60 obj->SetProperty<double>("axisVertical", 0.0f);
61 obj->SetProperty<double>("axisHorizontal", 0.0f);
62 obj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
63
64 JSRef<JSVal> param = obj;
65 JsFunction::ExecuteJS(1, ¶m);
66 }
67
GetOperatingHand(GestureEvent & info)68 static int32_t GetOperatingHand(GestureEvent& info)
69 {
70 int32_t left = 0;
71 int32_t right = 0;
72 for (const FingerInfo& fingerInfo : info.GetFingerList()) {
73 if (fingerInfo.operatingHand_ == HAND_LEFT) {
74 ++left;
75 } else if (fingerInfo.operatingHand_ == HAND_RIGHT) {
76 ++right;
77 }
78 }
79 if (left > right) {
80 return HAND_LEFT;
81 } else if (right > left) {
82 return HAND_RIGHT;
83 }
84 return HAND_NONE;
85 }
86
Execute(GestureEvent & info)87 void JsClickFunction::Execute(GestureEvent& info)
88 {
89 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
90 objectTemplate->SetInternalFieldCount(1);
91 JSRef<JSObject> obj = objectTemplate->NewInstance();
92 Offset globalOffset = info.GetGlobalLocation();
93 Offset localOffset = info.GetLocalLocation();
94 Offset screenOffset = info.GetScreenLocation();
95 obj->SetProperty<int32_t>("hand", GetOperatingHand(info));
96 obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
97 obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
98 obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
99 obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
100 obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
101 obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
102 obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
103 obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
104 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
105 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
106 obj->SetProperty<double>("pressure", info.GetForce());
107 obj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsClickPreventDefault));
108 obj->SetPropertyObject("getModifierKeyState",
109 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
110 obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
111 obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
112 obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
113 obj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
114 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
115 obj->SetProperty<double>("axisVertical", 0.0f);
116 obj->SetProperty<double>("axisHorizontal", 0.0f);
117 obj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
118 auto target = CreateEventTargetObject(info);
119 obj->SetPropertyObject("target", target);
120 obj->Wrap<GestureEvent>(&info);
121 JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
122 JsFunction::ExecuteJS(1, ¶m);
123 }
124
Execute(MouseInfo & info)125 void JsClickFunction::Execute(MouseInfo& info)
126 {
127 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
128 objectTemplate->SetInternalFieldCount(1);
129 JSRef<JSObject> obj = objectTemplate->NewInstance();
130 obj->SetProperty<int32_t>("button", static_cast<int32_t>(info.GetButton()));
131 obj->SetProperty<int32_t>("action", static_cast<int32_t>(info.GetAction()));
132 Offset globalOffset = info.GetGlobalLocation();
133 Offset localOffset = info.GetLocalLocation();
134 Offset screenOffset = info.GetScreenLocation();
135 obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
136 obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
137 obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
138 obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
139 obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
140 obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
141 obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
142 obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
143 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
144 obj->SetPropertyObject(
145 "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
146 obj->SetPropertyObject("getModifierKeyState",
147 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
148 obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
149 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
150 obj->SetProperty<double>("pressure", info.GetForce());
151 obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
152 obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
153 obj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
154 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
155 obj->SetProperty<double>("axisVertical", 0.0f);
156 obj->SetProperty<double>("axisHorizontal", 0.0f);
157 auto target = CreateEventTargetObject(info);
158 obj->SetPropertyObject("target", target);
159 obj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
160 obj->SetProperty<double>("rawDeltaX", PipelineBase::Px2VpWithCurrentDensity(info.GetRawDeltaX()));
161 obj->SetProperty<double>("rawDeltaY", PipelineBase::Px2VpWithCurrentDensity(info.GetRawDeltaY()));
162 JSRef<JSArray> pressedButtonArr = JSRef<JSArray>::New();
163 auto pressedButtons = info.GetPressedButtons();
164 uint32_t idx = 0;
165 for (const auto& button : pressedButtons) {
166 auto jsButton = JSRef<JSVal>::Make(ToJSValue(static_cast<int32_t>(button)));
167 pressedButtonArr->SetValueAt(idx++, jsButton);
168 }
169 obj->SetPropertyObject("pressedButtons", pressedButtonArr);
170 obj->Wrap<MouseInfo>(&info);
171
172 JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
173 JsFunction::ExecuteJS(1, ¶m);
174 }
175
Execute()176 void JsWeakClickFunction::Execute()
177 {
178 JsWeakFunction::ExecuteJS();
179
180 // This is required to request for new frame, which eventually will call
181 // FlushBuild, FlushLayout and FlushRender on the dirty elements
182 #ifdef USE_ARK_ENGINE
183 JsiDeclarativeEngineInstance::TriggerPageUpdate(JsiDeclarativeEngineInstance::GetCurrentRuntime());
184 #endif
185 }
186
Execute(const ClickInfo & info)187 void JsWeakClickFunction::Execute(const ClickInfo& info)
188 {
189 JSRef<JSObject> obj = JSRef<JSObject>::New();
190 Offset globalOffset = info.GetGlobalLocation();
191 Offset localOffset = info.GetLocalLocation();
192 Offset screenOffset = info.GetScreenLocation();
193 obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
194 obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
195 obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
196 obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
197 obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
198 obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
199 obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
200 obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
201 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
202 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
203 obj->SetPropertyObject("getModifierKeyState",
204 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
205 obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
206 auto target = CreateEventTargetObject(info);
207 obj->SetPropertyObject("target", target);
208 obj->SetProperty<double>("pressure", info.GetForce());
209 obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
210 obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
211 obj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
212 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
213 obj->SetProperty<double>("axisVertical", 0.0f);
214 obj->SetProperty<double>("axisHorizontal", 0.0f);
215
216 JSRef<JSVal> param = obj;
217 JsWeakFunction::ExecuteJS(1, ¶m);
218 }
219
Execute(GestureEvent & info)220 void JsWeakClickFunction::Execute(GestureEvent& info)
221 {
222 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
223 objectTemplate->SetInternalFieldCount(1);
224 JSRef<JSObject> obj = objectTemplate->NewInstance();
225 Offset globalOffset = info.GetGlobalLocation();
226 Offset localOffset = info.GetLocalLocation();
227 Offset screenOffset = info.GetScreenLocation();
228 obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
229 obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
230 obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
231 obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
232 obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
233 obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
234 obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
235 obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
236 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
237 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
238 obj->SetProperty<double>("pressure", info.GetForce());
239 obj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsClickPreventDefault));
240 obj->SetPropertyObject("getModifierKeyState",
241 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
242 obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
243 obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
244 obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
245 obj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
246 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
247 obj->SetProperty<double>("axisVertical", 0.0f);
248 obj->SetProperty<double>("axisHorizontal", 0.0f);
249 auto target = CreateEventTargetObject(info);
250 obj->SetPropertyObject("target", target);
251 obj->Wrap<GestureEvent>(&info);
252 JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
253 JsWeakFunction::ExecuteJS(1, ¶m);
254 }
255
Execute(MouseInfo & info)256 void JsWeakClickFunction::Execute(MouseInfo& info)
257 {
258 JSRef<JSObjTemplate> objTemp = JSRef<JSObjTemplate>::New();
259 objTemp->SetInternalFieldCount(1);
260 JSRef<JSObject> obj = objTemp->NewInstance();
261 Offset localOffset = info.GetLocalLocation();
262 Offset globalOffset = info.GetGlobalLocation();
263 Offset screenOffset = info.GetScreenLocation();
264 obj->SetProperty<int32_t>("button", static_cast<int32_t>(info.GetButton()));
265 obj->SetProperty<int32_t>("action", static_cast<int32_t>(info.GetAction()));
266 obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
267 obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
268 obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
269 obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
270 obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
271 obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
272 obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
273 obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
274 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
275 obj->SetPropertyObject(
276 "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
277 obj->SetPropertyObject("getModifierKeyState",
278 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
279 obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
280 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
281 obj->SetProperty<double>("pressure", info.GetForce());
282 obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
283 obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
284 obj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
285 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
286 obj->SetProperty<double>("axisVertical", 0.0f);
287 obj->SetProperty<double>("axisHorizontal", 0.0f);
288 auto target = CreateEventTargetObject(info);
289 obj->SetPropertyObject("target", target);
290 obj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
291 obj->SetProperty<double>("rawDeltaX", PipelineBase::Px2VpWithCurrentDensity(info.GetRawDeltaX()));
292 obj->SetProperty<double>("rawDeltaY", PipelineBase::Px2VpWithCurrentDensity(info.GetRawDeltaY()));
293 JSRef<JSArray> pressedButtonArr = JSRef<JSArray>::New();
294 auto pressedButtons = info.GetPressedButtons();
295 uint32_t idx = 0;
296 for (const auto& button : pressedButtons) {
297 auto jsButton = JSRef<JSVal>::Make(ToJSValue(static_cast<int32_t>(button)));
298 pressedButtonArr->SetValueAt(idx++, jsButton);
299 }
300 obj->SetPropertyObject("pressedButtons", pressedButtonArr);
301 obj->Wrap<MouseInfo>(&info);
302
303 JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
304 JsWeakFunction::ExecuteJS(1, ¶m);
305 }
306
307 } // namespace OHOS::Ace::Framework
308