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