• 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_WRAP_WRAP_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WRAP_WRAP_LAYOUT_ALGORITHM_H
18 
19 #include <string>
20 
21 #include "base/geometry/ng/size_t.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/base/geometry_node.h"
25 #include "core/components_ng/layout/layout_algorithm.h"
26 #include "core/components_ng/layout/layout_wrapper.h"
27 #include "core/components_ng/pattern/flex/flex_layout_algorithm.h"
28 #include "core/components_ng/pattern/flex/flex_layout_styles.h"
29 #include "core/components_ng/pattern/text/text_layout_property.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 struct ContentInfo {
ContentInfoContentInfo34     ContentInfo(float main, float cross, int32_t total, const std::list<RefPtr<LayoutWrapper>>& nodeList)
35         : mainLength(main), crossLength(cross), count(total), itemList(nodeList)
36     {}
37 
38     float mainLength = 0.0f;
39     float crossLength = 0.0f;
40     int32_t count = 0;
41     std::list<RefPtr<LayoutWrapper>> itemList;
42     float spaceBetween = 0.0f;
43     float maxBaselineDistance = 0.0f;
44 
ToStringContentInfo45     std::string ToString() const
46     {
47         std::string result;
48         result.append("main length: ");
49         result.append(std::to_string(mainLength));
50         result.append(", cross length: ");
51         result.append(std::to_string(crossLength));
52         result.append(", space between: ");
53         result.append(std::to_string(spaceBetween));
54         result.append(", child count ");
55         result.append(std::to_string(count));
56         result.append(", max baseline distance ");
57         result.append(std::to_string(maxBaselineDistance));
58         return result;
59     }
60 };
61 
62 class ACE_EXPORT WrapLayoutAlgorithm : public LayoutAlgorithm {
63     DECLARE_ACE_TYPE(WrapLayoutAlgorithm, LayoutAlgorithm);
64 
65 public:
66     WrapLayoutAlgorithm() = default;
WrapLayoutAlgorithm(bool stretch)67     explicit WrapLayoutAlgorithm(bool stretch) : isDialogStretch_(stretch) {};
68     ~WrapLayoutAlgorithm() override = default;
69 
70     void Measure(LayoutWrapper* layoutWrapper) override;
71 
72     void Layout(LayoutWrapper* layoutWrapper) override;
73 
74 private:
75     void PerformLayoutInitialize(const RefPtr<LayoutProperty>& layoutProp);
76     void HandleDialogStretch();
77     SizeF GetLeftSize(float crossLength, float mainLeftLength, float crossLeftLength);
78     void LayoutWholeWrap(
79         OffsetF& startPosition, OffsetF& spaceBetweenContentsOnCrossAxis, LayoutWrapper* layoutWrapper);
80 
81     float GetItemMainAxisLength(const RefPtr<GeometryNode>& item) const;
82     float GetItemCrossAxisLength(const RefPtr<GeometryNode>& item) const;
83     float GetMainAxisLengthOfSize(const SizeF& size) const;
84     float GetCrossAxisLengthOfSize(const SizeF& size) const;
85     float GetMainAxisOffset(const OffsetF& offset) const;
86     float GetCrossAxisOffset(const OffsetF& offset) const;
87     SizeF GetMainAxisRemainSpace(float totalMainLength) const;
88     SizeF GetCrossAxisRemainSpace(float totalCrossLength) const;
89 
90     void CalcItemMainAxisStartAndSpaceBetween(
91         OffsetF& startPosition, OffsetF& spaceBetweenItemsOnMainAxis, const ContentInfo& content);
92     float CalcItemCrossAxisOffset(
93         const ContentInfo& content, const OffsetF& contentOffset, const RefPtr<GeometryNode>& node);
94 
95     void StretchItemsInContent(LayoutWrapper* layoutWrapper, const ContentInfo& content);
96     void TraverseContent(const OffsetF& startPosition, const OffsetF& spaceBetweenContentsOnCrossAxis);
97     OffsetF GetItemMainOffset(float mainSpace) const;
98     void LayoutContent(const ContentInfo& content, const OffsetF& position);
99 
100     void AddExtraSpaceToStartPosition(OffsetF& startPosition, float extraSpace, bool onMainAxis) const;
101     void AddPaddingToStartPosition(OffsetF& startPosition) const;
102     void GetFlexItemProperties(const ContentInfo& content, FlexItemProperties& flexItemProperties);
103     void CalcFlexGrowLayout(
104         const RefPtr<LayoutWrapper>& itemWrapper, const FlexItemProperties& flexItemProperties, float remainSpace);
105 
106     WrapDirection direction_ = WrapDirection::VERTICAL;
107     WrapAlignment alignment_ = WrapAlignment::START;
108     WrapAlignment mainAlignment_ = WrapAlignment::START;
109     WrapAlignment crossAlignment_ = WrapAlignment::START;
110 
111     bool isHorizontal_ = true;
112     bool isReverse_ = false;
113     bool isDialogStretch_ = false;
114     float totalMainLength_ = 0.0f;
115     float totalCrossLength_ = 0.0f;
116     Dimension spacing_;
117     Dimension contentSpace_;
118     SizeF constraintMaxSize_;
119     PaddingPropertyF padding_;
120     SizeF frameSize_;
121     OffsetF frameOffset_;
122     bool hasIdealWidth_ = false;
123     bool hasIdealHeight_ = false;
124 
125     // Should be clear after Layout.
126     std::list<ContentInfo> contentList_;
127 
128     float mainLengthLimit_ = 0.0f;
129     float crossLengthLimit_ = 0.0f;
130     float currentMainLength_ = 0.0f;
131     std::list<RefPtr<LayoutWrapper>> outOfLayoutChildren_;
132 
133     ACE_DISALLOW_COPY_AND_MOVE(WrapLayoutAlgorithm);
134 };
135 } // namespace OHOS::Ace::NG
136 
137 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WRAP_WRAP_LAYOUT_ALGORITHM_H
138