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_TABS_TAB_BAR_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TABS_TAB_BAR_LAYOUT_ALGORITHM_H
18 
19 #include "base/geometry/axis.h"
20 #include "base/memory/referenced.h"
21 #include "core/components_ng/layout/layout_algorithm.h"
22 #include "core/components_ng/layout/layout_wrapper.h"
23 #include "core/components_ng/pattern/tabs/tab_bar_layout_property.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 struct ItemInfo {
28     float startPos = 0.0f;
29     float endPos = 0.0f;
30 };
31 
32 class ACE_EXPORT TabBarLayoutAlgorithm : public LayoutAlgorithm {
33     DECLARE_ACE_TYPE(TabBarLayoutAlgorithm, LayoutAlgorithm);
34 
35 public:
36     TabBarLayoutAlgorithm() = default;
37     ~TabBarLayoutAlgorithm() override = default;
38 
OnReset()39     void OnReset() override {}
40     void Measure(LayoutWrapper* layoutWrapper) override;
41     void Layout(LayoutWrapper* layoutWrapper) override;
42 
SetCurrentDelta(float currentDelta)43     void SetCurrentDelta(float currentDelta)
44     {
45         currentDelta_ = currentDelta;
46     }
47 
SetTabBarStyle(TabBarStyle tabBarStyle)48     void SetTabBarStyle(TabBarStyle tabBarStyle)
49     {
50         tabBarStyle_ = tabBarStyle;
51     }
52 
GetScrollMargin()53     float GetScrollMargin()
54     {
55         return scrollMargin_;
56     }
57 
SetJumpIndex(std::optional<int32_t> jumpIndex)58     void SetJumpIndex(std::optional<int32_t> jumpIndex)
59     {
60         jumpIndex_ = jumpIndex;
61     }
62 
GetJumpIndex()63     std::optional<int32_t> GetJumpIndex()
64     {
65         return jumpIndex_;
66     }
67 
SetTargetIndex(std::optional<int32_t> targetIndex)68     void SetTargetIndex(std::optional<int32_t> targetIndex)
69     {
70         targetIndex_ = targetIndex;
71     }
72 
SetVisibleItemPosition(std::map<int32_t,ItemInfo> visibleItemPosition)73     void SetVisibleItemPosition(std::map<int32_t, ItemInfo> visibleItemPosition)
74     {
75         visibleItemPosition_ = visibleItemPosition;
76     }
77 
GetVisibleItemPosition()78     std::map<int32_t, ItemInfo> GetVisibleItemPosition()
79     {
80         return visibleItemPosition_;
81     }
82 
SetCanOverScroll(bool canOverScroll)83     void SetCanOverScroll(bool canOverScroll)
84     {
85         canOverScroll_ = canOverScroll;
86     }
87 
88 private:
89     void MeasureFixedMode(LayoutWrapper* layoutWrapper, SizeF frameSize);
90     void MeasureScrollableMode(LayoutWrapper* layoutWrapper, SizeF frameSize);
91     LayoutConstraintF GetChildConstraint(LayoutWrapper* layoutWrapper, SizeF& frameSize);
92     void LayoutChildren(LayoutWrapper* layoutWrapper, const SizeF& frameSize, OffsetF& childOffset);
93     void LayoutMask(LayoutWrapper* layoutWrapper, const std::map<int32_t, OffsetF>& childOffsetDelta);
94     void HandleSpaceBetweenOrCenterLayoutStyle(LayoutWrapper* layoutWrapper);
95     void HandleAlwaysAverageSplitLayoutStyle(LayoutWrapper* layoutWrapper);
96     void MeasureVisibleItems(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
97     void MeasureTargetIndex(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
98     void MeasureJumpIndex(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
99     void MeasureWithOffset(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
100     void AdjustPosition(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint,
101         int32_t startIndex, int32_t endIndex, float startPos, float endPos);
102     void MeasureItem(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint, int32_t index);
103     void MeasureItemSecond(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint, SizeF& frameSize);
104     void LayoutForward(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint,
105         int32_t& endIndex, float& endPos);
106     void LayoutBackward(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint,
107         int32_t& startIndex, float& startPos);
108     void MeasureMaxHeight(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
109     GridSizeType GetGridSizeType(const SizeF& frameSize) const;
110     float GetGridWidth(const BarGridColumnOptions& option, const SizeF& frameSize, int32_t columns) const;
111     float ApplyBarGridAlign(const RefPtr<TabBarLayoutProperty>& layoutProperty, const SizeF& frameSize) const;
112     void ApplySymmetricExtensible(LayoutWrapper* layoutWrapper, float allocatedWidth);
113     void ApplyLayoutMode(LayoutWrapper* layoutWrapper, float allocatedWidth);
114     float GetContentMainSize(LayoutWrapper* layoutWrapper, const SizeF& frameSize) const;
115     void CalculateItemWidthsForSymmetricExtensible(LayoutWrapper* layoutWrapper,
116         const std::vector<float>& spaceRequests, const std::vector<float>& leftBuffer,
117         const std::vector<float>& rightBuffer, float allocatedWidth);
118     void UpdateHorizontalPadding(LayoutWrapper* layoutWrapper, float horizontalPadding) const;
119     void MeasureMask(LayoutWrapper* layoutWrapper) const;
120     void UpdateChildMarginProperty(float rightMargin, float leftMargin, const RefPtr<LayoutWrapper>& childWrapper);
121     bool GetBarAdaptiveHeight(LayoutWrapper* layoutWrapper);
122     bool NeedAdaptForAging(RefPtr<FrameNode> host);
123     void SetTabBarMargin(RefPtr<LayoutWrapper> layoutWrapper, int32_t index);
124     void UpdateMaxLines(LayoutWrapper* layoutWrapper, int32_t index);
125 
126     bool isRTL_ = false;
127     Axis axis_ = Axis::NONE;
128     TabBarStyle tabBarStyle_;
129     int32_t childCount_ = 0;
130     float scrollMargin_ = 0.0f;
131     float contentMainSize_ = 0.0f;
132     float visibleChildrenMainSize_ = 0.0f;
133     float startMainPos_ = 0.0f;
134     float endMainPos_ = 0.0f;
135     float currentDelta_ = 0.0f;
136     std::map<int32_t, float> visibleItemLength_;
137     std::map<int32_t, ItemInfo> visibleItemPosition_;
138     std::optional<int32_t> jumpIndex_;
139     std::optional<int32_t> targetIndex_;
140     std::optional<float> maxHeight_;
141     std::optional<float> defaultHeight_;
142     bool isBarAdaptiveHeight_ = false;
143     bool useItemWidth_ = true;
144     bool canOverScroll_ = false;
145     Dimension leftAndRightMargin_ = 0.0_vp;
146     Dimension indicatorStyleMarginTop_ = 0.0_vp;
147 };
148 } // namespace OHOS::Ace::NG
149 
150 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TABS_TAB_BAR_LAYOUT_ALGORITHM_H
151