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 
62     JSRef<JSVal> param = obj;
63     JsFunction::ExecuteJS(1, &param);
64 }
65 
Execute(GestureEvent & info)66 void JsClickFunction::Execute(GestureEvent& info)
67 {
68     JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
69     objectTemplate->SetInternalFieldCount(1);
70     JSRef<JSObject> obj = objectTemplate->NewInstance();
71     Offset globalOffset = info.GetGlobalLocation();
72     Offset localOffset = info.GetLocalLocation();
73     Offset screenOffset = info.GetScreenLocation();
74     obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
75     obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
76     obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
77     obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
78     obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
79     obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
80     obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
81     obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
82     obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
83     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
84     obj->SetProperty<double>("pressure", info.GetForce());
85     obj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsClickPreventDefault));
86     obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
87     obj->SetPropertyObject("getModifierKeyState",
88         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
89     obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
90     obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
91     obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
92     obj->SetProperty<double>("axisVertical", 0.0f);
93     obj->SetProperty<double>("axisHorizontal", 0.0f);
94     auto target = CreateEventTargetObject(info);
95     obj->SetPropertyObject("target", target);
96     obj->Wrap<GestureEvent>(&info);
97     JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
98     JsFunction::ExecuteJS(1, &param);
99 }
100 
Execute(MouseInfo & info)101 void JsClickFunction::Execute(MouseInfo& info)
102 {
103     JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
104     objectTemplate->SetInternalFieldCount(1);
105     JSRef<JSObject> obj = objectTemplate->NewInstance();
106     obj->SetProperty<int32_t>("button", static_cast<int32_t>(info.GetButton()));
107     obj->SetProperty<int32_t>("action", static_cast<int32_t>(info.GetAction()));
108     Offset globalOffset = info.GetGlobalLocation();
109     Offset localOffset = info.GetLocalLocation();
110     Offset screenOffset = info.GetScreenLocation();
111     obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
112     obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
113     obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
114     obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
115     obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
116     obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
117     obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
118     obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
119     obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
120     obj->SetPropertyObject(
121         "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
122     obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
123     obj->SetPropertyObject("getModifierKeyState",
124         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
125     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
126     obj->SetProperty<double>("pressure", info.GetForce());
127     obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
128     obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
129     obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
130     obj->SetProperty<double>("axisVertical", 0.0f);
131     obj->SetProperty<double>("axisHorizontal", 0.0f);
132     auto target = CreateEventTargetObject(info);
133     obj->SetPropertyObject("target", target);
134     obj->Wrap<MouseInfo>(&info);
135 
136     JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
137     JsFunction::ExecuteJS(1, &param);
138 }
139 
Execute()140 void JsWeakClickFunction::Execute()
141 {
142     JsWeakFunction::ExecuteJS();
143 
144     // This is required to request for new frame, which eventually will call
145     // FlushBuild, FlushLayout and FlushRender on the dirty elements
146 #ifdef USE_ARK_ENGINE
147     JsiDeclarativeEngineInstance::TriggerPageUpdate(JsiDeclarativeEngineInstance::GetCurrentRuntime());
148 #endif
149 }
150 
Execute(const ClickInfo & info)151 void JsWeakClickFunction::Execute(const ClickInfo& info)
152 {
153     JSRef<JSObject> obj = JSRef<JSObject>::New();
154     Offset globalOffset = info.GetGlobalLocation();
155     Offset localOffset = info.GetLocalLocation();
156     Offset screenOffset = info.GetScreenLocation();
157     obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
158     obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
159     obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
160     obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
161     obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
162     obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
163     obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
164     obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
165     obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
166     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
167     obj->SetPropertyObject("getModifierKeyState",
168         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
169     obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
170     auto target = CreateEventTargetObject(info);
171     obj->SetPropertyObject("target", target);
172     obj->SetProperty<double>("pressure", info.GetForce());
173     obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
174     obj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
175     obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
176     obj->SetProperty<double>("axisVertical", 0.0f);
177     obj->SetProperty<double>("axisHorizontal", 0.0f);
178 
179     JSRef<JSVal> param = obj;
180     JsWeakFunction::ExecuteJS(1, &param);
181 }
182 
Execute(GestureEvent & info)183 void JsWeakClickFunction::Execute(GestureEvent& info)
184 {
185     JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
186     objectTemplate->SetInternalFieldCount(1);
187     JSRef<JSObject> obj = objectTemplate->NewInstance();
188     Offset globalOffset = info.GetGlobalLocation();
189     Offset localOffset = info.GetLocalLocation();
190     Offset screenOffset = info.GetScreenLocation();
191     obj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetX()));
192     obj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenOffset.GetY()));
193     obj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
194     obj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
195     obj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
196     obj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
197     obj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
198     obj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
199     obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
200     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
201     obj->SetProperty<double>("pressure", info.GetForce());
202     obj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsClickPreventDefault));
203     obj->SetPropertyObject("getModifierKeyState",
204         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
205     obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
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     auto target = CreateEventTargetObject(info);
212     obj->SetPropertyObject("target", target);
213     obj->Wrap<GestureEvent>(&info);
214     JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
215     JsWeakFunction::ExecuteJS(1, &param);
216 }
217 
Execute(MouseInfo & info)218 void JsWeakClickFunction::Execute(MouseInfo& info)
219 {
220     JSRef<JSObjTemplate> objTemp = JSRef<JSObjTemplate>::New();
221     objTemp->SetInternalFieldCount(1);
222     JSRef<JSObject> obj = objTemp->NewInstance();
223     Offset localOffset = info.GetLocalLocation();
224     Offset globalOffset = info.GetGlobalLocation();
225     Offset screenOffset = info.GetScreenLocation();
226     obj->SetProperty<int32_t>("button", static_cast<int32_t>(info.GetButton()));
227     obj->SetProperty<int32_t>("action", static_cast<int32_t>(info.GetAction()));
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->SetPropertyObject(
238         "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
239     obj->SetPropertyObject("getModifierKeyState",
240         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
241     obj->SetProperty<double>("deviceId", static_cast<int32_t>(info.GetDeviceId()));
242     obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
243     obj->SetProperty<double>("pressure", info.GetForce());
244     obj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
245     obj->SetProperty<double>("tiltY", info.GetTiltY().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<MouseInfo>(&info);
252 
253     JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
254     JsWeakFunction::ExecuteJS(1, &param);
255 }
256 } // namespace OHOS::Ace::Framework
257