• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/common/layout/constants.h"
23 #include "core/pipeline/base/render_node.h"
24 
25 namespace OHOS::Ace {
26 
27 struct ContentInfo {
ContentInfoContentInfo28     ContentInfo(double mainLength, double crossLength, int32_t count, const std::list<RefPtr<RenderNode>>& itemList)
29         : mainLength_(mainLength), crossLength_(crossLength), count_(count), itemList_(itemList)
30     {}
31 
32     double mainLength_ = 0.0;
33     double crossLength_ = 0.0;
34     int32_t count_ = 0;
35     std::list<RefPtr<RenderNode>> itemList_;
36     double maxBaselineDistance = 0.0;
37 };
38 
39 class ACE_EXPORT RenderWrap : public RenderNode {
40     DECLARE_ACE_TYPE(RenderWrap, RenderNode);
41 
42 public:
43     static RefPtr<RenderNode> Create();
44 
45     void Update(const RefPtr<Component>& component) override;
46 
47     void PerformLayout() override;
48 
49     Size GetLeftSize(double crossLength, double mainLeftLength, double crossLeftLength) const;
50 
51     void LayoutWholeWrap();
52 
53     Offset GetContentOffset(double totalCrossLength) const;
54 
55     void TraverseContent(const Offset& startPosition, const Offset& betweenPosition) const;
56 
57     void PositionedItem(
58         double betweenSpace, const ContentInfo& content, const Offset& position, double crossSpace) const;
59 
60     Offset GetItemMainOffset(double mainSpace) const;
61 
SetWrapLayoutSize(double mainLength,double crossLength)62     void SetWrapLayoutSize(double mainLength, double crossLength)
63     {
64         Size wrapSize;
65         if (direction_ == WrapDirection::HORIZONTAL) {
66             wrapSize = GetLayoutParam().Constrain(Size(mainLength, crossLength));
67         } else {
68             wrapSize = GetLayoutParam().Constrain(Size(crossLength, mainLength));
69         }
70         if (horizontalMeasure_ == MeasureType::PARENT) {
71             wrapSize.SetWidth(GetLayoutParam().GetMaxSize().Width());
72         }
73         if (verticalMeasure_ == MeasureType::PARENT) {
74             wrapSize.SetHeight(GetLayoutParam().GetMaxSize().Height());
75         }
76         SetLayoutSize(wrapSize);
77         LOGD("wrap::wrap layout size width:%lf, height:%lf", GetLayoutSize().Width(), GetLayoutSize().Height());
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     WrapDirection direction_ = WrapDirection::VERTICAL;
127     WrapAlignment alignment_ = WrapAlignment::START;
128     WrapAlignment mainAlignment_ = WrapAlignment::START;
129     WrapAlignment crossAlignment_ = WrapAlignment::START;
130     Dimension spacing_;
131     Dimension contentSpace_;
132     double mainLengthLimit_ = 0.0;
133     double crossLengthLimit_ = 0.0;
134     double totalMainLength_ = 0.0;
135     double totalCrossLength_ = 0.0;
136     MeasureType horizontalMeasure_ = MeasureType::CONTENT;
137     MeasureType verticalMeasure_ = MeasureType::CONTENT;
138 
139     WrapDirection dialogDirection_ = WrapDirection::HORIZONTAL;
140     bool dialogStretch_ = false;
141 
142     bool isLeftToRight_ = true;
143 };
144 
145 } // namespace OHOS::Ace
146 
147 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WRAP_RENDER_WRAP_H
148