diff --git a/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.cpp index d65a534737bf8f846dcca3e7022a3a595640b65f..e31ee4c94b2b67014cbd94ac9910be79cf860841 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.cpp @@ -27,45 +27,31 @@ #include "core/components_ng/property/measure_utils.h" namespace OHOS::Ace::NG { -namespace { -void OffsetByAlign(LayoutConstraintF& layoutConstraint, float rowLen, float colLen, float& positionX, float& positionY) -{ - // only support Alignment.Center now - auto size = layoutConstraint.selfIdealSize; - if (size.Width().has_value()) { - auto width = size.Width().value(); - positionX += (colLen - width) / 2; - } - if (size.Height().has_value()) { - auto height = size.Height().value(); - positionY += (rowLen - height) / 2; - } -} -} // namespace LayoutConstraintF GridLayoutAlgorithm::CreateChildConstraint(const SizeF& idealSize, - const RefPtr& layoutProperty, int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan, - const RefPtr& childLayoutProperty) const + const RefPtr& layoutProperty, int32_t row, int32_t col, int32_t& rowSpan, + int32_t& colSpan) const { LayoutConstraintF layoutConstraint = layoutProperty->CreateChildConstraint(); + auto scale = layoutProperty->GetLayoutConstraint()->scaleProperty; + auto rowsGap = ConvertToPx(layoutProperty->GetRowsGap().value_or(0.0_vp), scale, idealSize.Height()).value_or(0); + auto columnsGap = + ConvertToPx(layoutProperty->GetColumnsGap().value_or(0.0_vp), scale, idealSize.Width()).value_or(0); float rowLen = 0.0; for (int32_t i = 0; i < rowSpan; ++i) { rowLen += gridCells_.at(row + i).at(col).Height(); } - rowLen += (rowSpan - 1) * rowsGap_; + rowLen += (rowSpan - 1) * rowsGap; float colLen = 0.0; for (int32_t i = 0; i < colSpan; ++i) { colLen += gridCells_.at(row).at(col + i).Width(); } - colLen += (colSpan - 1) * columnsGap_; + colLen += (colSpan - 1) * columnsGap; layoutConstraint.maxSize = SizeF(colLen, rowLen); - layoutConstraint.percentReference.UpdateIllegalSizeWithCheck(SizeF(colLen, rowLen)); - if (!childLayoutProperty->GetCalcLayoutConstraint()) { - layoutConstraint.selfIdealSize.UpdateIllegalSizeWithCheck(layoutConstraint.maxSize); - } + layoutConstraint.selfIdealSize.UpdateIllegalSizeWithCheck(SizeF(colLen, rowLen)); return layoutConstraint; } @@ -74,11 +60,12 @@ void GridLayoutAlgorithm::InitGridCeils(LayoutWrapper* layoutWrapper, const Size auto layoutProperty = DynamicCast(layoutWrapper->GetLayoutProperty()); CHECK_NULL_VOID(layoutProperty); auto scale = layoutProperty->GetLayoutConstraint()->scaleProperty; - rowsGap_ = ConvertToPx(layoutProperty->GetRowsGap().value_or(0.0_vp), scale, idealSize.Height()).value_or(0); - columnsGap_ = ConvertToPx(layoutProperty->GetColumnsGap().value_or(0.0_vp), scale, idealSize.Width()).value_or(0); - auto rowsLen = GridUtils::ParseArgs(layoutProperty->GetRowsTemplate().value_or(""), idealSize.Height(), rowsGap_); + auto rowsGap = ConvertToPx(layoutProperty->GetRowsGap().value_or(0.0_vp), scale, idealSize.Height()).value_or(0); + auto columnsGap = + ConvertToPx(layoutProperty->GetColumnsGap().value_or(0.0_vp), scale, idealSize.Width()).value_or(0); + auto rowsLen = GridUtils::ParseArgs(layoutProperty->GetRowsTemplate().value_or(""), idealSize.Height(), rowsGap); auto colsLen = - GridUtils::ParseArgs(layoutProperty->GetColumnsTemplate().value_or(""), idealSize.Width(), columnsGap_); + GridUtils::ParseArgs(layoutProperty->GetColumnsTemplate().value_or(""), idealSize.Width(), columnsGap); if (rowsLen.empty()) { rowsLen.push_back(idealSize.Height()); } @@ -165,12 +152,16 @@ void GridLayoutAlgorithm::GetNextGrid(int32_t& curRow, int32_t& curCol) const } } -OffsetF GridLayoutAlgorithm::ComputeItemPosition(LayoutWrapper* layoutWrapper, int32_t row, int32_t col, - int32_t& rowSpan, int32_t& colSpan, const RefPtr& childLayoutProperty) const +OffsetF GridLayoutAlgorithm::ComputeItemPosition( + LayoutWrapper* layoutWrapper, int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan) const { auto layoutProperty = DynamicCast(layoutWrapper->GetLayoutProperty()); CHECK_NULL_RETURN(layoutProperty, OffsetF()); auto frameSize = layoutWrapper->GetGeometryNode()->GetMarginFrameSize(); + auto scale = layoutProperty->GetLayoutConstraint()->scaleProperty; + auto rowsGap = ConvertToPx(layoutProperty->GetRowsGap().value_or(0.0_vp), scale, frameSize.Height()).value_or(0); + auto columnsGap = + ConvertToPx(layoutProperty->GetColumnsGap().value_or(0.0_vp), scale, frameSize.Width()).value_or(0); // Calculate the position for current child. float positionX = 0.0f; @@ -178,11 +169,11 @@ OffsetF GridLayoutAlgorithm::ComputeItemPosition(LayoutWrapper* layoutWrapper, i for (int32_t i = 0; i < row; ++i) { positionY += gridCells_.at(i).at(0).Height(); } - positionY += row * rowsGap_; + positionY += row * rowsGap; for (int32_t i = 0; i < col; ++i) { positionX += gridCells_.at(0).at(i).Width(); } - positionX += col * columnsGap_; + positionX += col * columnsGap; // Calculate the size for current child. float rowLen = 0.0f; @@ -190,16 +181,11 @@ OffsetF GridLayoutAlgorithm::ComputeItemPosition(LayoutWrapper* layoutWrapper, i for (int32_t i = 0; i < rowSpan; ++i) { rowLen += gridCells_.at(row + i).at(col).Height(); } - rowLen += (rowSpan - 1) * rowsGap_; + rowLen += (rowSpan - 1) * rowsGap; for (int32_t i = 0; i < colSpan; ++i) { colLen += gridCells_.at(row).at(col + i).Width(); } - colLen += (colSpan - 1) * columnsGap_; - - auto layoutConstraint = childLayoutProperty->GetLayoutConstraint(); - if (layoutConstraint.has_value()) { - OffsetByAlign(layoutConstraint.value(), rowLen, colLen, positionX, positionY); - } + colLen += (colSpan - 1) * columnsGap; // If RTL, place the item from right. if (rightToLeft_) { @@ -253,10 +239,10 @@ void GridLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper) if (itemRowStart >= 0 && itemRowStart < mainCount_ && itemColStart >= 0 && itemColStart < crossCount_ && CheckGridPlaced(itemIndex, itemRowStart, itemColStart, itemRowSpan, itemColSpan)) { - childLayoutWrapper->Measure(CreateChildConstraint(idealSize, gridLayoutProperty, itemRowStart, itemColStart, - itemRowSpan, itemColSpan, childLayoutProperty)); - itemsPosition_.try_emplace(index, ComputeItemPosition(layoutWrapper, itemRowStart, itemColStart, - itemRowSpan, itemColSpan, childLayoutProperty)); + childLayoutWrapper->Measure(CreateChildConstraint( + idealSize, gridLayoutProperty, itemRowStart, itemColStart, itemRowSpan, itemColSpan)); + itemsPosition_.try_emplace( + index, ComputeItemPosition(layoutWrapper, itemRowStart, itemColStart, itemRowSpan, itemColSpan)); } else { while (!CheckGridPlaced(itemIndex, rowIndex, colIndex, itemRowSpan, itemColSpan)) { GetNextGrid(rowIndex, colIndex); @@ -267,10 +253,10 @@ void GridLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper) if (rowIndex >= mainCount_ || colIndex >= crossCount_) { continue; } - childLayoutWrapper->Measure(CreateChildConstraint( - idealSize, gridLayoutProperty, rowIndex, colIndex, itemRowSpan, itemColSpan, childLayoutProperty)); - itemsPosition_.try_emplace(index, - ComputeItemPosition(layoutWrapper, rowIndex, colIndex, itemRowSpan, itemColSpan, childLayoutProperty)); + childLayoutWrapper->Measure( + CreateChildConstraint(idealSize, gridLayoutProperty, rowIndex, colIndex, itemRowSpan, itemColSpan)); + itemsPosition_.try_emplace( + index, ComputeItemPosition(layoutWrapper, rowIndex, colIndex, itemRowSpan, itemColSpan)); } ++itemIndex; } diff --git a/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.h b/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.h index 237a81fc9f7d17032a6e28307da93b3c45a2277d..f8d0a7ec3a40bba05cf5df095d6fb10eed730960 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.h +++ b/frameworks/core/components_ng/pattern/grid/grid_layout/grid_layout_algorithm.h @@ -42,20 +42,16 @@ private: void InitGridCeils(LayoutWrapper* layoutWrapper, const SizeF& idealSize); bool CheckGridPlaced(int32_t index, int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan); void GetNextGrid(int32_t& curRow, int32_t& curCol) const; - OffsetF ComputeItemPosition(LayoutWrapper* layoutWrapper, int32_t row, int32_t col, int32_t& rowSpan, - int32_t& colSpan, const RefPtr& childLayoutProperty) const; + OffsetF ComputeItemPosition( + LayoutWrapper* layoutWrapper, int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan) const; LayoutConstraintF CreateChildConstraint(const SizeF& idealSize, const RefPtr& layoutProperty, - int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan, - const RefPtr& childLayoutProperty) const; + int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan) const; int32_t crossCount_ = 0; int32_t mainCount_ = 0; bool isVertical_ = true; bool rightToLeft_ = false; - float rowsGap_ = 0; - float columnsGap_ = 0; - // Map structure: [rowIndex, [columnIndex - (width, height)]]. std::map> gridCells_;