Skip to content

Commit d95ba7a

Browse files
authored
Merge pull request #62 from cheonjaeung/negative-spacing
Fix divide by zero crash when Adaptive's minSize is equal to spacing
2 parents f9e42d1 + 26b9e96 commit d95ba7a

10 files changed

+206
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ interface SimpleGridCells {
101101
spacing: Int
102102
): List<Int> {
103103
val minSizePx = minSize.roundToPx()
104-
val count = max((availableSize + spacing) / (minSizePx + spacing), 1)
104+
val minSizeWithSpacingPx = minSizePx + spacing
105+
val count = if (minSizeWithSpacingPx != 0) {
106+
max((availableSize + spacing) / minSizeWithSpacingPx, 1)
107+
} else {
108+
1
109+
}
105110
val totalSpacing = spacing * (count - 1)
106111
val totalCellSize = availableSize - totalSpacing
107112
val cellSize = totalCellSize / count

grid/src/test/java/com/cheonjaeung/compose/grid/BoxGridTest.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,4 +747,62 @@ class BoxGridTest {
747747
}
748748
}
749749
}
750+
751+
@Test
752+
fun testNegativeSpacing() {
753+
val colors = listOf(
754+
Color.Blue.copy(alpha = 0.5f),
755+
Color.Green.copy(alpha = 0.5f),
756+
Color.Yellow.copy(alpha = 0.5f),
757+
Color.Red.copy(alpha = 0.5f)
758+
)
759+
760+
paparazzi.snapshot {
761+
Column {
762+
BoxGrid(
763+
modifier = Modifier
764+
.fillMaxSize()
765+
.background(Color.LightGray),
766+
rows = SimpleGridCells.Fixed(3),
767+
columns = SimpleGridCells.Fixed(3),
768+
horizontalSpacing = (-8).dp,
769+
verticalSpacing = (-16).dp
770+
) {
771+
for (row in 0 until 3) {
772+
for (column in 0 until 3) {
773+
val index = row * 3 + column
774+
775+
Box(
776+
modifier = Modifier
777+
.position(row, column)
778+
.size(100.dp)
779+
.background(colors[index % 4])
780+
)
781+
}
782+
}
783+
}
784+
}
785+
}
786+
}
787+
788+
@Test
789+
fun testAdaptiveMinSizeEqualsNegativeSpacing() {
790+
paparazzi.snapshot {
791+
BoxGrid(
792+
modifier = Modifier
793+
.fillMaxSize()
794+
.background(Color.LightGray),
795+
rows = SimpleGridCells.Adaptive(100.dp),
796+
columns = SimpleGridCells.Adaptive(100.dp),
797+
horizontalSpacing = (-100).dp,
798+
verticalSpacing = (-100).dp,
799+
) {
800+
Box(
801+
modifier = Modifier
802+
.size(100.dp)
803+
.background(Color.Blue)
804+
)
805+
}
806+
}
807+
}
750808
}

