1 /*
2  * Copyright (c) 2021 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_SCROLL_BAR_SCROLL_BAR_PROXY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_BAR_SCROLL_BAR_PROXY_H
18 
19 #include "core/components/scroll_bar/scroll_proxy.h"
20 #include "core/pipeline/base/render_node.h"
21 
22 namespace OHOS::Ace {
23 
24 struct ScrollableNodeInfo {
25     WeakPtr<RenderNode> scrollableNode;
26     std::function<bool(double, int32_t source)> onPositionChanged;
27 
28     bool operator==(const ScrollableNodeInfo& info) const
29     {
30         return scrollableNode == info.scrollableNode;
31     }
32 };
33 
34 class RenderScrollBar;
35 class ACE_EXPORT ScrollBarProxy : public ScrollProxy {
36     DECLARE_ACE_TYPE(ScrollBarProxy, ScrollProxy);
37 
38 public:
39     ScrollBarProxy() = default;
40     ~ScrollBarProxy() override = default;
41 
42     // Register scrollable node and scroll bar, scrollable node and scroll bar communicate through proxy.
43     void RegisterScrollableNode(const ScrollableNodeInfo& scrollableNode);
44     void RegisterScrollBar(const WeakPtr<RenderScrollBar>& scrollBar);
45 
46     // UnRegister scrollable node and scroll bar.
47     void UnRegisterScrollableNode(const WeakPtr<RenderNode>& scrollableNode);
48     void UnRegisterScrollBar(const WeakPtr<RenderScrollBar>& scrollBar);
49 
50     /*
51      * Notify scrollable node to update state, called by scroll bar.
52      * @param distance absolute distance that scroll bar has scrolled.
53      */
54     void NotifyScrollableNode(double distance, const WeakPtr<RenderScrollBar>& weakScrollBar) const;
55 
56     /*
57      * Notify scroll bar to update state, called by scrollable node.
58      * @param distance absolute distance that scrollable node has scrolled.
59      */
60     void NotifyScrollBar(const WeakPtr<RenderNode>& weakScrollableNode) const;
61 
62     /*
63      * Start animation of ScrollBar.
64      */
65     void StartScrollBarAnimator() const;
66 
67     /*
68      * Stop animation of ScrollBar, and show ScrollBar if needed, when scrollable node is scrolling.
69      */
70     void StopScrollBarAnimator() const;
71 
72 private:
73     bool CheckScrollable(const RefPtr<RenderNode>& node) const;
74     Axis GetScrollableAxis(const RefPtr<RenderNode>& node) const;
75     Size GetScrollableChildSize(
76         const RefPtr<RenderNode>& scrollable, const Size& scrollableChildSize, Axis scrollBarAxis) const;
77     void AdjustParam(const RefPtr<RenderNode>& scrollable, Axis scrollBarAxis, Axis& scrollableAxis,
78         Size& scrollableChildSize, Offset& scrollableChildPosition) const;
79 
80     std::list<ScrollableNodeInfo> scrollableNodes_;  // Scrollable nodes, like list, grid, scroll, etc.
81     std::list<WeakPtr<RenderScrollBar>> scrollBars_; // ScrollBar should effect with scrollable node.
82 };
83 
84 } // namespace OHOS::Ace
85 
86 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_BAR_SCROLL_BAR_PROXY_H
87