• 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/rect_t.h"
25 #include "base/geometry/ng/size_t.h"
26 #include "base/memory/ace_type.h"
27 #include "base/memory/referenced.h"
28 #include "base/utils/macros.h"
29 #include "base/utils/noncopyable.h"
30 #include "base/utils/utils.h"
31 #include "core/components/common/layout/constants.h"
32 #include "core/components/common/layout/grid_layout_info.h"
33 #include "core/components/common/layout/position_param.h"
34 #include "core/components/common/properties/alignment.h"
35 #include "core/components_ng/property/border_property.h"
36 #include "core/components_ng/property/layout_constraint.h"
37 #include "core/components_ng/property/layout_policy_property.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/property.h"
41 #include "ui/properties/safe_area_insets.h"
42 
43 namespace OHOS::Ace::NG {
44 
45 class FrameNode;
46 class UINode;
47 class InspectorFilter;
48 class GridProperty;
49 class FlexItemProperty;
50 struct PositionProperty;
51 struct SafeAreaExpandOpts;
52 class GeometryTransition;
53 struct SafeAreaInsets;
54 using BiasPair = std::pair<float, float>;
55 using ChainWeightPair = std::pair<std::optional<float>, std::optional<float>>; // <horizontal,vertical>
56 
57 class ACE_FORCE_EXPORT LayoutProperty : public Property {
58     DECLARE_ACE_TYPE(LayoutProperty, Property);
59 
60 public:
61     LayoutProperty();
62 
63     ~LayoutProperty() override;
64 
65     virtual RefPtr<LayoutProperty> Clone() const;
66 
67     virtual void Reset();
68 
69     virtual void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
70 
ToTreeJson(std::unique_ptr<JsonValue> & json,const InspectorConfig & config)71     virtual void ToTreeJson(std::unique_ptr<JsonValue>& json, const InspectorConfig& config) const {}
72 
73     virtual void FromJson(const std::unique_ptr<JsonValue>& json);
74 
GetLayoutConstraint()75     const std::optional<LayoutConstraintF>& GetLayoutConstraint() const
76     {
77         return layoutConstraint_;
78     }
79 
GetContentLayoutConstraint()80     const std::optional<LayoutConstraintF>& GetContentLayoutConstraint() const
81     {
82         return contentConstraint_;
83     }
84 
GetParentLayoutConstraint()85     const std::optional<LayoutConstraintF>& GetParentLayoutConstraint() const
86     {
87         return parentLayoutConstraint_;
88     }
89 
GetMagicItemProperty()90     MagicItemProperty& GetMagicItemProperty()
91     {
92         return magicItemProperty_;
93     }
94 
GetPaddingProperty()95     const std::unique_ptr<PaddingProperty>& GetPaddingProperty() const
96     {
97         return padding_;
98     }
99 
GetSafeAreaPaddingProperty()100     const std::unique_ptr<PaddingProperty>& GetSafeAreaPaddingProperty() const
101     {
102         return safeAreaPadding_;
103     }
104 
GetMarginProperty()105     const std::unique_ptr<MarginProperty>& GetMarginProperty() const
106     {
107         return margin_;
108     }
109 
GetBorderWidthProperty()110     const std::unique_ptr<BorderWidthProperty>& GetBorderWidthProperty() const
111     {
112         return borderWidth_;
113     }
114 
GetOuterBorderWidthProperty()115     const std::unique_ptr<BorderWidthProperty>& GetOuterBorderWidthProperty() const
116     {
117         return outerBorderWidth_;
118     }
119 
GetPositionProperty()120     const std::unique_ptr<PositionProperty>& GetPositionProperty() const
121     {
122         return positionProperty_;
123     }
124 
GetCalcLayoutConstraint()125     const std::unique_ptr<MeasureProperty>& GetCalcLayoutConstraint() const
126     {
127         return calcLayoutConstraint_;
128     }
129 
GetFlexItemProperty()130     const std::unique_ptr<FlexItemProperty>& GetFlexItemProperty() const
131     {
132         return flexItemProperty_;
133     }
134 
GetLayoutDirection()135     TextDirection GetLayoutDirection() const
136     {
137         return layoutDirection_.value_or(TextDirection::AUTO);
138     }
139 
140     TextDirection GetNonAutoLayoutDirection() const;
141 
GetBackgroundIgnoresLayoutSafeAreaEdges()142     uint32_t GetBackgroundIgnoresLayoutSafeAreaEdges() const
143     {
144         return backgroundIgnoresLayoutSafeAreaEdges_.value_or(NG::LAYOUT_SAFE_AREA_EDGE_NONE);
145     }
146 
GetLocalizedBackgroundIgnoresLayoutSafeAreaEdges()147     uint32_t GetLocalizedBackgroundIgnoresLayoutSafeAreaEdges() const
148     {
149         return localizedBackgroundIgnoresLayoutSafeAreaEdges_.value_or(NG::LAYOUT_SAFE_AREA_EDGE_NONE);
150     }
151 
152     RefPtr<GeometryTransition> GetGeometryTransition() const;
153 
154     MeasureType GetMeasureType(MeasureType defaultType = MeasureType::MATCH_CONTENT) const
155     {
156         return measureType_.value_or(defaultType);
157     }
158 
159     void LocalizedPaddingOrMarginChange(const PaddingProperty& value, std::unique_ptr<PaddingProperty>& padding);
160     void UpdatePadding(const PaddingProperty& value);
161     void UpdateSafeAreaPadding(const PaddingProperty& value);
162     void ResetSafeAreaPadding();
163 
164     void UpdateMargin(const MarginProperty& value);
165 
166     void UpdateBorderWidth(const BorderWidthProperty& value);
167 
168     void UpdateOuterBorderWidth(const BorderWidthProperty& value);
169 
170     void UpdateAlignment(Alignment value);
171 
172     void UpdateLocalizedAlignment(std::string value);
173 
174     void UpdateLayoutGravity(Alignment value);
175 
176     void UpdateIsMirrorable(bool value);
177 
178     void UpdateLayoutWeight(float value);
179 
180     void UpdateChainWeight(const ChainWeightPair& value);
181 
UpdatePixelRound(uint16_t value)182     void UpdatePixelRound(uint16_t value)
183     {
184         pixelRoundFlag_ = value;
185     }
186 
GetPixelRound()187     uint16_t GetPixelRound() const {
188         return pixelRoundFlag_;
189     }
190 
191     void UpdateLayoutDirection(TextDirection value);
192     void UpdateBackgroundIgnoresLayoutSafeAreaEdges(uint32_t value);
193 
194     void UpdateGeometryTransition(const std::string& id,
195         bool followWithoutTransition = false, bool doRegisterSharedTransition = true);
196 
197     void ResetGeometryTransition();
198 
199     void UpdateAspectRatio(float ratio);
200     void ResetAspectRatio();
201 
202     bool HasAspectRatio() const;
203     float GetAspectRatio() const;
204 
205     bool HasFixedWidth(bool checkPercent = true) const;
206     bool HasFixedHeight(bool checkPercent = true) const;
207 
UpdateMeasureType(MeasureType measureType)208     void UpdateMeasureType(MeasureType measureType)
209     {
210         if (measureType_ == measureType) {
211             return;
212         }
213         propertyChangeFlag_ = propertyChangeFlag_ | PROPERTY_UPDATE_MEASURE;
214         measureType_ = measureType;
215     }
216 
217     // user defined max, min, self size.
218     void UpdateCalcLayoutProperty(const MeasureProperty& constraint);
219 
220     void UpdateUserDefinedIdealSize(const CalcSize& value);
221 
222     void UpdateLayoutPolicyProperty(const LayoutCalPolicy layoutPolicy, bool isWidth);
223 
224     bool UpdateLayoutPolicyWithCheck(const LayoutCalPolicy layoutPolicy, bool isWidth);
225 
226     std::optional<NG::LayoutPolicyProperty> GetLayoutPolicyProperty();
227 
228     void ClearUserDefinedIdealSize(bool clearWidth, bool clearHeight);
229 
230     virtual void UpdateCalcMinSize(const CalcSize& value);
231 
232     virtual void UpdateCalcMaxSize(const CalcSize& value);
233 
234     void CalcToString(const CalcSize& calcSize, std::pair<std::vector<std::string>, std::vector<std::string>>& result);
235 
236     IgnoreLayoutSafeAreaOpts GenIgnoreOpts() const;
237 
238     virtual void ExpandConstraintWithSafeArea();
239 
240     void UpdateLayoutConstraint(const LayoutConstraintF& parentConstraint);
241 
UpdateParentLayoutConstraint(const LayoutConstraintF & parentConstraint)242     void UpdateParentLayoutConstraint(const LayoutConstraintF& parentConstraint)
243     {
244         parentLayoutConstraint_ = parentConstraint;
245     }
246 
247     void UpdateMarginSelfIdealSize(const SizeF& value);
248 
249     void ResetCalcMinSize();
250 
251     void ResetCalcMaxSize();
252 
253     void ResetCalcMinSize(bool resetWidth);
254 
255     void ResetCalcMaxSize(bool resetWidth);
256 
257     void UpdateFlexGrow(float flexGrow);
258 
259     void ResetFlexGrow();
260 
261     void UpdateFlexShrink(float flexShrink);
262 
263     void ResetFlexShrink();
264 
265     void UpdateFlexBasis(const Dimension& flexBasis);
266 
267     void UpdateAlignSelf(const FlexAlign& flexAlign);
268 
269     void ResetAlignSelf();
270 
271     void UpdateAlignRules(const std::map<AlignDirection, AlignRule>& alignRules);
272 
273     void UpdateChainStyle(const ChainInfo& chainInfo);
274 
275     void UpdateBias(const BiasPair& biasPair);
276 
277     void UpdateDisplayIndex(int32_t displayIndex);
278 
279     void UpdateGridProperty(
280         std::optional<int32_t> span, std::optional<int32_t> offset, GridSizeType type = GridSizeType::UNDEFINED);
281 
282     bool UpdateGridOffset(const RefPtr<FrameNode>& host);
283 
SetLayoutRect(const NG::RectF & rect)284     void SetLayoutRect(const NG::RectF& rect)
285     {
286         if (layoutRect_ != rect) {
287             propertyChangeFlag_ |= PROPERTY_UPDATE_MEASURE_SELF;
288             layoutRect_ = rect;
289         }
290     }
291 
ResetLayoutRect()292     void ResetLayoutRect()
293     {
294         if (layoutRect_) {
295             propertyChangeFlag_ |= PROPERTY_UPDATE_MEASURE_SELF;
296             layoutRect_.reset();
297         }
298     }
299 
GetLayoutRect()300     std::optional<NG::RectF> GetLayoutRect() const
301     {
302         return layoutRect_;
303     }
304 
305     void BuildGridProperty(const RefPtr<FrameNode>& host);
306 
307     void UpdateContentConstraint();
308     void UpdateLayoutConstraintWithLayoutRect();
309 
310     LayoutConstraintF CreateChildConstraint() const;
311 
312     LayoutConstraintF CreateContentConstraint() const;
313 
314     PaddingPropertyF CreatePaddingWithoutBorder(bool useRootConstraint = true, bool roundPixel = true);
315     PaddingPropertyF CreatePaddingAndBorder(bool includeSafeAreaPadding = true, bool forceReCreate = false);
316     PaddingPropertyF CreatePaddingAndBorderWithDefault(float paddingHorizontalDefault, float paddingVerticalDefault,
317         float borderHorizontalDefault, float borderVerticalDefault);
318     BorderWidthPropertyF CreateBorder();
319 
320     MarginPropertyF CreateMargin();
321     MarginPropertyF CreateMarginWithoutCache();
322 
323     void SetHost(const WeakPtr<FrameNode>& host);
324     RefPtr<FrameNode> GetHost() const;
325     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(IsBindOverlay, bool, PROPERTY_UPDATE_MEASURE);
326     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_GET(Visibility, VisibleType);
327 
328 public:
329     void UpdateVisibility(const VisibleType& value, bool allowTransition = false, bool isUserSet = false);
330     void OnVisibilityUpdate(VisibleType visible, bool allowTransition = false, bool isUserSet = false);
IsUserSetVisibility()331     bool IsUserSetVisibility()
332     {
333         return isUserSetVisibility_;
334     }
335 
336     void UpdateLayoutConstraint(const RefPtr<LayoutProperty>& layoutProperty);
337 
GetSafeAreaInsets()338     const std::unique_ptr<SafeAreaInsets>& GetSafeAreaInsets() const
339     {
340         return safeAreaInsets_;
341     }
342 
343     void UpdateSafeAreaInsets(const SafeAreaInsets& safeArea);
344 
GetSafeAreaExpandOpts()345     const std::unique_ptr<SafeAreaExpandOpts>& GetSafeAreaExpandOpts() const
346     {
347         return safeAreaExpandOpts_;
348     }
349 
350     void UpdateSafeAreaExpandOpts(const SafeAreaExpandOpts& opts);
351 
352     void UpdateIgnoreLayoutSafeAreaOpts(const IgnoreLayoutSafeAreaOpts& opts);
353 
354     bool IsExpandConstraintNeeded();
355 
GetIgnoreLayoutSafeAreaOpts()356     const std::unique_ptr<IgnoreLayoutSafeAreaOpts>& GetIgnoreLayoutSafeAreaOpts() const
357     {
358         return ignoreLayoutSafeAreaOpts_;
359     }
360 
IsIgnoreOptsValid()361     bool IsIgnoreOptsValid() const
362     {
363         return ignoreLayoutSafeAreaOpts_ && ignoreLayoutSafeAreaOpts_->NeedIgnoreLayoutSafeArea();
364     }
365 
IsUsingPosition()366     bool IsUsingPosition() const
367     {
368         return usingPosition_;
369     }
370 
SetUsingPosition(bool usingPosition)371     void SetUsingPosition(bool usingPosition)
372     {
373         usingPosition_ = usingPosition;
374     }
375 
SetIsOverlayNode(bool isOverlayNode)376     void SetIsOverlayNode(bool isOverlayNode)
377     {
378         isOverlayNode_ = isOverlayNode;
379     }
380 
IsOverlayNode()381     bool IsOverlayNode() const
382     {
383         return isOverlayNode_;
384     }
385 
386     void SetOverlayOffset(
387         const std::optional<Dimension>& overlayOffsetX, const std::optional<Dimension>& overlayOffsetY);
388 
389     void GetOverlayOffset(Dimension& overlayOffsetX, Dimension& overlayOffsetY);
390 
391     static void UpdateAllGeometryTransition(const RefPtr<UINode>& parent);
392 
393     // the returned value represents whether to compare percent reference when comparing old and new layout constrains.
394     // the first of returned value represents width, and the second of returned value represents height.
395     virtual std::pair<bool, bool> GetPercentSensitive();
396     std::pair<bool, bool> UpdatePercentSensitive(bool width, bool height);
397     bool ConstraintEqual(const std::optional<LayoutConstraintF>& preLayoutConstraint,
398         const std::optional<LayoutConstraintF>& preContentConstraint);
399 
400     PaddingPropertyF GetOrCreateSafeAreaPadding(bool forceReCreate = false);
401 
UpdateNeedPositionLocalizedEdges(bool needPositionLocalizedEdges)402     void UpdateNeedPositionLocalizedEdges(bool needPositionLocalizedEdges)
403     {
404         needPositionLocalizedEdges_ = needPositionLocalizedEdges;
405     }
406 
IsPositionLocalizedEdges()407     bool IsPositionLocalizedEdges() const
408     {
409         return needPositionLocalizedEdges_;
410     }
411 
UpdateNeedOffsetLocalizedEdges(bool needOffsetLocalizedEdges)412     void UpdateNeedOffsetLocalizedEdges(bool needOffsetLocalizedEdges)
413     {
414         needOffsetLocalizedEdges_ = needOffsetLocalizedEdges;
415     }
416 
IsOffsetLocalizedEdges()417     bool IsOffsetLocalizedEdges() const
418     {
419         return needOffsetLocalizedEdges_;
420     }
421 
ResetMarkAnchorStart()422     void ResetMarkAnchorStart()
423     {
424         markAnchorStart_.reset();
425     }
426 
UpdateMarkAnchorStart(const Dimension & markAnchorStart)427     void UpdateMarkAnchorStart(const Dimension& markAnchorStart)
428     {
429         markAnchorStart_ = markAnchorStart;
430     }
431 
SetNeedLazyLayout(bool value)432     void SetNeedLazyLayout(bool value)
433     {
434         needLazyLayout_ = true;
435     }
436 
GetNeedLazyLayout()437     bool GetNeedLazyLayout() const
438     {
439         return needLazyLayout_;
440     }
441 
442     void ConstraintViewPosRef(ViewPosReference& viewPosRef);
443 
444     void CheckPositionLocalizedEdges(TextDirection layoutDirection);
445     void CheckMarkAnchorPosition(TextDirection layoutDirection);
446     void CheckOffsetLocalizedEdges(TextDirection layoutDirection);
447     void CheckLocalizedBorderRadiuses(const TextDirection& direction);
448     void CheckLocalizedOuterBorderColor(const TextDirection& direction);
449     void CheckLocalizedPadding(const RefPtr<LayoutProperty>& layoutProperty, const TextDirection& direction);
450     void CheckLocalizedMargin(const RefPtr<LayoutProperty>& layoutProperty, const TextDirection& direction);
451     void CheckLocalizedEdgeWidths(const RefPtr<LayoutProperty>& layoutProperty, const TextDirection& direction);
452     void CheckLocalizedEdgeColors(const TextDirection& direction);
453     void CheckLocalizedBorderImageSlice(const TextDirection& direction);
454     void CheckLocalizedBorderImageWidth(const TextDirection& direction);
455     void CheckLocalizedBorderImageOutset(const TextDirection& direction);
456     void CheckLocalizedSafeAreaPadding(const TextDirection& direction);
457     void CheckIgnoreLayoutSafeArea(const TextDirection& direction);
458     void CheckBackgroundLayoutSafeAreaEdges(const TextDirection& direction);
459     void CheckLocalizedAlignment(const TextDirection& direction);
460     bool DecideMirror();
461 
OnPropertyChangeMeasure()462     virtual void OnPropertyChangeMeasure() {}
463 
464     std::string LayoutInfoToString();
465 
466     std::string GetAlignmentStringFromLocalized(TextDirection layoutDirection, std::string localizedAlignment);
467 
468 protected:
469     void UpdateLayoutProperty(const LayoutProperty* layoutProperty);
470 
471     virtual void Clone(RefPtr<LayoutProperty> layoutProperty) const;
472     std::optional<LayoutConstraintF> layoutConstraint_;
473 
474 private:
475     // This will call after ModifyLayoutConstraint.
476     void CheckSelfIdealSize(const SizeF& originMax);
477     void CheckCalcLayoutConstraint(const LayoutConstraintF& parentConstraint);
478 
479     void CheckAspectRatio();
480     void CheckBorderAndPadding();
481     void ConstraintContentByPadding();
482     void ConstraintContentByBorder();
483     void ConstraintContentBySafeAreaPadding();
484     PaddingPropertyF CreateSafeAreaPadding(bool adjustingRound = false);
485 
486     const std::string PixelRoundToJsonValue() const;
487 
488     void PaddingToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
489     void MarginToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
490     void IgnoreLayoutSafeAreaToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
491     void SafeAreaExpandToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
492     void SafeAreaPaddingToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
493 
494     // available in measure process.
495     std::optional<LayoutConstraintF> contentConstraint_;
496 
497     // layoutConstraint_ set by builderNode
498     std::optional<LayoutConstraintF> parentLayoutConstraint_;
499 
500     std::unique_ptr<MeasureProperty> calcLayoutConstraint_;
501     std::pair<std::vector<std::string>, std::vector<std::string>> calcSelfIdealSizeRpn_;
502     std::pair<std::vector<std::string>, std::vector<std::string>> calcMinSizeRpn_;
503     std::pair<std::vector<std::string>, std::vector<std::string>> calcMaxSizeRpn_;
504     std::unique_ptr<PaddingProperty> safeAreaPadding_;
505     std::unique_ptr<PaddingProperty> padding_;
506     std::unique_ptr<MarginProperty> margin_;
507     std::optional<MarginPropertyF> marginResult_;
508     std::optional<NG::LayoutPolicyProperty> layoutPolicy_;
509 
510     std::unique_ptr<SafeAreaExpandOpts> safeAreaExpandOpts_;
511     std::unique_ptr<IgnoreLayoutSafeAreaOpts> ignoreLayoutSafeAreaOpts_;
512     std::unique_ptr<SafeAreaInsets> safeAreaInsets_;
513 
514     std::unique_ptr<BorderWidthProperty> borderWidth_;
515     std::unique_ptr<BorderWidthProperty> outerBorderWidth_;
516     MagicItemProperty magicItemProperty_;
517     std::unique_ptr<PositionProperty> positionProperty_;
518     std::unique_ptr<FlexItemProperty> flexItemProperty_;
519     std::unique_ptr<GridProperty> gridProperty_;
520     std::optional<MeasureType> measureType_;
521     std::optional<TextDirection> layoutDirection_;
522     std::optional<RectF> layoutRect_;
523     std::optional<Dimension> markAnchorStart_;
524     std::optional<uint32_t> backgroundIgnoresLayoutSafeAreaEdges_;
525     std::optional<uint32_t> localizedBackgroundIgnoresLayoutSafeAreaEdges_;
526 
527     WeakPtr<GeometryTransition> geometryTransition_;
528 
529     WeakPtr<FrameNode> host_;
530 
531     bool usingPosition_ = true;
532 
533     uint16_t pixelRoundFlag_ = 0;
534 
535     bool isOverlayNode_ = false;
536     Dimension overlayOffsetX_;
537     Dimension overlayOffsetY_;
538 
539     bool heightPercentSensitive_ = false;
540     bool widthPercentSensitive_ = false;
541     bool needPositionLocalizedEdges_ = false;
542     bool needOffsetLocalizedEdges_ = false;
543     bool needLazyLayout_ = false;
544     bool isUserSetVisibility_ = false;
545 
546     ACE_DISALLOW_COPY_AND_MOVE(LayoutProperty);
547 };
548 } // namespace OHOS::Ace::NG
549 
550 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_PROPERTY_H
551