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_touch_function.h"
17 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
18
19 #include "base/log/log.h"
20 #include "bridge/declarative_frontend/engine/functions/js_function.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
22
23 namespace OHOS::Ace::Framework {
24
CreateTouchInfo(const TouchLocationInfo & touchInfo,TouchEventInfo & info)25 JSRef<JSObject> JsTouchFunction::CreateTouchInfo(const TouchLocationInfo& touchInfo, TouchEventInfo& info)
26 {
27 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
28 objectTemplate->SetInternalFieldCount(1);
29 JSRef<JSObject> touchInfoObj = objectTemplate->NewInstance();
30 const OHOS::Ace::Offset& globalLocation = touchInfo.GetGlobalLocation();
31 const OHOS::Ace::Offset& localLocation = touchInfo.GetLocalLocation();
32 const OHOS::Ace::Offset& screenLocation = touchInfo.GetScreenLocation();
33 touchInfoObj->SetProperty<int32_t>("type", static_cast<int32_t>(touchInfo.GetTouchType()));
34 touchInfoObj->SetProperty<int32_t>("id", touchInfo.GetFingerId());
35 touchInfoObj->SetProperty<double>("displayX", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX()));
36 touchInfoObj->SetProperty<double>("displayY", PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY()));
37 touchInfoObj->SetProperty<double>("windowX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
38 touchInfoObj->SetProperty<double>("windowY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
39 touchInfoObj->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
40 touchInfoObj->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
41 touchInfoObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
42 touchInfoObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
43 touchInfoObj->SetProperty<double>(
44 "pressedTime", static_cast<double>(touchInfo.GetPressedTime().time_since_epoch().count()));
45 touchInfoObj->SetProperty<double>("pressure", PipelineBase::Px2VpWithCurrentDensity(touchInfo.GetForce()));
46 touchInfoObj->SetProperty<double>("width", PipelineBase::Px2VpWithCurrentDensity(touchInfo.GetWidth()));
47 touchInfoObj->SetProperty<double>("height", PipelineBase::Px2VpWithCurrentDensity(touchInfo.GetHeight()));
48 touchInfoObj->Wrap<TouchEventInfo>(&info);
49 return touchInfoObj;
50 }
51
CreateJSEventInfo(TouchEventInfo & info)52 JSRef<JSObject> JsTouchFunction::CreateJSEventInfo(TouchEventInfo& info)
53 {
54 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
55 objectTemplate->SetInternalFieldCount(1);
56 JSRef<JSObject> eventObj = objectTemplate->NewInstance();
57 JSRef<JSArray> touchArr = JSRef<JSArray>::New();
58 JSRef<JSArray> changeTouchArr = JSRef<JSArray>::New();
59 eventObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
60 eventObj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
61 auto target = CreateEventTargetObject(info);
62 eventObj->SetPropertyObject("target", target);
63 eventObj->SetProperty<double>("pressure", info.GetForce());
64 eventObj->SetProperty<double>("deviceId", info.GetDeviceId());
65 eventObj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsTouchPreventDefault));
66 eventObj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
67 eventObj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
68 eventObj->SetProperty<double>("rollAngle", info.GetRollAngle().value_or(0.0f));
69 eventObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
70 eventObj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
71
72 const std::list<TouchLocationInfo>& touchList = info.GetTouches();
73 uint32_t idx = 0;
74 for (const TouchLocationInfo& location : touchList) {
75 JSRef<JSObject> element = CreateTouchInfo(location, info);
76 touchArr->SetValueAt(idx++, element);
77 }
78 eventObj->SetPropertyObject("touches", touchArr);
79
80 idx = 0; // reset index counter
81 const std::list<TouchLocationInfo>& changeTouch = info.GetChangedTouches();
82 for (const TouchLocationInfo& change : changeTouch) {
83 JSRef<JSObject> element = CreateTouchInfo(change, info);
84 changeTouchArr->SetValueAt(idx++, element);
85 }
86 if (changeTouch.size() > 0) {
87 eventObj->SetProperty<int32_t>("type", static_cast<int32_t>(changeTouch.front().GetTouchType()));
88 }
89 eventObj->SetPropertyObject("changedTouches", changeTouchArr);
90 eventObj->SetProperty<double>("axisVertical", 0.0f);
91 eventObj->SetProperty<double>("axisHorizontal", 0.0f);
92 eventObj->SetPropertyObject(
93 "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
94 eventObj->SetPropertyObject(
95 "getHistoricalPoints", JSRef<JSFunc>::New<FunctionCallback>(JsGetHistoricalPoints));
96 eventObj->SetPropertyObject(
97 "getModifierKeyState",
98 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
99 eventObj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsPreventDefault));
100 eventObj->Wrap<TouchEventInfo>(&info);
101 return eventObj;
102 }
103
Execute(TouchEventInfo & info)104 void JsTouchFunction::Execute(TouchEventInfo& info)
105 {
106 JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateJSEventInfo(info));
107 JsFunction::ExecuteJS(1, ¶m);
108 }
109
110 } // namespace OHOS::Ace::Framework
111