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