• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/js_types.h"
17 
18 namespace OHOS::Ace::Framework {
19 
20 static const std::unordered_set<std::string> g_clickPreventDefPattern = {"RichEditor"};
21 static const std::unordered_set<std::string> g_touchPreventDefPattern = {};
22 
23 #ifdef USE_ARK_ENGINE
JsStopPropagation(panda::JsiRuntimeCallInfo * info)24 Local<JSValueRef> JsStopPropagation(panda::JsiRuntimeCallInfo *info)
25 {
26     Local<JSValueRef> thisObj = info->GetThisRef();
27     auto eventInfo = static_cast<BaseEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
28         info->GetVM(), 0));
29     if (eventInfo) {
30         eventInfo->SetStopPropagation(true);
31     }
32     return JSValueRef::Undefined(info->GetVM());
33 }
34 
JsPreventDefault(panda::JsiRuntimeCallInfo * info)35 Local<JSValueRef> JsPreventDefault(panda::JsiRuntimeCallInfo *info)
36 {
37     Local<JSValueRef> thisObj = info->GetThisRef();
38     auto eventInfo = static_cast<BaseEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
39         info->GetVM(), 0));
40     if (eventInfo) {
41         eventInfo->SetPreventDefault(true);
42     }
43     return JSValueRef::Undefined(info->GetVM());
44 }
45 
JsClickPreventDefault(panda::JsiRuntimeCallInfo * info)46 Local<JSValueRef> JsClickPreventDefault(panda::JsiRuntimeCallInfo *info)
47 {
48     Local<JSValueRef> thisObj = info->GetThisRef();
49     auto eventInfo = static_cast<BaseEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
50         info->GetVM(), 0));
51     if (eventInfo) {
52         auto patternName = eventInfo->GetPatternName();
53         if (g_clickPreventDefPattern.find(patternName.c_str()) == g_clickPreventDefPattern.end()) {
54             JSException::Throw(ERROR_CODE_COMPONENT_NOT_SUPPORTED_PREVENT_FUNCTION, "%s",
55                 "Component does not support prevent function.");
56             return JSValueRef::Undefined(info->GetVM());
57         }
58         eventInfo->SetPreventDefault(true);
59     }
60     return JSValueRef::Undefined(info->GetVM());
61 }
62 
JsTouchPreventDefault(panda::JsiRuntimeCallInfo * info)63 Local<JSValueRef> JsTouchPreventDefault(panda::JsiRuntimeCallInfo *info)
64 {
65     Local<JSValueRef> thisObj = info->GetThisRef();
66     auto eventInfo = static_cast<BaseEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
67         info->GetVM(), 0));
68     if (eventInfo) {
69         auto patternName = eventInfo->GetPatternName();
70         if (g_touchPreventDefPattern.find(patternName.c_str()) == g_touchPreventDefPattern.end()) {
71             JSException::Throw(ERROR_CODE_COMPONENT_NOT_SUPPORTED_PREVENT_FUNCTION, "%s",
72                 "Component does not support prevent function.");
73             return JSValueRef::Undefined(info->GetVM());
74         }
75         eventInfo->SetPreventDefault(true);
76     }
77     return JSValueRef::Undefined(info->GetVM());
78 }
79 
JsGetHistoricalPoints(panda::JsiRuntimeCallInfo * info)80 Local<JSValueRef> JsGetHistoricalPoints(panda::JsiRuntimeCallInfo *info)
81 {
82     Local<JSValueRef> thisObj = info->GetThisRef();
83     auto eventInfo = static_cast<TouchEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
84         info->GetVM(), 0));
85     if (!eventInfo) {
86         return JSValueRef::Undefined(info->GetVM());
87     }
88     std::list<TouchLocationInfo> history = eventInfo->GetHistory();
89     Local<ArrayRef> valueArray = ArrayRef::New(info->GetVM(), history.size());
90     auto index = 0;
91     for (auto const &point : history) {
92         Local<ObjectRef> touchObject = ObjectRef::New(info->GetVM());
93         const OHOS::Ace::Offset& globalLocation = point.GetGlobalLocation();
94         const OHOS::Ace::Offset& localLocation = point.GetLocalLocation();
95         const OHOS::Ace::Offset& screenLocation = point.GetScreenLocation();
96         auto x = PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX());
97         auto y = PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY());
98         auto globalX = PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX());
99         auto globalY = PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY());
100         auto displayX = PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX());
101         auto displayY = PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY());
102         touchObject->Set(info->GetVM(), ToJSValue("id"), ToJSValue(point.GetFingerId()));
103         touchObject->Set(info->GetVM(), ToJSValue("type"), ToJSValue(static_cast<int32_t>(point.GetTouchType())));
104         touchObject->Set(info->GetVM(), ToJSValue("x"), ToJSValue(x));
105         touchObject->Set(info->GetVM(), ToJSValue("y"), ToJSValue(y));
106         touchObject->Set(info->GetVM(), ToJSValue("screenX"), ToJSValue(globalX));
107         touchObject->Set(info->GetVM(), ToJSValue("screenY"), ToJSValue(globalY));
108         touchObject->Set(info->GetVM(), ToJSValue("windowX"), ToJSValue(globalX));
109         touchObject->Set(info->GetVM(), ToJSValue("windowY"), ToJSValue(globalY));
110         touchObject->Set(info->GetVM(), ToJSValue("displayX"), ToJSValue(displayX));
111         touchObject->Set(info->GetVM(), ToJSValue("displayY"), ToJSValue(displayY));
112         touchObject->Set(info->GetVM(), ToJSValue("hand"), ToJSValue(point.GetOperatingHand()));
113         touchObject->Set(info->GetVM(), ToJSValue("pressedTime"),
114             ToJSValue(static_cast<double>(point.GetPressedTime().time_since_epoch().count())));
115         touchObject->Set(info->GetVM(), ToJSValue("pressure"), ToJSValue(point.GetForce()));
116         touchObject->Set(info->GetVM(),
117             ToJSValue("width"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(point.GetWidth())));
118         touchObject->Set(info->GetVM(),
119             ToJSValue("height"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(point.GetHeight())));
120 
121         Local<ObjectRef> objRef = ObjectRef::New(info->GetVM());
122         objRef->Set(info->GetVM(), ToJSValue("touchObject"), (touchObject));
123         objRef->Set(info->GetVM(), ToJSValue("size"), ToJSValue(point.GetSize()));
124         objRef->Set(info->GetVM(), ToJSValue("force"), ToJSValue(static_cast<double>(point.GetForce())));
125         objRef->Set(info->GetVM(), ToJSValue("timestamp"),
126             ToJSValue(static_cast<double>(point.GetTimeStamp().time_since_epoch().count())));
127 
128         ArrayRef::SetValueAt(info->GetVM(), valueArray, index++, objRef);
129     }
130 
131     return valueArray;
132 }
133 #endif
134 
135 } // namespace OHOS::Ace::Framework
136