• 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 class ACE_EXPORT TabBarLayoutAlgorithm : public LayoutAlgorithm {
28     DECLARE_ACE_TYPE(TabBarLayoutAlgorithm, LayoutAlgorithm);
29 
30 public:
31     TabBarLayoutAlgorithm() = default;
32     ~TabBarLayoutAlgorithm() override = default;
33 
OnReset()34     void OnReset() override {}
35     void Measure(LayoutWrapper* layoutWrapper) override;
36     void Layout(LayoutWrapper* layoutWrapper) override;
37 
GetTabItemOffset()38     const std::vector<OffsetF>& GetTabItemOffset() const
39     {
40         return tabItemOffset_;
41     }
42 
SetCurrentOffset(float currentOffset)43     void SetCurrentOffset(float currentOffset)
44     {
45         currentOffset_ = currentOffset;
46     }
47 
GetCurrentOffset()48     float GetCurrentOffset() const
49     {
50         return currentOffset_;
51     }
52 
SetChildrenMainSize(float childrenMainSize)53     void SetChildrenMainSize(float childrenMainSize)
54     {
55         childrenMainSize_ = childrenMainSize;
56     }
57 
GetChildrenMainSize()58     float GetChildrenMainSize() const
59     {
60         return childrenMainSize_;
61     }
62 
SetIndicator(int32_t indicator)63     void SetIndicator(int32_t indicator)
64     {
65         indicator_ = indicator;
66     }
67 
GetIndicator()68     int32_t GetIndicator() const
69     {
70         return indicator_;
71     }
72 
SetIsBuilder(bool isBuilder)73     void SetIsBuilder(bool isBuilder)
74     {
75         isBuilder_ = isBuilder;
76     }
77 
SetTabBarStyle(TabBarStyle tabBarStyle)78     void SetTabBarStyle(TabBarStyle tabBarStyle)
79     {
80         tabBarStyle_ = tabBarStyle;
81     }
82 
SetNeedSetCentered()83     void SetNeedSetCentered()
84     {
85         needSetCentered_ = true;
86     }
87 
SetScrollMargin(float scrollMargin)88     void SetScrollMargin(float scrollMargin)
89     {
90         scrollMargin_ = scrollMargin;
91     }
92 
GetScrollMargin()93     float GetScrollMargin()
94     {
95         return scrollMargin_;
96     }
97 
98 private:
99     Axis GetAxis(LayoutWrapper* layoutWrapper) const;
100     void UpdateChildConstraint(LayoutConstraintF& childConstraint, const RefPtr<TabBarLayoutProperty>& layoutProperty,
101         const SizeF& ideaSize, int32_t childCount, Axis axis);
102     float GetSpace(LayoutWrapper* layoutWrapper, int32_t indicator, const SizeF& frameSize, Axis axis);
103     float CalculateFrontChildrenMainSize(LayoutWrapper* layoutWrapper, int32_t indicator, Axis axis);
104     void LayoutChildren(LayoutWrapper* layoutWrapper, const SizeF& frameSize, Axis axis, OffsetF& childOffset);
105     void LayoutMask(LayoutWrapper* layoutWrapper);
106     float CalculateBackChildrenMainSize(LayoutWrapper* layoutWrapper, int32_t indicator, Axis axis);
107     void HandleFixedMode(LayoutWrapper* layoutWrapper, const SizeF& frameSize, int32_t childCount);
108     void HandleSpaceBetweenOrCenterLayoutStyle(
109         LayoutWrapper* layoutWrapper, const SizeF& frameSize, int32_t childCount);
110     void HandleAlwaysAverageSplitLayoutStyle(LayoutWrapper* layoutWrapper, const SizeF& frameSize, int32_t childCount);
111     void MeasureItemWidths(LayoutWrapper* layoutWrapper, int32_t childCount);
112     void MeasureMaxHeight(LayoutWrapper* layoutWrapper, int32_t childCount);
113     GridSizeType GetGridSizeType(const SizeF& frameSize) const;
114     float GetGridWidth(const BarGridColumnOptions& option, const SizeF& frameSize, int32_t columns) const;
115     float ApplyBarGridAlign(const RefPtr<TabBarLayoutProperty>& layoutProperty, const SizeF& frameSize) const;
116     void ApplySymmetricExtensible(LayoutWrapper* layoutWrapper, float allocatedWidth, int32_t childCount);
117     void ApplyLayoutMode(LayoutWrapper* layoutWrapper, float allocatedWidth, int32_t childCount);
118     void ConfigHorizontal(LayoutWrapper* layoutWrapper, const SizeF& frameSize, int32_t childCount);
119     float GetContentWidth(const RefPtr<TabBarLayoutProperty>& layoutProperty, const SizeF& frameSize) const;
120     void CalculateItemWidthsForSymmetricExtensible(LayoutWrapper* layoutWrapper, int32_t childCount,
121         const std::vector<float>& spaceRequests, const std::vector<float>& leftBuffer,
122         const std::vector<float>& rightBuffer, float allocatedWidth);
123     void UpdateHorizontalPadding(LayoutWrapper* layoutWrapper, float horizontalPadding) const;
124     void AdjustFixedItem(const RefPtr<LayoutWrapper>& childWrapper, const OptionalSizeF& frameSize, Axis axis) const;
125     void MeasureMask(LayoutWrapper* layoutWrapper, int32_t childCount) const;
126 
127     std::vector<OffsetF> tabItemOffset_;
128     float currentOffset_ = 0.0f;
129     float childrenMainSize_ = 0.0f; // Children total size in main axis.
130     int32_t indicator_ = 0;
131     bool isBuilder_ = false;
132     float scrollMargin_ = 0.0f;
133     float maxHeight_ = 0.0f;
134     float previousChildrenMainSize_ = 0.0f;
135 
136     TabBarStyle tabBarStyle_;
137     bool needSetCentered_ = false;
138     std::vector<float> itemWidths_;
139     bool useItemWidth_ = true;
140 };
141 } // namespace OHOS::Ace::NG
142 
143 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TABS_TAB_BAR_LAYOUT_ALGORITHM_H
144