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_BOX_DRAG_DROP_EVENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_DRAG_DROP_EVENT_H 18 19 #include "core/gestures/drag_event.h" 20 #include "core/pipeline/base/component.h" 21 #include "frameworks/base/memory/ace_type.h" 22 #include "frameworks/base/window/drag_window.h" 23 #include "frameworks/core/gestures/gesture_recognizer.h" 24 #include "frameworks/core/common/clipboard/clipboard_proxy.h" 25 26 namespace OHOS::Ace { 27 28 class PipelineContext; 29 class Component; 30 31 /** 32 * @brief Used for drag event info. 33 */ 34 struct DragItemInfo { 35 RefPtr<Component> customComponent; 36 RefPtr<PixelMap> pixelMap; 37 std::string extraInfo; 38 }; 39 40 using OnDragFunc = std::function<DragItemInfo(const RefPtr<DragEvent>&, const std::string&)>; 41 using OnDropFunc = std::function<void(const RefPtr<DragEvent>&, const std::string&)>; 42 using UpdateBuilderFunc = std::function<void(const Dimension&, const Dimension&)>; 43 44 class DragDropEvent : public virtual AceType { 45 DECLARE_ACE_TYPE(DragDropEvent, AceType); 46 47 public: 48 DragDropEvent() = default; 49 ~DragDropEvent() override = default; 50 51 // render GetOnDragStart()52 const OnDragFunc& GetOnDragStart() const 53 { 54 return onDragStart_; 55 } 56 GetOnDragEnter()57 const OnDropFunc& GetOnDragEnter() const 58 { 59 return onDragEnter_; 60 } 61 GetOnDragMove()62 const OnDropFunc& GetOnDragMove() const 63 { 64 return onDragMove_; 65 } 66 GetOnDragLeave()67 const OnDropFunc& GetOnDragLeave() const 68 { 69 return onDragLeave_; 70 } 71 GetOnDrop()72 const OnDropFunc& GetOnDrop() const 73 { 74 return onDrop_; 75 } 76 SetPreDragDropNode(const RefPtr<DragDropEvent> & preDragDropNode)77 void SetPreDragDropNode(const RefPtr<DragDropEvent>& preDragDropNode) 78 { 79 preDragDropNode_ = preDragDropNode; 80 } 81 GetPreDragDropNode()82 const RefPtr<DragDropEvent>& GetPreDragDropNode() const 83 { 84 return preDragDropNode_; 85 } 86 SetLocalPoint(const Point & localPoint)87 void SetLocalPoint(const Point& localPoint) 88 { 89 localPoint_ = localPoint; 90 } 91 GetLocalPoint()92 const Point& GetLocalPoint() const 93 { 94 return localPoint_; 95 } 96 GetUpdateBuilderFuncId()97 const UpdateBuilderFunc& GetUpdateBuilderFuncId() const 98 { 99 return updateBuilder_; 100 } 101 SetUpdateBuilderFuncId(const UpdateBuilderFunc & updateBuilder)102 void SetUpdateBuilderFuncId(const UpdateBuilderFunc& updateBuilder) 103 { 104 updateBuilder_ = updateBuilder; 105 } 106 107 void CreateDragDropRecognizer(const WeakPtr<PipelineContext>& pipelineContext); 108 void LongPressOnAction(const GestureEvent& info); 109 virtual void PanOnActionStart(const GestureEvent& info) = 0; 110 virtual void PanOnActionUpdate(const GestureEvent& info) = 0; 111 virtual void PanOnActionEnd(const GestureEvent& info) = 0; 112 virtual void PanOnActionCancel() = 0; 113 virtual DragItemInfo GenerateDragItemInfo(const RefPtr<PipelineContext>& context, const GestureEvent& info) = 0; 114 RefPtr<DragDropEvent> FindDragDropNode(const RefPtr<PipelineContext>& context, const GestureEvent& info); 115 virtual void AddDataToClipboard(const RefPtr<PipelineContext>& context, const std::string& extraInfo, 116 const std::string& selectedText, const std::string& imageSrc); 117 void MergeClipboardData(const RefPtr<PipelineContext>& context, const std::string& newData); 118 void RestoreCilpboardData(const RefPtr<PipelineContext>& context); 119 Point UpdatePoint(const RefPtr<PipelineContext>& context, const Point& prePoint); 120 121 protected: 122 RefPtr<GestureRecognizer> dragDropGesture_; 123 OnDragFunc onDragStart_; 124 OnDropFunc onDragEnter_; 125 OnDropFunc onDragMove_; 126 OnDropFunc onDragLeave_; 127 OnDropFunc onDrop_; 128 RefPtr<DragWindow> dragWindow_; 129 bool isDragDropNode_ = false; 130 bool hasDragItem_ = false; 131 RefPtr<DragDropEvent> preDragDropNode_; 132 RefPtr<DragDropEvent> initialDragDropNode_; 133 Size selectedItemSize_; 134 Point localPoint_; 135 Point startPoint_; 136 UpdateBuilderFunc updateBuilder_; 137 std::function<void(const std::string&)> clipboardCallback_ = nullptr; 138 std::function<void(const std::string&)> deleteDataCallback_ = nullptr; 139 RefPtr<Clipboard> clipboard_; 140 }; 141 142 } // namespace OHOS::Ace 143 144 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_DRAG_DROP_EVENT_H 145