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)> onPositionChanged;
31     std::function<bool(double, int32_t source, bool)> scrollStartCallback;
32     std::function<void(bool)> scrollEndCallback;
33     CalePredictSnapOffsetCallback calePredictSnapOffsetCallback;
34     StartScrollSnapMotionCallback startScrollSnapMotionCallback;
35     ScrollBarFRCallback scrollbarFRcallback;
36     std::function<void(bool, bool smooth)> scrollPageCallback;
37 
38     bool operator==(const ScrollableNodeInfo& info) const
39     {
40         return scrollableNode == info.scrollableNode;
41     }
42 };
43 
44 class ScrollBarPattern;
45 class ACE_EXPORT ScrollBarProxy : public ScrollProxy {
46     DECLARE_ACE_TYPE(ScrollBarProxy, ScrollProxy);
47 
48 public:
49     ScrollBarProxy() = default;
50     ~ScrollBarProxy() override = default;
51 
52     // Register scrollable node and scroll bar, scrollable node and scroll bar communicate through proxy.
53     void RegisterScrollableNode(const ScrollableNodeInfo& scrollableNode);
54     void RegisterScrollBar(const WeakPtr<ScrollBarPattern>& scrollBar);
55 
56     // UnRegister scrollable node and scroll bar.
57     void UnRegisterScrollBar(const WeakPtr<ScrollBarPattern>& scrollBar);
58 
59     /*
60      * Notify scrollable node to update state, called by scroll bar.
61      * @param distance absolute distance that scroll bar has scrolled.
62      */
63     void NotifyScrollableNode(float distance, int32_t source, const WeakPtr<ScrollBarPattern>& weakScrollBar) 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      * @param distance absolute distance that scrollable node has scrolled.
77      */
78     void NotifyScrollBar() const;
79 
80     /*
81      * Start animation of ScrollBar.
82      */
83     void StartScrollBarAnimator() const;
84 
85     /*
86      * Stop animation of ScrollBar, and show ScrollBar if needed, when scrollable node is scrolling.
87      */
88     void StopScrollBarAnimator() const;
89 
90     /*
91      * Notify scrollable node to snap scroll, called by scroll bar.
92      */
93     bool NotifySnapScroll(float delta, float velocity, float barScrollableDistance, float dragDistance) const;
94 
95     float CalcPatternOffset(float controlDistance, float barScrollableDistance, float delta) const;
96 
97     void NotifyScrollBarNode(float distance, int32_t source) const;
98 
SetScrollSnapTrigger_(bool scrollSnapTrigger)99     void SetScrollSnapTrigger_(bool scrollSnapTrigger)
100     {
101         scrollSnapTrigger_ = scrollSnapTrigger;
102     }
103 
IsScrollSnapTrigger()104     bool IsScrollSnapTrigger() const
105     {
106         return scrollSnapTrigger_;
107     }
108     void SetScrollEnabled(bool scrollEnabled, const WeakPtr<ScrollablePattern>& weakScrollableNode) const;
109     void ScrollPage(bool reverse, bool smooth);
110 
111     void RegisterNestScrollableNode(const ScrollableNodeInfo& scrollableNode);
112 
113     void UnRegisterNestScrollableNode(const WeakPtr<ScrollablePattern>& scrollableNode);
114 
GetScrollableNodeInfo()115     ScrollableNodeInfo& GetScrollableNodeInfo()
116     {
117         return scorllableNode_;
118     }
119 
120     bool IsNestScroller() const;
121 private:
122     /*
123      * Drag the built-in or external scroll bar to slide the Scroll.
124      * When the sliding stops and the fingers are not raised, prevent scrolling to the limit point
125      */
126     bool scrollSnapTrigger_ = false;
127     ScrollableNodeInfo scorllableNode_; // Scrollable node, like list, grid, scroll, etc.
128     std::list<ScrollableNodeInfo> nestScrollableNodes_; // Scrollable nodes, like scroll.
129     std::list<WeakPtr<ScrollBarPattern>> scrollBars_; // ScrollBar should effect with scrollable node.
130 };
131 
132 } // namespace OHOS::Ace::NG
133 
134 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_PROXY_SCROLL_BAR_PROXY_H
135