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 RefPtr<UnifiedData> udData = UdmfClient::GetInstance()->TransformUnifiedDataForNative(dragAction->unifiedData);
47 internalDragAction->unifiedData = udData;
48 internalDragAction->instanceId = dragAction->instanceId;
49 internalDragAction->touchPointX = dragAction->touchPointX;
50 internalDragAction->touchPointY = dragAction->touchPointY;
51 internalDragAction->hasTouchPoint = dragAction->hasTouchPoint;
52 }
53
StartDrag(ArkUIDragAction * dragAction)54 ArkUI_Int32 StartDrag(ArkUIDragAction* dragAction)
55 {
56 CHECK_NULL_RETURN(dragAction, -1);
57 auto internalDragAction = std::make_shared<OHOS::Ace::NG::ArkUIInteralDragAction>();
58
59 CHECK_NULL_RETURN(internalDragAction, -1);
60 auto callbacks = [listener = dragAction->listener, userData = dragAction->userData,
61 instanceId = dragAction->instanceId](const DragNotifyMsg& dragNotifyMsg, int32_t status) {
62 auto pipelineContext = NG::PipelineContext::GetContextByContainerId(instanceId);
63 CHECK_NULL_VOID(pipelineContext);
64 auto manager = pipelineContext->GetDragDropManager();
65 CHECK_NULL_VOID(manager);
66 ArkUIDragEvent dragEvent;
67 dragEvent.dragResult = static_cast<int32_t>(dragNotifyMsg.result);
68 dragEvent.dragBehavior = static_cast<int32_t>(dragNotifyMsg.dragBehavior);
69
70 auto action = manager->GetDragAction();
71 if (action != nullptr) {
72 action->hasHandle = false;
73 }
74 ArkUIDragAndDropInfo outInfo;
75 outInfo.status = status;
76 outInfo.dragEvent = &dragEvent;
77 CHECK_NULL_VOID(listener);
78 listener(&outInfo, userData);
79 };
80 auto* pixelMapTemp = reinterpret_cast<std::shared_ptr<void*>*>(dragAction->pixelmapArray);
81 for (int index = 0; index < dragAction->size; index++) {
82 auto pixelMap = PixelMap::CreatePixelMap(&pixelMapTemp[index]);
83 internalDragAction->pixelMapList.push_back(pixelMap);
84 }
85 internalDragAction->callback = callbacks;
86 DragActionConvert(dragAction, internalDragAction);
87 OHOS::Ace::NG::DragDropFuncWrapper::StartDragAction(internalDragAction);
88 return 0;
89 }
90
RegisterStatusListener(ArkUIDragAction * dragAction,void * userData,DragStatusCallback listener)91 void RegisterStatusListener(ArkUIDragAction* dragAction, void* userData, DragStatusCallback listener)
92 {
93 CHECK_NULL_VOID(dragAction);
94 dragAction->listener = listener;
95 dragAction->userData = userData;
96 }
97
UnRegisterStatusListener(ArkUIDragAction * dragAction)98 void UnRegisterStatusListener(ArkUIDragAction* dragAction)
99 {
100 CHECK_NULL_VOID(dragAction);
101 dragAction->listener = nullptr;
102 dragAction->userData = nullptr;
103 }
104
CreateDragActionWithNode(ArkUINodeHandle node)105 ArkUIDragAction* CreateDragActionWithNode(ArkUINodeHandle node)
106 {
107 CHECK_NULL_RETURN(node, nullptr);
108 auto* frameNode = reinterpret_cast<NG::FrameNode*>(node);
109 CHECK_NULL_RETURN(frameNode, nullptr);
110 auto pipeline = frameNode->GetContext();
111 CHECK_NULL_RETURN(pipeline, nullptr);
112 auto* dragAction = new ArkUIDragAction();
113 CHECK_NULL_RETURN(dragAction, nullptr);
114 dragAction->instanceId = pipeline->GetInstanceId();
115 return dragAction;
116 }
117
CreateDragActionWithContext(ArkUIContext * context)118 ArkUIDragAction* CreateDragActionWithContext(ArkUIContext* context)
119 {
120 CHECK_NULL_RETURN(context, nullptr);
121 auto* dragAction = new ArkUIDragAction();
122 CHECK_NULL_RETURN(dragAction, nullptr);
123 dragAction->instanceId = context->id;
124 return dragAction;
125 }
126
SetDragPreview(ArkUINodeHandle node,void * dragPreview)127 void SetDragPreview(ArkUINodeHandle node, void* dragPreview)
128 {
129 auto* frameNode = reinterpret_cast<NG::FrameNode*>(node);
130 CHECK_NULL_VOID(frameNode);
131 NG::DragDropInfo dragPreviewInfo;
132 dragPreviewInfo.pixelMap = PixelMap::CreatePixelMap(dragPreview);
133 NG::ViewAbstract::SetDragPreview(frameNode, dragPreviewInfo);
134 }
135
SetDragEventStrictReportingEnabledWithNode(bool enabled)136 void SetDragEventStrictReportingEnabledWithNode(bool enabled)
137 {
138 NG::ViewAbstract::SetDragEventStrictReportingEnabled(enabled);
139 }
140
SetDragEventStrictReportingEnabledWithContext(ArkUI_Int32 instanceId,bool enabled)141 void SetDragEventStrictReportingEnabledWithContext(ArkUI_Int32 instanceId, bool enabled)
142 {
143 NG::ViewAbstract::SetDragEventStrictReportingEnabled(instanceId, enabled);
144 }
145
RequestDragEndPending()146 ArkUI_Int32 RequestDragEndPending()
147 {
148 return NG::DragDropFuncWrapper::RequestDragEndPending();
149 }
150
NotifyDragResult(ArkUI_Int32 requestId,ArkUI_Int32 result)151 ArkUI_Int32 NotifyDragResult(ArkUI_Int32 requestId, ArkUI_Int32 result)
152 {
153 return NG::DragDropFuncWrapper::NotifyDragResult(requestId, result);
154 }
155
NotifyDragEndPendingDone(ArkUI_Int32 requestId)156 ArkUI_Int32 NotifyDragEndPendingDone(ArkUI_Int32 requestId)
157 {
158 return NG::DragDropFuncWrapper::NotifyDragEndPendingDone(requestId);
159 }
160
161 } // namespace
GetDragAdapterAPI()162 const ArkUIDragAdapterAPI* GetDragAdapterAPI()
163 {
164 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
165 static const ArkUIDragAdapterAPI impl {
166 .startDrag = StartDrag,
167 .registerStatusListener = RegisterStatusListener,
168 .unregisterStatusListener = UnRegisterStatusListener,
169 .createDragActionWithNode = CreateDragActionWithNode,
170 .createDragActionWithContext = CreateDragActionWithContext,
171 .setDragPreview = SetDragPreview,
172 .setDragEventStrictReportingEnabledWithNode = SetDragEventStrictReportingEnabledWithNode,
173 .setDragEventStrictReportingEnabledWithContext = SetDragEventStrictReportingEnabledWithContext,
174 .requestDragEndPending = RequestDragEndPending,
175 .notifyDragResult = NotifyDragResult,
176 .notifyDragEndPendingDone = NotifyDragEndPendingDone
177 };
178 CHECK_INITIALIZED_FIELDS_END(impl, 0, 0, 0); // don't move this line
179 return &impl;
180 }
181 } // namespace OHOS::Ace::DragAdapter
182