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>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
69 eventObj->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
70
71 const std::list<TouchLocationInfo>& touchList = info.GetTouches();
72 uint32_t idx = 0;
73 for (const TouchLocationInfo& location : touchList) {
74 JSRef<JSObject> element = CreateTouchInfo(location, info);
75 touchArr->SetValueAt(idx++, element);
76 }
77 eventObj->SetPropertyObject("touches", touchArr);
78
79 idx = 0; // reset index counter
80 const std::list<TouchLocationInfo>& changeTouch = info.GetChangedTouches();
81 for (const TouchLocationInfo& change : changeTouch) {
82 JSRef<JSObject> element = CreateTouchInfo(change, info);
83 changeTouchArr->SetValueAt(idx++, element);
84 }
85 if (changeTouch.size() > 0) {
86 eventObj->SetProperty<int32_t>("type", static_cast<int32_t>(changeTouch.front().GetTouchType()));
87 }
88 eventObj->SetPropertyObject("changedTouches", changeTouchArr);
89 eventObj->SetProperty<double>("axisVertical", 0.0f);
90 eventObj->SetProperty<double>("axisHorizontal", 0.0f);
91 eventObj->SetPropertyObject(
92 "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
93 eventObj->SetPropertyObject(
94 "getHistoricalPoints", JSRef<JSFunc>::New<FunctionCallback>(JsGetHistoricalPoints));
95 eventObj->SetPropertyObject(
96 "getModifierKeyState",
97 JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
98 eventObj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsPreventDefault));
99 eventObj->Wrap<TouchEventInfo>(&info);
100 return eventObj;
101 }
102
Execute(TouchEventInfo & info)103 void JsTouchFunction::Execute(TouchEventInfo& info)
104 {
105 JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateJSEventInfo(info));
106 JsFunction::ExecuteJS(1, ¶m);
107 }
108
109 } // namespace OHOS::Ace::Framework
110