1 /* 2 * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_DRAG_EVENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_DRAG_EVENT_H 18 19 #include <functional> 20 #include <list> 21 22 #include "base/memory/ace_type.h" 23 #include "base/memory/referenced.h" 24 #include "core/components/common/properties/blur_style_option.h" 25 #include "core/components_ng/event/gesture_event_actuator.h" 26 #include "core/components_ng/gestures/recognizers/sequenced_recognizer.h" 27 #include "core/gestures/drag_event.h" 28 29 namespace OHOS::Ace::NG { 30 31 class GestureEventHub; 32 class PanRecognizer; 33 class LongPressRecognizer; 34 class FrameNode; 35 class OverlayManager; 36 class ScrollablePattern; 37 struct GatherNodeChildInfo; 38 39 class DragEvent : public AceType { 40 DECLARE_ACE_TYPE(DragEvent, AceType); 41 public: DragEvent(GestureEventFunc && actionStart,GestureEventFunc && actionUpdate,GestureEventFunc && actionEnd,GestureEventNoParameter && actionCancel)42 DragEvent(GestureEventFunc&& actionStart, GestureEventFunc&& actionUpdate, GestureEventFunc&& actionEnd, 43 GestureEventNoParameter&& actionCancel) 44 : actionStart_(std::move(actionStart)), actionUpdate_(std::move(actionUpdate)), 45 actionEnd_(std::move(actionEnd)), actionCancel_(std::move(actionCancel)) 46 {} 47 ~DragEvent() override = default; 48 GetActionStartEventFunc()49 const GestureEventFunc& GetActionStartEventFunc() const 50 { 51 return actionStart_; 52 } 53 GetActionUpdateEventFunc()54 const GestureEventFunc& GetActionUpdateEventFunc() const 55 { 56 return actionUpdate_; 57 } 58 GetActionEndEventFunc()59 const GestureEventFunc& GetActionEndEventFunc() const 60 { 61 return actionEnd_; 62 } 63 GetActionCancelEventFunc()64 const GestureEventNoParameter& GetActionCancelEventFunc() const 65 { 66 return actionCancel_; 67 } 68 GetLongPressEventFunc()69 const GestureEventFunc& GetLongPressEventFunc() const 70 { 71 return actionLongPress_; 72 } 73 SetLongPressEventFunc(GestureEventFunc && actionLongPress)74 void SetLongPressEventFunc(GestureEventFunc&& actionLongPress) 75 { 76 actionLongPress_ = std::move(actionLongPress); 77 } 78 GetPressedKeyCodes()79 const std::vector<KeyCode>& GetPressedKeyCodes() const 80 { 81 return pressedKeyCodes_; 82 } 83 SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)84 void SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes) 85 { 86 pressedKeyCodes_ = pressedKeyCodes; 87 } 88 89 private: 90 GestureEventFunc actionStart_; 91 GestureEventFunc actionUpdate_; 92 GestureEventFunc actionEnd_; 93 GestureEventNoParameter actionCancel_; 94 GestureEventFunc actionLongPress_; 95 std::vector<KeyCode> pressedKeyCodes_; 96 }; 97 98 class ACE_EXPORT DragEventActuator : public GestureEventActuator { 99 DECLARE_ACE_TYPE(DragEventActuator, GestureEventActuator); 100 public: 101 DragEventActuator( 102 const WeakPtr<GestureEventHub>& gestureEventHub, PanDirection direction, int32_t fingers, float distance); 103 ~DragEventActuator() override = default; 104 ReplaceDragEvent(const RefPtr<DragEvent> & dragEvent)105 void ReplaceDragEvent(const RefPtr<DragEvent>& dragEvent) 106 { 107 if (userCallback_) { 108 userCallback_.Reset(); 109 } 110 userCallback_ = dragEvent; 111 } 112 SetCustomDragEvent(const RefPtr<DragEvent> & dragEvent)113 void SetCustomDragEvent(const RefPtr<DragEvent>& dragEvent) 114 { 115 if (customCallback_) { 116 customCallback_.Reset(); 117 } 118 customCallback_ = dragEvent; 119 } 120 HasDragEvent()121 bool HasDragEvent() const 122 { 123 return static_cast<bool>(userCallback_); 124 } 125 ClearDragEvent()126 void ClearDragEvent() 127 { 128 if (userCallback_) { 129 userCallback_.Reset(); 130 } 131 if (customCallback_) { 132 customCallback_.Reset(); 133 } 134 } 135 136 void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict, 137 const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result, 138 ResponseLinkResult& responseLinkResult) override; 139 void InitDragDropStatusToIdle(); 140 virtual void SetThumbnailCallback(std::function<void(Offset)>&& callback); 141 void SetFilter(const RefPtr<DragEventActuator>& actuator); 142 static void UpdatePreviewPositionAndScale( 143 const RefPtr<FrameNode>& imageNode, const OffsetF& frameOffset, float scale = -1.0f); 144 static void UpdatePreviewAttr(const RefPtr<FrameNode>& frameNode, const RefPtr<FrameNode>& imageNode); 145 static void UpdateGatherAnimatePosition( 146 std::vector<GatherNodeChildInfo>& gatherNodeChildrenInfo, const OffsetF& GatherNodeOffset); 147 static void SetPreviewDefaultAnimateProperty(const RefPtr<FrameNode>& imageNode); 148 static void ExecutePreDragAction(const PreDragStatus preDragStatus, const RefPtr<FrameNode>& frameNode = nullptr); 149 static void ExecutePreDragFunc(const RefPtr<FrameNode>& node, const PreDragStatus preDragStatus, 150 const PreDragStatus onPreDragStatus); 151 void SetPixelMap(const RefPtr<DragEventActuator>& actuator); 152 void SetEventColumn(const RefPtr<DragEventActuator>& actuator); 153 void HideFilter(); 154 void HidePixelMap(bool startDrag = false, double x = 0, double y = 0, bool showAnimation = true); 155 void HideEventColumn(); 156 void BindClickEvent(const RefPtr<FrameNode>& columnNode); 157 void ShowPixelMapAnimation(const RefPtr<FrameNode>& imageNode, const RefPtr<FrameNode>& frameNode, 158 bool hasContextMenu); 159 void SetTextAnimation(const RefPtr<GestureEventHub>& gestureHub, const Offset& globalLocation); 160 void HideTextAnimation(bool startDrag = false, double globalX = 0, double globalY = 0); 161 bool GetIsBindOverlayValue(const RefPtr<DragEventActuator>& actuator); 162 bool IsAllowedDrag(); 163 void SetDragDampStartPointInfo(const Point& point, int32_t pointerId); 164 void HandleDragDampingMove(const Point& point, int32_t pointerId, bool isRedragStart = false); 165 void SetTextPixelMap(const RefPtr<GestureEventHub>& gestureHub); 166 virtual void RestartDragTask(const GestureEvent& info); 167 static void UpdateDragNodePosition( 168 const RefPtr<FrameNode>& imageNode, const RefPtr<FrameNode>& frameNode, float width, float height); GetDirection()169 PanDirection GetDirection() const 170 { 171 return direction_; 172 } 173 GetFingers()174 int32_t GetFingers() const 175 { 176 return fingers_; 177 } 178 GetDistance()179 float GetDistance() const 180 { 181 return distance_; 182 } 183 184 bool IsNotNeedShowPreviewForWeb(const RefPtr<FrameNode>& frameNode); 185 void StartDragTaskForWeb(const GestureEvent& info); 186 void StartLongPressActionForWeb(); 187 void CancelDragForWeb(); ResetDragActionForWeb()188 void ResetDragActionForWeb() { 189 if (isReceivedLongPress_) { 190 isReceivedLongPress_ = false; 191 } 192 } 193 SetIsNotInPreviewState(bool isNotInPreviewState)194 void SetIsNotInPreviewState(bool isNotInPreviewState) 195 { 196 isNotInPreviewState_ = isNotInPreviewState; 197 } 198 GetIsNotInPreviewState()199 virtual bool GetIsNotInPreviewState() const 200 { 201 return isNotInPreviewState_; 202 } 203 SetIsDragUserReject(bool isDragUserReject)204 void SetIsDragUserReject(bool isDragUserReject) 205 { 206 isDragUserReject_ = isDragUserReject; 207 } 208 IsDragUserReject()209 bool IsDragUserReject() const 210 { 211 return isDragUserReject_; 212 } 213 SetIsDefaultOnDragStartExecuted(bool defaultOnDragStartExecuted)214 void SetIsDefaultOnDragStartExecuted(bool defaultOnDragStartExecuted) 215 { 216 defaultOnDragStartExecuted_ = defaultOnDragStartExecuted; 217 } 218 IsDefaultOnDragStartExecuted()219 bool IsDefaultOnDragStartExecuted() const 220 { 221 return defaultOnDragStartExecuted_; 222 } 223 GetOptionsAfterApplied()224 const OptionsAfterApplied& GetOptionsAfterApplied() 225 { 226 return optionsAfterApplied_; 227 } 228 GetIsNewFwk()229 bool GetIsNewFwk() const 230 { 231 return isNewFwk_; 232 } 233 SetIsNewFwk(bool isNewFwk)234 void SetIsNewFwk(bool isNewFwk) 235 { 236 isNewFwk_ = isNewFwk; 237 } 238 GetLastTouchFingerId()239 int32_t GetLastTouchFingerId() 240 { 241 return lastTouchFingerId_; 242 } 243 SetRestartDrag(bool isRestartDrag)244 void SetRestartDrag(bool isRestartDrag) 245 { 246 isRestartDrag_ = isRestartDrag; 247 } 248 GetRestartDrag()249 bool GetRestartDrag() const 250 { 251 return isRestartDrag_; 252 } 253 SetIsForDragDrop(bool isForDragDrop)254 void SetIsForDragDrop(bool isForDragDrop) 255 { 256 isForDragDrop_ = isForDragDrop; 257 } 258 259 void CopyDragEvent(const RefPtr<DragEventActuator>& dragEventActuator); 260 261 void SetGatherNodeAboveFilter(const RefPtr<DragEventActuator>& actuator); 262 bool IsBelongToMultiItemNode(const RefPtr<FrameNode>& frameNode); 263 static bool IsSelectedItemNode(const RefPtr<UINode>& uiNode); 264 void FindItemParentNode(const RefPtr<FrameNode>& frameNode); 265 virtual bool IsNeedGather() const; 266 static RefPtr<FrameNode> GetOrCreateGatherNode(const RefPtr<NG::OverlayManager>& overlayManager, 267 const RefPtr<DragEventActuator>& actuator, std::vector<GatherNodeChildInfo>& gatherNodeChildrenInfo); 268 static RefPtr<FrameNode> CreateGatherNode(const RefPtr<DragEventActuator>& actuator); 269 static RefPtr<FrameNode> CreateImageNode( 270 const RefPtr<FrameNode>& frameNode, GatherNodeChildInfo& gatherNodeChildInfo); 271 static void MarkDirtyNode(const RefPtr<FrameNode>& gatherNode); 272 static void InitGatherNodesPosition(const std::vector<GatherNodeChildInfo>& gatherNodeChildrenInfo); 273 static void MountGatherNode(const RefPtr<OverlayManager>& overlayManager, const RefPtr<FrameNode>& frameNode, 274 const RefPtr<FrameNode>& gatherNode, const std::vector<GatherNodeChildInfo>& gatherNodeChildrenInfo); 275 static void GetFrameNodePreviewPixelMap(const RefPtr<FrameNode>& frameNode); 276 void SetGatherNode(const RefPtr<FrameNode>& gatherNode); 277 RefPtr<FrameNode> GetGatherNode() const; 278 const std::vector<GatherNodeChildInfo>& GetGatherNodeChildrenInfo() const; 279 void ClearGatherNodeChildrenInfo(); 280 void PushBackGatherNodeChild(GatherNodeChildInfo& gatherNodeChild); 281 void AddTouchListener(const TouchRestrict& touchRestrict) override; 282 virtual void HandleTouchEvent(const TouchEventInfo& info, bool isRestartDrag = false); 283 void HandleTouchUpEvent(); 284 void HandleTouchMoveEvent(); 285 void HandleTouchCancelEvent(); 286 const RefPtr<FrameNode> GetItemParentNode() const; 287 RefPtr<FrameNode> GetFrameNode(); 288 static void PrepareShadowParametersForDragData(const RefPtr<FrameNode>& frameNode, 289 std::unique_ptr<JsonValue>& arkExtraInfoJson, float scale); 290 static void PrepareRadiusParametersForDragData(const RefPtr<FrameNode>& frameNode, 291 std::unique_ptr<JsonValue>& arkExtraInfoJson); 292 static void ParseShadowInfo(Shadow& shadow, std::unique_ptr<JsonValue>& arkExtraInfoJson); 293 static std::optional<Shadow> GetDefaultShadow(); 294 static std::optional<BorderRadiusProperty> GetDefaultBorderRadius(); 295 296 inline static void FlushSyncGeometryNodeTasks(); 297 298 void SetResponseRegionFull(); 299 void ResetResponseRegion(); 300 static void ResetDragStatus(); 301 void PrepareFinalPixelMapForDragThroughTouch(RefPtr<PixelMap> pixelMap, bool immediately); 302 void DoPixelMapScaleForDragThroughTouch(RefPtr<PixelMap> pixelMap, float targetScale); 303 virtual RefPtr<PixelMap> GetPreScaledPixelMapForDragThroughTouch(float& preScale); 304 virtual void ResetPreScaledPixelMapForDragThroughTouch(); NotifyDragStart()305 virtual void NotifyDragStart() {}; NotifyDragEnd()306 virtual void NotifyDragEnd() {}; NotifyPreDragStatus(const PreDragStatus preDragStatus)307 virtual void NotifyPreDragStatus(const PreDragStatus preDragStatus) {}; 308 SetIsThumbnailCallbackTriggered(bool isThumbnailCallbackTriggered)309 void SetIsThumbnailCallbackTriggered(bool isThumbnailCallbackTriggered) 310 { 311 isThumbnailCallbackTriggered_ = isThumbnailCallbackTriggered; 312 } 313 314 void TryTriggerThumbnailCallback(); 315 316 void GetThumbnailPixelMap(bool isSync); 317 RecordTouchDownPoint(const TouchEvent & downTouchEvent)318 void RecordTouchDownPoint(const TouchEvent& downTouchEvent) 319 { 320 touchDownPoint_ = downTouchEvent; 321 } 322 GetTouchDownPoint()323 const TouchEvent& GetTouchDownPoint() 324 { 325 return touchDownPoint_; 326 } 327 NotifyTransDragWindowToFwk()328 virtual void NotifyTransDragWindowToFwk() {} 329 NotifyMenuShow(bool isMenuShow)330 virtual void NotifyMenuShow(bool isMenuShow) {} 331 332 void CallTimerCallback(const RefPtr<FrameNode>& frameNode); 333 void SetExecTimerCallback(bool isExecCallback); 334 void RemovePixelMap(); 335 protected: 336 DragEventActuator(const WeakPtr<GestureEventHub>& gestureEventHub); 337 338 private: 339 void UpdatePreviewOptionFromModifier(const RefPtr<FrameNode>& frameNode); 340 void UpdatePreviewOptionDefaultAttr(const RefPtr<FrameNode>& frameNode); 341 // check the current node's status to decide if it can initiate one drag operation 342 bool IsCurrentNodeStatusSuitableForDragging( 343 const RefPtr<FrameNode>& frameNode, const TouchRestrict& touchRestrict); 344 void HandleTextDragCallback(Offset offset); 345 void HandleOnPanActionCancel(); 346 protected: 347 RefPtr<PanRecognizer> panRecognizer_; 348 RefPtr<LongPressRecognizer> longPressRecognizer_; 349 RefPtr<LongPressRecognizer> previewLongPressRecognizer_; 350 RefPtr<SequencedRecognizer> SequencedRecognizer_; 351 352 private: 353 WeakPtr<GestureEventHub> gestureEventHub_; 354 WeakPtr<FrameNode> itemParentNode_; 355 RefPtr<DragEvent> userCallback_; 356 RefPtr<DragEvent> customCallback_; 357 RefPtr<FrameNode> gatherNode_; 358 RefPtr<TouchEventImpl> touchListener_; 359 360 RefPtr<PixelMap> textPixelMap_; 361 RefPtr<PixelMap> preScaledPixelMap_; 362 std::function<void(GestureEvent&)> actionStart_; 363 std::function<void(GestureEvent&)> longPressUpdate_; 364 std::function<void(GestureEvent&)> actionCancel_; 365 std::function<void(Offset)> textDragCallback_; 366 GestureEvent longPressInfo_; 367 TouchEvent touchDownPoint_; 368 bool isReceivedLongPress_ = false; 369 bool isNotInPreviewState_ = false; 370 std::vector<GatherNodeChildInfo> gatherNodeChildrenInfo_; 371 std::vector<DimensionRect> responseRegion_; 372 bool isSelectedItemNode_ = false; 373 bool isOnBeforeLiftingAnimation_ = false; 374 bool isDragPrepareFinish_ = false; 375 bool isThumbnailCallbackTriggered_ = false; 376 377 bool isDragUserReject_ = false; 378 bool defaultOnDragStartExecuted_ = false; 379 bool isResponseRegionFull_ = false; 380 OptionsAfterApplied optionsAfterApplied_; 381 382 PanDirection direction_; 383 int32_t fingers_ = 1; 384 float distance_ = 0.0f; 385 float preScaleValue_ = 1.0f; 386 bool isRedragStart_ = false; 387 int32_t lastTouchFingerId_ = 0; 388 bool isNewFwk_ = false; 389 bool isRestartDrag_ = false; 390 bool isForDragDrop_ = false; 391 bool isExecCallback_ = false; 392 }; 393 394 } // namespace OHOS::Ace::NG 395 396 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_DRAG_EVENT_H 397