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_FLEX_RENDER_FLEX_ITEM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FLEX_RENDER_FLEX_ITEM_H
18 
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/common/layout/grid_column_info.h"
21 #include "core/components/proxy/render_proxy.h"
22 
23 namespace OHOS::Ace {
24 
25 class ACE_EXPORT RenderFlexItem final : public RenderProxy {
26     DECLARE_ACE_TYPE(RenderFlexItem, RenderProxy);
27 
28 public:
29     static RefPtr<RenderNode> Create();
30 
OnAttachContext()31     void OnAttachContext() override
32     {
33         positionParam_.left.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
34             auto renderFlexItem = weak.Upgrade();
35             if (renderFlexItem) {
36                 renderFlexItem->MarkNeedLayout();
37             }
38         });
39         positionParam_.right.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
40             auto renderFlexItem = weak.Upgrade();
41             if (renderFlexItem) {
42                 renderFlexItem->MarkNeedLayout();
43             }
44         });
45         positionParam_.top.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
46             auto renderFlexItem = weak.Upgrade();
47             if (renderFlexItem) {
48                 renderFlexItem->MarkNeedLayout();
49             }
50         });
51         positionParam_.bottom.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] {
52             auto renderFlexItem = weak.Upgrade();
53             if (renderFlexItem) {
54                 renderFlexItem->MarkNeedLayout();
55             }
56         });
57     }
58 
59     void Update(const RefPtr<Component>& component) override;
60 
61     void PerformLayout() override;
62 
GetFlexGrow()63     double GetFlexGrow() const
64     {
65         return flexGrow_;
66     }
67 
SetFlexGrow(double flexGrow)68     void SetFlexGrow(double flexGrow)
69     {
70         flexGrow_ = flexGrow;
71     }
72 
GetFlexShrink()73     double GetFlexShrink() const
74     {
75         return flexShrink_;
76     }
77 
SetFlexShrink(double flexShrink)78     void SetFlexShrink(double flexShrink)
79     {
80         flexShrink_ = flexShrink;
81     }
82 
83     // should not used in Update.
GetFlexBasisToPx()84     double GetFlexBasisToPx() const
85     {
86         auto basis = NormalizeToPx(flexBasis_);
87         return basis;
88     }
89 
90     // should not used in Update.
GetFlexBasis()91     const Dimension& GetFlexBasis() const
92     {
93         return flexBasis_;
94     }
95 
SetFlexBasis(const Dimension & flexBasis)96     void SetFlexBasis(const Dimension& flexBasis)
97     {
98         flexBasis_ = flexBasis;
99     }
100 
SetFlexBasis(double flexBasis)101     void SetFlexBasis(double flexBasis)
102     {
103         flexBasis_ = Dimension(flexBasis);
104     }
105 
GetStretchFlag()106     bool GetStretchFlag() const
107     {
108         return canStretch_;
109     }
110 
GetAlignSelf()111     FlexAlign GetAlignSelf() const
112     {
113         return alignSelf_;
114     }
115 
SetAlignSelf(FlexAlign alignSelf)116     void SetAlignSelf(FlexAlign alignSelf)
117     {
118         alignSelf_ = alignSelf;
119     }
120 
GetNormalizedConstraints()121     LayoutParam GetNormalizedConstraints() const
122     {
123         Size min = Size(NormalizePercentToPx(minWidth_, false), NormalizePercentToPx(minHeight_, true));
124         Size max = Size(NormalizePercentToPx(maxWidth_, false), NormalizePercentToPx(maxHeight_, true));
125         if (LessNotEqual(max.Width(), min.Width())) {
126             max.SetWidth(min.Width());
127         }
128         if (LessNotEqual(max.Height(), min.Height())) {
129             max.SetHeight(min.Height());
130         }
131         LayoutParam constraint = LayoutParam(max, min);
132         if (!constraint.IsValid()) {
133             return LayoutParam(Size(Size::INFINITE_SIZE, Size::INFINITE_SIZE), Size());
134         }
135         return constraint;
136     }
137 
IsHidden()138     bool IsHidden() const
139     {
140         return isHidden_;
141     }
142 
MustStretch()143     bool MustStretch() const
144     {
145         return mustStretch_;
146     }
GetDisplayType()147     DisplayType GetDisplayType() const
148     {
149         return displayType_;
150     }
151 
SetAlignRules(const std::map<AlignDirection,AlignRule> & alignRules)152     void SetAlignRules(const std::map<AlignDirection, AlignRule>& alignRules)
153     {
154         alignRules_ = alignRules;
155     }
156 
GetAlignRules()157     const std::map<AlignDirection, AlignRule>& GetAlignRules() const
158     {
159         return alignRules_;
160     }
161 
SetId(const std::string & id)162     void SetId(const std::string& id)
163     {
164         id_ = id;
165     }
166 
GetId()167     const std::string& GetId() const
168     {
169         return id_;
170     }
171 
172     void SetAligned(const AlignDirection& alignDirection);
173 
GetTwoHorizontalDirectionAligned()174     bool GetTwoHorizontalDirectionAligned() const
175     {
176         return (relativeLeftAligned_ && relativeRightAligned_) || (relativeRightAligned_ && relativeMiddleAligned_) ||
177             (relativeLeftAligned_ && relativeMiddleAligned_);
178     }
179 
GetTwoVerticalDirectionAligned()180     bool GetTwoVerticalDirectionAligned() const
181     {
182         return (relativeTopAligned_ && relativeCenterAligned_) || (relativeBottomAligned_ && relativeCenterAligned_) ||
183             (relativeTopAligned_ && relativeBottomAligned_);
184     }
185 
186     bool GetAligned(AlignDirection& alignDirection);
187 
188     double GetAlignValue(AlignDirection& alignDirection);
189 
190     void SetAlignValue(AlignDirection& alignDirection, double value);
191 
192     void Dump() override;
193 
194 protected:
195     void ClearRenderObject() override;
196     bool MaybeRelease() override;
197 
198 private:
199     double flexGrow_ = 0.0;
200     double flexShrink_ = 0.0;
201     Dimension flexBasis_ = 0.0_px;
202     bool canStretch_ = true;
203     bool mustStretch_ = false;
204     bool isHidden_ = false;
205     RefPtr<GridColumnInfo> gridColumnInfo_;
206 
207     Dimension minWidth_ = Dimension();
208     Dimension minHeight_ = Dimension();
209     Dimension maxWidth_ = Dimension(Size::INFINITE_SIZE);
210     Dimension maxHeight_ = Dimension(Size::INFINITE_SIZE);
211     FlexAlign alignSelf_ = FlexAlign::AUTO;
212     DisplayType displayType_ = DisplayType::NO_SETTING;
213     std::map<AlignDirection, AlignRule> alignRules_;
214     bool relativeLeftAligned_ = false;
215     bool relativeRightAligned_ = false;
216     bool relativeMiddleAligned_ = false;
217     bool relativeTopAligned_ = false;
218     bool relativeBottomAligned_ = false;
219     bool relativeCenterAligned_ = false;
220     double relativeLeft_ = 0.0;
221     double relativeRight_ = 0.0;
222     double relativeMiddle_ = 0.0;
223     double relativeTop_ = 0.0;
224     double relativeBottom_ = 0.0;
225     double relativeCenter_ = 0.0;
226     std::string id_;
227 };
228 
229 } // namespace OHOS::Ace
230 
231 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FLEX_RENDER_FLEX_ITEM_H
232