• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_NG_LAYOUTS_LAYOUT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_PROPERTY_H
18 
19 #include <cstddef>
20 #include <memory>
21 #include <optional>
22 
23 #include "base/geometry/dimension.h"
24 #include "base/geometry/ng/size_t.h"
25 #include "base/memory/ace_type.h"
26 #include "base/memory/referenced.h"
27 #include "base/utils/macros.h"
28 #include "base/utils/noncopyable.h"
29 #include "base/utils/utils.h"
30 #include "core/components/common/layout/constants.h"
31 #include "core/components_ng/event/focus_hub.h"
32 #include "core/components_ng/property/border_property.h"
33 #include "core/components_ng/property/calc_length.h"
34 #include "core/components_ng/property/flex_property.h"
35 #include "core/components_ng/property/geometry_property.h"
36 #include "core/components_ng/property/grid_property.h"
37 #include "core/components_ng/property/layout_constraint.h"
38 #include "core/components_ng/property/magic_layout_property.h"
39 #include "core/components_ng/property/measure_property.h"
40 #include "core/components_ng/property/position_property.h"
41 #include "core/components_ng/property/property.h"
42 #include "core/components_ng/property/safe_area_insets.h"
43 #include "core/pipeline/base/element_register.h"
44 #include "core/pipeline_ng/ui_task_scheduler.h"
45 
46 namespace OHOS::Ace::NG {
47 
48 class FrameNode;
49 
50 class ACE_FORCE_EXPORT LayoutProperty : public Property {
51     DECLARE_ACE_TYPE(LayoutProperty, Property);
52 
53 public:
54     LayoutProperty() = default;
55 
56     ~LayoutProperty() override = default;
57 
58     virtual RefPtr<LayoutProperty> Clone() const;
59 
60     virtual void Reset();
61 
62     virtual void ToJsonValue(std::unique_ptr<JsonValue>& json) const;
63 
64     virtual void FromJson(const std::unique_ptr<JsonValue>& json);
65 
GetLayoutConstraint()66     const std::optional<LayoutConstraintF>& GetLayoutConstraint() const
67     {
68         return layoutConstraint_;
69     }
70 
GetContentLayoutConstraint()71     const std::optional<LayoutConstraintF>& GetContentLayoutConstraint() const
72     {
73         return contentConstraint_;
74     }
75 
GetMagicItemProperty()76     const std::unique_ptr<MagicItemProperty>& GetMagicItemProperty() const
77     {
78         return magicItemProperty_;
79     }
80 
GetPaddingProperty()81     const std::unique_ptr<PaddingProperty>& GetPaddingProperty() const
82     {
83         return padding_;
84     }
85 
GetMarginProperty()86     const std::unique_ptr<MarginProperty>& GetMarginProperty() const
87     {
88         return margin_;
89     }
90 
GetBorderWidthProperty()91     const std::unique_ptr<BorderWidthProperty>& GetBorderWidthProperty() const
92     {
93         return borderWidth_;
94     }
95 
GetPositionProperty()96     const std::unique_ptr<PositionProperty>& GetPositionProperty() const
97     {
98         return positionProperty_;
99     }
100 
GetCalcLayoutConstraint()101     const std::unique_ptr<MeasureProperty>& GetCalcLayoutConstraint() const
102     {
103         return calcLayoutConstraint_;
104     }
105 
GetFlexItemProperty()106     const std::unique_ptr<FlexItemProperty>& GetFlexItemProperty() const
107     {
108         return flexItemProperty_;
109     }
110 
GetLayoutDirection()111     TextDirection GetLayoutDirection() const
112     {
113         return layoutDirection_.value_or(TextDirection::AUTO);
114     }
115 
GetGeometryTransition()116     RefPtr<GeometryTransition> GetGeometryTransition() const
117     {
118         return geometryTransition_.Upgrade();
119     }
120 
121     MeasureType GetMeasureType(MeasureType defaultType = MeasureType::MATCH_CONTENT) const
122     {
123         return measureType_.value_or(defaultType);
124     }
125 
126     void UpdatePadding(const PaddingProperty& value);
127 
128     void UpdateMargin(const MarginProperty& value);
129 
130     void UpdateBorderWidth(const BorderWidthProperty& value);
131 
132     void UpdateAlignment(Alignment value);
133 
134     void UpdateLayoutWeight(float value);
135 
136     void UpdateLayoutDirection(TextDirection value);
137 
138     void UpdateGeometryTransition(const std::string& id, bool followWithoutTransition = false);
139 
140     void UpdateAspectRatio(float ratio);
141     void ResetAspectRatio();
142 
143     bool HasAspectRatio() const;
144     float GetAspectRatio() const;
145 
146     bool HasFixedWidth() const;
147     bool HasFixedHeight() const;
148 
UpdateMeasureType(MeasureType measureType)149     void UpdateMeasureType(MeasureType measureType)
150     {
151         if (measureType_ == measureType) {
152             return;
153         }
154         propertyChangeFlag_ = propertyChangeFlag_ | PROPERTY_UPDATE_MEASURE;
155         measureType_ = measureType;
156     }
157 
158     // user defined max, min, self size.
159     void UpdateCalcLayoutProperty(const MeasureProperty& constraint);
160 
161     void UpdateUserDefinedIdealSize(const CalcSize& value);
162 
163     void ClearUserDefinedIdealSize(bool clearWidth, bool clearHeight);
164 
165     void UpdateCalcMinSize(const CalcSize& value);
166 
167     void UpdateCalcMaxSize(const CalcSize& value);
168 
169     void UpdateLayoutConstraint(const LayoutConstraintF& parentConstraint);
170 
171     void UpdateMarginSelfIdealSize(const SizeF& value);
172 
173     void ResetCalcMinSize();
174 
175     void ResetCalcMaxSize();
176 
177     void ResetCalcMinSize(bool resetWidth);
178 
179     void ResetCalcMaxSize(bool resetWidth);
180 
181     void UpdateFlexGrow(float flexGrow);
182 
183     void ResetFlexGrow();
184 
185     void UpdateFlexShrink(float flexShrink);
186 
187     void ResetFlexShrink();
188 
189     void UpdateFlexBasis(const Dimension& flexBasis);
190 
191     void UpdateAlignSelf(const FlexAlign& flexAlign);
192 
193     void ResetAlignSelf();
194 
195     void UpdateAlignRules(const std::map<AlignDirection, AlignRule>& alignRules);
196 
197     void UpdateDisplayIndex(int32_t displayIndex);
198 
199     void UpdateGridProperty(
200         std::optional<int32_t> span, std::optional<int32_t> offset, GridSizeType type = GridSizeType::UNDEFINED);
201 
202     bool UpdateGridOffset(const RefPtr<FrameNode>& host);
203 
204     void BuildGridProperty(const RefPtr<FrameNode>& host);
205 
206     void UpdateContentConstraint();
207 
208     LayoutConstraintF CreateChildConstraint() const;
209 
210     LayoutConstraintF CreateContentConstraint() const;
211 
212     PaddingPropertyF CreatePaddingWithoutBorder();
213     PaddingPropertyF CreatePaddingAndBorder();
214     PaddingPropertyF CreatePaddingAndBorderWithDefault(float paddingHorizontalDefault, float paddingVerticalDefault,
215         float borderHorizontalDefault, float borderVerticalDefault);
216 
217     MarginPropertyF CreateMargin();
218 
219     void SetHost(const WeakPtr<FrameNode>& host);
220     RefPtr<FrameNode> GetHost() const;
221 #ifdef ENABLE_DRAG_FRAMEWORK
222     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(IsBindOverlay, bool, PROPERTY_UPDATE_MEASURE);
223 #endif // ENABLE_DRAG_FRAMEWORK
224     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_GET(Visibility, VisibleType);
225 
226 public:
227     void UpdateVisibility(const VisibleType& value, bool allowTransition = false);
228     void OnVisibilityUpdate(VisibleType visible, bool allowTransition = false);
229 
230     void UpdateLayoutConstraint(const RefPtr<LayoutProperty>& layoutProperty);
231 
GetSafeAreaInsets()232     const std::unique_ptr<SafeAreaInsets>& GetSafeAreaInsets() const
233     {
234         return safeAreaInsets_;
235     }
236 
237     void UpdateSafeAreaInsets(const SafeAreaInsets& safeArea);
238 
GetSafeAreaExpandOpts()239     const std::unique_ptr<SafeAreaExpandOpts>& GetSafeAreaExpandOpts() const
240     {
241         return safeAreaExpandOpts_;
242     }
243 
244     void UpdateSafeAreaExpandOpts(const SafeAreaExpandOpts& opts);
245 
IsUsingPosition()246     bool IsUsingPosition() const
247     {
248         return usingPosition_;
249     }
250 
SetUsingPosition(bool usingPosition)251     void SetUsingPosition(bool usingPosition)
252     {
253         usingPosition_ = usingPosition;
254     }
255 
SetIsOverlayNode(bool isOverlayNode)256     void SetIsOverlayNode(bool isOverlayNode)
257     {
258         isOverlayNode_ = isOverlayNode;
259     }
260 
IsOverlayNode()261     bool IsOverlayNode()
262     {
263         return isOverlayNode_;
264     }
265 
266     void SetOverlayOffset(
267         const std::optional<Dimension>& overlayOffsetX, const std::optional<Dimension>& overlayOffsetY);
268 
269     void GetOverlayOffset(Dimension& overlayOffsetX, Dimension& overlayOffsetY);
270 
271     static void UpdateAllGeometryTransition(const RefPtr<UINode>& parent);
272 
273     std::pair<bool, bool> GetPercentSensitive();
274     std::pair<bool, bool> UpdatePercentSensitive(bool width, bool height);
275     bool ConstraintEqual(const std::optional<LayoutConstraintF>& preLayoutConstraint,
276         const std::optional<LayoutConstraintF>& preContentConstraint);
277 
278 protected:
279     void UpdateLayoutProperty(const LayoutProperty* layoutProperty);
280 
281     virtual void Clone(RefPtr<LayoutProperty> layoutProperty) const;
282 
283 private:
284     // This will call after ModifyLayoutConstraint.
285     void CheckSelfIdealSize();
286 
287     void CheckAspectRatio();
288     void CheckBorderAndPadding();
289 
290     // available in measure process.
291     std::optional<LayoutConstraintF> layoutConstraint_;
292     std::optional<LayoutConstraintF> contentConstraint_;
293 
294     std::unique_ptr<MeasureProperty> calcLayoutConstraint_;
295     std::unique_ptr<PaddingProperty> padding_;
296     std::unique_ptr<MarginProperty> margin_;
297     std::optional<MarginPropertyF> marginResult_;
298 
299     std::unique_ptr<SafeAreaExpandOpts> safeAreaExpandOpts_;
300     std::unique_ptr<SafeAreaInsets> safeAreaInsets_;
301 
302     std::unique_ptr<BorderWidthProperty> borderWidth_;
303     std::unique_ptr<MagicItemProperty> magicItemProperty_;
304     std::unique_ptr<PositionProperty> positionProperty_;
305     std::unique_ptr<FlexItemProperty> flexItemProperty_;
306     std::unique_ptr<GridProperty> gridProperty_;
307     std::optional<MeasureType> measureType_;
308     std::optional<TextDirection> layoutDirection_;
309 
310     WeakPtr<GeometryTransition> geometryTransition_;
311 
312     WeakPtr<FrameNode> host_;
313 
314     bool usingPosition_ = true;
315 
316     bool isOverlayNode_ = false;
317     Dimension overlayOffsetX_;
318     Dimension overlayOffsetY_;
319 
320     bool heightPercentSensitive_ = false;
321     bool widthPercentSensitive_ = false;
322 
323     ACE_DISALLOW_COPY_AND_MOVE(LayoutProperty);
324 };
325 } // namespace OHOS::Ace::NG
326 
327 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_PROPERTY_H
328