• 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_PATTERN_SCROLL_SCROLL_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SCROLL_SCROLL_LAYOUT_ALGORITHM_H
18 
19 #include <map>
20 
21 #include "base/geometry/axis.h"
22 #include "base/memory/referenced.h"
23 #include "core/components/common/layout/grid_system_manager.h"
24 #include "core/components/common/properties/alignment.h"
25 #include "core/components_ng/layout/layout_algorithm.h"
26 #include "core/components_ng/layout/layout_wrapper.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 constexpr int32_t SCROLL_FROM_JUMP = 3;
31 
32 class ACE_EXPORT ScrollLayoutAlgorithm : public LayoutAlgorithm {
33     DECLARE_ACE_TYPE(ScrollLayoutAlgorithm, LayoutAlgorithm);
34 
35 public:
36     explicit ScrollLayoutAlgorithm(float currentOffset, float crossOffset = 0.0f)
crossOffset_(crossOffset)37         : crossOffset_(crossOffset), currentOffset_(currentOffset)
38     {}
39     ~ScrollLayoutAlgorithm() override = default;
40 
OnReset()41     void OnReset() override {}
42 
GetCurrentOffset()43     float GetCurrentOffset() const
44     {
45         return currentOffset_;
46     }
47 
GetFreeOffset()48     OffsetF GetFreeOffset() const
49     {
50         return { currentOffset_, crossOffset_ };
51     }
52 
GetScrollableDistance()53     float GetScrollableDistance() const
54     {
55         return scrollableDistance_;
56     }
57 
58     /**
59      * @return 2D scrollable distance for free mode.
60      */
GetScrollableArea()61     SizeF GetScrollableArea() const
62     {
63         return { scrollableDistance_, viewPortExtent_.Height() - viewPort_.Height() };
64     }
65 
GetViewPortLength()66     float GetViewPortLength() const
67     {
68         return viewPortLength_;
69     }
70 
GetViewPort()71     const SizeF& GetViewPort() const
72     {
73         return viewPort_;
74     }
75 
GetViewSize()76     const SizeF& GetViewSize() const
77     {
78         return viewSize_;
79     }
80 
GetViewPortExtent()81     const SizeF& GetViewPortExtent() const
82     {
83         return viewPortExtent_;
84     }
85 
86     void Measure(LayoutWrapper* layoutWrapper) override;
87 
88     void Layout(LayoutWrapper* layoutWrapper) override;
89     void UpdateOverlay(LayoutWrapper* layoutWrapper);
90     void MarkAndCheckNewOpIncNode(const RefPtr<LayoutWrapper>& layoutWrapper, Axis axis);
91 
92 private:
93     void UseInitialOffset(Axis axis, SizeF selfSize, LayoutWrapper* layoutWrapper);
94     bool UnableOverScroll(LayoutWrapper* layoutWrapper) const;
95     void OnSurfaceChanged(LayoutWrapper* layoutWrapper, float contentMainSize);
96 
97     float crossOffset_;
98     float currentOffset_ = 0.0f;
99     float scrollableDistance_ = 0.0f;
100     float viewPortLength_ = 0.0f;
101     SizeF viewPort_;       // content area size (viewSize_ minus padding)
102     SizeF viewPortExtent_; // size of child (scrollable area)
103     SizeF viewSize_;       // size of the Scroll component
104     void UpdateScrollAlignment(Alignment& scrollAlignment);
105 };
106 
107 } // namespace OHOS::Ace::NG
108 
109 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SCROLL_SCROLL_LAYOUT_ALGORITHM_H
110