• 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_FLEX_RENDER_FLEX_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FLEX_RENDER_FLEX_H
18 
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/common/properties/text_style.h"
21 #include "core/components/flex/render_flex_item.h"
22 #include "core/pipeline/base/render_node.h"
23 
24 namespace OHOS::Ace {
25 
26 class FlexComponent;
27 
28 struct FlexItemProperties {
29     double totalShrink = 0.0;
30     double totalGrow = 0.0;
31     RefPtr<RenderFlexItem> lastShrinkChild;
32     RefPtr<RenderFlexItem> lastGrowChild;
33 };
34 
35 struct BaselineProperties {
36     double maxBaselineDistance = 0.0;
37     double maxDistanceAboveBaseline = 0.0;
38     double maxDistanceBelowBaseline = 0.0;
39 };
40 
41 struct MagicLayoutNode {
42     LayoutParam innerLayout = LayoutParam(Size(), Size());
43     RefPtr<RenderNode> node;
44 };
45 
46 class ACE_EXPORT RenderFlex : public RenderNode {
47     DECLARE_ACE_TYPE(RenderFlex, RenderNode);
48 
49 public:
50     static RefPtr<RenderNode> Create();
51 
52     void Update(const RefPtr<Component>& component) override;
53 
54     void PerformLayout() override;
55 
GetDirection()56     FlexDirection GetDirection() const
57     {
58         return direction_;
59     }
60 
GetJustifyContent()61     FlexAlign GetJustifyContent() const
62     {
63         return mainAxisAlign_;
64     }
65 
GetAlignItems()66     FlexAlign GetAlignItems() const
67     {
68         return crossAxisAlign_;
69     }
70 
GetSpace()71     double GetSpace() const
72     {
73         return space_;
74     }
75 
GetInspectorSpace()76     Dimension GetInspectorSpace() const
77     {
78         return inspectorSpace_;
79     }
80 
GetBaseline()81     TextBaseline GetBaseline() const
82     {
83         return textBaseline_;
84     }
85 
IsStretchToParent()86     bool IsStretchToParent() const
87     {
88         return stretchToParent_;
89     }
90 
GetAlignItem()91     FlexAlign GetAlignItem() const
92     {
93         return crossAxisAlign_;
94     }
95 
GetAlignPtr()96     AlignDeclarationPtr GetAlignPtr() const
97     {
98         return alignPtr_;
99     }
100     double GetBaselineDistance(TextBaseline baseline) override;
101 
102     Size GetChildViewPort() override;
103 
104     void OnChildRemoved(const RefPtr<RenderNode>& child) override;
105 
106     void Dump() override;
107 
IsChildOverflow()108     bool IsChildOverflow() const
109     {
110         return isChildOverflow_;
111     }
112 
113     bool GetAlignDeclarationOffset(AlignDeclarationPtr alignDeclarationPtr, Offset& offset) const override;
114 
115     bool CheckIfNeedLayoutAgain() override;
116 
117 protected:
118     void ClearRenderObject() override;
119     bool MaybeRelease() override;
120 
121     Overflow overflow_ = Overflow::CLIP;
122     bool isChildOverflow_ = false;
123 
124 private:
125     void PerformLayoutInWeightMode();
126     void PerformLayoutInIndexMode();
127     void PerformLayoutInItemMode();
128 
129     void LayoutMagicNodes(BaselineProperties& baselineProperties);
130     void RelayoutForStretchMagicNode();
131     void RelayoutForStretchFlexNode(const FlexItemProperties& flexItemProperties);
132     void LayoutHiddenNodes();
133 
134     /**
135      * This function is used to travel a single flex item in the first time layout to determine the flexItemProperties.
136      *
137      * @param flexItem The item under operation.
138      * @param flexItemProperties Store all the flex item properties.
139      */
140     void LayoutFlexItem(RefPtr<RenderFlexItem>& flexItem, FlexItemProperties& flexItemProperties);
141 
142     void RelayoutFlexItem(const RefPtr<RenderFlexItem>& flexItem, double flexSize, BaselineProperties& baselineProps,
143         double& allocatedFlexSpace);
144 
145     void LayoutInfinityChild(const RefPtr<RenderNode>& item, double mainSize, BaselineProperties& baselineProperties);
146 
147     /**
148      * This function is used to determine all the items' sizes in the second time layout according to the
149      * flexItemProperties. The size of Flex(Row/Column) will be resized by the flex item.
150      *
151      * @param flexItemProps FlexItemProperties.
152      * @param baselineProps The distance properties of baseline.
153      */
154     void ResizeItems(const FlexItemProperties& flexItemProps, BaselineProperties& baselineProps);
155 
156     void DetermineSelfSize(MainAxisSize mainAxisSize, bool useViewPort);
157 
158     void DetermineItemsPosition(const BaselineProperties& baselineProperties);
159 
160     /**
161      * This function is used to calculate the frontSpace and the betweenSpace according to mainAxisAlign and
162      * remainSpace. Only when the allocated size is smaller than the maxMainSize, it can be effective. In other word, it
163      * requires all the items are not flex items.
164      *
165      * @param remainSpace Remain space.
166      * @param frontSpace The space before the first item.
167      * @param betweenSpace The space between two items.
168      */
169     void CalculateSpace(double remainSpace, double& frontSpace, double& betweenSpace) const;
170 
171     /**
172      * This function is used to place all the items according to the size and the space.
173      *
174      * @param frontSpace The space before the first item.
175      * @param betweenSpace The space between two items.
176      * @param baselineProperties The distance properties of baseline.
177      */
178     void PlaceChildren(double frontSpace, double betweenSpace, const BaselineProperties& baselineProperties);
179 
180     void LayoutAbsoluteChildren();
181 
182     void CheckBaselineProperties(const RefPtr<RenderNode>& item, BaselineProperties& baselineProperties);
183 
184     LayoutParam MakeStretchInnerLayoutParam(const RefPtr<RenderNode>& item) const;
185     LayoutParam MakeLayoutParamWithLimit(double minMainLimit, double maxMainLimit, bool isStretch) const;
186     LayoutParam MakeConstrainedLayoutParam(double mainFlexExtent, const LayoutParam& constraint, bool isStretch,
187         bool supportZerolayout = false) const;
188 
189     void ResizeByItem(const RefPtr<RenderNode>& item, double &allocatedSize);
190     void CheckSizeValidity(const RefPtr<RenderNode>& item);
191     Size GetConstrainedSize(double mainSize);
192     double GetAvailableMainSize();
193     double GetMainSize(const RefPtr<RenderNode>& item) const;
194     double GetCrossSize(const RefPtr<RenderNode>& item) const;
195     double GetStretchCrossLimit() const;
196     void InitFlexProperties();
197     void TravelChildrenFlexProps();
198     void ClearChildrenLists();
199     FlexAlign GetSelfAlign(const RefPtr<RenderNode>& item) const;
200     TextDirection AdjustTextDirectionByDir();
201     void UpdateAccessibilityAttr();
202     void OnPaintFinish() override;
203 
204     bool IsStartTopLeft(FlexDirection direction, TextDirection textDir) const;
205 
206     void PerformItemAlign(std::list<RefPtr<RenderNode>>& nodelist);
207 
208     FlexDirection direction_ = FlexDirection::ROW;
209     FlexAlign mainAxisAlign_ = FlexAlign::FLEX_START;
210     FlexAlign crossAxisAlign_ = FlexAlign::FLEX_START;
211     MainAxisSize mainAxisSize_ = MainAxisSize::MAX;
212     CrossAxisSize crossAxisSize_ = CrossAxisSize::MIN;
213     TextBaseline textBaseline_ = TextBaseline::ALPHABETIC;
214     FlexLayoutMode layoutMode_ = FlexLayoutMode::FLEX_ITEM_MODE;
215     bool stretchToParent_ = false;
216 
217     double mainSize_ = 0.0;
218     double crossSize_ = 0.0;
219     double allocatedSize_ = 0.0;
220 
221     double space_ = 0.0;
222     Dimension inspectorSpace_;
223 
224     std::set<RefPtr<RenderNode>> infinityLayoutNodes_;
225     std::set<RefPtr<RenderNode>> absoluteNodes_;
226     std::list<RefPtr<RenderNode>> relativeNodes_;
227     std::list<RefPtr<RenderFlexItem>> stretchNodes_;
228     // use map to order the magic Nodes
229     std::map<int32_t, std::list<MagicLayoutNode>> magicNodes_;
230     std::map<int32_t, double> magicWeightMaps_;
231     std::set<RefPtr<RenderNode>> displayNodes_; // displayNodes_ used to record all display nodes in magic layout
232 
233     RefPtr<RenderNode> scrollNode;
234     bool isMainInfinite_ = false;
235     bool isCrossInfinite_ = false;
236     bool useViewPort_ = false;
237     bool containsNavigation_ = false;
238     double navigationMainSize_ = 0.0;
239     int32_t validSizeCount_ = 0;
240     double totalFlexWeight_ = 0.0;
241     int32_t maxDisplayIndex_ = 0;
242     bool useOldLayoutVersion_ = false;
243 
244     AlignDeclarationPtr alignPtr_ = nullptr;
245 };
246 
247 } // namespace OHOS::Ace
248 
249 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FLEX_RENDER_FLEX_H
250