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", "Hyperlink" };
21 static const std::unordered_set<std::string> g_touchPreventDefPattern = { "Hyperlink" };
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
JsPropagation(panda::JsiRuntimeCallInfo * info)35 Local<JSValueRef> JsPropagation(panda::JsiRuntimeCallInfo* info)
36 {
37 Local<JSValueRef> thisObj = info->GetThisRef();
38 auto eventInfo =
39 static_cast<BaseEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(info->GetVM(), 0));
40 if (eventInfo) {
41 eventInfo->SetStopPropagation(false);
42 }
43 return JSValueRef::Undefined(info->GetVM());
44 }
45
JsPreventDefault(panda::JsiRuntimeCallInfo * info)46 Local<JSValueRef> JsPreventDefault(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 eventInfo->SetPreventDefault(true);
53 }
54 return JSValueRef::Undefined(info->GetVM());
55 }
56
JsClickPreventDefault(panda::JsiRuntimeCallInfo * info)57 Local<JSValueRef> JsClickPreventDefault(panda::JsiRuntimeCallInfo *info)
58 {
59 Local<JSValueRef> thisObj = info->GetThisRef();
60 auto eventInfo = static_cast<BaseEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
61 info->GetVM(), 0));
62 if (eventInfo) {
63 auto patternName = eventInfo->GetPatternName();
64 if (g_clickPreventDefPattern.find(patternName.c_str()) == g_clickPreventDefPattern.end()) {
65 JSException::Throw(ERROR_CODE_COMPONENT_NOT_SUPPORTED_PREVENT_FUNCTION, "%s",
66 "Component does not support prevent function.");
67 return JSValueRef::Undefined(info->GetVM());
68 }
69 eventInfo->SetPreventDefault(true);
70 }
71 return JSValueRef::Undefined(info->GetVM());
72 }
73
JsTouchPreventDefault(panda::JsiRuntimeCallInfo * info)74 Local<JSValueRef> JsTouchPreventDefault(panda::JsiRuntimeCallInfo *info)
75 {
76 Local<JSValueRef> thisObj = info->GetThisRef();
77 auto eventInfo = static_cast<BaseEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
78 info->GetVM(), 0));
79 if (eventInfo) {
80 auto patternName = eventInfo->GetPatternName();
81 if (g_touchPreventDefPattern.find(patternName.c_str()) == g_touchPreventDefPattern.end()) {
82 JSException::Throw(ERROR_CODE_COMPONENT_NOT_SUPPORTED_PREVENT_FUNCTION, "%s",
83 "Component does not support prevent function.");
84 return JSValueRef::Undefined(info->GetVM());
85 }
86 eventInfo->SetPreventDefault(true);
87 }
88 return JSValueRef::Undefined(info->GetVM());
89 }
90
JsGetHistoricalPoints(panda::JsiRuntimeCallInfo * info)91 Local<JSValueRef> JsGetHistoricalPoints(panda::JsiRuntimeCallInfo *info)
92 {
93 Local<JSValueRef> thisObj = info->GetThisRef();
94 auto eventInfo = static_cast<TouchEventInfo*>(panda::Local<panda::ObjectRef>(thisObj)->GetNativePointerField(
95 info->GetVM(), 0));
96 if (!eventInfo) {
97 return JSValueRef::Undefined(info->GetVM());
98 }
99 std::list<TouchLocationInfo> history = eventInfo->GetHistory();
100 Local<ArrayRef> valueArray = ArrayRef::New(info->GetVM(), history.size());
101 auto index = 0;
102 for (auto const &point : history) {
103 Local<ObjectRef> touchObject = ObjectRef::New(info->GetVM());
104 const OHOS::Ace::Offset& globalLocation = point.GetGlobalLocation();
105 const OHOS::Ace::Offset& localLocation = point.GetLocalLocation();
106 const OHOS::Ace::Offset& screenLocation = point.GetScreenLocation();
107 const OHOS::Ace::Offset& globalDisplayLocation = point.GetGlobalDisplayLocation();
108 auto x = PipelineBase::Px2VpWithCurrentDensity(localLocation.GetX());
109 auto y = PipelineBase::Px2VpWithCurrentDensity(localLocation.GetY());
110 auto globalX = PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetX());
111 auto globalY = PipelineBase::Px2VpWithCurrentDensity(globalLocation.GetY());
112 auto displayX = PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetX());
113 auto displayY = PipelineBase::Px2VpWithCurrentDensity(screenLocation.GetY());
114 auto globalDisplayX = PipelineBase::Px2VpWithCurrentDensity(globalDisplayLocation.GetX());
115 auto globalDisplayY = PipelineBase::Px2VpWithCurrentDensity(globalDisplayLocation.GetY());
116 touchObject->Set(info->GetVM(), ToJSValue("id"), ToJSValue(point.GetFingerId()));
117 touchObject->Set(info->GetVM(), ToJSValue("type"), ToJSValue(static_cast<int32_t>(point.GetTouchType())));
118 touchObject->Set(info->GetVM(), ToJSValue("x"), ToJSValue(x));
119 touchObject->Set(info->GetVM(), ToJSValue("y"), ToJSValue(y));
120 touchObject->Set(info->GetVM(), ToJSValue("screenX"), ToJSValue(globalX));
121 touchObject->Set(info->GetVM(), ToJSValue("screenY"), ToJSValue(globalY));
122 touchObject->Set(info->GetVM(), ToJSValue("windowX"), ToJSValue(globalX));
123 touchObject->Set(info->GetVM(), ToJSValue("windowY"), ToJSValue(globalY));
124 touchObject->Set(info->GetVM(), ToJSValue("displayX"), ToJSValue(displayX));
125 touchObject->Set(info->GetVM(), ToJSValue("displayY"), ToJSValue(displayY));
126 touchObject->Set(info->GetVM(), ToJSValue("globalDisplayX"), ToJSValue(globalDisplayX));
127 touchObject->Set(info->GetVM(), ToJSValue("globalDisplayY"), ToJSValue(globalDisplayY));
128 touchObject->Set(info->GetVM(), ToJSValue("pressedTime"),
129 ToJSValue(static_cast<double>(point.GetPressedTime().time_since_epoch().count())));
130 touchObject->Set(info->GetVM(), ToJSValue("pressure"), ToJSValue(point.GetForce()));
131 touchObject->Set(info->GetVM(),
132 ToJSValue("width"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(point.GetWidth())));
133 touchObject->Set(info->GetVM(),
134 ToJSValue("height"), ToJSValue(PipelineBase::Px2VpWithCurrentDensity(point.GetHeight())));
135 touchObject->Set(info->GetVM(), ToJSValue("hand"), ToJSValue(point.GetOperatingHand()));
136
137 Local<ObjectRef> objRef = ObjectRef::New(info->GetVM());
138 objRef->Set(info->GetVM(), ToJSValue("touchObject"), (touchObject));
139 objRef->Set(info->GetVM(), ToJSValue("size"), ToJSValue(point.GetSize()));
140 objRef->Set(info->GetVM(), ToJSValue("force"), ToJSValue(static_cast<double>(point.GetForce())));
141 objRef->Set(info->GetVM(), ToJSValue("timestamp"),
142 ToJSValue(static_cast<double>(point.GetTimeStamp().time_since_epoch().count())));
143
144 ArrayRef::SetValueAt(info->GetVM(), valueArray, index++, objRef);
145 }
146
147 return valueArray;
148 }
149 #endif
150
151 } // namespace OHOS::Ace::Framework
152