Skip to content

Commit 8406dec

Browse files
authored
Merge pull request #65 from cheonjaeung/optimize-boxgrid-arrange
Optimize box grid arrangement
2 parents 5ec6bc1 + 608bf19 commit 8406dec

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

grid/src/commonMain/kotlin/com/cheonjaeung/compose/grid/BoxGridMeasurePolicy.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ private class BoxGridMeasureHelper(
183183
): GridArrangeResult = with(measureScope) {
184184
val placeableCount = measureResult.placeableMeasureInfos.size
185185
val placeablePositionInfos = mutableListOfNulls<PlaceablePositionInfo?>(placeableCount)
186+
val rowCount = cellHeightConstraintList.size
187+
val columnCount = cellWidthConstraintList.size
188+
189+
val horizontalSpacingPx = horizontalSpacing.roundToPx()
190+
val verticalSpacingPx = verticalSpacing.roundToPx()
191+
192+
var currentX = 0
193+
var currentY = 0
194+
val xPositions = IntArray(columnCount)
195+
val yPositions = IntArray(rowCount)
196+
for (i in 0 until columnCount) {
197+
xPositions[i] = currentX
198+
currentX += cellWidthConstraintList[i] + horizontalSpacingPx
199+
}
200+
for (i in 0 until rowCount) {
201+
yPositions[i] = currentY
202+
currentY += cellHeightConstraintList[i] + verticalSpacingPx
203+
}
186204

187205
measureResult.placeableMeasureInfos.fastForEachIndexed { index, placeableMeasureInfo ->
188206
if (placeableMeasureInfo != null) {
@@ -196,25 +214,12 @@ private class BoxGridMeasureHelper(
196214
height = placeableMeasureInfo.cellConstraints.maxHeight
197215
)
198216

199-
val xPosition = if (layoutDirection == LayoutDirection.Ltr) {
200-
val xWithoutSpacing = cellWidthConstraintList.sumOfIndexed { i, c ->
201-
if (i < columnPosition) c else 0
202-
}
203-
xWithoutSpacing + horizontalSpacing.roundToPx() * columnPosition
217+
val xPosition = xPositions[if (layoutDirection == LayoutDirection.Ltr) {
218+
columnPosition
204219
} else {
205-
val layoutWidth = measureResult.layoutSize.width.roundToInt()
206-
val xWithoutSpacing = layoutWidth - cellWidthConstraintList.sumOfIndexed { i, c ->
207-
if (i <= columnPosition) c else 0
208-
}
209-
xWithoutSpacing - horizontalSpacing.roundToPx() * columnPosition
210-
}
211-
212-
val yPosition = run {
213-
val yWithoutSpacing = cellHeightConstraintList.sumOfIndexed { i, c ->
214-
if (i < rowPosition) c else 0
215-
}
216-
yWithoutSpacing + verticalSpacing.roundToPx() * rowPosition
217-
}
220+
columnCount - columnPosition - 1
221+
}]
222+
val yPosition = yPositions[rowPosition]
218223

219224
val alignedOffset = alignment.align(
220225
size = placeable.size(),

grid/src/commonMain/kotlin/com/cheonjaeung/compose/grid/CollectionUtils.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,3 @@ internal fun IntArray.maxOrZero(): Int {
3131
}
3232
return maxValue
3333
}
34-
35-
/**
36-
* Returns the sum of the all values produced by [selector] function applied to each element.
37-
*/
38-
internal inline fun <T> Iterable<T>.sumOfIndexed(selector: (Int, T) -> Int): Int {
39-
var sum = 0
40-
for ((index, element) in this.withIndex()) {
41-
sum += selector(index, element)
42-
}
43-
return sum
44-
}

0 commit comments

Comments
 (0)