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