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->Wrap<TouchEventInfo>(&info);
44     return touchInfoObj;
45 }
46 
CreateJSEventInfo(TouchEventInfo & info)47 JSRef<JSObject> JsTouchFunction::CreateJSEventInfo(TouchEventInfo& info)
48 {
49     JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
50     objectTemplate->SetInternalFieldCount(1);
51     JSRef<JSObject> eventObj = objectTemplate->NewInstance();
52     JSRef<JSArray> touchArr = JSRef<JSArray>::New();
53     JSRef<JSArray> changeTouchArr = JSRef<JSArray>::New();
54     eventObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
55     eventObj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
56     auto target = CreateEventTargetObject(info);
57     eventObj->SetPropertyObject("target", target);
58     eventObj->SetProperty<double>("pressure", info.GetForce());
59     eventObj->SetProperty<double>("deviceId", info.GetDeviceId());
60     eventObj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsTouchPreventDefault));
61     eventObj->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
62     eventObj->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
63     eventObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
64 
65     const std::list<TouchLocationInfo>& touchList = info.GetTouches();
66     uint32_t idx = 0;
67     for (const TouchLocationInfo& location : touchList) {
68         JSRef<JSObject> element = CreateTouchInfo(location, info);
69         touchArr->SetValueAt(idx++, element);
70     }
71     eventObj->SetPropertyObject("touches", touchArr);
72 
73     idx = 0; // reset index counter
74     const std::list<TouchLocationInfo>& changeTouch = info.GetChangedTouches();
75     for (const TouchLocationInfo& change : changeTouch) {
76         JSRef<JSObject> element = CreateTouchInfo(change, info);
77         changeTouchArr->SetValueAt(idx++, element);
78     }
79     if (changeTouch.size() > 0) {
80         eventObj->SetProperty<int32_t>("type", static_cast<int32_t>(changeTouch.front().GetTouchType()));
81     }
82     eventObj->SetPropertyObject("changedTouches", changeTouchArr);
83     eventObj->SetProperty<double>("axisVertical", 0.0f);
84     eventObj->SetProperty<double>("axisHorizontal", 0.0f);
85     eventObj->SetPropertyObject(
86         "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
87     eventObj->SetPropertyObject(
88         "getHistoricalPoints", JSRef<JSFunc>::New<FunctionCallback>(JsGetHistoricalPoints));
89     eventObj->SetPropertyObject(
90         "getModifierKeyState",
91         JSRef<JSFunc>::New<FunctionCallback>(NG::ArkTSUtils::JsGetModifierKeyState));
92     eventObj->SetPropertyObject("preventDefault", JSRef<JSFunc>::New<FunctionCallback>(JsPreventDefault));
93     eventObj->Wrap<TouchEventInfo>(&info);
94     return eventObj;
95 }
96 
Execute(TouchEventInfo & info)97 void JsTouchFunction::Execute(TouchEventInfo& info)
98 {
99     JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateJSEventInfo(info));
100     JsFunction::ExecuteJS(1, &param);
101 }
102 
103 } // namespace OHOS::Ace::Framework
104