• 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 Pattern;
28 struct ScrollableNodeInfo {
29     WeakPtr<Pattern> 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 
36     bool operator==(const ScrollableNodeInfo& info) const
37     {
38         return scrollableNode == info.scrollableNode;
39     }
40 };
41 
42 class ScrollBarPattern;
43 class ACE_EXPORT ScrollBarProxy : public ScrollProxy {
44     DECLARE_ACE_TYPE(ScrollBarProxy, ScrollProxy);
45 
46 public:
47     ScrollBarProxy() = default;
48     ~ScrollBarProxy() override = default;
49 
50     // Register scrollable node and scroll bar, scrollable node and scroll bar communicate through proxy.
51     void RegisterScrollableNode(const ScrollableNodeInfo& scrollableNode);
52     void RegisterScrollBar(const WeakPtr<ScrollBarPattern>& scrollBar);
53 
54     // UnRegister scrollable node and scroll bar.
55     void UnRegisterScrollableNode(const WeakPtr<Pattern>& scrollableNode);
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, const WeakPtr<ScrollBarPattern>& weakScrollBar) const;
63 
64     /*
65      * Notify scrollable node to callback scrollStart, called by scroll bar.
66      */
67     void NotifyScrollStart() const;
68 
69     /*
70      * Notify scrollable node to callback scrollStop, called by scroll bar.
71      */
72     void NotifyScrollStop() const;
73     /*
74      * Notify scroll bar to update state, called by scrollable node.
75      * @param distance absolute distance that scrollable node has scrolled.
76      */
77     void NotifyScrollBar(const WeakPtr<Pattern>& weakScrollableNode) const;
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 controlDistance) const;
93 
94     float CalcPatternOffset(float scrollableDistance, float controlDistance, float delta) const;
95 
96 private:
97     std::list<ScrollableNodeInfo> scrollableNodes_;  // Scrollable nodes, like list, grid, scroll, etc.
98     std::list<WeakPtr<ScrollBarPattern>> scrollBars_; // ScrollBar should effect with scrollable node.
99 };
100 
101 } // namespace OHOS::Ace::NG
102 
103 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_BAR_PROXY_SCROLL_BAR_PROXY_H
104