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