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