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 GetParentLayoutConstraint()76 const std::optional<LayoutConstraintF>& GetParentLayoutConstraint() const 77 { 78 return parentLayoutConstraint_; 79 } 80 GetMagicItemProperty()81 MagicItemProperty& GetMagicItemProperty() 82 { 83 return magicItemProperty_; 84 } 85 GetPaddingProperty()86 const std::unique_ptr<PaddingProperty>& GetPaddingProperty() const 87 { 88 return padding_; 89 } 90 GetMarginProperty()91 const std::unique_ptr<MarginProperty>& GetMarginProperty() const 92 { 93 return margin_; 94 } 95 GetBorderWidthProperty()96 const std::unique_ptr<BorderWidthProperty>& GetBorderWidthProperty() const 97 { 98 return borderWidth_; 99 } 100 GetOuterBorderWidthProperty()101 const std::unique_ptr<BorderWidthProperty>& GetOuterBorderWidthProperty() const 102 { 103 return outerBorderWidth_; 104 } 105 GetPositionProperty()106 const std::unique_ptr<PositionProperty>& GetPositionProperty() const 107 { 108 return positionProperty_; 109 } 110 GetCalcLayoutConstraint()111 const std::unique_ptr<MeasureProperty>& GetCalcLayoutConstraint() const 112 { 113 return calcLayoutConstraint_; 114 } 115 GetFlexItemProperty()116 const std::unique_ptr<FlexItemProperty>& GetFlexItemProperty() const 117 { 118 return flexItemProperty_; 119 } 120 GetLayoutDirection()121 TextDirection GetLayoutDirection() const 122 { 123 return layoutDirection_.value_or(TextDirection::AUTO); 124 } 125 GetGeometryTransition()126 RefPtr<GeometryTransition> GetGeometryTransition() const 127 { 128 return geometryTransition_.Upgrade(); 129 } 130 131 MeasureType GetMeasureType(MeasureType defaultType = MeasureType::MATCH_CONTENT) const 132 { 133 return measureType_.value_or(defaultType); 134 } 135 136 void UpdatePadding(const PaddingProperty& value); 137 138 void UpdateMargin(const MarginProperty& value); 139 140 void UpdateBorderWidth(const BorderWidthProperty& value); 141 142 void UpdateOuterBorderWidth(const BorderWidthProperty& value); 143 144 void UpdateAlignment(Alignment value); 145 146 void UpdateLayoutWeight(float value); 147 UpdatePixelRound(uint8_t value)148 void UpdatePixelRound(uint8_t value) 149 { 150 pixelRoundFlag_ = value; 151 } 152 GetPixelRound()153 uint8_t GetPixelRound() const { 154 return pixelRoundFlag_; 155 } 156 157 void UpdateLayoutDirection(TextDirection value); 158 159 void UpdateGeometryTransition(const std::string& id, bool followWithoutTransition = false); 160 161 void UpdateAspectRatio(float ratio); 162 void ResetAspectRatio(); 163 164 bool HasAspectRatio() const; 165 float GetAspectRatio() const; 166 167 bool HasFixedWidth() const; 168 bool HasFixedHeight() const; 169 UpdateMeasureType(MeasureType measureType)170 void UpdateMeasureType(MeasureType measureType) 171 { 172 if (measureType_ == measureType) { 173 return; 174 } 175 propertyChangeFlag_ = propertyChangeFlag_ | PROPERTY_UPDATE_MEASURE; 176 measureType_ = measureType; 177 } 178 179 // user defined max, min, self size. 180 void UpdateCalcLayoutProperty(const MeasureProperty& constraint); 181 182 void UpdateUserDefinedIdealSize(const CalcSize& value); 183 184 void ClearUserDefinedIdealSize(bool clearWidth, bool clearHeight); 185 186 virtual void UpdateCalcMinSize(const CalcSize& value); 187 188 virtual void UpdateCalcMaxSize(const CalcSize& value); 189 190 void UpdateLayoutConstraint(const LayoutConstraintF& parentConstraint); 191 UpdateParentLayoutConstraint(const LayoutConstraintF & parentConstraint)192 void UpdateParentLayoutConstraint(const LayoutConstraintF& parentConstraint) 193 { 194 parentLayoutConstraint_ = parentConstraint; 195 } 196 197 void UpdateMarginSelfIdealSize(const SizeF& value); 198 199 void ResetCalcMinSize(); 200 201 void ResetCalcMaxSize(); 202 203 void ResetCalcMinSize(bool resetWidth); 204 205 void ResetCalcMaxSize(bool resetWidth); 206 207 void UpdateFlexGrow(float flexGrow); 208 209 void ResetFlexGrow(); 210 211 void UpdateFlexShrink(float flexShrink); 212 213 void ResetFlexShrink(); 214 215 void UpdateFlexBasis(const Dimension& flexBasis); 216 217 void UpdateAlignSelf(const FlexAlign& flexAlign); 218 219 void ResetAlignSelf(); 220 221 void UpdateAlignRules(const std::map<AlignDirection, AlignRule>& alignRules); 222 223 void UpdateBias(const BiasPair& biasPair); 224 225 void UpdateDisplayIndex(int32_t displayIndex); 226 227 void UpdateGridProperty( 228 std::optional<int32_t> span, std::optional<int32_t> offset, GridSizeType type = GridSizeType::UNDEFINED); 229 230 bool UpdateGridOffset(const RefPtr<FrameNode>& host); 231 232 void BuildGridProperty(const RefPtr<FrameNode>& host); 233 234 void UpdateContentConstraint(); 235 236 LayoutConstraintF CreateChildConstraint() const; 237 238 LayoutConstraintF CreateContentConstraint() const; 239 240 PaddingPropertyF CreatePaddingWithoutBorder(); 241 PaddingPropertyF CreatePaddingAndBorder(); 242 PaddingPropertyF CreatePaddingAndBorderWithDefault(float paddingHorizontalDefault, float paddingVerticalDefault, 243 float borderHorizontalDefault, float borderVerticalDefault); 244 245 MarginPropertyF CreateMargin(); 246 247 void SetHost(const WeakPtr<FrameNode>& host); 248 RefPtr<FrameNode> GetHost() const; 249 ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(IsBindOverlay, bool, PROPERTY_UPDATE_MEASURE); 250 ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_GET(Visibility, VisibleType); 251 252 public: 253 void UpdateVisibility(const VisibleType& value, bool allowTransition = false); 254 void OnVisibilityUpdate(VisibleType visible, bool allowTransition = false); 255 256 void UpdateLayoutConstraint(const RefPtr<LayoutProperty>& layoutProperty); 257 GetSafeAreaInsets()258 const std::unique_ptr<SafeAreaInsets>& GetSafeAreaInsets() const 259 { 260 return safeAreaInsets_; 261 } 262 263 void UpdateSafeAreaInsets(const SafeAreaInsets& safeArea); 264 GetSafeAreaExpandOpts()265 const std::unique_ptr<SafeAreaExpandOpts>& GetSafeAreaExpandOpts() const 266 { 267 return safeAreaExpandOpts_; 268 } 269 270 void UpdateSafeAreaExpandOpts(const SafeAreaExpandOpts& opts); 271 IsUsingPosition()272 bool IsUsingPosition() const 273 { 274 return usingPosition_; 275 } 276 SetUsingPosition(bool usingPosition)277 void SetUsingPosition(bool usingPosition) 278 { 279 usingPosition_ = usingPosition; 280 } 281 SetIsOverlayNode(bool isOverlayNode)282 void SetIsOverlayNode(bool isOverlayNode) 283 { 284 isOverlayNode_ = isOverlayNode; 285 } 286 IsOverlayNode()287 bool IsOverlayNode() 288 { 289 return isOverlayNode_; 290 } 291 292 void SetOverlayOffset( 293 const std::optional<Dimension>& overlayOffsetX, const std::optional<Dimension>& overlayOffsetY); 294 295 void GetOverlayOffset(Dimension& overlayOffsetX, Dimension& overlayOffsetY); 296 297 static void UpdateAllGeometryTransition(const RefPtr<UINode>& parent); 298 299 std::pair<bool, bool> GetPercentSensitive(); 300 std::pair<bool, bool> UpdatePercentSensitive(bool width, bool height); 301 bool ConstraintEqual(const std::optional<LayoutConstraintF>& preLayoutConstraint, 302 const std::optional<LayoutConstraintF>& preContentConstraint); 303 304 protected: 305 void UpdateLayoutProperty(const LayoutProperty* layoutProperty); 306 307 virtual void Clone(RefPtr<LayoutProperty> layoutProperty) const; 308 309 private: 310 // This will call after ModifyLayoutConstraint. 311 void CheckSelfIdealSize(const LayoutConstraintF& parentConstraint, const SizeF& originMax); 312 313 void CheckAspectRatio(); 314 void CheckBorderAndPadding(); 315 316 const std::string PixelRoundToJsonValue() const; 317 318 // available in measure process. 319 std::optional<LayoutConstraintF> layoutConstraint_; 320 std::optional<LayoutConstraintF> contentConstraint_; 321 322 // layoutConstraint_ set by builderNode 323 std::optional<LayoutConstraintF> parentLayoutConstraint_; 324 325 std::unique_ptr<MeasureProperty> calcLayoutConstraint_; 326 std::unique_ptr<PaddingProperty> padding_; 327 std::unique_ptr<MarginProperty> margin_; 328 std::optional<MarginPropertyF> marginResult_; 329 330 std::unique_ptr<SafeAreaExpandOpts> safeAreaExpandOpts_; 331 std::unique_ptr<SafeAreaInsets> safeAreaInsets_; 332 333 std::unique_ptr<BorderWidthProperty> borderWidth_; 334 std::unique_ptr<BorderWidthProperty> outerBorderWidth_; 335 MagicItemProperty magicItemProperty_; 336 std::unique_ptr<PositionProperty> positionProperty_; 337 std::unique_ptr<FlexItemProperty> flexItemProperty_; 338 std::unique_ptr<GridProperty> gridProperty_; 339 std::optional<MeasureType> measureType_; 340 std::optional<TextDirection> layoutDirection_; 341 342 WeakPtr<GeometryTransition> geometryTransition_; 343 344 WeakPtr<FrameNode> host_; 345 346 bool usingPosition_ = true; 347 348 uint8_t pixelRoundFlag_ = 0; 349 350 bool isOverlayNode_ = false; 351 Dimension overlayOffsetX_; 352 Dimension overlayOffsetY_; 353 354 bool heightPercentSensitive_ = false; 355 bool widthPercentSensitive_ = false; 356 357 ACE_DISALLOW_COPY_AND_MOVE(LayoutProperty); 358 }; 359 } // namespace OHOS::Ace::NG 360 361 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_PROPERTY_H 362