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_ng/event/gesture_event_actuator.h" 25 #include "core/components_ng/gestures/recognizers/sequenced_recognizer.h" 26 #include "core/gestures/drag_event.h" 27 28 namespace OHOS::Ace::NG { 29 30 class GestureEventHub; 31 class PanRecognizer; 32 class LongPressRecognizer; 33 class FrameNode; 34 class OverlayManager; 35 36 class DragEvent : public AceType { DECLARE_ACE_TYPE(DragEvent,AceType)37 DECLARE_ACE_TYPE(DragEvent, AceType) 38 public: 39 DragEvent(GestureEventFunc&& actionStart, GestureEventFunc&& actionUpdate, GestureEventFunc&& actionEnd, 40 GestureEventNoParameter&& actionCancel) 41 : actionStart_(std::move(actionStart)), actionUpdate_(std::move(actionUpdate)), 42 actionEnd_(std::move(actionEnd)), actionCancel_(std::move(actionCancel)) 43 {} 44 ~DragEvent() override = default; 45 GetActionStartEventFunc()46 const GestureEventFunc& GetActionStartEventFunc() const 47 { 48 return actionStart_; 49 } 50 GetActionUpdateEventFunc()51 const GestureEventFunc& GetActionUpdateEventFunc() const 52 { 53 return actionUpdate_; 54 } 55 GetActionEndEventFunc()56 const GestureEventFunc& GetActionEndEventFunc() const 57 { 58 return actionEnd_; 59 } 60 GetActionCancelEventFunc()61 const GestureEventNoParameter& GetActionCancelEventFunc() const 62 { 63 return actionCancel_; 64 } 65 66 private: 67 GestureEventFunc actionStart_; 68 GestureEventFunc actionUpdate_; 69 GestureEventFunc actionEnd_; 70 GestureEventNoParameter actionCancel_; 71 }; 72 73 class ACE_EXPORT DragEventActuator : public GestureEventActuator { 74 DECLARE_ACE_TYPE(DragEventActuator, GestureEventActuator) 75 public: 76 DragEventActuator( 77 const WeakPtr<GestureEventHub>& gestureEventHub, PanDirection direction, int32_t fingers, float distance); 78 ~DragEventActuator() override = default; 79 ReplaceDragEvent(const RefPtr<DragEvent> & dragEvent)80 void ReplaceDragEvent(const RefPtr<DragEvent>& dragEvent) 81 { 82 if (userCallback_) { 83 userCallback_.Reset(); 84 } 85 userCallback_ = dragEvent; 86 } 87 SetCustomDragEvent(const RefPtr<DragEvent> & dragEvent)88 void SetCustomDragEvent(const RefPtr<DragEvent>& dragEvent) 89 { 90 if (customCallback_) { 91 customCallback_.Reset(); 92 } 93 customCallback_ = dragEvent; 94 } 95 ClearDragEvent()96 void ClearDragEvent() 97 { 98 if (userCallback_) { 99 userCallback_.Reset(); 100 } 101 if (customCallback_) { 102 customCallback_.Reset(); 103 } 104 } 105 106 void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict, 107 const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result) override; 108 void SetThumbnailCallback(std::function<void(Offset)>&& callback); 109 void SetFilter(const RefPtr<DragEventActuator>& actuator); 110 static void UpdatePreviewPositionAndScale(const RefPtr<FrameNode>& imageNode, const OffsetF& frameOffset); 111 static void CreatePreviewNode(const RefPtr<FrameNode>& frameNode, OHOS::Ace::RefPtr<FrameNode>& imageNode); 112 static void SetPreviewDefaultAnimateProperty(const RefPtr<FrameNode>& imageNode); 113 static void MountPixelMap(const RefPtr<OverlayManager>& overlayManager, const RefPtr<GestureEventHub>& manager, 114 const RefPtr<FrameNode>& imageNode); 115 void SetPixelMap(const RefPtr<DragEventActuator>& actuator); 116 void SetEventColumn(const RefPtr<DragEventActuator>& actuator); 117 void HideFilter(); 118 void HidePixelMap(bool startDrag = false, double x = 0, double y = 0); 119 void HideEventColumn(); 120 void BindClickEvent(const RefPtr<FrameNode>& columnNode); 121 void ShowPixelMapAnimation(const RefPtr<FrameNode>& imageNode, bool hasContextMenu); 122 void SetTextAnimation(const RefPtr<GestureEventHub>& gestureHub, const Offset& globalLocation); 123 void HideTextAnimation(bool startDrag = false, double globalX = 0, double globalY = 0); 124 bool GetIsBindOverlayValue(const RefPtr<DragEventActuator>& actuator); 125 bool IsAllowedDrag(); 126 void SetTextPixelMap(const RefPtr<GestureEventHub>& gestureHub); 127 OffsetF GetFloatImageOffset(const RefPtr<FrameNode>& frameNode, const RefPtr<PixelMap>& pixelMap); GetDirection()128 PanDirection GetDirection() const 129 { 130 return direction_; 131 } 132 GetFingers()133 int32_t GetFingers() const 134 { 135 return fingers_; 136 } 137 GetDistance()138 float GetDistance() const 139 { 140 return distance_; 141 } 142 143 void StartDragTaskForWeb(const GestureEvent& info); 144 void StartLongPressActionForWeb(); 145 void CancelDragForWeb(); ResetDragActionForWeb()146 void ResetDragActionForWeb() { 147 if (isReceivedLongPress_) { 148 isReceivedLongPress_ = false; 149 } 150 } 151 SetIsNotInPreviewState(bool isNotInPreviewState)152 void SetIsNotInPreviewState(bool isNotInPreviewState) 153 { 154 isNotInPreviewState_ = isNotInPreviewState; 155 } 156 GetIsNotInPreviewState()157 bool GetIsNotInPreviewState() const 158 { 159 return isNotInPreviewState_; 160 } 161 SetIsDragUserReject(bool isDragUserReject)162 void SetIsDragUserReject(bool isDragUserReject) 163 { 164 isDragUserReject_ = isDragUserReject; 165 } 166 IsDragUserReject()167 bool IsDragUserReject() const 168 { 169 return isDragUserReject_; 170 } 171 172 void CopyDragEvent(const RefPtr<DragEventActuator>& dragEventActuator); 173 174 inline static void FlushSyncGeometryNodeTasks(); 175 176 private: 177 WeakPtr<GestureEventHub> gestureEventHub_; 178 RefPtr<DragEvent> userCallback_; 179 RefPtr<DragEvent> customCallback_; 180 RefPtr<PanRecognizer> panRecognizer_; 181 RefPtr<LongPressRecognizer> longPressRecognizer_; 182 RefPtr<LongPressRecognizer> previewLongPressRecognizer_; 183 RefPtr<SequencedRecognizer> SequencedRecognizer_; 184 std::function<void(GestureEvent&)> actionStart_; 185 186 std::function<void(GestureEvent&)> longPressUpdate_; 187 std::function<void()> actionCancel_; 188 std::function<void(Offset)> textDragCallback_; 189 GestureEvent longPressInfo_; 190 bool isReceivedLongPress_ = false; 191 bool isNotInPreviewState_ = false; 192 193 bool isDragUserReject_ = false; 194 195 PanDirection direction_; 196 int32_t fingers_ = 1; 197 float distance_ = 0.0f; 198 }; 199 200 } // namespace OHOS::Ace::NG 201 202 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_DRAG_EVENT_H 203