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
18 #include "base/log/log.h"
19 #include "bridge/declarative_frontend/engine/functions/js_function.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
21
22 namespace OHOS::Ace::Framework {
23
CreateTouchInfo(const TouchLocationInfo & touchInfo)24 JSRef<JSObject> JsTouchFunction::CreateTouchInfo(const TouchLocationInfo& touchInfo)
25 {
26 JSRef<JSObject> touchInfoObj = JSRef<JSObject>::New();
27 const OHOS::Ace::Offset& globalLocation = touchInfo.GetGlobalLocation();
28 const OHOS::Ace::Offset& localLocation = touchInfo.GetLocalLocation();
29 touchInfoObj->SetProperty<int32_t>("type", static_cast<int32_t>(touchInfo.GetTouchType()));
30 touchInfoObj->SetProperty<int32_t>("id", touchInfo.GetFingerId());
31 touchInfoObj->SetProperty<double>("screenX", SystemProperties::Px2Vp(globalLocation.GetX()));
32 touchInfoObj->SetProperty<double>("screenY", SystemProperties::Px2Vp(globalLocation.GetY()));
33 touchInfoObj->SetProperty<double>("x", SystemProperties::Px2Vp(localLocation.GetX()));
34 touchInfoObj->SetProperty<double>("y", SystemProperties::Px2Vp(localLocation.GetY()));
35 return touchInfoObj;
36 }
37
CreateJSEventInfo(TouchEventInfo & info)38 JSRef<JSObject> JsTouchFunction::CreateJSEventInfo(TouchEventInfo& info)
39 {
40 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
41 objectTemplate->SetInternalFieldCount(1);
42 JSRef<JSObject> eventObj = objectTemplate->NewInstance();
43 JSRef<JSArray> touchArr = JSRef<JSArray>::New();
44 JSRef<JSArray> changeTouchArr = JSRef<JSArray>::New();
45 eventObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
46 eventObj->SetProperty<double>("timestamp", static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
47 auto target = CreateEventTargetObject(info);
48 eventObj->SetPropertyObject("target", target);
49 eventObj->SetProperty<double>("pressure", info.GetForce());
50 if (info.GetTiltX().has_value()) {
51 eventObj->SetProperty<double>("tiltX", info.GetTiltX().value());
52 }
53 if (info.GetTiltY().has_value()) {
54 eventObj->SetProperty<double>("tiltY", info.GetTiltY().value());
55 }
56 eventObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
57
58 const std::list<TouchLocationInfo>& touchList = info.GetTouches();
59 uint32_t idx = 0;
60 for (const TouchLocationInfo& location : touchList) {
61 JSRef<JSObject> element = CreateTouchInfo(location);
62 touchArr->SetValueAt(idx++, element);
63 }
64 eventObj->SetPropertyObject("touches", touchArr);
65
66 idx = 0; // reset index counter
67 const std::list<TouchLocationInfo>& changeTouch = info.GetChangedTouches();
68 for (const TouchLocationInfo& change : changeTouch) {
69 JSRef<JSObject> element = CreateTouchInfo(change);
70 changeTouchArr->SetValueAt(idx++, element);
71 }
72 if (changeTouch.size() > 0) {
73 eventObj->SetProperty<int32_t>("type", static_cast<int32_t>(changeTouch.front().GetTouchType()));
74 }
75 eventObj->SetPropertyObject("changedTouches", changeTouchArr);
76 eventObj->SetPropertyObject(
77 "stopPropagation", JSRef<JSFunc>::New<FunctionCallback>(JsStopPropagation));
78 eventObj->Wrap<TouchEventInfo>(&info);
79 return eventObj;
80 }
81
Execute(TouchEventInfo & info)82 void JsTouchFunction::Execute(TouchEventInfo& info)
83 {
84 LOGD("JsTouchFunction: eventType[%s]", info.GetType().c_str());
85 JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateJSEventInfo(info));
86 JsFunction::ExecuteJS(1, ¶m);
87 }
88
89 } // namespace OHOS::Ace::Framework
90