1 /*
2  * Copyright (c) 2021-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_WRAP_RENDER_WRAP_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WRAP_RENDER_WRAP_H
18 
19 #include <list>
20 #include <vector>
21 
22 #include "core/components/box/render_box_base.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/pipeline/base/render_node.h"
25 
26 namespace OHOS::Ace {
27 
28 struct ContentInfo {
ContentInfoContentInfo29     ContentInfo(double mainLength, double crossLength, int32_t count, const std::list<RefPtr<RenderNode>>& itemList)
30         : mainLength_(mainLength), crossLength_(crossLength), count_(count), itemList_(itemList)
31     {}
32 
33     double mainLength_ = 0.0;
34     double crossLength_ = 0.0;
35     int32_t count_ = 0;
36     std::list<RefPtr<RenderNode>> itemList_;
37     double maxBaselineDistance = 0.0;
38 };
39 
40 class ACE_EXPORT RenderWrap : public RenderNode {
41     DECLARE_ACE_TYPE(RenderWrap, RenderNode);
42 
43 public:
44     static RefPtr<RenderNode> Create();
45 
46     void Update(const RefPtr<Component>& component) override;
47 
48     void PerformLayout() override;
49 
50     Size GetLeftSize(double crossLength, double mainLeftLength, double crossLeftLength) const;
51 
52     void LayoutWholeWrap();
53 
54     Offset GetContentOffset(double totalCrossLength) const;
55 
56     void TraverseContent(const Offset& startPosition, const Offset& betweenPosition) const;
57 
58     void PositionedItem(
59         double betweenSpace, const ContentInfo& content, const Offset& position, double crossSpace) const;
60 
61     Offset GetItemMainOffset(double mainSpace) const;
62 
SetWrapLayoutSize(double mainLength,double crossLength)63     void SetWrapLayoutSize(double mainLength, double crossLength)
64     {
65         Size wrapSize;
66         if (direction_ == WrapDirection::HORIZONTAL || direction_ == WrapDirection::HORIZONTAL_REVERSE) {
67             wrapSize = GetLayoutParam().Constrain(Size(mainLength, crossLength));
68         } else {
69             wrapSize = GetLayoutParam().Constrain(Size(crossLength, mainLength));
70         }
71         if (horizontalMeasure_ == MeasureType::PARENT) {
72             wrapSize.SetWidth(GetLayoutParam().GetMaxSize().Width());
73         }
74         if (verticalMeasure_ == MeasureType::PARENT) {
75             wrapSize.SetHeight(GetLayoutParam().GetMaxSize().Height());
76         }
77         SetLayoutSize(wrapSize);
78     }
79 
GetDialogDirection()80     WrapDirection GetDialogDirection() const
81     {
82         return dialogDirection_;
83     }
84 
GetDirection()85     WrapDirection GetDirection() const
86     {
87         return direction_;
88     }
89 
GetJustifyContent()90     WrapAlignment GetJustifyContent() const
91     {
92         return mainAlignment_;
93     }
94 
GetAlignItems()95     WrapAlignment GetAlignItems() const
96     {
97         return crossAlignment_;
98     }
99 
GetAlignContent()100     WrapAlignment GetAlignContent() const
101     {
102         return alignment_;
103     }
104 
105 protected:
106     void PerformLayoutInitialize();
107     void PlaceItemAndLog(const RefPtr<RenderNode>& node, const Offset& position, const std::string& align) const;
108     void HandleCenterAlignment(double totalCrossSpace, const RefPtr<RenderNode>& node, const Offset& position,
109         double betweenSpace, Offset& itemPositionOffset) const;
110     void HandleEndAlignment(double totalCrossSpace, const RefPtr<RenderNode>& node, const Offset& position,
111         double betweenSpace, Offset& itemPositionOffset) const;
112     void HandleStartAlignment(
113         const RefPtr<RenderNode>& item, const Offset& position, double betweenSpace, Offset& itemPositionOffset) const;
114     void HandleBaselineAlignment(double totalCrossSpace, const RefPtr<RenderNode>& node, const Offset& position,
115         double betweenSpace, Offset& itemPositionOffset) const;
116     double GetMainItemLength(const RefPtr<RenderNode>& item) const;
117     double GetCrossItemLength(const RefPtr<RenderNode>& item) const;
118 
119     void HandleDialogStretch(const LayoutParam& layoutParam);
120     std::list<ContentInfo> contentList_;
121 
122     void ClearRenderObject() override;
123     bool MaybeRelease() override;
124 
125 private:
126     void SetDefault(const RefPtr<RenderNode>& item);
127     void AddBlock(int32_t& count, const RefPtr<RenderNode>& item, std::list<RefPtr<RenderNode>>& itemsList,
128         double& currentMainLength, double& currentCrossLength, double& baselineDistance);
129     void CalculateMargin(const RefPtr<RenderNode>& item, bool& beforeIsBlock, double& beforeMarginBottom);
130     WrapDirection direction_ = WrapDirection::VERTICAL;
131     WrapAlignment alignment_ = WrapAlignment::START;
132     WrapAlignment mainAlignment_ = WrapAlignment::START;
133     WrapAlignment crossAlignment_ = WrapAlignment::START;
134     Dimension spacing_;
135     Dimension contentSpace_;
136     double mainLengthLimit_ = 0.0;
137     double crossLengthLimit_ = 0.0;
138     double totalMainLength_ = 0.0;
139     double totalCrossLength_ = 0.0;
140     MeasureType horizontalMeasure_ = MeasureType::CONTENT;
141     MeasureType verticalMeasure_ = MeasureType::CONTENT;
142 
143     WrapDirection dialogDirection_ = WrapDirection::HORIZONTAL;
144     bool dialogStretch_ = false;
145 
146     bool isLeftToRight_ = true;
147 };
148 
149 } // namespace OHOS::Ace
150 
151 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WRAP_RENDER_WRAP_H
152