• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, &param);
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", PipelineBase::Px2VpWithCurrentDensity(info.GetOffsetX()));
39     gestureInfoObj->SetProperty<double>("offsetY", PipelineBase::Px2VpWithCurrentDensity(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>(
45         "globalX", PipelineBase::Px2VpWithCurrentDensity(info.GetGlobalLocation().GetX()));
46     gestureInfoObj->SetProperty<double>(
47         "globalY", PipelineBase::Px2VpWithCurrentDensity(info.GetGlobalLocation().GetY()));
48     gestureInfoObj->SetProperty<double>(
49         "localX", PipelineBase::Px2VpWithCurrentDensity(info.GetLocalLocation().GetX()));
50     gestureInfoObj->SetProperty<double>(
51         "localY", PipelineBase::Px2VpWithCurrentDensity(info.GetLocalLocation().GetY()));
52     gestureInfoObj->SetProperty<double>(
53         "pinchCenterX", PipelineBase::Px2VpWithCurrentDensity(info.GetPinchCenter().GetX()));
54     gestureInfoObj->SetProperty<double>(
55         "pinchCenterY", PipelineBase::Px2VpWithCurrentDensity(info.GetPinchCenter().GetY()));
56     gestureInfoObj->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
57     gestureInfoObj->SetProperty<double>("pressure", info.GetForce());
58     if (info.GetTiltX().has_value()) {
59         gestureInfoObj->SetProperty<double>("tiltX", info.GetTiltX().value());
60     }
61     if (info.GetTiltY().has_value()) {
62         gestureInfoObj->SetProperty<double>("tiltY", info.GetTiltY().value());
63     }
64     gestureInfoObj->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
65 
66     gestureInfoObj->SetProperty<double>(
67         "velocityX", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityX()));
68     gestureInfoObj->SetProperty<double>(
69         "velocityY", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityY()));
70     gestureInfoObj->SetProperty<double>(
71         "velocity", PipelineBase::Px2VpWithCurrentDensity(info.GetVelocity().GetVelocityValue()));
72 
73     JSRef<JSArray> fingerArr = JSRef<JSArray>::New();
74     const std::list<FingerInfo>& fingerList = info.GetFingerList();
75     std::list<FingerInfo> notTouchFingerList;
76     int32_t maxFingerId = -1;
77     for (const FingerInfo& fingerInfo : fingerList) {
78         JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
79         if (fingerInfo.sourceType_ == SourceType::TOUCH && fingerInfo.sourceTool_ == SourceTool::FINGER) {
80             fingerArr->SetValueAt(fingerInfo.fingerId_, element);
81             if (fingerInfo.fingerId_ > maxFingerId) {
82                 maxFingerId = fingerInfo.fingerId_;
83             }
84         } else {
85             notTouchFingerList.emplace_back(fingerInfo);
86         }
87     }
88     auto idx = maxFingerId + 1;
89     for (const FingerInfo& fingerInfo : notTouchFingerList) {
90         JSRef<JSObject> element = CreateFingerInfo(fingerInfo);
91         fingerArr->SetValueAt(idx++, element);
92     }
93     gestureInfoObj->SetPropertyObject("fingerList", fingerArr);
94 
95     auto target = CreateEventTargetObject(info);
96     gestureInfoObj->SetPropertyObject("target", target);
97     return gestureInfoObj;
98 }
99 
CreateFingerInfo(const FingerInfo & fingerInfo)100 JSRef<JSObject> JsGestureFunction::CreateFingerInfo(const FingerInfo& fingerInfo)
101 {
102     JSRef<JSObject> fingerInfoObj = JSRef<JSObject>::New();
103     const OHOS::Ace::Offset& globalLocation = fingerInfo.globalLocation_;
104     const OHOS::Ace::Offset& localLocation = fingerInfo.localLocation_;
105     fingerInfoObj->SetProperty<int32_t>("id", fingerInfo.fingerId_);
106     fingerInfoObj->SetProperty<double>("globalX", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX()));
107     fingerInfoObj->SetProperty<double>("globalY", PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY()));
108     fingerInfoObj->SetProperty<double>("localX", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX()));
109     fingerInfoObj->SetProperty<double>("localY", PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY()));
110     return fingerInfoObj;
111 }
112 
113 } // namespace OHOS::Ace::Framework
114