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_PATTERNS_SCROLL_BAR_PROXY_SCROLL_BAR_PROXY_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_PROXY_SCROLL_BAR_PROXY_H 18 19 #include <functional> 20 #include <list> 21 22 #include "core/components/scroll_bar/scroll_proxy.h" 23 #include "core/components_ng/pattern/scrollable/scrollable_properties.h" 24 25 namespace OHOS::Ace::NG { 26 27 class ScrollablePattern; 28 struct ScrollableNodeInfo { 29 WeakPtr<ScrollablePattern> scrollableNode; 30 std::function<bool(double, int32_t source, bool, bool)> onPositionChanged; 31 std::function<bool(double, int32_t source, bool)> scrollStartCallback; 32 std::function<void(bool)> scrollEndCallback; 33 StartSnapAnimationCallback startSnapAnimationCallback; 34 ScrollBarFRCallback scrollbarFRcallback; 35 std::function<void(bool, bool smooth)> scrollPageCallback; 36 37 bool operator==(const ScrollableNodeInfo& info) const 38 { 39 return scrollableNode == info.scrollableNode; 40 } 41 }; 42 43 class ScrollBarPattern; 44 class ACE_EXPORT ScrollBarProxy : public ScrollProxy { 45 DECLARE_ACE_TYPE(ScrollBarProxy, ScrollProxy); 46 47 public: 48 ScrollBarProxy() = default; 49 ~ScrollBarProxy() override = default; 50 51 // Register scrollable node and scroll bar, scrollable node and scroll bar communicate through proxy. 52 void RegisterScrollableNode(const ScrollableNodeInfo& scrollableNode); 53 void RegisterScrollBar(const WeakPtr<ScrollBarPattern>& scrollBar); 54 55 // UnRegister scrollable node and scroll bar. 56 void UnRegisterScrollBar(const WeakPtr<ScrollBarPattern>& scrollBar); 57 58 /* 59 * Notify scrollable node to update state, called by scroll bar. 60 * @param distance absolute distance that scroll bar has scrolled. 61 */ 62 void NotifyScrollableNode(float distance, int32_t source, const WeakPtr<ScrollBarPattern>& weakScrollBar, 63 bool isMouseWheelScroll = false) const; 64 65 /* 66 * Notify scrollable node to callback scrollStart, called by scroll bar. 67 */ 68 void NotifyScrollStart() const; 69 70 /* 71 * Notify scrollable node to callback scrollStop, called by scroll bar. 72 */ 73 void NotifyScrollStop() const; 74 /* 75 * Notify scroll bar to update state, called by scrollable node. 76 */ 77 void NotifyScrollBar(int32_t scrollSource); 78 79 /* 80 * Start animation of ScrollBar. 81 */ 82 void StartScrollBarAnimator() const; 83 84 /* 85 * Stop animation of ScrollBar, and show ScrollBar if needed, when scrollable node is scrolling. 86 */ 87 void StopScrollBarAnimator() const; 88 89 /* 90 * Notify scrollable node to snap scroll, called by scroll bar. 91 */ 92 bool NotifySnapScroll(float delta, float velocity, float barScrollableDistance, float dragDistance) const; 93 94 bool NotifySnapScrollWithoutChild(SnapAnimationOptions snapAnimationOptions) const; 95 96 float CalcPatternOffset(float controlDistance, float barScrollableDistance, float delta) const; 97 98 void NotifyScrollBarNode(float distance, int32_t source, bool isMouseWheelScroll = false) const; 99 SetScrollSnapTrigger_(bool scrollSnapTrigger)100 void SetScrollSnapTrigger_(bool scrollSnapTrigger) 101 { 102 scrollSnapTrigger_ = scrollSnapTrigger; 103 } 104 105 bool IsScrollSnapTrigger() const; 106 107 void SetScrollEnabled(bool scrollEnabled, const WeakPtr<ScrollablePattern>& weakScrollableNode) const; 108 void ScrollPage(bool reverse, bool smooth); 109 110 void RegisterNestScrollableNode(const ScrollableNodeInfo& scrollableNode); 111 112 void UnRegisterNestScrollableNode(const WeakPtr<ScrollablePattern>& scrollableNode); 113 GetScrollableNodeInfo()114 ScrollableNodeInfo& GetScrollableNodeInfo() 115 { 116 return scorllableNode_; 117 } 118 119 bool IsNestScroller() const; 120 121 void MarkScrollBarDirty() const; 122 SetIsScrollableNodeScrolling(bool isScrolling)123 void SetIsScrollableNodeScrolling(bool isScrolling) 124 { 125 isScrollableNodeScrolling_ = isScrolling; 126 } 127 IsScrollableNodeScrolling()128 bool IsScrollableNodeScrolling() const 129 { 130 return isScrollableNodeScrolling_; 131 } 132 private: 133 /* 134 * Drag the built-in or external scroll bar to slide the Scroll. 135 * When the sliding stops and the fingers are not raised, prevent scrolling to the limit point 136 */ 137 bool scrollSnapTrigger_ = false; 138 ScrollableNodeInfo scorllableNode_; // Scrollable node, like list, grid, scroll, etc. 139 std::list<ScrollableNodeInfo> nestScrollableNodes_; // Scrollable nodes, like scroll. 140 std::list<WeakPtr<ScrollBarPattern>> scrollBars_; // ScrollBar should effect with scrollable node. 141 float lastControlDistance_ = 0.f; 142 float lastScrollableNodeOffset_ = 0.f; 143 bool isScrollableNodeScrolling_ = false; 144 }; 145 146 } // namespace OHOS::Ace::NG 147 148 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_PROXY_SCROLL_BAR_PROXY_H 149