1 /* 2 * Copyright (c) 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_PATTERNS_SCROLLABLE_SCROLLABLE_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLLABLE_SCROLLABLE_PATTERN_H 18 19 #include "base/geometry/axis.h" 20 #include "core/components_ng/pattern/pattern.h" 21 #include "core/components_ng/pattern/scroll/inner/scroll_bar.h" 22 #include "core/components_ng/pattern/scroll_bar/proxy/scroll_bar_proxy.h" 23 #include "core/components_ng/pattern/scrollable/scrollable_coordination_event.h" 24 #include "core/components_ng/pattern/scrollable/scrollable_paint_property.h" 25 26 namespace OHOS::Ace::NG { 27 class ScrollablePattern : public Pattern { 28 DECLARE_ACE_TYPE(ScrollablePattern, Pattern); 29 30 public: IsAtomicNode()31 bool IsAtomicNode() const override 32 { 33 return false; 34 } 35 36 // scrollable GetAxis()37 Axis GetAxis() const 38 { 39 return axis_; 40 } 41 void SetAxis(Axis axis); 42 virtual bool UpdateCurrentOffset(float delta, int32_t source) = 0; IsScrollable()43 virtual bool IsScrollable() const 44 { 45 return false; 46 } 47 virtual bool IsAtTop() const = 0; 48 virtual bool IsAtBottom() const = 0; 49 void AddScrollEvent(); GetScrollableEvent()50 RefPtr<ScrollableEvent> GetScrollableEvent() 51 { 52 return scrollableEvent_; 53 } 54 virtual bool OnScrollCallback(float offset, int32_t source); OnScrollEndCallback()55 virtual void OnScrollEndCallback() {}; ScrollableIdle()56 bool ScrollableIdle() 57 { 58 return !scrollableEvent_ || scrollableEvent_->Idle(); 59 } SetScrollEnable(bool enable)60 void SetScrollEnable(bool enable) 61 { 62 CHECK_NULL_VOID_NOLOG(scrollableEvent_); 63 scrollableEvent_->SetEnabled(enable); 64 } 65 void SetScrollableAxis(Axis axis); 66 const RefPtr<GestureEventHub>& GetGestureHub(); 67 const RefPtr<InputEventHub>& GetInputHub(); 68 69 // edgeEffect GetScrollEdgeEffect()70 const RefPtr<ScrollEdgeEffect>& GetScrollEdgeEffect() const 71 { 72 return scrollEffect_; 73 } 74 void SetEdgeEffect(EdgeEffect edgeEffect); 75 void AddScrollEdgeEffect(RefPtr<ScrollEdgeEffect> edgeEffect); 76 bool HandleEdgeEffect(float offset, int32_t source, const SizeF& size); SetEdgeEffectCallback(const RefPtr<ScrollEdgeEffect> & scrollEffect)77 virtual void SetEdgeEffectCallback(const RefPtr<ScrollEdgeEffect>& scrollEffect) {} IsRestrictBoundary()78 bool IsRestrictBoundary() 79 { 80 return !scrollEffect_ || scrollEffect_->IsRestrictBoundary(); 81 } 82 83 // scrollBar 84 virtual void UpdateScrollBarOffset() = 0; 85 void SetScrollBar(const std::unique_ptr<ScrollBarProperty>& property); 86 void SetScrollBar(DisplayMode displayMode); 87 void SetScrollBarProxy(const RefPtr<ScrollBarProxy>& scrollBarProxy); 88 GetScrollableDistance()89 float GetScrollableDistance() const 90 { 91 return estimatedHeight_; 92 } 93 GetCurrentPosition()94 float GetCurrentPosition() const 95 { 96 return barOffset_; 97 } 98 GetScrollBarOutBoundaryExtent()99 double GetScrollBarOutBoundaryExtent() const 100 { 101 return scrollBarOutBoundaryExtent_; 102 } 103 SetScrollBarOutBoundaryExtent(double scrollBarOutBoundaryExtent)104 void SetScrollBarOutBoundaryExtent(double scrollBarOutBoundaryExtent) 105 { 106 scrollBarOutBoundaryExtent_ = scrollBarOutBoundaryExtent; 107 } 108 GetMainSize(const SizeF & size)109 double GetMainSize(const SizeF& size) const 110 { 111 return axis_ == Axis::HORIZONTAL ? size.Width() : size.Height(); 112 } 113 SetCoordinationEvent(RefPtr<ScrollableCoordinationEvent> coordinationEvent)114 void SetCoordinationEvent(RefPtr<ScrollableCoordinationEvent> coordinationEvent) 115 { 116 coordinationEvent_ = coordinationEvent; 117 } 118 IsScrollableStopped()119 bool IsScrollableStopped() const 120 { 121 CHECK_NULL_RETURN_NOLOG(scrollableEvent_, true); 122 auto scrollable = scrollableEvent_->GetScrollable(); 123 CHECK_NULL_RETURN_NOLOG(scrollable, true); 124 return scrollable->IsStopped(); 125 } 126 StopScrollable()127 void StopScrollable() 128 { 129 CHECK_NULL_VOID_NOLOG(scrollableEvent_); 130 auto scrollable = scrollableEvent_->GetScrollable(); 131 CHECK_NULL_VOID_NOLOG(scrollable); 132 scrollable->StopScrollable(); 133 } 134 135 protected: GetScrollBar()136 RefPtr<ScrollBar> GetScrollBar() const 137 { 138 return scrollBar_; 139 } GetScrollBarProxy()140 RefPtr<NG::ScrollBarProxy> GetScrollBarProxy() const 141 { 142 return scrollBarProxy_; 143 } SetScrollBarDriving(bool Driving)144 void SetScrollBarDriving(bool Driving) 145 { 146 if (scrollBar_) { 147 scrollBar_->SetDriving(Driving); 148 } 149 } 150 void UpdateScrollBarRegion(float offset, float estimatedHeight, Size viewPort); 151 152 private: 153 void RegisterScrollBarEventTask(); 154 void OnScrollEnd(); 155 bool OnScrollPosition(double offset, int32_t source); 156 157 Axis axis_; 158 RefPtr<ScrollableEvent> scrollableEvent_; 159 RefPtr<ScrollEdgeEffect> scrollEffect_; 160 RefPtr<ScrollableCoordinationEvent> coordinationEvent_; 161 // scrollBar 162 RefPtr<ScrollBar> scrollBar_; 163 RefPtr<NG::ScrollBarProxy> scrollBarProxy_; 164 float barOffset_ = 0.0f; 165 float estimatedHeight_ = 0.0f; 166 bool isReactInParentMovement_ = false; 167 double scrollBarOutBoundaryExtent_ = 0.0; 168 }; 169 } // namespace OHOS::Ace::NG 170 171 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_SCROLL_PATTERN_H 172