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_SCROLLABLE_EVENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_SCROLLABLE_EVENT_H 18 19 #include <list> 20 #include <unordered_map> 21 22 #include "base/geometry/axis.h" 23 #include "base/memory/referenced.h" 24 #include "core/components/scroll/scrollable.h" 25 #include "core/components_ng/event/gesture_event_actuator.h" 26 #include "core/components_ng/pattern/scroll/scroll_edge_effect.h" 27 28 namespace OHOS::Ace::NG { 29 30 using OnScrollCallback = std::function<void(Dimension, Dimension)>; 31 using ScrollEndCallback = std::function<void()>; 32 33 class GestureEventHub; 34 35 class ScrollableEvent : public AceType { DECLARE_ACE_TYPE(ScrollableEvent,AceType)36 DECLARE_ACE_TYPE(ScrollableEvent, AceType) 37 public: 38 explicit ScrollableEvent(Axis axis) : axis_(axis) {}; 39 ~ScrollableEvent() override = default; 40 SetScrollPositionCallback(ScrollPositionCallback && callback)41 void SetScrollPositionCallback(ScrollPositionCallback&& callback) 42 { 43 if (!callback) { 44 return; 45 } 46 callback_ = std::move(callback); 47 } 48 GetScrollPositionCallback()49 const ScrollPositionCallback& GetScrollPositionCallback() const 50 { 51 return callback_; 52 } 53 SetScrollBeginCallback(ScrollBeginCallback && scrollBeginCallback)54 void SetScrollBeginCallback(ScrollBeginCallback&& scrollBeginCallback) 55 { 56 if (!scrollBeginCallback) { 57 return; 58 } 59 scrollBeginCallback_ = std::move(scrollBeginCallback); 60 } 61 GetScrollBeginCallback()62 const ScrollBeginCallback& GetScrollBeginCallback() const 63 { 64 return scrollBeginCallback_; 65 } 66 SetScrollFrameBeginCallback(ScrollFrameBeginCallback && scrollFrameBeginCallback)67 void SetScrollFrameBeginCallback(ScrollFrameBeginCallback&& scrollFrameBeginCallback) 68 { 69 if (!scrollFrameBeginCallback) { 70 return; 71 } 72 scrollFrameBeginCallback_ = std::move(scrollFrameBeginCallback); 73 } 74 GetScrollFrameBeginCallback()75 const ScrollFrameBeginCallback& GetScrollFrameBeginCallback() const 76 { 77 return scrollFrameBeginCallback_; 78 } 79 SetOnScrollCallback(OnScrollCallback && onScrollCallback)80 void SetOnScrollCallback(OnScrollCallback&& onScrollCallback) 81 { 82 if (!onScrollCallback) { 83 return; 84 } 85 onScrollCallback_ = std::move(onScrollCallback); 86 } 87 GetOnScrollCallback()88 const OnScrollCallback& GetOnScrollCallback() const 89 { 90 return onScrollCallback_; 91 } 92 SetScrollEndCallback(ScrollEndCallback && scrollEndCallback)93 void SetScrollEndCallback(ScrollEndCallback&& scrollEndCallback) 94 { 95 if (!scrollEndCallback) { 96 return; 97 } 98 scrollEndCallback_ = std::move(scrollEndCallback); 99 } 100 GetScrollEndCallback()101 const ScrollEndCallback& GetScrollEndCallback() const 102 { 103 return scrollEndCallback_; 104 } 105 SetOutBoundaryCallback(OutBoundaryCallback && outBoundaryCallback)106 void SetOutBoundaryCallback(OutBoundaryCallback&& outBoundaryCallback) 107 { 108 if (!outBoundaryCallback) { 109 return; 110 } 111 outBoundaryCallback_ = std::move(outBoundaryCallback); 112 } 113 GetOutBoundaryCallback()114 const OutBoundaryCallback& GetOutBoundaryCallback() const 115 { 116 return outBoundaryCallback_; 117 } 118 GetAxis()119 Axis GetAxis() const 120 { 121 return axis_; 122 } 123 SetAxis(Axis axis)124 void SetAxis(Axis axis) 125 { 126 axis_ = axis; 127 if (scrollable_) { 128 scrollable_->SetAxis(axis); 129 } 130 } 131 SetScrollable(const RefPtr<Scrollable> & scrollable)132 void SetScrollable(const RefPtr<Scrollable>& scrollable) 133 { 134 scrollable_ = scrollable; 135 } 136 GetScrollable()137 const RefPtr<Scrollable>& GetScrollable() const 138 { 139 return scrollable_; 140 } 141 SetEnabled(bool enable)142 void SetEnabled(bool enable) 143 { 144 enable_ = enable; 145 } 146 GetEnable()147 bool GetEnable() const 148 { 149 return enable_; 150 } 151 Idle()152 bool Idle() const 153 { 154 if (scrollable_) { 155 return scrollable_->Idle(); 156 } 157 return true; 158 } 159 160 private: 161 ScrollPositionCallback callback_; 162 OnScrollCallback onScrollCallback_; 163 ScrollBeginCallback scrollBeginCallback_; 164 ScrollFrameBeginCallback scrollFrameBeginCallback_; 165 ScrollEndCallback scrollEndCallback_; 166 OutBoundaryCallback outBoundaryCallback_; 167 168 Axis axis_ = Axis::VERTICAL; 169 bool enable_ = true; 170 RefPtr<Scrollable> scrollable_; 171 }; 172 173 class ScrollableActuator : public GestureEventActuator { 174 DECLARE_ACE_TYPE(ScrollableActuator, GestureEventActuator) 175 public: 176 explicit ScrollableActuator(const WeakPtr<GestureEventHub>& gestureEventHub); 177 ~ScrollableActuator() override = default; 178 AddScrollableEvent(const RefPtr<ScrollableEvent> & scrollableEvent)179 void AddScrollableEvent(const RefPtr<ScrollableEvent>& scrollableEvent) 180 { 181 scrollableEvents_[scrollableEvent->GetAxis()] = scrollableEvent; 182 initialized_ = false; 183 } 184 RemoveScrollableEvent(const RefPtr<ScrollableEvent> & scrollableEvent)185 void RemoveScrollableEvent(const RefPtr<ScrollableEvent>& scrollableEvent) 186 { 187 scrollableEvents_.erase(scrollableEvent->GetAxis()); 188 initialized_ = false; 189 } 190 191 void AddScrollEdgeEffect(const Axis& axis, const RefPtr<ScrollEdgeEffect>& effect); 192 bool RemoveScrollEdgeEffect(const RefPtr<ScrollEdgeEffect>& effect); 193 194 void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict, 195 const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result) override; 196 197 private: 198 void InitializeScrollable(); 199 200 std::unordered_map<Axis, RefPtr<ScrollableEvent>> scrollableEvents_; 201 std::unordered_map<Axis, RefPtr<ScrollEdgeEffect>> scrollEffects_; 202 WeakPtr<GestureEventHub> gestureEventHub_; 203 bool initialized_ = false; 204 }; 205 206 } // namespace OHOS::Ace::NG 207 208 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_SCROLLABLE_EVENT_HUB_H 209