• 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_FLEX_FLEX_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FLEX_FLEX_LAYOUT_ALGORITHM_H
18 
19 #include "core/components/common/layout/constants.h"
20 #include "core/components_ng/layout/layout_algorithm.h"
21 #include "core/components_ng/layout/layout_wrapper.h"
22 #include "core/components_ng/pattern/flex/flex_layout_styles.h"
23 
24 namespace OHOS::Ace::NG {
25 
26 struct FlexItemProperties {
27     float totalShrink = 0.0f;
28     float totalGrow = 0.0f;
29     RefPtr<LayoutWrapper> lastShrinkChild;
30     RefPtr<LayoutWrapper> lastGrowChild;
31 };
32 
33 struct MagicLayoutNode {
34     LayoutConstraintF layoutConstraint;
35     RefPtr<LayoutWrapper> layoutWrapper;
36     OptionalSizeF calcSize;
37 };
38 
39 struct BaselineProperties {
40     float maxBaselineDistance = 0.0f;
41     float maxDistanceAboveBaseline = 0.0f;
42     float maxDistanceBelowBaseline = 0.0f;
43 
ResetBaselineProperties44     void Reset()
45     {
46         maxBaselineDistance = 0.0f;
47         maxDistanceAboveBaseline = 0.0f;
48         maxDistanceBelowBaseline = 0.0f;
49     }
50 };
51 
52 class ACE_EXPORT FlexLayoutAlgorithm : public LayoutAlgorithm {
53     DECLARE_ACE_TYPE(FlexLayoutAlgorithm, LayoutAlgorithm);
54 
55 public:
56     FlexLayoutAlgorithm() = default;
57     ~FlexLayoutAlgorithm() override = default;
58 
59     void Measure(LayoutWrapper* layoutWrapper) override;
60 
61     void Layout(LayoutWrapper* layoutWrapper) override;
62 
SetLinearLayoutFeature()63     void SetLinearLayoutFeature()
64     {
65         isLinearLayoutFeature_ = true;
66     }
67 
68 private:
69     void InitFlexProperties(LayoutWrapper* layoutWrapper);
70     void TravelChildrenFlexProps(LayoutWrapper* layoutWrapper, const SizeF& realSize);
71     void UpdateAllocatedSize(const RefPtr<LayoutWrapper>& layoutWrapper, float& crossAxisSize);
72     float GetChildMainAxisSize(const RefPtr<LayoutWrapper>& layoutWrapper) const;
73     float GetChildCrossAxisSize(const RefPtr<LayoutWrapper>& layoutWrapper) const;
74     float GetSelfCrossAxisSize(const RefPtr<LayoutWrapper>& layoutWrapper) const;
75     void CheckSizeValidity(const RefPtr<LayoutWrapper>& layoutWrapper);
76     void CheckBaselineProperties(const RefPtr<LayoutWrapper>& layoutWrapper);
77     void CalculateSpace(float remainSpace, float& frontSpace, float& betweenSpace) const;
78     void PlaceChildren(
79         LayoutWrapper* layoutWrapper, float frontSpace, float betweenSpace, const OffsetF& paddingOffset);
80     FlexAlign GetSelfAlign(const RefPtr<LayoutWrapper>& layoutWrapper) const;
81     float GetStretchCrossAxisLimit() const;
82     void MeasureOutOfLayoutChildren(LayoutWrapper* layoutWrapper);
83     void MeasureAndCleanMagicNodes(FlexItemProperties& flexItemProperties);
84     void SecondaryMeasureByProperty(FlexItemProperties& flexItemProperties, LayoutWrapper* layoutWrapper);
85     void UpdateLayoutConstraintOnMainAxis(LayoutConstraintF& layoutConstraint, float size);
86     void UpdateLayoutConstraintOnCrossAxis(LayoutConstraintF& layoutConstraint, float size);
87     void AdjustTotalAllocatedSize(LayoutWrapper* layoutWrapper);
88 
89     OptionalSizeF realSize_;
90     float mainAxisSize_ = 0.0f;
91     float crossAxisSize_ = 0.0f;
92     float selfIdealCrossAxisSize_ = -1.0f;
93     float allocatedSize_ = 0.0f;
94     float space_ = 0.0f;
95     float totalFlexWeight_ = 0.0f;
96     int32_t maxDisplayPriority_ = 0;
97     int32_t validSizeCount_ = 0;
98     FlexAlign crossAxisAlign_ = FlexAlign::FLEX_START;
99     FlexAlign mainAxisAlign_ = FlexAlign::FLEX_START;
100 
101     std::map<int32_t, std::list<MagicLayoutNode>> magicNodes_;
102     std::map<int32_t, float> magicNodeWeights_;
103     std::list<MagicLayoutNode> secondaryMeasureList_;
104     std::list<RefPtr<LayoutWrapper>> outOfLayoutChildren_;
105 
106     FlexDirection direction_ = FlexDirection::ROW;
107     friend class LinearLayoutUtils;
108     BaselineProperties baselineProperties_;
109     bool isLinearLayoutFeature_ = false;
110     bool isInfiniteLayout_ = false;
111     TextDirection textDir_ = TextDirection::LTR;
112 
113     ACE_DISALLOW_COPY_AND_MOVE(FlexLayoutAlgorithm);
114 };
115 } // namespace OHOS::Ace::NG
116 
117 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FLEX_FLEX_LAYOUT_ALGORITHM_H
118