• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_SCROLLABLE_NESTABLE_SCROLL_CONTAINER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLLABLE_NESTABLE_SCROLL_CONTAINER_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/utils/noncopyable.h"
21 #include "core/components_ng/pattern/pattern.h"
22 #include "core/components_ng/pattern/scrollable/scrollable.h"
23 
24 namespace OHOS::Ace::NG {
25 class NestableScrollContainer : public virtual Pattern {
26     DECLARE_ACE_TYPE(NestableScrollContainer, Pattern);
27 public:
28     NestableScrollContainer() = default;
29     ~NestableScrollContainer() override = default;
30 
31     virtual Axis GetAxis() const = 0;
32 
33     /**
34      * @brief Handle regular scroll motion.
35      *
36      * @param offset delta on the main axis.
37      * @param source scroll source type defined in scrollable_properties.h.
38      * @param state current nested state. Defines how this function is triggered.
39      * @param velocity current velocity on the main axis.
40      * @return remaining offset and whether the edge is reached
41      */
42     virtual ScrollResult HandleScroll(
43         float offset, int32_t source, NestedState state = NestedState::GESTURE, float velocity = 0.f) = 0;
44 
45     /**
46      * @brief Handle drag end velocity, triggering a scroll animation or a bounce animation on overScroll.
47      *
48      * @param velocity drag end velocity.
49      * @param child components that call HandleScrollVelocity.
50      * @return true if velocity is consumed
51      */
52     virtual bool HandleScrollVelocity(float velocity, const RefPtr<NestableScrollContainer>& child = nullptr) = 0;
53 
54     /**
55      * @brief Called when the scroll starts, recursively pass upwards.
56      *
57      * @param child components that call OnScrollStartRecursive.
58      * @param position The global position of the first touch point.
59      * @param velocity current velocity on the main axis.
60      */
61     virtual void OnScrollStartRecursive(
62         WeakPtr<NestableScrollContainer> child, float position, float velocity = 0.f) = 0;
63 
64     /**
65      * @brief This function is called when the scrolling ends, recursively pass upwards.
66      *
67      * @param velocity The velocity of the DragEnd event. Optionally passed to parent when the child won't call
68      * HandleScrollVelocity.
69      */
70     virtual void OnScrollEndRecursive(const std::optional<float>& velocity) = 0;
71 
72     /**
73      * @brief Determine if a component is in an out-of-bounds state.
74      */
NestedScrollOutOfBoundary()75     virtual bool NestedScrollOutOfBoundary()
76     {
77         return false;
78     }
79 
80     void UpdateNestedModeForChildren(const NestedScrollOptions& childNestedScroll);
81 
82     void SetNestedScroll(const NestedScrollOptions& nestedScroll, bool isFixedNestedScrollMode = false);
83 
GetNestedScroll()84     const NestedScrollOptions GetNestedScroll() const
85     {
86         return nestedScroll_;
87     }
88 
89     void SetParentScrollable();
90 
GetNestedScrollParent()91     RefPtr<NestableScrollContainer> GetNestedScrollParent()
92     {
93         return parent_.Upgrade();
94     }
95 
96     /**
97      * @brief Passes the velocity of the current component to the child component for processing.
98      *
99      * @param remainVelocity the velocity of the current component.
100      */
RemainVelocityToChild(float remainVelocity)101     virtual void RemainVelocityToChild(float remainVelocity) {}
102 
103     virtual void OnScrollDragEndRecursive();
104 
StopScrollAnimation()105     virtual void StopScrollAnimation() {};
106 
SetWebNestedScrollExisted(bool webNestedScrollExisted)107     void SetWebNestedScrollExisted(bool webNestedScrollExisted)
108     {
109         webNestedScrollExisted_ = webNestedScrollExisted;
110         auto parent = GetNestedScrollParent();
111         if (parent) {
112             parent->SetWebNestedScrollExisted(webNestedScrollExisted);
113         }
114     }
115 
GetWebNestedScrollExisted()116     bool GetWebNestedScrollExisted() const
117     {
118         return webNestedScrollExisted_;
119     }
120 
GetNeedLinked()121     bool GetNeedLinked() const
122     {
123         return needLinked_;
124     }
125 
SetNeedLinked(bool needLinked)126     void SetNeedLinked(bool needLinked)
127     {
128         needLinked_ = needLinked;
129     }
130 
131 protected:
132     /**
133      * @brief Helper function. Searches for the parent NestableScrollContainer of the current instance.
134      *
135      * @return RefPtr<NestableScrollContainer> A reference to the parent NestableScrollContainer.
136      */
137     virtual RefPtr<NestableScrollContainer> SearchParent();
138 
GetNestedModeForChildren()139     const std::unique_ptr<NestedScrollOptions>& GetNestedModeForChildren() const
140     {
141         return childNestedScroll_;
142     }
143 
SetIsFixedNestedScrollMode(bool isFixedNestedScrollMode)144     void SetIsFixedNestedScrollMode(bool isFixedNestedScrollMode)
145     {
146         isFixedNestedScrollMode_ = isFixedNestedScrollMode;
147     }
148 
GetIsFixedNestedScrollMode()149     bool GetIsFixedNestedScrollMode() const
150     {
151         return isFixedNestedScrollMode_;
152     }
153 
SetNestedScrollParent(RefPtr<NestableScrollContainer> parent)154     void SetNestedScrollParent(RefPtr<NestableScrollContainer> parent)
155     {
156         parent_ = parent;
157     }
158 
SetIsSearchRefresh(bool isSearchRefresh)159     void SetIsSearchRefresh(bool isSearchRefresh)
160     {
161         isSearchRefresh_ = isSearchRefresh;
162     }
163 
GetIsSearchRefresh()164     bool GetIsSearchRefresh() const
165     {
166         return isSearchRefresh_;
167     }
168 
SetIsNestedInterrupt(bool isNestedInterrupt)169     void SetIsNestedInterrupt(bool isNestedInterrupt)
170     {
171         isNestedInterrupt_ = isNestedInterrupt;
172     }
173 
GetIsNestedInterrupt()174     bool GetIsNestedInterrupt() const
175     {
176         return isNestedInterrupt_;
177     }
178 
179 private:
180     std::unique_ptr<NestedScrollOptions> childNestedScroll_;
181     WeakPtr<NestableScrollContainer> parent_;
182 
183     NestedScrollOptions nestedScroll_ = {
184         .forward = NestedScrollMode::SELF_ONLY,
185         .backward = NestedScrollMode::SELF_ONLY,
186     };
187 
188     bool isFixedNestedScrollMode_ = false;
189     bool isSearchRefresh_ = true;
190     bool isNestedInterrupt_ = false; // nested scroll interrupted by change of nested mode
191     bool webNestedScrollExisted_ = false;
192     bool needLinked_ = true;
193     ACE_DISALLOW_COPY_AND_MOVE(NestableScrollContainer);
194 };
195 } // namespace OHOS::Ace::NG
196 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLLABLE_NESTABLE_SCROLL_CONTAINER_H
197