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