grid/src/test/java/com/cheonjaeung/compose/grid/HorizontalGridArrangementTest.kt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,75 @@ class HorizontalGridArrangementTest {
434434
}
435435
}
436436
}
437+
438+
@Test
439+
fun testNegativeSpacedBy() {
440+
paparazzi.snapshot {
441+
val colors = listOf(
442+
Color.Blue.copy(alpha = 0.5f),
443+
Color.Green.copy(alpha = 0.5f),
444+
Color.Yellow.copy(alpha = 0.5f),
445+
Color.Red.copy(alpha = 0.5f)
446+
)
447+
448+
Column {
449+
HorizontalGrid(
450+
modifier = Modifier
451+
.fillMaxWidth()
452+
.weight(1f)
453+
.background(Color.LightGray),
454+
rows = SimpleGridCells.Fixed(3),
455+
horizontalArrangement = Arrangement.spacedBy((-8).dp),
456+
verticalArrangement = Arrangement.spacedBy((-16).dp)
457+
) {
458+
for (i in 0 until 9) {
459+
Box(
460+
modifier = Modifier
461+
.size(100.dp)
462+
.background(colors[i % 4])
463+
)
464+
}
465+
}
466+
467+
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
468+
HorizontalGrid(
469+
modifier = Modifier
470+
.fillMaxWidth()
471+
.weight(1f)
472+
.background(Color.Gray),
473+
rows = SimpleGridCells.Fixed(3),
474+
horizontalArrangement = Arrangement.spacedBy((-8).dp),
475+
verticalArrangement = Arrangement.spacedBy((-16).dp)
476+
) {
477+
for (i in 0 until 9) {
478+
Box(
479+
modifier = Modifier
480+
.size(100.dp)
481+
.background(colors[i % 4])
482+
)
483+
}
484+
}
485+
}
486+
}
487+
}
488+
}
489+
490+
@Test
491+
fun testAdaptiveMinSizeEqualsNegativeSpacedBy() {
492+
paparazzi.snapshot {
493+
HorizontalGrid(
494+
modifier = Modifier
495+
.fillMaxWidth()
496+
.background(Color.LightGray),
497+
rows = SimpleGridCells.Adaptive(100.dp),
498+
verticalArrangement = Arrangement.spacedBy((-100).dp),
499+
) {
500+
Box(
501+
modifier = Modifier
502+
.size(100.dp)
503+
.background(Color.Blue)
504+
)
505+
}
506+
}
507+
}
437508
}

grid/src/test/java/com/cheonjaeung/compose/grid/VerticalGridArrangementTest.kt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,75 @@ class VerticalGridArrangementTest {
434434
}
435435
}
436436
}
437+
438+
@Test
439+
fun testNegativeSpacedBy() {
440+
paparazzi.snapshot {
441+
val colors = listOf(
442+
Color.Blue.copy(alpha = 0.5f),
443+
Color.Green.copy(alpha = 0.5f),
444+
Color.Yellow.copy(alpha = 0.5f),
445+
Color.Red.copy(alpha = 0.5f)
446+
)
447+
448+
Column {
449+
VerticalGrid(
450+
modifier = Modifier
451+
.fillMaxWidth()
452+
.weight(1f)
453+
.background(Color.LightGray),
454+
columns = SimpleGridCells.Fixed(3),
455+
horizontalArrangement = Arrangement.spacedBy((-8).dp),
456+
verticalArrangement = Arrangement.spacedBy((-16).dp)
457+
) {
458+
for (i in 0 until 9) {
459+
Box(
460+
modifier = Modifier
461+
.size(100.dp)
462+
.background(colors[i % 4])
463+
)
464+
}
465+
}
466+
467+
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
468+
VerticalGrid(
469+
modifier = Modifier
470+
.fillMaxWidth()
471+
.weight(1f)
472+
.background(Color.Gray),
473+
columns = SimpleGridCells.Fixed(3),
474+
horizontalArrangement = Arrangement.spacedBy((-8).dp),
475+
verticalArrangement = Arrangement.spacedBy((-16).dp)
476+
) {
477+
for (i in 0 until 9) {
478+
Box(
479+
modifier = Modifier
480+
.size(100.dp)
481+
.background(colors[i % 4])
482+
)
483+
}
484+
}
485+
}
486+
}
487+
}
488+
}
489+
490+
@Test
491+
fun testAdaptiveMinSizeEqualsNegativeSpacedBy() {
492+
paparazzi.snapshot {
493+
VerticalGrid(
494+
modifier = Modifier
495+
.fillMaxWidth()
496+
.background(Color.LightGray),
497+
columns = SimpleGridCells.Adaptive(100.dp),
498+
horizontalArrangement = Arrangement.spacedBy((-100).dp),
499+
) {
500+
Box(
501+
modifier = Modifier
502+
.size(100.dp)
503+
.background(Color.Blue)
504+
)
505+
}
506+
}
507+
}
437508
}
3.18 KB
Loading
6.07 KB
Loading
Loading
7.64 KB
Loading
Loading
5.42 KB
Loading

0 commit comments

Comments
 (0)