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;
89 history = eventInfo->GetHistory();
90 Local<ArrayRef> valueArray = ArrayRef::New(info->GetVM(), history.size());
91 auto index = 0;
92 for (auto const &point : history) {
93 Local<ObjectRef> touchObject = ObjectRef::New(info->GetVM());
94 const OHOS::Ace::Offset& globalLocation = point.GetGlobalLocation();
95 const OHOS::Ace::Offset& localLocation = point.GetLocalLocation();
96 const OHOS::Ace::Offset& screenLocation = point.GetScreenLocation();
97 touchObject->Set(info->GetVM(), ToJSValue("id"), ToJSValue(point.GetFingerId()));
98 touchObject->Set(info->GetVM(), ToJSValue("type"), ToJSValue(static_cast<int32_t>(point.GetTouchType())));
99 touchObject->Set(info->GetVM(),
100 ToJSValue("x"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX())));
101 touchObject->Set(info->GetVM(),
102 ToJSValue("y"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY())));
103 touchObject->Set(info->GetVM(),
104 ToJSValue("screenX"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX())));
105 touchObject->Set(info->GetVM(),
106 ToJSValue("screenY"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY())));
107 touchObject->Set(info->GetVM(),
108 ToJSValue("windowX"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX())));
109 touchObject->Set(info->GetVM(),
110 ToJSValue("windowY"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY())));
111 touchObject->Set(info->GetVM(),
112 ToJSValue("displayX"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX())));
113 touchObject->Set(info->GetVM(),
114 ToJSValue("displayY"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY())));
115
116 Local<ObjectRef> objRef = ObjectRef::New(info->GetVM());
117 objRef->Set(info->GetVM(), ToJSValue("touchObject"), (touchObject));
118 objRef->Set(info->GetVM(), ToJSValue("size"), ToJSValue(point.GetSize()));
119 objRef->Set(info->GetVM(), ToJSValue("force"), ToJSValue(static_cast<double>(point.GetForce())));
120 objRef->Set(info->GetVM(), ToJSValue("timestamp"),
121 ToJSValue(static_cast<double>(point.GetTimeStamp().time_since_epoch().count())));
122
123 ArrayRef::SetValueAt(info->GetVM(), valueArray, index++, objRef);
124 }
125
126 return valueArray;
127 }
128 #endif
129
130 } // namespace OHOS::Ace::Framework
131