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
16 #include "core/components_ng/manager/drag_drop/drag_drop_global_controller.h"
17
18 #include <csignal>
19 #include <thread>
20
21 #include "core/components_ng/base/frame_node.h"
22
23 namespace OHOS::Ace::NG {
24
~DragDropGlobalController()25 DragDropGlobalController::~DragDropGlobalController() {}
26
GetInstance()27 DragDropGlobalController& DragDropGlobalController::GetInstance()
28 {
29 static DragDropGlobalController singleInstance;
30 return singleInstance;
31 }
32
UpdateMenuShowingStatus(bool isShowing)33 void DragDropGlobalController::UpdateMenuShowingStatus(bool isShowing)
34 {
35 std::unique_lock<std::shared_mutex> lock(mutex_);
36 isContextMenuShowing_ = isShowing;
37 }
38
PublishMenuStatusWithNode(bool isShowing,const RefPtr<FrameNode> & targetNode)39 void DragDropGlobalController::PublishMenuStatusWithNode(bool isShowing, const RefPtr<FrameNode>& targetNode)
40 {
41 UpdateMenuShowingStatus(isShowing);
42 if (isShowing) {
43 menuLiftingTargetNode_ = targetNode;
44 }
45 auto frameNode = menuLiftingTargetNode_.Upgrade();
46 CHECK_NULL_VOID(frameNode);
47 auto eventHub = frameNode->GetEventHub<EventHub>();
48 CHECK_NULL_VOID(eventHub);
49 auto gestureHub = eventHub->GetGestureEventHub();
50 CHECK_NULL_VOID(gestureHub);
51 auto dragEventActuator = gestureHub->GetDragEventActuator();
52 CHECK_NULL_VOID(dragEventActuator);
53 dragEventActuator->NotifyMenuShow(isShowing);
54 }
55
IsMenuShowing() const56 bool DragDropGlobalController::IsMenuShowing() const
57 {
58 std::shared_lock<std::shared_mutex> lock(mutex_);
59 return isContextMenuShowing_;
60 }
61
SetDragStartRequestStatus(DragStartRequestStatus dragStartRequestStatus)62 void DragDropGlobalController::SetDragStartRequestStatus(DragStartRequestStatus dragStartRequestStatus)
63 {
64 std::unique_lock<std::shared_mutex> lock(mutex_);
65 dragStartRequestStatus_ = dragStartRequestStatus;
66 }
67
GetDragStartRequestStatus()68 DragStartRequestStatus DragDropGlobalController::GetDragStartRequestStatus()
69 {
70 std::shared_lock<std::shared_mutex> lock(mutex_);
71 return dragStartRequestStatus_;
72 }
73
SetAsyncDragCallback(std::function<void ()> asyncDragCallbac)74 void DragDropGlobalController::SetAsyncDragCallback(std::function<void()> asyncDragCallbac)
75 {
76 std::unique_lock<std::shared_mutex> lock(mutex_);
77 asyncDragCallback_ = asyncDragCallbac;
78 }
79
SetCallAnsyncDragEnd(std::function<void (DragStartRequestStatus)> callSyncDragEnd)80 void DragDropGlobalController::SetCallAnsyncDragEnd(std::function<void(DragStartRequestStatus)> callSyncDragEnd)
81 {
82 std::unique_lock<std::shared_mutex> lock(mutex_);
83 callSyncDragEnd_ = callSyncDragEnd;
84 }
85
GetCallAnsyncEnd()86 std::function<void(DragStartRequestStatus)> DragDropGlobalController::GetCallAnsyncEnd()
87 {
88 std::shared_lock<std::shared_mutex> lock(mutex_);
89 return callSyncDragEnd_;
90 }
91
GetAsyncDragCallback()92 std::function<void()> DragDropGlobalController::GetAsyncDragCallback()
93 {
94 std::shared_lock<std::shared_mutex> lock(mutex_);
95 return asyncDragCallback_;
96 }
97
UpdateDragDropInitiatingStatus(const RefPtr<FrameNode> & frameNode,const DragDropInitiatingStatus & dragStatus)98 void DragDropGlobalController::UpdateDragDropInitiatingStatus(const RefPtr<FrameNode>& frameNode,
99 const DragDropInitiatingStatus& dragStatus)
100 {
101 std::unique_lock<std::shared_mutex> lock(mutex_);
102 CHECK_NULL_VOID(frameNode);
103 if (dragStatus == DragDropInitiatingStatus::MOVING) {
104 currentDragNode_ = frameNode;
105 }
106 }
107
IsInMoving() const108 bool DragDropGlobalController::IsInMoving() const
109 {
110 std::shared_lock<std::shared_mutex> lock(mutex_);
111 return currentDragNode_;
112 }
113
ResetDragDropInitiatingStatus()114 void DragDropGlobalController::ResetDragDropInitiatingStatus()
115 {
116 std::unique_lock<std::shared_mutex> lock(mutex_);
117 currentDragNode_ = nullptr;
118 }
119
SetPrepareDragFrameNode(const WeakPtr<FrameNode> & prepareDragFrameNode)120 void DragDropGlobalController::SetPrepareDragFrameNode(const WeakPtr<FrameNode>& prepareDragFrameNode)
121 {
122 std::unique_lock<std::shared_mutex> lock(mutex_);
123 prepareDragFrameNode_ = prepareDragFrameNode;
124 }
125
GetPrepareDragFrameNode() const126 const WeakPtr<FrameNode> DragDropGlobalController::GetPrepareDragFrameNode() const
127 {
128 std::shared_lock<std::shared_mutex> lock(mutex_);
129 return prepareDragFrameNode_;
130 }
131
SetPreDragStatus(PreDragStatus preDragStatus)132 void DragDropGlobalController::SetPreDragStatus(PreDragStatus preDragStatus)
133 {
134 std::unique_lock<std::shared_mutex> lock(mutex_);
135 preDragStatus_ = preDragStatus;
136 }
137
GetPreDragStatus() const138 PreDragStatus DragDropGlobalController::GetPreDragStatus() const
139 {
140 std::shared_lock<std::shared_mutex> lock(mutex_);
141 return preDragStatus_;
142 }
143
UpdateDragFilterShowingStatus(bool isShowing)144 void DragDropGlobalController::UpdateDragFilterShowingStatus(bool isShowing)
145 {
146 std::unique_lock<std::shared_mutex> lock(mutex_);
147 isDragFilterShowing_ = isShowing;
148 }
149
IsDragFilterShowing() const150 bool DragDropGlobalController::IsDragFilterShowing() const
151 {
152 std::shared_lock<std::shared_mutex> lock(mutex_);
153 return isDragFilterShowing_;
154 }
155
IsOnOnDropPhase()156 bool DragDropGlobalController::IsOnOnDropPhase()
157 {
158 std::shared_lock<std::shared_mutex> lock(mutex_);
159 return isOnOnDropPhase_;
160 }
161
SetIsOnOnDropPhase(bool isOnOnDropPhase)162 void DragDropGlobalController::SetIsOnOnDropPhase(bool isOnOnDropPhase)
163 {
164 std::unique_lock<std::shared_mutex> lock(mutex_);
165 isOnOnDropPhase_ = isOnOnDropPhase;
166 }
167
SetEnableDropDisallowedBadge(bool enableDropDisallowedBadge)168 void DragDropGlobalController::SetEnableDropDisallowedBadge(bool enableDropDisallowedBadge)
169 {
170 std::unique_lock<std::shared_mutex> lock(mutex_);
171 enableDropDisallowedBadge_ = enableDropDisallowedBadge;
172 }
173
GetEnableDropDisallowedBadge() const174 bool DragDropGlobalController::GetEnableDropDisallowedBadge() const
175 {
176 std::shared_lock<std::shared_mutex> lock(mutex_);
177 return enableDropDisallowedBadge_;
178 }
179
RequestDragEndCallback(int32_t requestId,DragRet dragResult,std::function<void (const DragRet &)> stopDragCallback)180 bool DragDropGlobalController::RequestDragEndCallback(int32_t requestId,
181 DragRet dragResult, std::function<void(const DragRet&)> stopDragCallback)
182 {
183 std::unique_lock<std::shared_mutex> lock(mutex_);
184 if (requestId == -1 || stopDragCallback == nullptr || !isOnOnDropPhase_) {
185 return false;
186 }
187 requestId_ = requestId;
188 stopDragCallback_ = stopDragCallback;
189 dragResult_ = dragResult;
190 return true;
191 }
192
NotifyDragResult(int32_t requestId,int32_t result)193 int32_t DragDropGlobalController::NotifyDragResult(int32_t requestId, int32_t result)
194 {
195 std::unique_lock<std::shared_mutex> lock(mutex_);
196 if (requestId_ != requestId) {
197 return -1;
198 }
199 dragResult_ = static_cast<DragRet>(result);
200 return 0;
201 }
202
NotifyDragEndPendingDone(int32_t requestId)203 int32_t DragDropGlobalController::NotifyDragEndPendingDone(int32_t requestId)
204 {
205 {
206 std::unique_lock<std::shared_mutex> lock(mutex_);
207 if (requestId_ != requestId || !isOnOnDropPhase_) {
208 return -1;
209 }
210 requestId_ = -1;
211 isOnOnDropPhase_ = false;
212 }
213 if (stopDragCallback_) {
214 stopDragCallback_(dragResult_);
215 }
216 stopDragCallback_ = nullptr;
217 dragResult_ = DragRet::DRAG_FAIL;
218 return 0;
219 }
220
SetIsAppGlobalDragEnabled(bool isAppGlobalDragEnabled)221 void DragDropGlobalController::SetIsAppGlobalDragEnabled(bool isAppGlobalDragEnabled)
222 {
223 std::unique_lock<std::shared_mutex> lock(mutex_);
224 isAppGlobalDragEnabled_ = isAppGlobalDragEnabled;
225 isAlreadyGetAppGlobalDrag_ = true;
226 }
227
IsAppGlobalDragEnabled() const228 bool DragDropGlobalController::IsAppGlobalDragEnabled() const
229 {
230 std::shared_lock<std::shared_mutex> lock(mutex_);
231 return isAppGlobalDragEnabled_;
232 }
233
IsAlreadyGetAppGlobalDrag() const234 bool DragDropGlobalController::IsAlreadyGetAppGlobalDrag() const
235 {
236 std::shared_lock<std::shared_mutex> lock(mutex_);
237 return isAlreadyGetAppGlobalDrag_;
238 }
239
IsCurrentDrag(int32_t requestId) const240 bool DragDropGlobalController::IsCurrentDrag(int32_t requestId) const
241 {
242 return requestId_ == requestId;
243 }
244
245 } // namespace OHOS::Ace
246