• 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_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 
SetFocusIndex(std::optional<int32_t> focusIndex)73     void SetFocusIndex(std::optional<int32_t> focusIndex)
74     {
75         focusIndex_ = focusIndex;
76     }
77 
SetVisibleItemPosition(std::map<int32_t,ItemInfo> visibleItemPosition)78     void SetVisibleItemPosition(std::map<int32_t, ItemInfo> visibleItemPosition)
79     {
80         visibleItemPosition_ = visibleItemPosition;
81     }
82 
GetVisibleItemPosition()83     std::map<int32_t, ItemInfo> GetVisibleItemPosition()
84     {
85         return visibleItemPosition_;
86     }
87 
SetCanOverScroll(bool canOverScroll)88     void SetCanOverScroll(bool canOverScroll)
89     {
90         canOverScroll_ = canOverScroll;
91     }
92 
GetBarGridMargin()93     float GetBarGridMargin()
94     {
95         return barGridMargin_;
96     }
97 
98 private:
99     void MeasureFixedMode(LayoutWrapper* layoutWrapper, SizeF frameSize);
100     void MeasureScrollableMode(LayoutWrapper* layoutWrapper, SizeF frameSize);
101     LayoutConstraintF GetChildConstraint(LayoutWrapper* layoutWrapper, SizeF& frameSize);
102     void LayoutChildren(LayoutWrapper* layoutWrapper, const SizeF& contentSize, OffsetF& childOffset);
103     void LayoutMask(LayoutWrapper* layoutWrapper, const std::map<int32_t, OffsetF>& childOffsetDelta);
104     void HandleSpaceBetweenOrCenterLayoutStyle(LayoutWrapper* layoutWrapper);
105     void HandleAlwaysAverageSplitLayoutStyle(LayoutWrapper* layoutWrapper);
106     void MeasureVisibleItems(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
107     void MeasureTargetIndex(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
108     void MeasureJumpIndex(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
109     void MeasureFocusIndex(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
110     void MeasureWithOffset(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
111     void AdjustPosition(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint,
112         int32_t startIndex, int32_t endIndex, float startPos, float endPos);
113     void MeasureItem(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint, int32_t index);
114     void MeasureItemSecond(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint, SizeF& frameSize);
115     void LayoutForward(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint,
116         int32_t& endIndex, float& endPos);
117     void LayoutBackward(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint,
118         int32_t& startIndex, float& startPos);
119     void MeasureMaxHeight(LayoutWrapper* layoutWrapper, LayoutConstraintF& childLayoutConstraint);
120     GridSizeType GetGridSizeType(const SizeF& frameSize) const;
121     float GetGridWidth(const BarGridColumnOptions& option, const SizeF& frameSize, int32_t columns) const;
122     float ApplyBarGridAlign(const RefPtr<TabBarLayoutProperty>& layoutProperty, const SizeF& contentSize) const;
123     void ApplySymmetricExtensible(LayoutWrapper* layoutWrapper, float allocatedWidth);
124     void ApplyLayoutMode(LayoutWrapper* layoutWrapper, float allocatedWidth);
125     float GetContentMainSize(LayoutWrapper* layoutWrapper, const SizeF& contentSize);
126     void CalculateItemWidthsForSymmetricExtensible(LayoutWrapper* layoutWrapper,
127         const std::vector<float>& spaceRequests, const std::vector<float>& leftBuffer,
128         const std::vector<float>& rightBuffer, float allocatedWidth);
129     void MeasureMask(LayoutWrapper* layoutWrapper) const;
130     void UpdateChildMarginProperty(float rightMargin, float leftMargin, const RefPtr<LayoutWrapper>& childWrapper);
131     bool GetBarAdaptiveHeight(LayoutWrapper* layoutWrapper);
132     bool GetNoMinHeightLimit(LayoutWrapper* layoutWrapper);
133     bool NeedAdaptForAging(RefPtr<FrameNode> host);
134     void SetTabBarMargin(RefPtr<LayoutWrapper> layoutWrapper, int32_t index);
135     void UpdateMaxLines(LayoutWrapper* layoutWrapper, int32_t index);
136     float GetCurrentOffset(RefPtr<TabBarLayoutProperty>& layoutProperty, ScrollableBarModeOptions& layoutStyle);
137     void CheckBorderAndPadding(SizeF& frameSize, const PaddingPropertyF& padding);
138 
139     bool isRTL_ = false;
140     Axis axis_ = Axis::NONE;
141     TabBarStyle tabBarStyle_;
142     int32_t childCount_ = 0;
143     float scrollMargin_ = 0.0f;
144     float contentMainSize_ = 0.0f;
145     float visibleChildrenMainSize_ = 0.0f;
146     float startMainPos_ = 0.0f;
147     float endMainPos_ = 0.0f;
148     float currentDelta_ = 0.0f;
149     float barGridMargin_ = 0.0f;
150     float verticalPadding_ = 0.0f;
151     std::map<int32_t, float> visibleItemLength_;
152     std::map<int32_t, ItemInfo> visibleItemPosition_;
153     std::optional<int32_t> jumpIndex_;
154     std::optional<int32_t> targetIndex_;
155     std::optional<int32_t> focusIndex_;
156     std::optional<float> maxHeight_;
157     std::optional<float> defaultHeight_;
158     bool isBarAdaptiveHeight_ = false;
159     bool isNoMinHeightLimit_ = false;
160     bool useItemWidth_ = true;
161     bool canOverScroll_ = false;
162     Dimension leftAndRightMargin_ = 0.0_vp;
163     Dimension indicatorStyleMarginTop_ = 0.0_vp;
164 };
165 } // namespace OHOS::Ace::NG
166 
167 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TABS_TAB_BAR_LAYOUT_ALGORITHM_H
168