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
18 #include "base/log/log.h"
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_V8_ENGINE
30 V8DeclarativeEngineInstance::TriggerPageUpdate();
31 #elif USE_QUICKJS_ENGINE
32 QJSDeclarativeEngineInstance::TriggerPageUpdate(QJSDeclarativeEngineInstance::GetCurrentContext());
33 #elif USE_ARK_ENGINE
34 JsiDeclarativeEngineInstance::TriggerPageUpdate(JsiDeclarativeEngineInstance::GetCurrentRuntime());
35 #endif
36 }
37
Execute(const ClickInfo & info)38 void JsClickFunction::Execute(const ClickInfo& info)
39 {
40 JSRef<JSObject> obj = JSRef<JSObject>::New();
41 Offset globalOffset = info.GetGlobalLocation();
42 Offset localOffset = info.GetLocalLocation();
43 obj->SetProperty<double>("screenX", SystemProperties::Px2Vp(globalOffset.GetX()));
44 obj->SetProperty<double>("screenY", SystemProperties::Px2Vp(globalOffset.GetY()));
45 obj->SetProperty<double>("x", SystemProperties::Px2Vp(localOffset.GetX()));
46 obj->SetProperty<double>("y", SystemProperties::Px2Vp(localOffset.GetY()));
47 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
48 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
49 auto target = CreateEventTargetObject(info);
50 obj->SetPropertyObject("target", target);
51 obj->SetProperty<double>("pressure", info.GetForce());
52 if (info.GetTiltX().has_value()) {
53 obj->SetProperty<double>("tiltX", info.GetTiltX().value());
54 }
55 if (info.GetTiltY().has_value()) {
56 obj->SetProperty<double>("tiltY", info.GetTiltY().value());
57 }
58 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
59
60 LOGD("globalOffset.GetX() = %lf, globalOffset.GetY() = %lf, localOffset.GetX() = %lf, localOffset.GetY() = %lf",
61 globalOffset.GetX(), globalOffset.GetY(), localOffset.GetX(), localOffset.GetY());
62
63 JSRef<JSVal> param = obj;
64 JsFunction::ExecuteJS(1, ¶m);
65 }
66
Execute(const GestureEvent & info)67 void JsClickFunction::Execute(const GestureEvent& info)
68 {
69 JSRef<JSObject> obj = JSRef<JSObject>::New();
70 Offset globalOffset = info.GetGlobalLocation();
71 Offset localOffset = info.GetLocalLocation();
72 obj->SetProperty<double>("screenX", SystemProperties::Px2Vp(globalOffset.GetX()));
73 obj->SetProperty<double>("screenY", SystemProperties::Px2Vp(globalOffset.GetY()));
74 obj->SetProperty<double>("x", SystemProperties::Px2Vp(localOffset.GetX()));
75 obj->SetProperty<double>("y", SystemProperties::Px2Vp(localOffset.GetY()));
76 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
77 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
78 obj->SetProperty<double>("pressure", info.GetForce());
79 if (info.GetTiltX().has_value()) {
80 obj->SetProperty<double>("tiltX", info.GetTiltX().value());
81 }
82 if (info.GetTiltY().has_value()) {
83 obj->SetProperty<double>("tiltY", info.GetTiltY().value());
84 }
85 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
86 auto target = CreateEventTargetObject(info);
87 obj->SetPropertyObject("target", target);
88
89 LOGD("globalOffset.GetX() = %lf, globalOffset.GetY() = %lf, localOffset.GetX() = %lf, localOffset.GetY() = %lf",
90 globalOffset.GetX(), globalOffset.GetY(), localOffset.GetX(), localOffset.GetY());
91
92 JSRef<JSVal> param = obj;
93 JsFunction::ExecuteJS(1, ¶m);
94 }
95
Execute(MouseInfo & info)96 void JsClickFunction::Execute(MouseInfo& info)
97 {
98 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
99 objectTemplate->SetInternalFieldCount(1);
100 JSRef<JSObject> obj = objectTemplate->NewInstance();
101 obj->SetProperty<int32_t>("button", static_cast<int32_t>(info.GetButton()));
102 obj->SetProperty<int32_t>("action", static_cast<int32_t>(info.GetAction()));
103 Offset globalOffset = info.GetGlobalLocation();
104 Offset localOffset = info.GetLocalLocation();
105 obj->SetProperty<double>("screenX", SystemProperties::Px2Vp(globalOffset.GetX()));
106 obj->SetProperty<double>("screenY", SystemProperties::Px2Vp(globalOffset.GetY()));
107 obj->SetProperty<double>("x", SystemProperties::Px2Vp(localOffset.GetX()));
108 obj->SetProperty<double>("y", SystemProperties::Px2Vp(localOffset.GetY()));
109 obj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
110 obj->SetPropertyObject(
111 "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
112 obj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
113 obj->SetProperty<double>("pressure", info.GetForce());
114 if (info.GetTiltX().has_value()) {
115 obj->SetProperty<double>("tiltX", info.GetTiltX().value());
116 }
117 if (info.GetTiltY().has_value()) {
118 obj->SetProperty<double>("tiltY", info.GetTiltY().value());
119 }
120 obj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
121 auto target = CreateEventTargetObject(info);
122 obj->SetPropertyObject("target", target);
123 obj->Wrap<MouseInfo>(&info);
124
125 LOGD("button = %d, action = %d, globalOffset = (%lf, %lf), localOffset = (%lf, %lf),", info.GetButton(),
126 info.GetAction(), globalOffset.GetX(), globalOffset.GetY(), localOffset.GetX(), localOffset.GetY());
127
128 JSRef<JSVal> param = JSRef<JSObject>::Cast(obj);
129 JsFunction::ExecuteJS(1, ¶m);
130 }
131
132 } // namespace OHOS::Ace::Framework
133