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_gesture_function.h"
17
18 #include "base/log/log.h"
19 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
20
21 namespace OHOS::Ace::Framework {
22
Execute()23 void JsGestureFunction::Execute()
24 {
25 JsFunction::Execute();
26 }
27
Execute(const GestureEvent & info)28 void JsGestureFunction::Execute(const GestureEvent& info)
29 {
30 JSRef<JSVal> param = JSRef<JSObject>::Cast(CreateGestureEvent(info));
31 JsFunction::ExecuteJS(1, ¶m);
32 }
33
CreateGestureEvent(const GestureEvent & info)34 JSRef<JSObject> JsGestureFunction::CreateGestureEvent(const GestureEvent& info)
35 {
36 JSRef<JSObject> gestureInfoObj = JSRef<JSObject>::New();
37 gestureInfoObj->SetProperty<bool>("repeat", info.GetRepeat());
38 gestureInfoObj->SetProperty<double>("offsetX", SystemProperties::Px2Vp(info.GetOffsetX()));
39 gestureInfoObj->SetProperty<double>("offsetY", SystemProperties::Px2Vp(info.GetOffsetY()));
40 gestureInfoObj->SetProperty<double>("scale", info.GetScale());
41 gestureInfoObj->SetProperty<double>("angle", info.GetAngle());
42 gestureInfoObj->SetProperty<double>("speed", info.GetSpeed());
43 gestureInfoObj->SetProperty<double>("timestamp", info.GetTimeStamp().time_since_epoch().count());
44 gestureInfoObj->SetProperty<double>("globalX", SystemProperties::Px2Vp(info.GetGlobalLocation().GetX()));
45 gestureInfoObj->SetProperty<double>("globalY", SystemProperties::Px2Vp(info.GetGlobalLocation().GetY()));
46 gestureInfoObj->SetProperty<double>("localX", SystemProperties::Px2Vp(info.GetLocalLocation().GetX()));
47 gestureInfoObj->SetProperty<double>("localY", SystemProperties::Px2Vp(info.GetLocalLocation().GetY()));
48 gestureInfoObj->SetProperty<double>("pinchCenterX", SystemProperties::Px2Vp(info.GetPinchCenter().GetX()));
49 gestureInfoObj->SetProperty<double>("pinchCenterY", SystemProperties::Px2Vp(info.GetPinchCenter().GetY()));
50 gestureInfoObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
51 gestureInfoObj->SetProperty<double>("pressure", info.GetForce());
52 if (info.GetTiltX().has_value()) {
53 gestureInfoObj->SetProperty<double>("tiltX", info.GetTiltX().value());
54 }
55 if (info.GetTiltY().has_value()) {
56 gestureInfoObj->SetProperty<double>("tiltY", info.GetTiltY().value());
57 }
58 gestureInfoObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
59
60 JSRef<JSArray> fingerArr = JSRef<JSArray>::New();
61 const std::list<FingerInfo>& fingerList = info.GetFingerList();
62 uint32_t idx = 0;
63 for (const FingerInfo& info : fingerList) {
64 JSRef<JSObject> element = CreateFingerInfo(info);
65 fingerArr->SetValueAt(idx++, element);
66 }
67 gestureInfoObj->SetPropertyObject("fingerList", fingerArr);
68
69 auto target = CreateEventTargetObject(info);
70 gestureInfoObj->SetPropertyObject("target", target);
71 return gestureInfoObj;
72 }
73
CreateFingerInfo(const FingerInfo & fingerInfo)74 JSRef<JSObject> JsGestureFunction::CreateFingerInfo(const FingerInfo& fingerInfo)
75 {
76 JSRef<JSObject> fingerInfoObj = JSRef<JSObject>::New();
77 const OHOS::Ace::Offset& globalLocation = fingerInfo.globalLocation_;
78 const OHOS::Ace::Offset& localLocation = fingerInfo.localLocation_;
79 fingerInfoObj->SetProperty<int32_t>("id", fingerInfo.fingerId_);
80 fingerInfoObj->SetProperty<double>("globalX", SystemProperties::Px2Vp(globalLocation.GetX()));
81 fingerInfoObj->SetProperty<double>("globalY", SystemProperties::Px2Vp(globalLocation.GetY()));
82 fingerInfoObj->SetProperty<double>("localX", SystemProperties::Px2Vp(localLocation.GetX()));
83 fingerInfoObj->SetProperty<double>("localY", SystemProperties::Px2Vp(localLocation.GetY()));
84 return fingerInfoObj;
85 }
86
87 } // namespace OHOS::Ace::Framework
88