From 9fe3b752316084e2dfb46b9dbb4831d89621fbcd Mon Sep 17 00:00:00 2001 From: Shi Bofan Date: Tue, 14 May 2024 20:50:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFlex=E5=9C=A8=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=AD=90=E7=BB=84=E4=BB=B6=E6=97=B6constraintSize?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E4=B8=8D=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Shi Bofan --- frameworks/base/geometry/ng/size_t.h | 19 ++++---- .../pattern/flex/flex_layout_algorithm.cpp | 10 +++- .../pattern/flex/flex_layout_algorithm.h | 1 + .../components_ng/property/measure_utils.cpp | 46 +++++++++++++++++-- .../components_ng/property/measure_utils.h | 3 +- 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/frameworks/base/geometry/ng/size_t.h b/frameworks/base/geometry/ng/size_t.h index 8c3c5251608..cc08c166115 100644 --- a/frameworks/base/geometry/ng/size_t.h +++ b/frameworks/base/geometry/ng/size_t.h @@ -681,31 +681,32 @@ public: return isModified; } - void UpdateMin(const SizeT& minSize) + void UpdateMin(const SizeT& minSize, bool forceOverwriteInvalidValue = false) { - if (NonNegative(minSize.Width()) && width_) { + if (NonNegative(minSize.Width()) && (width_ || forceOverwriteInvalidValue)) { width_ = width_.value_or(0) > minSize.Width() ? width_ : minSize.Width(); } - if (NonNegative(minSize.Height()) && height_) { + if (NonNegative(minSize.Height()) && (height_ || forceOverwriteInvalidValue)) { height_ = height_.value_or(0) > minSize.Height() ? height_ : minSize.Height(); } } - void UpdateMax(const SizeT& maxSize) + void UpdateMax(const SizeT& maxSize, bool forceOverwriteInvalidValue = false) { - if (NonNegative(maxSize.Width()) && width_) { + if (NonNegative(maxSize.Width()) && (width_ || forceOverwriteInvalidValue)) { width_ = width_.value_or(0) < maxSize.Width() ? width_ : maxSize.Width(); } - if (NonNegative(maxSize.Height()) && height_) { + if (NonNegative(maxSize.Height()) && (height_ || forceOverwriteInvalidValue)) { height_ = height_.value_or(0) < maxSize.Height() ? Height() : maxSize.Height(); } } - void Constrain(const SizeT& minSize, const SizeT& maxSize, bool version10OrLarger = false) + void Constrain(const SizeT& minSize, const SizeT& maxSize, bool version10OrLarger = false, + bool forceOverwriteInvalidValue = false) { if (version10OrLarger) { - UpdateMax(maxSize); - UpdateMin(minSize); + UpdateMax(maxSize, forceOverwriteInvalidValue); + UpdateMin(minSize, forceOverwriteInvalidValue); return; } UpdateMin(minSize); diff --git a/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.cpp index 7782e99247b..fa9c52cf8dd 100644 --- a/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.cpp @@ -798,6 +798,11 @@ bool FlexLayoutAlgorithm::MarginOnMainAxisNegative(LayoutWrapper* layoutWrapper) return LessNotEqual(margin->top.value_or(0.0f) + margin->bottom.value_or(0.0f), 0.0f); } +bool FlexLayoutAlgorithm::CheckSetConstraint(const std::unique_ptr& propertyPtr) +{ + return propertyPtr && (propertyPtr->minSize || propertyPtr->maxSize); +} + void FlexLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper) { const auto& children = layoutWrapper->GetAllChildrenWithBuild(); @@ -805,11 +810,14 @@ void FlexLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper) * Obtain the main axis size and cross axis size based on user setting. */ const auto& layoutConstraint = layoutWrapper->GetLayoutProperty()->GetLayoutConstraint(); + const auto& rawConstraint = layoutWrapper->GetLayoutProperty()->GetCalcLayoutConstraint(); + bool needToConstraint = CheckSetConstraint(rawConstraint); const auto& measureType = layoutWrapper->GetLayoutProperty()->GetMeasureType(); InitFlexProperties(layoutWrapper); Axis axis = (direction_ == FlexDirection::ROW || direction_ == FlexDirection::ROW_REVERSE) ? Axis::HORIZONTAL : Axis::VERTICAL; - auto realSize = CreateIdealSizeByPercentRef(layoutConstraint.value(), axis, measureType).ConvertToSizeT(); + auto realSize = CreateIdealSizeByPercentRef(layoutConstraint.value(), axis, measureType, needToConstraint, + rawConstraint).ConvertToSizeT(); if (children.empty()) { layoutWrapper->GetGeometryNode()->SetFrameSize(realSize); return; diff --git a/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.h b/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.h index bca74253459..2d532b1c56d 100644 --- a/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.h +++ b/frameworks/core/components_ng/pattern/flex/flex_layout_algorithm.h @@ -95,6 +95,7 @@ private: float MainAxisMinValue(LayoutWrapper* layoutWrapper); bool MarginOnMainAxisNegative(LayoutWrapper* layoutWrapper); bool IsKeepMinSize(const RefPtr& childLayoutWrapper, float& flexSize); + bool CheckSetConstraint(const std::unique_ptr& propertyPtr); OptionalSizeF realSize_; float mainAxisSize_ = 0.0f; diff --git a/frameworks/core/components_ng/property/measure_utils.cpp b/frameworks/core/components_ng/property/measure_utils.cpp index f91e7783b7d..085ab60f529 100644 --- a/frameworks/core/components_ng/property/measure_utils.cpp +++ b/frameworks/core/components_ng/property/measure_utils.cpp @@ -390,8 +390,48 @@ void UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(OptionalSizeF& frameSize, } } +void UpdateConstraintByRawConstraint(SizeF& validMinSize, SizeF& validMaxSize, + const std::unique_ptr& rawConstraint) +{ + if (rawConstraint->minSize) { + if (!rawConstraint->minSize.value().Width()) { + validMinSize.SetWidth(-1.0f); + } + if (!rawConstraint->minSize.value().Height()) { + validMinSize.SetHeight(-1.0f); + } + } else { + validMinSize = SizeF(-1.0f, -1.0f); + } + if (rawConstraint->maxSize) { + if (!rawConstraint->maxSize.value().Width()) { + validMaxSize.SetWidth(-1.0f); + } + if (!rawConstraint->maxSize.value().Height()) { + validMaxSize.SetHeight(-1.0f); + } + } else { + validMaxSize = SizeF(-1.0f, -1.0f); + } +} + +void ApplyConstraint(OptionalSizeF& idealSize, const LayoutConstraintF& layoutConstraint, + const std::unique_ptr& rawConstraint) +{ + auto validMinSize = layoutConstraint.minSize; + auto validMaxSize = layoutConstraint.maxSize; + if (rawConstraint) { + UpdateConstraintByRawConstraint(validMinSize, validMaxSize, rawConstraint); + } + idealSize.Constrain(validMinSize, validMaxSize, + PipelineBase::GetCurrentContext() && + PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN, + rawConstraint != nullptr); +} + OptionalSizeF CreateIdealSizeByPercentRef( - const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain) + const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain, + const std::unique_ptr& rawConstraint) { OptionalSizeF idealSize; do { @@ -436,9 +476,7 @@ OptionalSizeF CreateIdealSizeByPercentRef( } } while (false); if (needToConstrain) { - idealSize.Constrain(layoutConstraint.minSize, layoutConstraint.maxSize, - PipelineBase::GetCurrentContext() && - PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN); + ApplyConstraint(idealSize, layoutConstraint, rawConstraint); } return idealSize; } diff --git a/frameworks/core/components_ng/property/measure_utils.h b/frameworks/core/components_ng/property/measure_utils.h index b58791f726f..939f735e56a 100644 --- a/frameworks/core/components_ng/property/measure_utils.h +++ b/frameworks/core/components_ng/property/measure_utils.h @@ -132,7 +132,8 @@ ACE_FORCE_EXPORT OptionalSizeF CreateIdealSize( * @return SizeF the node size info. */ OptionalSizeF CreateIdealSizeByPercentRef( - const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain = false); + const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain = false, + const std::unique_ptr& rawConstraint = nullptr); /** * @brief Create max size for children which is parent's max size minus margin and padding. -- Gitee