• 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_drag_function.h"
17 
18 #include "base/log/log.h"
19 #include "core/common/udmf/data_load_params.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_utils.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
22 
23 #include "js_native_api_types.h"
24 #include "napi/native_api.h"
25 #include "native_engine/native_engine.h"
26 #include "native_engine/native_value.h"
27 
28 #include "core/common/interaction/interaction_interface.h"
29 #include "core/common/udmf/udmf_client.h"
30 #include "core/components_ng/manager/drag_drop/drag_drop_global_controller.h"
31 #include "frameworks/bridge/common/utils/engine_helper.h"
32 #include "frameworks/bridge/declarative_frontend/engine/js_converter.h"
33 #include "frameworks/bridge/declarative_frontend/engine/js_types.h"
34 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
35 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
36 
37 namespace OHOS::Ace::Framework {
38 namespace {
NapiThrow(const RefPtr<Framework::JsEngine> & engine,int32_t errCode,const std::string & message)39 void NapiThrow(const RefPtr<Framework::JsEngine>& engine, int32_t errCode, const std::string& message)
40 {
41     NativeEngine* nativeEngine = engine->GetNativeEngine();
42     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
43     napi_value code = nullptr;
44     std::string strCode = std::to_string(errCode);
45     napi_create_string_utf8(env, strCode.c_str(), strCode.length(), &code);
46     napi_value msg = nullptr;
47     napi_create_string_utf8(env, message.c_str(), message.length(), &msg);
48     napi_value error = nullptr;
49     napi_create_error(env, code, msg, &error);
50     napi_throw(env, error);
51 }
52 
53 } // namespace
54 
55 class JsPasteData : public Referenced {
56 public:
JSBind(BindingTarget globalObj)57     static void JSBind(BindingTarget globalObj)
58     {
59         JSClass<JsPasteData>::Declare("PasteData");
60         JSClass<JsPasteData>::CustomMethod("setPlainText", &JsPasteData::SetPlainText);
61         JSClass<JsPasteData>::CustomMethod("getPlainText", &JsPasteData::GetPlainText);
62         JSClass<JsPasteData>::Bind(globalObj, &JsPasteData::Constructor, &JsPasteData::Destructor);
63     }
64 
SetPlainText(const JSCallbackInfo & args)65     void SetPlainText(const JSCallbackInfo& args)
66     {
67         if (args[0]->IsString()) {
68             pasteData_->SetPlainText(args[0]->ToString());
69         }
70     }
71 
GetPlainText(const JSCallbackInfo & args)72     void GetPlainText(const JSCallbackInfo& args)
73     {
74         auto plainText = JSVal(ToJSValue(pasteData_->GetPlainText()));
75         auto plainTextRef = JSRef<JSVal>::Make(plainText);
76         args.SetReturnValue(plainTextRef);
77     }
78 
SetPasteData(const RefPtr<PasteData> & pasteData)79     void SetPasteData(const RefPtr<PasteData>& pasteData)
80     {
81         pasteData_ = pasteData;
82     }
83 
GetPasteData() const84     RefPtr<PasteData> GetPasteData() const
85     {
86         return pasteData_;
87     }
88 
89 private:
Constructor(const JSCallbackInfo & args)90     static void Constructor(const JSCallbackInfo& args)
91     {
92         auto jsPasteData = Referenced::MakeRefPtr<JsPasteData>();
93         jsPasteData->IncRefCount();
94         args.SetReturnValue(Referenced::RawPtr(jsPasteData));
95     }
96 
Destructor(JsPasteData * jsPasteData)97     static void Destructor(JsPasteData* jsPasteData)
98     {
99         if (jsPasteData != nullptr) {
100             jsPasteData->DecRefCount();
101         }
102     }
103 
104     RefPtr<PasteData> pasteData_;
105 };
106 
JSBind(BindingTarget globalObj)107 void JsDragEvent::JSBind(BindingTarget globalObj)
108 {
109     JSClass<JsDragEvent>::Declare("DragEvent");
110     JSClass<JsDragEvent>::CustomMethod("getPasteData", &JsDragEvent::GetJsPasteData);
111     JSClass<JsDragEvent>::CustomMethod("getDisplayX", &JsDragEvent::GetScreenX);
112     JSClass<JsDragEvent>::CustomMethod("getDisplayY", &JsDragEvent::GetScreenY);
113     JSClass<JsDragEvent>::CustomMethod("getGlobalDisplayX", &JsDragEvent::GetGlobalDisplayX);
114     JSClass<JsDragEvent>::CustomMethod("getGlobalDisplayY", &JsDragEvent::GetGlobalDisplayY);
115     JSClass<JsDragEvent>::CustomMethod("getDragSource", &JsDragEvent::GetDragSource);
116     JSClass<JsDragEvent>::CustomMethod("isRemote", &JsDragEvent::IsRemote);
117     JSClass<JsDragEvent>::CustomMethod("getWindowX", &JsDragEvent::GetX);
118     JSClass<JsDragEvent>::CustomMethod("getWindowY", &JsDragEvent::GetY);
119     JSClass<JsDragEvent>::CustomMethod("getX", &JsDragEvent::GetX);
120     JSClass<JsDragEvent>::CustomMethod("getY", &JsDragEvent::GetY);
121     JSClass<JsDragEvent>::CustomMethod("getDescription", &JsDragEvent::GetDescription);
122     JSClass<JsDragEvent>::CustomMethod("setDescription", &JsDragEvent::SetDescription);
123     JSClass<JsDragEvent>::CustomMethod("setData", &JsDragEvent::SetData);
124     JSClass<JsDragEvent>::CustomMethod("getData", &JsDragEvent::GetData);
125     JSClass<JsDragEvent>::CustomMethod("getSummary", &JsDragEvent::GetSummary);
126     JSClass<JsDragEvent>::CustomMethod("setResult", &JsDragEvent::SetResult);
127     JSClass<JsDragEvent>::CustomMethod("getResult", &JsDragEvent::GetResult);
128     JSClass<JsDragEvent>::CustomMethod("getPreviewRect", &JsDragEvent::GetPreviewRect);
129     JSClass<JsDragEvent>::CustomProperty(
130         "useCustomDropAnimation", &JsDragEvent::GetUseCustomDropAnimation, &JsDragEvent::SetUseCustomDropAnimation);
131     JSClass<JsDragEvent>::CustomMethod("setDragInfo", &JsDragEvent::SetDragInfo);
132     JSClass<JsDragEvent>::CustomMethod("getDragInfo", &JsDragEvent::GetDragInfo);
133     JSClass<JsDragEvent>::CustomProperty("dragBehavior", &JsDragEvent::GetDragBehavior, &JsDragEvent::SetDragBehavior);
134     JSClass<JsDragEvent>::CustomMethod("getVelocityX", &JsDragEvent::GetVelocityX);
135     JSClass<JsDragEvent>::CustomMethod("getVelocityY", &JsDragEvent::GetVelocityY);
136     JSClass<JsDragEvent>::CustomMethod("getVelocity", &JsDragEvent::GetVelocity);
137     JSClass<JsDragEvent>::CustomMethod("getModifierKeyState", &JsDragEvent::GetModifierKeyState);
138     JSClass<JsDragEvent>::CustomMethod("executeDropAnimation", &JsDragEvent::ExecuteDropAnimation);
139     JSClass<JsDragEvent>::CustomMethod("startDataLoading", &JsDragEvent::StartDataLoading);
140     JSClass<JsDragEvent>::CustomMethod("getDisplayId", &JsDragEvent::GetDisplayId);
141     JSClass<JsDragEvent>::CustomMethod("enableInternalDropAnimation", &JsDragEvent::EnableInternalDropAnimation);
142     JSClass<JsDragEvent>::CustomMethod("setDataLoadParams", &JsDragEvent::SetDataLoadParams);
143     JSClass<JsDragEvent>::Bind(globalObj, &JsDragEvent::Constructor, &JsDragEvent::Destructor);
144 }
145 
SetJsPasteData(const JSRef<JSObject> & jsPasteData)146 void JsDragEvent::SetJsPasteData(const JSRef<JSObject>& jsPasteData)
147 {
148     jsPasteData_ = jsPasteData;
149 }
150 
GetJsPasteData(const JSCallbackInfo & args)151 void JsDragEvent::GetJsPasteData(const JSCallbackInfo& args)
152 {
153     args.SetReturnValue(jsPasteData_);
154 }
155 
GetDisplayId(const JSCallbackInfo & args)156 void JsDragEvent::GetDisplayId(const JSCallbackInfo& args)
157 {
158     CHECK_NULL_VOID(dragEvent_);
159     auto xValue = JSVal(ToJSValue(static_cast<int32_t>(dragEvent_->GetDisplayId())));
160     auto xValueRef = JSRef<JSVal>::Make(xValue);
161     args.SetReturnValue(xValueRef);
162 }
163 
GetScreenX(const JSCallbackInfo & args)164 void JsDragEvent::GetScreenX(const JSCallbackInfo& args)
165 {
166     auto xValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetScreenX())));
167     auto xValueRef = JSRef<JSVal>::Make(xValue);
168     args.SetReturnValue(xValueRef);
169 }
170 
GetScreenY(const JSCallbackInfo & args)171 void JsDragEvent::GetScreenY(const JSCallbackInfo& args)
172 {
173     auto yValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetScreenY())));
174     auto yValueRef = JSRef<JSVal>::Make(yValue);
175     args.SetReturnValue(yValueRef);
176 }
177 
GetGlobalDisplayX(const JSCallbackInfo & args)178 void JsDragEvent::GetGlobalDisplayX(const JSCallbackInfo& args)
179 {
180     auto xValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetGlobalDisplayX())));
181     auto xValueRef = JSRef<JSVal>::Make(xValue);
182     args.SetReturnValue(xValueRef);
183 }
184 
GetGlobalDisplayY(const JSCallbackInfo & args)185 void JsDragEvent::GetGlobalDisplayY(const JSCallbackInfo& args)
186 {
187     auto yValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetGlobalDisplayY())));
188     auto yValueRef = JSRef<JSVal>::Make(yValue);
189     args.SetReturnValue(yValueRef);
190 }
191 
GetDragSource(const JSCallbackInfo & args)192 void JsDragEvent::GetDragSource(const JSCallbackInfo& args)
193 {
194     CHECK_NULL_VOID(dragEvent_);
195     JSRef<JSVal> dragSource = JSRef<JSVal>::Make(ToJSValue(dragEvent_->GetDragSource()));
196     args.SetReturnValue(dragSource);
197 }
198 
IsRemote(const JSCallbackInfo & args)199 void JsDragEvent::IsRemote(const JSCallbackInfo& args)
200 {
201     CHECK_NULL_VOID(dragEvent_);
202     JSRef<JSVal> isRemoteDev = JSRef<JSVal>::Make(ToJSValue(dragEvent_->isRemoteDev()));
203     args.SetReturnValue(isRemoteDev);
204 }
205 
GetX(const JSCallbackInfo & args)206 void JsDragEvent::GetX(const JSCallbackInfo& args)
207 {
208     auto xValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetX())));
209     auto xValueRef = JSRef<JSVal>::Make(xValue);
210     args.SetReturnValue(xValueRef);
211 }
212 
GetY(const JSCallbackInfo & args)213 void JsDragEvent::GetY(const JSCallbackInfo& args)
214 {
215     auto yValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetY())));
216     auto yValueRef = JSRef<JSVal>::Make(yValue);
217     args.SetReturnValue(yValueRef);
218 }
219 
GetDescription(const JSCallbackInfo & args)220 void JsDragEvent::GetDescription(const JSCallbackInfo& args)
221 {
222     auto description = JSVal(ToJSValue(dragEvent_->GetDescription()));
223     auto descriptionRef = JSRef<JSVal>::Make(description);
224     args.SetReturnValue(descriptionRef);
225 }
226 
SetDescription(const JSCallbackInfo & args)227 void JsDragEvent::SetDescription(const JSCallbackInfo& args)
228 {
229     if (args[0]->IsString()) {
230         dragEvent_->SetDescription(args[0]->ToString());
231     }
232 }
233 
SetData(const JSCallbackInfo & args)234 void JsDragEvent::SetData(const JSCallbackInfo& args)
235 {
236     if (!args[0]->IsObject()) {
237         return;
238     }
239     auto engine = EngineHelper::GetCurrentEngine();
240     CHECK_NULL_VOID(engine);
241     NativeEngine* nativeEngine = engine->GetNativeEngine();
242     panda::Local<JsiValue> value = args[0].Get().GetLocalHandle();
243     JSValueWrapper valueWrapper = value;
244     ScopeRAII scope(reinterpret_cast<napi_env>(nativeEngine));
245     napi_value nativeValue = nativeEngine->ValueToNapiValue(valueWrapper);
246     RefPtr<UnifiedData> udData = UdmfClient::GetInstance()->TransformUnifiedData(nativeValue);
247     CHECK_NULL_VOID(udData);
248     dragEvent_->SetUseDataLoadParams(false);
249     dragEvent_->SetData(udData);
250 }
251 
SetDataLoadParams(const JSCallbackInfo & args)252 void JsDragEvent::SetDataLoadParams(const JSCallbackInfo& args)
253 {
254     if (!args[0]->IsObject()) {
255         return;
256     }
257     CHECK_NULL_VOID(dragEvent_);
258     auto engine = EngineHelper::GetCurrentEngine();
259     CHECK_NULL_VOID(engine);
260     NativeEngine* nativeEngine = engine->GetNativeEngine();
261     CHECK_NULL_VOID(nativeEngine);
262     panda::Local<JsiValue> value = args[0].Get().GetLocalHandle();
263     JSValueWrapper valueWrapper = value;
264     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
265     ScopeRAII scope(env);
266     napi_value nativeValue = nativeEngine->ValueToNapiValue(valueWrapper);
267     RefPtr<DataLoadParams> udDataLoadParams = UdmfClient::GetInstance()->TransformDataLoadParams(env, nativeValue);
268     CHECK_NULL_VOID(udDataLoadParams);
269     dragEvent_->SetUseDataLoadParams(true);
270     dragEvent_->SetDataLoadParams(udDataLoadParams);
271 }
272 
StartDataLoading(const JSCallbackInfo & args)273 void JsDragEvent::StartDataLoading(const JSCallbackInfo& args)
274 {
275     if (!args[0]->IsObject()) {
276         return;
277     }
278     auto engine = EngineHelper::GetCurrentEngine();
279     CHECK_NULL_VOID(engine);
280     std::string udKey = dragEvent_->GetUdKey();
281     if (udKey.empty()) {
282         args.SetReturnValue(JSVal::Undefined());
283         NapiThrow(engine, ERROR_CODE_DRAG_DATA_NOT_ONDROP, "Operation not allowed for current phase.");
284         return;
285     }
286     NativeEngine* nativeEngine = engine->GetNativeEngine();
287     panda::Local<JsiValue> value = args[0].Get().GetLocalHandle();
288     JSValueWrapper valueWrapper = value;
289     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
290     ScopeRAII scope(env);
291     napi_value nativeValue = nativeEngine->ValueToNapiValue(valueWrapper);
292     auto status = UdmfClient::GetInstance()->StartAsyncDataRetrieval(env, nativeValue, udKey);
293     if (status != 0) {
294         args.SetReturnValue(JSVal::Undefined());
295         napi_value result;
296         napi_get_and_clear_last_exception(env, &result);
297         NapiThrow(engine, ERROR_CODE_PARAM_INVALID, "Invalid input parameter.");
298         return;
299     }
300     auto jsUdKey = JSVal(ToJSValue(udKey));
301     auto jsUdKeyRef = JSRef<JSVal>::Make(jsUdKey);
302     args.SetReturnValue(jsUdKeyRef);
303 }
304 
EnableInternalDropAnimation(const JSCallbackInfo & args)305 void JsDragEvent::EnableInternalDropAnimation(const JSCallbackInfo& args)
306 {
307     auto engine = EngineHelper::GetCurrentEngine();
308     CHECK_NULL_VOID(engine);
309 
310     if (!NG::DragDropGlobalController::GetInstance().IsOnOnDropPhase()) {
311         NapiThrow(engine, ERROR_CODE_DRAG_DATA_NOT_ONDROP, "Operation not allowed for current phase.");
312         return;
313     }
314     if (!args[0]->IsString()) {
315         NapiThrow(engine, ERROR_CODE_PARAM_INVALID, "Invalid input parameter.");
316         return;
317     }
318     std::string configuration = args[0]->ToString();
319     TAG_LOGI(AceLogTag::ACE_DRAG, "Internal drop animation configuration is: %{public}s", configuration.c_str());
320     auto configurationJson = JsonUtil::ParseJsonString(configuration);
321     if (!configurationJson || configurationJson->IsNull() || !configurationJson->IsObject()) {
322         NapiThrow(engine, ERROR_CODE_PARAM_INVALID, "Invalid input parameter.");
323         return;
324     }
325 
326     auto interactionInterface = OHOS::Ace::InteractionInterface::GetInstance();
327     CHECK_NULL_VOID(interactionInterface);
328     int32_t ret = interactionInterface->EnableInternalDropAnimation(configuration);
329     if (ret == 0) {
330         CHECK_NULL_VOID(dragEvent_);
331         dragEvent_->SetNeedDoInternalDropAnimation(true);
332         return;
333     }
334 
335     switch (ret) {
336         case ERROR_CODE_PARAM_INVALID:
337             NapiThrow(engine, ERROR_CODE_PARAM_INVALID, "Invalid input parameter.");
338             break;
339         case ERROR_CODE_VERIFICATION_FAILED:
340             NapiThrow(engine, ERROR_CODE_VERIFICATION_FAILED, "Permission verification failed.");
341             break;
342         case ERROR_CODE_SYSTEMCAP_ERROR:
343             NapiThrow(engine, ERROR_CODE_SYSTEMCAP_ERROR, "Capability not supported.");
344             break;
345         default:
346             TAG_LOGW(AceLogTag::ACE_DRAG, "Enable internal drop animation failed, return value is %{public}d", ret);
347             break;
348     }
349 }
350 
GetData(const JSCallbackInfo & args)351 void JsDragEvent::GetData(const JSCallbackInfo& args)
352 {
353     auto dragData = dragEvent_->GetData();
354     if (!dragEvent_->IsGetDataSuccess()) {
355         TAG_LOGI(AceLogTag::ACE_DRAG, "UDMF GetData failed in first attempt");
356         std::string udKey = dragEvent_->GetUdKey();
357         if (udKey.empty()) {
358             args.SetReturnValue(JSVal::Undefined());
359             return;
360         }
361         int ret = UdmfClient::GetInstance()->GetData(dragData, udKey);
362         if (ret != 0) {
363             TAG_LOGW(AceLogTag::ACE_DRAG, "UDMF GetData failed: %{public}d", ret);
364             auto engine = EngineHelper::GetCurrentEngine();
365             if (!engine) {
366                 args.SetReturnValue(JSVal::Undefined());
367                 return;
368             }
369             auto errorInfo = UdmfClient::GetInstance()->GetErrorInfo(ret);
370             NapiThrow(engine, errorInfo.first, errorInfo.second);
371             return;
372         } else {
373             dragEvent_->SetData(dragData);
374             dragEvent_->SetIsGetDataSuccess(true);
375         }
376     }
377     CHECK_NULL_VOID(dragData);
378     napi_value nativeValue = UdmfClient::GetInstance()->TransformUdmfUnifiedData(dragData);
379     CHECK_NULL_VOID(nativeValue);
380     auto jsValue = JsConverter::ConvertNapiValueToJsVal(nativeValue);
381     args.SetReturnValue(jsValue);
382 }
383 
GetSummary(const JSCallbackInfo & args)384 void JsDragEvent::GetSummary(const JSCallbackInfo& args)
385 {
386     auto engine = EngineHelper::GetCurrentEngine();
387     CHECK_NULL_VOID(engine);
388     auto summary = dragEvent_->GetSummary();
389     napi_value nativeValue = UdmfClient::GetInstance()->TransformSummary(summary);
390     CHECK_NULL_VOID(nativeValue);
391     auto jsValue = JsConverter::ConvertNapiValueToJsVal(nativeValue);
392     args.SetReturnValue(jsValue);
393 }
394 
SetResult(const JSCallbackInfo & args)395 void JsDragEvent::SetResult(const JSCallbackInfo& args)
396 {
397     if (args[0]->IsNumber()) {
398         auto dragRet = args[0]->ToNumber<int32_t>();
399         dragEvent_->SetResult((DragRet)dragRet);
400     }
401 }
402 
GetResult(const JSCallbackInfo & args)403 void JsDragEvent::GetResult(const JSCallbackInfo& args)
404 {
405     CHECK_NULL_VOID(dragEvent_);
406     auto dragRet = JSVal(ToJSValue(static_cast<int32_t>(dragEvent_->GetResult())));
407     auto dragRetRef = JSRef<JSVal>::Make(dragRet);
408     args.SetReturnValue(dragRetRef);
409 }
410 
GetPreviewRect(const JSCallbackInfo & args)411 void JsDragEvent::GetPreviewRect(const JSCallbackInfo& args)
412 {
413     auto rectObj = CreateRectangle(dragEvent_->GetPreviewRect());
414     JSRef<JSVal> previewRect = JSRef<JSObject>::Cast(rectObj);
415     args.SetReturnValue(previewRect);
416 }
417 
SetUseCustomDropAnimation(const JSCallbackInfo & args)418 void JsDragEvent::SetUseCustomDropAnimation(const JSCallbackInfo& args)
419 {
420     if (args[0]->IsBoolean()) {
421         dragEvent_->UseCustomAnimation(args[0]->ToBoolean());
422     }
423 }
424 
GetUseCustomDropAnimation(const JSCallbackInfo & args)425 void JsDragEvent::GetUseCustomDropAnimation(const JSCallbackInfo& args)
426 {
427     auto useCustomAnimation = JSVal(ToJSValue(dragEvent_->IsUseCustomAnimation()));
428     auto useCustomAnimationRef = JSRef<JSVal>::Make(useCustomAnimation);
429     args.SetReturnValue(useCustomAnimationRef);
430 }
431 
SetDragInfo(const JSCallbackInfo & args)432 void JsDragEvent::SetDragInfo(const JSCallbackInfo& args)
433 {
434     if (!args[0]->IsObject()) {
435         return;
436     }
437     auto engine = EngineHelper::GetCurrentEngine();
438     CHECK_NULL_VOID(engine);
439     NativeEngine* nativeEngine = engine->GetNativeEngine();
440     panda::Local<JsiValue> value = args[0].Get().GetLocalHandle();
441     JSValueWrapper valueWrapper = value;
442     ScopeRAII scope(reinterpret_cast<napi_env>(nativeEngine));
443     napi_value nativeValue = nativeEngine->ValueToNapiValue(valueWrapper);
444     RefPtr<UnifiedData> udData = UdmfClient::GetInstance()->TransformUnifiedData(nativeValue);
445     CHECK_NULL_VOID(udData);
446     dragEvent_->SetData(udData);
447 }
448 
GetDragInfo(const JSCallbackInfo & args)449 void JsDragEvent::GetDragInfo(const JSCallbackInfo& args)
450 {
451     auto dragData = dragEvent_->GetDragInfo();
452     CHECK_NULL_VOID(dragData);
453     napi_value nativeValue = UdmfClient::GetInstance()->TransformUdmfUnifiedData(dragData);
454     CHECK_NULL_VOID(nativeValue);
455     auto jsValue = JsConverter::ConvertNapiValueToJsVal(nativeValue);
456     args.SetReturnValue(jsValue);
457 }
458 
convertDragBehavior(int32_t dragBehavior)459 OHOS::Ace::DragBehavior convertDragBehavior(int32_t dragBehavior)
460 {
461     switch (dragBehavior) {
462         case 0:
463             return OHOS::Ace::DragBehavior::COPY;
464         case 1:
465             return OHOS::Ace::DragBehavior::MOVE;
466         default:
467             return OHOS::Ace::DragBehavior::UNKNOWN;
468     }
469 }
470 
SetDragBehavior(const JSCallbackInfo & args)471 void JsDragEvent::SetDragBehavior(const JSCallbackInfo& args)
472 {
473     if (args[0]->IsNumber()) {
474         dragEvent_->SetCopy(!static_cast<bool>(args[0]->ToNumber<int32_t>()));
475         dragEvent_->SetDragBehavior(convertDragBehavior(args[0]->ToNumber<int32_t>()));
476     }
477 }
478 
GetDragBehavior(const JSCallbackInfo & args)479 void JsDragEvent::GetDragBehavior(const JSCallbackInfo& args)
480 {
481     auto dragBehavior = JSVal(ToJSValue(static_cast<int32_t>(
482         dragEvent_->GetDragBehavior() == OHOS::Ace::DragBehavior::MOVE ? OHOS::Ace::DragBehavior::MOVE
483                                                                        : OHOS::Ace::DragBehavior::COPY)));
484     auto dragBehaviorRef = JSRef<JSVal>::Make(dragBehavior);
485     args.SetReturnValue(dragBehaviorRef);
486 }
487 
GetVelocityX(const JSCallbackInfo & args)488 void JsDragEvent::GetVelocityX(const JSCallbackInfo& args)
489 {
490     auto jsValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetVelocity().GetVelocityX())));
491     auto jsValueRef = JSRef<JSVal>::Make(jsValue);
492     args.SetReturnValue(jsValueRef);
493 }
494 
GetVelocityY(const JSCallbackInfo & args)495 void JsDragEvent::GetVelocityY(const JSCallbackInfo& args)
496 {
497     auto jsValue = JSVal(ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetVelocity().GetVelocityY())));
498     auto jsValueRef = JSRef<JSVal>::Make(jsValue);
499     args.SetReturnValue(jsValueRef);
500 }
501 
GetVelocity(const JSCallbackInfo & args)502 void JsDragEvent::GetVelocity(const JSCallbackInfo& args)
503 {
504     auto jsValue = JSVal(
505         ToJSValue(PipelineBase::Px2VpWithCurrentDensity(dragEvent_->GetVelocity().GetVelocityValue())));
506     auto jsValueRef = JSRef<JSVal>::Make(jsValue);
507     args.SetReturnValue(jsValueRef);
508 }
509 
GetModifierKeyState(const JSCallbackInfo & args)510 void JsDragEvent::GetModifierKeyState(const JSCallbackInfo& args)
511 {
512     bool ret = false;
513     auto keyState = NG::ArkTSUtils::GetModifierKeyState(args.GetJsiRuntimeCallInfo(),
514         dragEvent_->GetPressedKeyCodes());
515     if (keyState->IsTrue()) {
516         ret = true;
517     }
518 
519     auto jsValueRef = JSRef<JSVal>::Make(ToJSValue(ret));
520     args.SetReturnValue(jsValueRef);
521 }
522 
ExecuteDropAnimation(const JSCallbackInfo & args)523 void JsDragEvent::ExecuteDropAnimation(const JSCallbackInfo& args)
524 {
525     if (!args[0]->IsFunction()) {
526         return;
527     }
528     RefPtr<JsFunction> jsExecuteDropAnimation =
529         AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
530     WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
531     auto executeDropAnimation = [execCtx = args.GetExecutionContext(), func = std::move(jsExecuteDropAnimation),
532                                     node = frameNode]() {
533         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
534         PipelineContext::SetCallBackNode(node);
535         func->Execute();
536     };
537     dragEvent_->SetDropAnimation(std::move(executeDropAnimation));
538 }
539 
Constructor(const JSCallbackInfo & args)540 void JsDragEvent::Constructor(const JSCallbackInfo& args)
541 {
542     auto dragEvent = Referenced::MakeRefPtr<JsDragEvent>();
543     CHECK_NULL_VOID(dragEvent);
544     dragEvent->IncRefCount();
545     args.SetReturnValue(Referenced::RawPtr(dragEvent));
546 }
547 
Destructor(JsDragEvent * dragEvent)548 void JsDragEvent::Destructor(JsDragEvent* dragEvent)
549 {
550     if (dragEvent != nullptr) {
551         dragEvent->DecRefCount();
552     }
553 }
554 
CreateRectangle(const Rect & info)555 JSRef<JSObject> JsDragEvent::CreateRectangle(const Rect& info)
556 {
557     JSRef<JSObject> rectObj = JSRef<JSObject>::New();
558     rectObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(info.Left()));
559     rectObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(info.Top()));
560     rectObj->SetProperty<double>("width", PipelineBase::Px2VpWithCurrentDensity(info.Width()));
561     rectObj->SetProperty<double>("height", PipelineBase::Px2VpWithCurrentDensity(info.Height()));
562     return rectObj;
563 }
564 
JSBind(BindingTarget globalObj)565 void JsDragSpringLoadingContext::JSBind(BindingTarget globalObj)
566 {
567     JSClass<JsDragSpringLoadingContext>::Declare("SpringLoadingContext");
568     JSClass<JsDragSpringLoadingContext>::CustomProperty(
569         "state", &JsDragSpringLoadingContext::GetState, &JsDragSpringLoadingContext::SetState);
570     JSClass<JsDragSpringLoadingContext>::CustomProperty("currentNotifySequence",
571         &JsDragSpringLoadingContext::GetCurrentNotifySequence, &JsDragSpringLoadingContext::SetCurrentNotifySequence);
572     JSClass<JsDragSpringLoadingContext>::CustomProperty(
573         "dragInfos", &JsDragSpringLoadingContext::GetDragInfos, &JsDragSpringLoadingContext::SetDragInfos);
574     JSClass<JsDragSpringLoadingContext>::CustomProperty(
575         "currentConfig", &JsDragSpringLoadingContext::GetCurrentConfig, &JsDragSpringLoadingContext::SetCurrentConfig);
576     JSClass<JsDragSpringLoadingContext>::CustomMethod("abort", &JsDragSpringLoadingContext::Abort);
577     JSClass<JsDragSpringLoadingContext>::CustomMethod(
578         "updateConfiguration", &JsDragSpringLoadingContext::UpdateConfiguration);
579     JSClass<JsDragSpringLoadingContext>::Bind(
580         globalObj, &JsDragSpringLoadingContext::Constructor, &JsDragSpringLoadingContext::Destructor);
581 }
582 
GetState(const JSCallbackInfo & args)583 void JsDragSpringLoadingContext::GetState(const JSCallbackInfo& args)
584 {
585     CHECK_NULL_VOID(context_);
586     auto state = JSVal(ToJSValue(static_cast<int32_t>(context_->GetState())));
587     auto stateRef = JSRef<JSVal>::Make(state);
588     args.SetReturnValue(stateRef);
589 }
590 
SetState(const JSCallbackInfo & args)591 void JsDragSpringLoadingContext::SetState(const JSCallbackInfo& args)
592 {
593     TAG_LOGD(AceLogTag::ACE_DRAG, "JsDragSpringLoadingContext can not support set state value.");
594 }
595 
GetCurrentNotifySequence(const JSCallbackInfo & args)596 void JsDragSpringLoadingContext::GetCurrentNotifySequence(const JSCallbackInfo& args)
597 {
598     CHECK_NULL_VOID(context_);
599     auto state = JSVal(ToJSValue(static_cast<int32_t>(context_->GetCurrentNotifySequence())));
600     auto stateRef = JSRef<JSVal>::Make(state);
601     args.SetReturnValue(stateRef);
602 }
603 
SetCurrentNotifySequence(const JSCallbackInfo & args)604 void JsDragSpringLoadingContext::SetCurrentNotifySequence(const JSCallbackInfo& args)
605 {
606     TAG_LOGD(AceLogTag::ACE_DRAG, "JsDragSpringLoadingContext can not support set currentNotifySequence value.");
607 }
608 
GetDragInfos(const JSCallbackInfo & args)609 void JsDragSpringLoadingContext::GetDragInfos(const JSCallbackInfo& args)
610 {
611     CHECK_NULL_VOID(context_);
612     JSRef<JSObject> dragInfosObj = JSRef<JSObject>::New();
613     auto summary = context_->GetSummary();
614     napi_value nativeValue = UdmfClient::GetInstance()->TransformSummary(summary);
615     CHECK_NULL_VOID(nativeValue);
616     auto jsValue = JsConverter::ConvertNapiValueToJsVal(nativeValue);
617     dragInfosObj->SetPropertyObject("dataSummary", jsValue);
618     dragInfosObj->SetProperty<std::string>("extraInfos", context_->GetExtraInfos());
619     JSRef<JSVal> dragInfosRef = dragInfosObj;
620     args.SetReturnValue(dragInfosRef);
621 }
622 
SetDragInfos(const JSCallbackInfo & args)623 void JsDragSpringLoadingContext::SetDragInfos(const JSCallbackInfo& args)
624 {
625     TAG_LOGD(AceLogTag::ACE_DRAG, "JsDragSpringLoadingContext can not support set dragInfos value.");
626 }
627 
GetCurrentConfig(const JSCallbackInfo & args)628 void JsDragSpringLoadingContext::GetCurrentConfig(const JSCallbackInfo& args)
629 {
630     CHECK_NULL_VOID(context_);
631     JSRef<JSObject> curConfigObj = JSRef<JSObject>::New();
632     const auto& config = context_->GetDragSpringLoadingConfiguration();
633     CHECK_NULL_VOID(config);
634     curConfigObj->SetProperty<int32_t>("stillTimeLimit", config->stillTimeLimit);
635     curConfigObj->SetProperty<int32_t>("updateInterval", config->updateInterval);
636     curConfigObj->SetProperty<int32_t>("updateNotifyCount", config->updateNotifyCount);
637     curConfigObj->SetProperty<int32_t>("updateToFinishInterval", config->updateToFinishInterval);
638     JSRef<JSVal> curConfigRef = curConfigObj;
639     args.SetReturnValue(curConfigRef);
640 }
641 
SetCurrentConfig(const JSCallbackInfo & args)642 void JsDragSpringLoadingContext::SetCurrentConfig(const JSCallbackInfo& args)
643 {
644     TAG_LOGD(AceLogTag::ACE_DRAG, "JsDragSpringLoadingContext can not support set currentConfig value.");
645 }
646 
Abort(const JSCallbackInfo & args)647 void JsDragSpringLoadingContext::Abort(const JSCallbackInfo& args)
648 {
649     CHECK_NULL_VOID(context_);
650     context_->SetSpringLoadingAborted();
651 }
652 
UpdateConfiguration(const JSCallbackInfo & args)653 void JsDragSpringLoadingContext::UpdateConfiguration(const JSCallbackInfo& args)
654 {
655     if (!args[0]->IsObject()) {
656         return;
657     }
658     CHECK_NULL_VOID(context_);
659 
660     auto validateAndSet = [](double value, int32_t defaultValue) -> int32_t {
661         return (std::isnan(value) || value < 0 || value > INT32_MAX) ? defaultValue : static_cast<int32_t>(value);
662     };
663 
664     auto config = MakeRefPtr<NG::DragSpringLoadingConfiguration>();
665     JSRef<JSObject> jsObj = JSRef<JSObject>::Cast(args[0]);
666 
667     config->stillTimeLimit = validateAndSet(
668         jsObj->GetPropertyValue<double>("stillTimeLimit", NG::DEFAULT_STILL_TIME_LIMIT), NG::DEFAULT_STILL_TIME_LIMIT);
669     config->updateInterval = validateAndSet(
670         jsObj->GetPropertyValue<double>("updateInterval", NG::DEFAULT_UPDATE_INTERVAL), NG::DEFAULT_UPDATE_INTERVAL);
671     config->updateNotifyCount =
672         validateAndSet(jsObj->GetPropertyValue<double>("updateNotifyCount", NG::DEFAULT_UPDATE_NOTIFY_COUNT),
673             NG::DEFAULT_UPDATE_NOTIFY_COUNT);
674     config->updateToFinishInterval =
675         validateAndSet(jsObj->GetPropertyValue<double>("updateToFinishInterval", NG::DEFAULT_UPDATE_TO_FINISH_INTERVAL),
676             NG::DEFAULT_UPDATE_TO_FINISH_INTERVAL);
677 
678     context_->SetDragSpringLoadingConfiguration(std::move(config));
679 }
680 
Constructor(const JSCallbackInfo & args)681 void JsDragSpringLoadingContext::Constructor(const JSCallbackInfo& args)
682 {
683     auto springLoadingContext = Referenced::MakeRefPtr<JsDragSpringLoadingContext>();
684     CHECK_NULL_VOID(springLoadingContext);
685     springLoadingContext->IncRefCount();
686     args.SetReturnValue(Referenced::RawPtr(springLoadingContext));
687 }
688 
Destructor(JsDragSpringLoadingContext * springLoadingContext)689 void JsDragSpringLoadingContext::Destructor(JsDragSpringLoadingContext* springLoadingContext)
690 {
691     if (springLoadingContext != nullptr) {
692         springLoadingContext->DecRefCount();
693     }
694 }
695 
JSBind(BindingTarget globalObj)696 void JsDragFunction::JSBind(BindingTarget globalObj)
697 {
698     JsPasteData::JSBind(globalObj);
699     JsDragEvent::JSBind(globalObj);
700     JsDragSpringLoadingContext::JSBind(globalObj);
701 }
702 
Execute()703 void JsDragFunction::Execute()
704 {
705     JsFunction::Execute();
706 }
707 
Execute(const RefPtr<DragEvent> & info,const std::string & extraParams)708 JSRef<JSVal> JsDragFunction::Execute(const RefPtr<DragEvent>& info, const std::string& extraParams)
709 {
710     JSRef<JSVal> dragInfo = JSRef<JSObject>::Cast(CreateDragEvent(info));
711     JSRef<JSVal> jsonInfo = JSRef<JSVal>::Make(ToJSValue(extraParams));
712     JSRef<JSVal> params[] = { dragInfo, jsonInfo };
713     return JsFunction::ExecuteJS(2, params);
714 }
715 
Execute(const RefPtr<DragEvent> & info)716 JSRef<JSVal> JsDragFunction::Execute(const RefPtr<DragEvent>& info)
717 {
718     JSRef<JSVal> dragInfo = JSRef<JSObject>::Cast(CreateDragEvent(info));
719     JSRef<JSVal> params[] = { dragInfo };
720     return JsFunction::ExecuteJS(1, params);
721 }
722 
DragSpringLoadingExecute(const RefPtr<DragSpringLoadingContext> & info)723 JSRef<JSVal> JsDragFunction::DragSpringLoadingExecute(const RefPtr<DragSpringLoadingContext>& info)
724 {
725     JSRef<JSVal> springLoadingContext = CreateSpringLoadingContext(info);
726     JSRef<JSVal> params[] = { springLoadingContext };
727     return JsFunction::ExecuteJS(1, params);
728 }
729 
ItemDragStartExecute(const ItemDragInfo & info,int32_t itemIndex)730 JSRef<JSVal> JsDragFunction::ItemDragStartExecute(const ItemDragInfo& info, int32_t itemIndex)
731 {
732     JSRef<JSVal> itemDragInfo = JSRef<JSObject>::Cast(CreateItemDragInfo(info));
733     JSRef<JSVal> itemIndexParam = JSRef<JSVal>::Make(ToJSValue(itemIndex));
734     JSRef<JSVal> params[] = { itemDragInfo, itemIndexParam };
735     return JsFunction::ExecuteJS(2, params);
736 }
737 
ItemDragEnterExecute(const ItemDragInfo & info)738 void JsDragFunction::ItemDragEnterExecute(const ItemDragInfo& info)
739 {
740     JSRef<JSObject> itemDragInfo = JSRef<JSObject>::Cast(CreateItemDragInfo(info));
741     JSRef<JSVal> param = itemDragInfo;
742     JsFunction::ExecuteJS(1, &param);
743 }
744 
ItemDragMoveExecute(const ItemDragInfo & info,int32_t itemIndex,int32_t insertIndex)745 void JsDragFunction::ItemDragMoveExecute(const ItemDragInfo& info, int32_t itemIndex, int32_t insertIndex)
746 {
747     JSRef<JSVal> itemDragInfo = JSRef<JSObject>::Cast(CreateItemDragInfo(info));
748     JSRef<JSVal> itemIndexParam = JSRef<JSVal>::Make(ToJSValue(itemIndex));
749     JSRef<JSVal> insertIndexParam = JSRef<JSVal>::Make(ToJSValue(insertIndex));
750     JSRef<JSVal> params[] = { itemDragInfo, itemIndexParam, insertIndexParam };
751     JsFunction::ExecuteJS(3, params);
752 }
753 
ItemDragLeaveExecute(const ItemDragInfo & info,int32_t itemIndex)754 void JsDragFunction::ItemDragLeaveExecute(const ItemDragInfo& info, int32_t itemIndex)
755 {
756     JSRef<JSVal> itemDragInfo = JSRef<JSObject>::Cast(CreateItemDragInfo(info));
757     JSRef<JSVal> itemIndexParam = JSRef<JSVal>::Make(ToJSValue(itemIndex));
758     JSRef<JSVal> params[] = { itemDragInfo, itemIndexParam };
759     JsFunction::ExecuteJS(2, params);
760 }
761 
ItemDropExecute(const ItemDragInfo & info,int32_t itemIndex,int32_t insertIndex,bool isSuccess)762 void JsDragFunction::ItemDropExecute(const ItemDragInfo& info, int32_t itemIndex, int32_t insertIndex, bool isSuccess)
763 {
764     JSRef<JSVal> itemDragInfo = JSRef<JSObject>::Cast(CreateItemDragInfo(info));
765     JSRef<JSVal> itemIndexParam = JSRef<JSVal>::Make(ToJSValue(itemIndex));
766     JSRef<JSVal> insertIndexParam = JSRef<JSVal>::Make(ToJSValue(insertIndex));
767     JSRef<JSVal> isSuccessParam = JSRef<JSVal>::Make(ToJSValue(isSuccess));
768     JSRef<JSVal> params[] = { itemDragInfo, itemIndexParam, insertIndexParam, isSuccessParam };
769     JsFunction::ExecuteJS(4, params);
770 }
771 
PreDragExecute(const PreDragStatus preDragStatus)772 void JsDragFunction::PreDragExecute(const PreDragStatus preDragStatus)
773 {
774     JSRef<JSVal> preDragStatusParam = JSRef<JSVal>::Make(ToJSValue(static_cast<int32_t>(preDragStatus)));
775     JSRef<JSVal> params[] = { preDragStatusParam };
776     JsFunction::ExecuteJS(1, params);
777 }
778 
CreateDragEvent(const RefPtr<DragEvent> & info)779 JSRef<JSObject> JsDragFunction::CreateDragEvent(const RefPtr<DragEvent>& info)
780 {
781     JSRef<JSObject> dragObj = JSClass<JsDragEvent>::NewInstance();
782     auto dragEvent = Referenced::Claim(dragObj->Unwrap<JsDragEvent>());
783     CHECK_NULL_RETURN(dragEvent, dragObj);
784     dragEvent->SetDragEvent(info);
785     auto pasteDataInfo = dragEvent->GetDragEvent()->GetPasteData();
786     JSRef<JSObject> pasteData = CreatePasteData(pasteDataInfo);
787     dragEvent->SetJsPasteData(pasteData);
788     return dragObj;
789 }
790 
CreatePasteData(const RefPtr<PasteData> & info)791 JSRef<JSObject> JsDragFunction::CreatePasteData(const RefPtr<PasteData>& info)
792 {
793     JSRef<JSObject> pasteObj = JSClass<JsPasteData>::NewInstance();
794     auto pasteData = Referenced::Claim(pasteObj->Unwrap<JsPasteData>());
795     CHECK_NULL_RETURN(pasteData, pasteObj);
796     pasteData->SetPasteData(info);
797     return pasteObj;
798 }
799 
CreateSpringLoadingContext(const RefPtr<DragSpringLoadingContext> & info)800 JSRef<JSObject> JsDragFunction::CreateSpringLoadingContext(const RefPtr<DragSpringLoadingContext>& info)
801 {
802     JSRef<JSObject> contextObj = JSClass<JsDragSpringLoadingContext>::NewInstance();
803     auto springLoadingContext = Referenced::Claim(contextObj->Unwrap<JsDragSpringLoadingContext>());
804     CHECK_NULL_RETURN(springLoadingContext, contextObj);
805     springLoadingContext->SetContext(info);
806     return contextObj;
807 }
808 
CreateItemDragInfo(const ItemDragInfo & info)809 JSRef<JSObject> JsDragFunction::CreateItemDragInfo(const ItemDragInfo& info)
810 {
811     JSRef<JSObject> itemDragInfoObj = JSRef<JSObject>::New();
812     itemDragInfoObj->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(info.GetX()));
813     itemDragInfoObj->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(info.GetY()));
814     return itemDragInfoObj;
815 }
816 
817 // use for ArkTs1.2 interop begin
GetDragEventPointer()818 int64_t JsDragEvent::GetDragEventPointer()
819 {
820     CHECK_NULL_RETURN(dragEvent_, 0);
821     return reinterpret_cast<int64_t>(AceType::RawPtr(dragEvent_));
822 }
823 
CreateDragEvent(void * dragEventPtr)824 JSRef<JSObject> JsDragEvent::CreateDragEvent(void* dragEventPtr)
825 {
826     JSRef<JSObject> dragObj = JSClass<JsDragEvent>::NewInstance();
827     CHECK_NULL_RETURN(dragEventPtr, dragObj);
828     auto dragInfoPtr = reinterpret_cast<DragEvent*>(dragEventPtr);
829     auto dragEvent = AceType::Claim(dragInfoPtr);
830     CHECK_NULL_RETURN(dragEvent, dragObj);
831     auto jsDragEvent = Referenced::Claim(dragObj->Unwrap<JsDragEvent>());
832     CHECK_NULL_RETURN(jsDragEvent, dragObj);
833     jsDragEvent->SetDragEvent(dragEvent);
834     return dragObj;
835 }
836 //use for ArkTs1.2 end
837 } // namespace OHOS::Ace::Framework
838