1 /* 2 * Copyright (c) 2022-2023 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_PATTERN_GRID_GRID_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_EVENT_HUB_H 18 19 #include <functional> 20 21 #include "base/memory/ace_type.h" 22 #include "core/components_ng/base/ui_node.h" 23 #include "core/components_ng/event/event_hub.h" 24 #include "core/components_v2/grid/grid_event.h" 25 26 namespace OHOS::Ace::NG { 27 28 using ScrollToIndexFunc = std::function<void(const BaseEventInfo*)>; 29 using ScrollBarUpdateFunc = std::function<std::pair<std::optional<float>, std::optional<float>>(int32_t, Dimension)>; 30 using ItemDragStartFunc = std::function<RefPtr<UINode>(const ItemDragInfo&, int32_t)>; 31 using ItemDragEnterFunc = std::function<void(const ItemDragInfo&)>; 32 using ItemDragMoveFunc = std::function<void(const ItemDragInfo&, int32_t, int32_t)>; 33 using ItemDragLeaveFunc = std::function<void(const ItemDragInfo&, int32_t)>; 34 using ItemDropFunc = std::function<void(const ItemDragInfo&, int32_t, int32_t, bool)>; 35 using ScrollIndexFunc = std::function<void(int32_t, int32_t)>; 36 37 class GridEventHub : public EventHub { 38 DECLARE_ACE_TYPE(GridEventHub, EventHub) 39 40 public: 41 GridEventHub() = default; 42 ~GridEventHub() override = default; 43 SetOnScrollToIndex(ScrollToIndexFunc && onScrollToIndex)44 void SetOnScrollToIndex(ScrollToIndexFunc&& onScrollToIndex) 45 { 46 onScrollToIndex_ = std::move(onScrollToIndex); 47 } 48 SetOnScrollBarUpdate(ScrollBarUpdateFunc && onScrollBarUpdate)49 void SetOnScrollBarUpdate(ScrollBarUpdateFunc&& onScrollBarUpdate) 50 { 51 onScrollBarUpdate_ = std::move(onScrollBarUpdate); 52 } 53 SetOnItemDragStart(ItemDragStartFunc && onItemDragStart)54 void SetOnItemDragStart(ItemDragStartFunc&& onItemDragStart) 55 { 56 onItemDragStart_ = std::move(onItemDragStart); 57 } 58 SetOnItemDragEnter(ItemDragEnterFunc && onItemDragEnter)59 void SetOnItemDragEnter(ItemDragEnterFunc&& onItemDragEnter) 60 { 61 onItemDragEnter_ = std::move(onItemDragEnter); 62 } 63 SetOnItemDragMove(ItemDragMoveFunc && onItemDragMove)64 void SetOnItemDragMove(ItemDragMoveFunc&& onItemDragMove) 65 { 66 onItemDragMove_ = std::move(onItemDragMove); 67 } 68 SetOnItemDragLeave(ItemDragLeaveFunc && onItemDragLeave)69 void SetOnItemDragLeave(ItemDragLeaveFunc&& onItemDragLeave) 70 { 71 onItemDragLeave_ = std::move(onItemDragLeave); 72 } 73 SetOnItemDrop(ItemDropFunc && onItemDrop)74 void SetOnItemDrop(ItemDropFunc&& onItemDrop) 75 { 76 onItemDrop_ = std::move(onItemDrop); 77 } 78 FireOnScrollToIndex(int32_t param)79 void FireOnScrollToIndex(int32_t param) const 80 { 81 if (onScrollToIndex_) { 82 V2::GridEventInfo info(param); 83 onScrollToIndex_(&info); 84 } 85 } 86 SetOnScroll(OnScrollEvent && onScroll)87 void SetOnScroll(OnScrollEvent&& onScroll) 88 { 89 onScrollEvent_ = std::move(onScroll); 90 } 91 GetOnScroll()92 const OnScrollEvent& GetOnScroll() const 93 { 94 return onScrollEvent_; 95 } 96 SetOnScrollFrameBegin(OnScrollFrameBeginEvent && onScrollFrameBegin)97 void SetOnScrollFrameBegin(OnScrollFrameBeginEvent&& onScrollFrameBegin) 98 { 99 onScrollFrameBeginEvent_ = std::move(onScrollFrameBegin); 100 } 101 GetOnScrollFrameBegin()102 const OnScrollFrameBeginEvent& GetOnScrollFrameBegin() const 103 { 104 return onScrollFrameBeginEvent_; 105 } 106 SetOnScrollStart(OnScrollStartEvent && onScrollStart)107 void SetOnScrollStart(OnScrollStartEvent&& onScrollStart) 108 { 109 onScrollStartEvent_ = std::move(onScrollStart); 110 } 111 GetOnScrollStart()112 const OnScrollStartEvent& GetOnScrollStart() const 113 { 114 return onScrollStartEvent_; 115 } 116 SetOnScrollStop(OnScrollStopEvent && onScrollStop)117 void SetOnScrollStop(OnScrollStopEvent&& onScrollStop) 118 { 119 onScrollStopEvent_ = std::move(onScrollStop); 120 } 121 GetOnScrollStop()122 const OnScrollStopEvent& GetOnScrollStop() const 123 { 124 return onScrollStopEvent_; 125 } 126 SetOnScrollIndex(ScrollIndexFunc && onScrollIndex)127 void SetOnScrollIndex(ScrollIndexFunc&& onScrollIndex) 128 { 129 onScrollIndexEvent_ = std::move(onScrollIndex); 130 } 131 GetOnScrollIndex()132 const ScrollIndexFunc& GetOnScrollIndex() const 133 { 134 return onScrollIndexEvent_; 135 } 136 SetOnReachStart(OnReachEvent && onReachStart)137 void SetOnReachStart(OnReachEvent&& onReachStart) 138 { 139 onReachStartEvent_ = std::move(onReachStart); 140 } 141 GetOnReachStart()142 const OnReachEvent& GetOnReachStart() const 143 { 144 return onReachStartEvent_; 145 } 146 SetOnReachEnd(OnReachEvent && onReachEnd)147 void SetOnReachEnd(OnReachEvent&& onReachEnd) 148 { 149 onReachEndEvent_ = std::move(onReachEnd); 150 } 151 GetOnReachEnd()152 const OnReachEvent& GetOnReachEnd() const 153 { 154 return onReachEndEvent_; 155 } 156 FireOnScrollBarUpdate(int32_t index,const Dimension & offset)157 std::pair<std::optional<float>, std::optional<float>> FireOnScrollBarUpdate(int32_t index, const Dimension& offset) 158 { 159 if (onScrollBarUpdate_) { 160 return onScrollBarUpdate_(index, offset); 161 } 162 return std::pair<std::optional<float>, std::optional<float>>(); 163 } 164 FireOnItemDragStart(const ItemDragInfo & dragInfo,int32_t itemIndex)165 RefPtr<UINode> FireOnItemDragStart(const ItemDragInfo& dragInfo, int32_t itemIndex) const 166 { 167 if (onItemDragStart_) { 168 return onItemDragStart_(dragInfo, itemIndex); 169 } 170 return nullptr; 171 } 172 173 void FireOnItemDragEnter(const ItemDragInfo& dragInfo); 174 175 void FireOnItemDragMove(const ItemDragInfo& dragInfo, int32_t itemIndex, int32_t insertIndex) const; 176 177 void FireOnItemDragLeave(const ItemDragInfo& dragInfo, int32_t itemIndex); 178 179 bool FireOnItemDrop(const ItemDragInfo& dragInfo, int32_t itemIndex, int32_t insertIndex, bool isSuccess); 180 HasOnItemDragMove()181 bool HasOnItemDragMove() override 182 { 183 return static_cast<bool>(onItemDragMove_); 184 } 185 HasOnItemDrop()186 bool HasOnItemDrop() override 187 { 188 return static_cast<bool>(onItemDrop_); 189 } 190 191 void InitItemDragEvent(const RefPtr<GestureEventHub>& gestureHub); 192 void HandleOnItemDragStart(const GestureEvent& info); 193 void HandleOnItemDragUpdate(const GestureEvent& info); 194 void HandleOnItemDragEnd(const GestureEvent& info); 195 void HandleOnItemDragCancel(); 196 int32_t GetGridItemIndex(const RefPtr<FrameNode>& frameNode); 197 bool CheckPostionInGrid(float x, float y); 198 int32_t GetInsertPosition(float x, float y); 199 int GetFrameNodeChildSize(); 200 201 private: 202 bool GetEditable() const; 203 void MoveItems(int32_t itemIndex, int32_t insertIndex) const; 204 205 ScrollToIndexFunc onScrollToIndex_; 206 ScrollBarUpdateFunc onScrollBarUpdate_; 207 ItemDragStartFunc onItemDragStart_; 208 ItemDragEnterFunc onItemDragEnter_; 209 ItemDragMoveFunc onItemDragMove_; 210 ItemDragLeaveFunc onItemDragLeave_; 211 ItemDropFunc onItemDrop_; 212 RefPtr<DragDropProxy> dragDropProxy_; 213 int32_t draggedIndex_ = 0; 214 RefPtr<FrameNode> draggingItem_; 215 216 OnScrollEvent onScrollEvent_; 217 OnScrollStartEvent onScrollStartEvent_; 218 OnScrollStopEvent onScrollStopEvent_; 219 ScrollIndexFunc onScrollIndexEvent_; 220 OnScrollFrameBeginEvent onScrollFrameBeginEvent_; 221 OnReachEvent onReachStartEvent_; 222 OnReachEvent onReachEndEvent_; 223 }; 224 225 } // namespace OHOS::Ace::NG 226 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_EVENT_HUB_H