1 /*
2 * Copyright (c) 2024 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 #include "core/interfaces/native/node/drag_adapter_impl.h"
16
17 #include "core/common/udmf/udmf_client.h"
18 #include "core/components_ng/base/view_abstract.h"
19 #include "core/components_ng/manager/drag_drop/drag_drop_func_wrapper.h"
20
21 namespace OHOS::Ace::DragAdapter {
22 namespace {
DragActionConvert(ArkUIDragAction * dragAction,std::shared_ptr<OHOS::Ace::NG::ArkUIInteralDragAction> internalDragAction)23 static void DragActionConvert(
24 ArkUIDragAction* dragAction, std::shared_ptr<OHOS::Ace::NG::ArkUIInteralDragAction> internalDragAction)
25 {
26 CHECK_NULL_VOID(dragAction);
27 CHECK_NULL_VOID(internalDragAction);
28 internalDragAction->dragPointerEvent.pointerId = dragAction->pointerId;
29 internalDragAction->size = dragAction->size;
30 internalDragAction->previewOption.isScaleEnabled = dragAction->dragPreviewOption.isScaleEnabled;
31 if (!internalDragAction->previewOption.isScaleEnabled) {
32 internalDragAction->previewOption.isDefaultShadowEnabled = dragAction->dragPreviewOption.isDefaultShadowEnabled;
33 internalDragAction->previewOption.isDefaultRadiusEnabled = dragAction->dragPreviewOption.isDefaultRadiusEnabled;
34 }
35 internalDragAction->previewOption.defaultAnimationBeforeLifting =
36 dragAction->dragPreviewOption.defaultAnimationBeforeLifting;
37 internalDragAction->previewOption.enableHapticFeedback = dragAction->dragPreviewOption.enableHapticFeedback;
38 internalDragAction->previewOption.isMultiSelectionEnabled = dragAction->dragPreviewOption.isMultiSelectionEnabled;
39 internalDragAction->previewOption.enableEdgeAutoScroll = dragAction->dragPreviewOption.enableEdgeAutoScroll;
40 internalDragAction->previewOption.isNumber = dragAction->dragPreviewOption.isNumberBadgeEnabled;
41 if (dragAction->dragPreviewOption.badgeNumber > 1) {
42 internalDragAction->previewOption.badgeNumber = dragAction->dragPreviewOption.badgeNumber;
43 } else {
44 internalDragAction->previewOption.isShowBadge = dragAction->dragPreviewOption.isShowBadge;
45 }
46 if (!dragAction->useDataLoadParams) {
47 RefPtr<UnifiedData> udData = UdmfClient::GetInstance()->TransformUnifiedDataForNative(dragAction->unifiedData);
48 internalDragAction->unifiedData = udData;
49 } else {
50 RefPtr<DataLoadParams> udDataLoadParams =
51 UdmfClient::GetInstance()->TransformDataLoadParamsForNative(dragAction->dataLoadParams);
52 internalDragAction->dataLoadParams = udDataLoadParams;
53 }
54 internalDragAction->instanceId = dragAction->instanceId;
55 internalDragAction->touchPointX = dragAction->touchPointX;
56 internalDragAction->touchPointY = dragAction->touchPointY;
57 internalDragAction->hasTouchPoint = dragAction->hasTouchPoint;
58 }
59
StartDrag(ArkUIDragAction * dragAction)60 ArkUI_Int32 StartDrag(ArkUIDragAction* dragAction)
61 {
62 CHECK_NULL_RETURN(dragAction, -1);
63 auto internalDragAction = std::make_shared<OHOS::Ace::NG::ArkUIInteralDragAction>();
64
65 CHECK_NULL_RETURN(internalDragAction, -1);
66 auto callbacks = [listener = dragAction->listener, userData = dragAction->userData,
67 instanceId = dragAction->instanceId](const DragNotifyMsg& dragNotifyMsg, int32_t status) {
68 auto pipelineContext = NG::PipelineContext::GetContextByContainerId(instanceId);
69 CHECK_NULL_VOID(pipelineContext);
70 auto manager = pipelineContext->GetDragDropManager();
71 CHECK_NULL_VOID(manager);
72 ArkUIDragEvent dragEvent;
73 dragEvent.dragResult = static_cast<int32_t>(dragNotifyMsg.result);
74 dragEvent.dragBehavior = static_cast<int32_t>(dragNotifyMsg.dragBehavior);
75
76 auto action = manager->GetDragAction();
77 if (action != nullptr) {
78 action->hasHandle = false;
79 }
80 ArkUIDragAndDropInfo outInfo;
81 outInfo.status = status;
82 outInfo.dragEvent = &dragEvent;
83 CHECK_NULL_VOID(listener);
84 listener(&outInfo, userData);
85 };
86 auto* pixelMapTemp = reinterpret_cast<std::shared_ptr<void*>*>(dragAction->pixelmapArray);
87 for (int index = 0; index < dragAction->size; index++) {
88 auto pixelMap = PixelMap::CreatePixelMap(&pixelMapTemp[index]);
89 internalDragAction->pixelMapList.push_back(pixelMap);
90 }
91 internalDragAction->callback = callbacks;
92 DragActionConvert(dragAction, internalDragAction);
93 auto ret = OHOS::Ace::NG::DragDropFuncWrapper::StartDragAction(internalDragAction);
94 if (ret == -1) {
95 DragNotifyMsg dragNotifyMsg;
96 dragNotifyMsg.result = DragRet::DRAG_CANCEL;
97 OHOS::Ace::NG::DragDropFuncWrapper::HandleCallback(internalDragAction,
98 dragNotifyMsg, NG::DragAdapterStatus::ENDED);
99 }
100 return 0;
101 }
102
RegisterStatusListener(ArkUIDragAction * dragAction,void * userData,DragStatusCallback listener)103 void RegisterStatusListener(ArkUIDragAction* dragAction, void* userData, DragStatusCallback listener)
104 {
105 CHECK_NULL_VOID(dragAction);
106 dragAction->listener = listener;
107 dragAction->userData = userData;
108 }
109
UnRegisterStatusListener(ArkUIDragAction * dragAction)110 void UnRegisterStatusListener(ArkUIDragAction* dragAction)
111 {
112 CHECK_NULL_VOID(dragAction);
113 dragAction->listener = nullptr;
114 dragAction->userData = nullptr;
115 }
116
CreateDragActionWithNode(ArkUINodeHandle node)117 ArkUIDragAction* CreateDragActionWithNode(ArkUINodeHandle node)
118 {
119 CHECK_NULL_RETURN(node, nullptr);
120 auto* frameNode = reinterpret_cast<NG::FrameNode*>(node);
121 CHECK_NULL_RETURN(frameNode, nullptr);
122 auto pipeline = frameNode->GetContext();
123 CHECK_NULL_RETURN(pipeline, nullptr);
124 auto* dragAction = new ArkUIDragAction();
125 CHECK_NULL_RETURN(dragAction, nullptr);
126 dragAction->instanceId = pipeline->GetInstanceId();
127 return dragAction;
128 }
129
CreateDragActionWithContext(ArkUIContext * context)130 ArkUIDragAction* CreateDragActionWithContext(ArkUIContext* context)
131 {
132 CHECK_NULL_RETURN(context, nullptr);
133 auto* dragAction = new ArkUIDragAction();
134 CHECK_NULL_RETURN(dragAction, nullptr);
135 dragAction->instanceId = context->id;
136 return dragAction;
137 }
138
SetDragPreview(ArkUINodeHandle node,void * dragPreview)139 void SetDragPreview(ArkUINodeHandle node, void* dragPreview)
140 {
141 auto* frameNode = reinterpret_cast<NG::FrameNode*>(node);
142 CHECK_NULL_VOID(frameNode);
143 NG::DragDropInfo dragPreviewInfo;
144 dragPreviewInfo.pixelMap = PixelMap::CreatePixelMap(dragPreview);
145 NG::ViewAbstract::SetDragPreview(frameNode, dragPreviewInfo);
146 }
147
SetDragEventStrictReportingEnabledWithNode(bool enabled)148 void SetDragEventStrictReportingEnabledWithNode(bool enabled)
149 {
150 NG::ViewAbstract::SetDragEventStrictReportingEnabled(enabled);
151 }
152
SetDragEventStrictReportingEnabledWithContext(ArkUI_Int32 instanceId,bool enabled)153 void SetDragEventStrictReportingEnabledWithContext(ArkUI_Int32 instanceId, bool enabled)
154 {
155 NG::ViewAbstract::SetDragEventStrictReportingEnabled(instanceId, enabled);
156 }
157
EnableDropDisallowedBadge(bool enabled)158 void EnableDropDisallowedBadge(bool enabled)
159 {
160 NG::ViewAbstract::EnableDropDisallowedBadge(enabled);
161 }
162
RequestDragEndPending()163 ArkUI_Int32 RequestDragEndPending()
164 {
165 return NG::DragDropFuncWrapper::RequestDragEndPending();
166 }
167
NotifyDragResult(ArkUI_Int32 requestId,ArkUI_Int32 result)168 ArkUI_Int32 NotifyDragResult(ArkUI_Int32 requestId, ArkUI_Int32 result)
169 {
170 return NG::DragDropFuncWrapper::NotifyDragResult(requestId, result);
171 }
172
NotifyDragEndPendingDone(ArkUI_Int32 requestId)173 ArkUI_Int32 NotifyDragEndPendingDone(ArkUI_Int32 requestId)
174 {
175 return NG::DragDropFuncWrapper::NotifyDragEndPendingDone(requestId);
176 }
177
178 } // namespace
GetDragAdapterAPI()179 const ArkUIDragAdapterAPI* GetDragAdapterAPI()
180 {
181 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
182 static const ArkUIDragAdapterAPI impl {
183 .startDrag = StartDrag,
184 .registerStatusListener = RegisterStatusListener,
185 .unregisterStatusListener = UnRegisterStatusListener,
186 .createDragActionWithNode = CreateDragActionWithNode,
187 .createDragActionWithContext = CreateDragActionWithContext,
188 .setDragPreview = SetDragPreview,
189 .setDragEventStrictReportingEnabledWithNode = SetDragEventStrictReportingEnabledWithNode,
190 .setDragEventStrictReportingEnabledWithContext = SetDragEventStrictReportingEnabledWithContext,
191 .requestDragEndPending = RequestDragEndPending,
192 .notifyDragResult = NotifyDragResult,
193 .notifyDragEndPendingDone = NotifyDragEndPendingDone,
194 .enableDropDisallowedBadge = EnableDropDisallowedBadge,
195 };
196 CHECK_INITIALIZED_FIELDS_END(impl, 0, 0, 0); // don't move this line
197 return &impl;
198 }
199 } // namespace OHOS::Ace::DragAdapter
200