• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)> onPositionChanged;
31     std::function<bool(double, int32_t source)> scrollStartCallback;
32     std::function<void()> scrollEndCallback;
33     CalePredictSnapOffsetCallback calePredictSnapOffsetCallback;
34     StartScrollSnapMotionCallback startScrollSnapMotionCallback;
35     ScrollBarFRCallback scrollbarFRcallback;
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 UnRegisterScrollableNode(const WeakPtr<ScrollablePattern>& scrollableNode);
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 WeakPtr<ScrollablePattern>& weakScrollableNode) 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 
109     void SetScrollEnabled(bool scrollEnabled, const WeakPtr<ScrollablePattern>& weakScrollableNode) const;
110 
111 private:
112     /*
113      * Drag the built-in or external scroll bar to slide the Scroll.
114      * When the sliding stops and the fingers are not raised, prevent scrolling to the limit point
115      */
116     bool scrollSnapTrigger_ = false;
117     std::list<ScrollableNodeInfo> scrollableNodes_;  // Scrollable nodes, like list, grid, scroll, etc.
118     std::list<WeakPtr<ScrollBarPattern>> scrollBars_; // ScrollBar should effect with scrollable node.
119 };
120 
121 } // namespace OHOS::Ace::NG
122 
123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_PROXY_SCROLL_BAR_PROXY_H
124