• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         LOGD("wrap::wrap layout size width:%lf, height:%lf", GetLayoutSize().Width(), GetLayoutSize().Height());
79     }
80 
GetDialogDirection()81     WrapDirection GetDialogDirection() const
82     {
83         return dialogDirection_;
84     }
85 
GetDirection()86     WrapDirection GetDirection() const
87     {
88         return direction_;
89     }
90 
GetJustifyContent()91     WrapAlignment GetJustifyContent() const
92     {
93         return mainAlignment_;
94     }
95 
GetAlignItems()96     WrapAlignment GetAlignItems() const
97     {
98         return crossAlignment_;
99     }
100 
GetAlignContent()101     WrapAlignment GetAlignContent() const
102     {
103         return alignment_;
104     }
105 
106 protected:
107     void PerformLayoutInitialize();
108     void PlaceItemAndLog(const RefPtr<RenderNode>& node, const Offset& position, const std::string& align) const;
109     void HandleCenterAlignment(double totalCrossSpace, const RefPtr<RenderNode>& node, const Offset& position,
110         double betweenSpace, Offset& itemPositionOffset) const;
111     void HandleEndAlignment(double totalCrossSpace, const RefPtr<RenderNode>& node, const Offset& position,
112         double betweenSpace, Offset& itemPositionOffset) const;
113     void HandleStartAlignment(
114         const RefPtr<RenderNode>& item, const Offset& position, double betweenSpace, Offset& itemPositionOffset) const;
115     void HandleBaselineAlignment(double totalCrossSpace, const RefPtr<RenderNode>& node, const Offset& position,
116         double betweenSpace, Offset& itemPositionOffset) const;
117     double GetMainItemLength(const RefPtr<RenderNode>& item) const;
118     double GetCrossItemLength(const RefPtr<RenderNode>& item) const;
119 
120     void HandleDialogStretch(const LayoutParam& layoutParam);
121     std::list<ContentInfo> contentList_;
122 
123     void ClearRenderObject() override;
124     bool MaybeRelease() override;
125 
126 private:
127     void SetDefault(const RefPtr<RenderNode>& item);
128     void AddBlock(int32_t& count, const RefPtr<RenderNode>& item, std::list<RefPtr<RenderNode>>& itemsList,
129         double& currentMainLength, double& currentCrossLength, double& baselineDistance);
130     void CalculateMargin(const RefPtr<RenderNode>& item, bool& beforeIsBlock, double& beforeMarginBottom);
131     WrapDirection direction_ = WrapDirection::VERTICAL;
132     WrapAlignment alignment_ = WrapAlignment::START;
133     WrapAlignment mainAlignment_ = WrapAlignment::START;
134     WrapAlignment crossAlignment_ = WrapAlignment::START;
135     Dimension spacing_;
136     Dimension contentSpace_;
137     double mainLengthLimit_ = 0.0;
138     double crossLengthLimit_ = 0.0;
139     double totalMainLength_ = 0.0;
140     double totalCrossLength_ = 0.0;
141     MeasureType horizontalMeasure_ = MeasureType::CONTENT;
142     MeasureType verticalMeasure_ = MeasureType::CONTENT;
143 
144     WrapDirection dialogDirection_ = WrapDirection::HORIZONTAL;
145     bool dialogStretch_ = false;
146 
147     bool isLeftToRight_ = true;
148 };
149 
150 } // namespace OHOS::Ace
151 
152 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WRAP_RENDER_WRAP_H
153