Skip to content

Commit 4d4a9e7

Browse files
authored
Merge pull request #56 from cheonjaeung/position
Add position modifier to BoxGridScope
2 parents c92bfe8 + a777ed5 commit 4d4a9e7

File tree

11 files changed

+112
-203
lines changed

11 files changed

+112
-203
lines changed

docs/layout-composables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ BoxGrid(
2424
Item(modifier = Modifier.row(1))
2525

2626
// It will be placed at 1, 2.
27-
Item(modifier = Modifier.column(1).row(2))
27+
Item(modifier = Modifier.position(row = 2, column = 1))
2828

2929
// It will be placed at 2, 0.
3030
Itme(modifier = Modifier.column(2))

docs/span.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ BoxGrid(
1414
) {
1515
Item(modifier = Modifier.span { BoxGridItemSpan(row = 2) })
1616
Item(modifier = Modifier.column(2))
17-
Item(modifier = Modifier.row(1).column(1).span { BoxGridItemSpan(column = 2) })
17+
Item(modifier = Modifier.position(row = 1, column = 1).span { BoxGridItemSpan(column = 2) })
1818
Item(modifier = Modifier.row(2)span { BoxGridItemSpan(column = 2) })
1919
}
2020
```

grid/api/android/grid.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public abstract interface class com/cheonjaeung/compose/grid/BoxGridItemSpanScop
2424
public abstract interface class com/cheonjaeung/compose/grid/BoxGridScope {
2525
public abstract fun align (Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Alignment;)Landroidx/compose/ui/Modifier;
2626
public abstract fun column (Landroidx/compose/ui/Modifier;I)Landroidx/compose/ui/Modifier;
27+
public abstract fun position (Landroidx/compose/ui/Modifier;II)Landroidx/compose/ui/Modifier;
2728
public abstract fun row (Landroidx/compose/ui/Modifier;I)Landroidx/compose/ui/Modifier;
2829
public abstract fun span (Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;)Landroidx/compose/ui/Modifier;
2930
}

grid/api/jvm/grid.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public abstract interface class com/cheonjaeung/compose/grid/BoxGridItemSpanScop
2424
public abstract interface class com/cheonjaeung/compose/grid/BoxGridScope {
2525
public abstract fun align (Landroidx/compose/ui/Modifier;Landroidx/compose/ui/Alignment;)Landroidx/compose/ui/Modifier;
2626
public abstract fun column (Landroidx/compose/ui/Modifier;I)Landroidx/compose/ui/Modifier;
27+
public abstract fun position (Landroidx/compose/ui/Modifier;II)Landroidx/compose/ui/Modifier;
2728
public abstract fun row (Landroidx/compose/ui/Modifier;I)Landroidx/compose/ui/Modifier;
2829
public abstract fun span (Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;)Landroidx/compose/ui/Modifier;
2930
}

grid/src/commonMain/kotlin/com/cheonjaeung/compose/grid/BoxGridRowColumnModifier.kt renamed to grid/src/commonMain/kotlin/com/cheonjaeung/compose/grid/BoxGridCellPositionModifier.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import androidx.compose.ui.node.ParentDataModifierNode
66
import androidx.compose.ui.platform.InspectorInfo
77
import androidx.compose.ui.unit.Density
88

9-
internal class BoxGridRowColumnElement(
10-
val row: Int = BoxGridParentData.UNSPECIFIED_ROW,
11-
val column: Int = BoxGridParentData.UNSPECIFIED_COLUMN,
9+
internal class BoxGridCellPositionElement(
10+
val row: Int? = null,
11+
val column: Int? = null,
1212
val inspectorInfo: InspectorInfo.() -> Unit
13-
) : ModifierNodeElement<BoxGridRowColumnNode>() {
14-
override fun create(): BoxGridRowColumnNode {
15-
return BoxGridRowColumnNode(row, column)
13+
) : ModifierNodeElement<BoxGridCellPositionNode>() {
14+
override fun create(): BoxGridCellPositionNode {
15+
return BoxGridCellPositionNode(row, column)
1616
}
1717

18-
override fun update(node: BoxGridRowColumnNode) {
18+
override fun update(node: BoxGridCellPositionNode) {
1919
node.row = row
2020
node.column = column
2121
}
@@ -27,7 +27,7 @@ internal class BoxGridRowColumnElement(
2727
override fun equals(other: Any?): Boolean {
2828
if (this === other) return true
2929
if (other == null) return false
30-
if (other !is BoxGridRowColumnElement) return false
30+
if (other !is BoxGridCellPositionElement) return false
3131
return this.row == other.row && this.column == other.column
3232
}
3333

@@ -38,16 +38,16 @@ internal class BoxGridRowColumnElement(
3838
}
3939
}
4040

41-
internal class BoxGridRowColumnNode(
42-
var row: Int,
43-
var column: Int
41+
internal class BoxGridCellPositionNode(
42+
var row: Int?,
43+
var column: Int?
4444
) : Modifier.Node(), ParentDataModifierNode {
4545
override fun Density.modifyParentData(parentData: Any?): Any {
4646
val p = parentData as? BoxGridParentData ?: BoxGridParentData()
47-
if (row != BoxGridParentData.UNSPECIFIED_ROW) {
47+
if (row != null) {
4848
p.row = row
4949
}
50-
if (column != BoxGridParentData.UNSPECIFIED_COLUMN) {
50+
if (column != null) {
5151
p.column = column
5252
}
5353
return p

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,10 @@ private class BoxGridMeasureHelper(
9393
}
9494

9595
private val BoxGridParentData?.rowOrDefault: Int
96-
get() {
97-
val row = this?.row
98-
return if (row == null || row == BoxGridParentData.UNSPECIFIED_ROW) {
99-
BoxGridParentData.DEFAULT_ROW
100-
} else {
101-
row
102-
}
103-
}
96+
get() = this?.row ?: 0
10497

10598
private val BoxGridParentData?.columnOrDefault: Int
106-
get() {
107-
val column = this?.column
108-
return if (column == null || column == BoxGridParentData.UNSPECIFIED_COLUMN) {
109-
BoxGridParentData.DEFAULT_COLUMN
110-
} else {
111-
column
112-
}
113-
}
99+
get() = this?.column ?: 0
114100

115101
private val BoxGridParentData?.alignmentOrDefault: Alignment
116102
get() = this?.alignment ?: defaultAlignment

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,8 @@ package com.cheonjaeung.compose.grid
33
import androidx.compose.ui.Alignment
44

55
internal data class BoxGridParentData(
6-
var row: Int = UNSPECIFIED_ROW,
7-
var column: Int = UNSPECIFIED_COLUMN,
6+
var row: Int? = null,
7+
var column: Int? = null,
88
var span: (BoxGridItemSpanScope.() -> BoxGridItemSpan)? = null,
99
var alignment: Alignment? = null
10-
) {
11-
companion object {
12-
internal const val UNSPECIFIED_ROW: Int = -1
13-
internal const val UNSPECIFIED_COLUMN: Int = -1
14-
internal const val DEFAULT_ROW: Int = 0
15-
internal const val DEFAULT_COLUMN: Int = 0
16-
}
17-
}
10+
)

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ interface BoxGridScope {
3030
@ExperimentalGridApi
3131
fun Modifier.column(column: Int): Modifier
3232

33+
/**
34+
* Sets the row and column position of the cell. The position starts from 0 and
35+
* the default is 0.
36+
*/
37+
@Stable
38+
@ExperimentalGridApi
39+
fun Modifier.position(row: Int, column: Int): Modifier
40+
3341
/**
3442
* Sets the row and column span of the cell. The default span size is 1.
3543
*
@@ -53,7 +61,7 @@ internal object BoxGridScopeInstance : BoxGridScope {
5361
override fun Modifier.row(row: Int): Modifier {
5462
require(row >= 0) { "$row is invalid value, must be zero or positive" }
5563
return this.then(
56-
BoxGridRowColumnElement(
64+
BoxGridCellPositionElement(
5765
row = row,
5866
inspectorInfo = debugInspectorInfo {
5967
name = "row"
@@ -66,7 +74,7 @@ internal object BoxGridScopeInstance : BoxGridScope {
6674
override fun Modifier.column(column: Int): Modifier {
6775
require(column >= 0) { "$column is invalid value, must be zero or positive" }
6876
return this.then(
69-
BoxGridRowColumnElement(
77+
BoxGridCellPositionElement(
7078
column = column,
7179
inspectorInfo = debugInspectorInfo {
7280
name = "column"
@@ -76,6 +84,22 @@ internal object BoxGridScopeInstance : BoxGridScope {
7684
)
7785
}
7886

87+
override fun Modifier.position(row: Int, column: Int): Modifier {
88+
require(row >= 0) { "$row is invalid value, must be zero or positive" }
89+
require(column >= 0) { "$column is invalid value, must be zero or positive" }
90+
return this.then(
91+
BoxGridCellPositionElement(
92+
row = row,
93+
column = column,
94+
inspectorInfo = debugInspectorInfo {
95+
name = "position"
96+
properties["row"] = row
97+
properties["column"] = column
98+
}
99+
)
100+
)
101+
}
102+
79103
override fun Modifier.span(span: (BoxGridItemSpanScope.() -> BoxGridItemSpan)?): Modifier {
80104
return this.then(
81105
BoxGridSpanElement(

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class BoxGridAlignmentTest {
4949

5050
Box(
5151
modifier = Modifier
52-
.row(row)
53-
.column(column)
52+
.position(row, column)
5453
.size(100.dp)
5554
.background(colors[index % 4])
5655
)
@@ -73,8 +72,7 @@ class BoxGridAlignmentTest {
7372

7473
Box(
7574
modifier = Modifier
76-
.row(row)
77-
.column(column)
75+
.position(row, column)
7876
.size(100.dp)
7977
.background(colors[index % 4])
8078
)
@@ -124,8 +122,7 @@ class BoxGridAlignmentTest {
124122

125123
Box(
126124
modifier = Modifier
127-
.row(row)
128-
.column(column)
125+
.position(row, column)
129126
.size(100.dp)
130127
.background(colors[index % 4])
131128
.align(alignment)
@@ -150,8 +147,7 @@ class BoxGridAlignmentTest {
150147

151148
Box(
152149
modifier = Modifier
153-
.row(row)
154-
.column(column)
150+
.position(row, column)
155151
.size(100.dp)
156152
.background(colors[index % 4])
157153
.align(alignment)
@@ -203,8 +199,7 @@ class BoxGridAlignmentTest {
203199

204200
Box(
205201
modifier = Modifier
206-
.row(row)
207-
.column(column)
202+
.position(row, column)
208203
.size(100.dp)
209204
.background(colors[index % 4])
210205
.then(
@@ -236,8 +231,7 @@ class BoxGridAlignmentTest {
236231

237232
Box(
238233
modifier = Modifier
239-
.row(row)
240-
.column(column)
234+
.position(row, column)
241235
.size(100.dp)
242236
.background(colors[index % 4])
243237
.then(

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ class BoxGridSpanTest {
3636
)
3737
Box(
3838
modifier = Modifier
39-
.row(1)
40-
.column(1)
39+
.position(row = 1, column = 1)
4140
.size(100.dp)
4241
.background(Color.Green)
4342
)
4443
Box(
4544
modifier = Modifier
46-
.row(2)
47-
.column(2)
45+
.position(row = 2, column = 2)
4846
.size(100.dp)
4947
.background(Color.Yellow)
5048
)
@@ -70,16 +68,14 @@ class BoxGridSpanTest {
7068
)
7169
Box(
7270
modifier = Modifier
73-
.row(1)
74-
.column(1)
71+
.position(row = 1, column = 1)
7572
.size(100.dp)
7673
.background(Color.Green)
7774
.span { BoxGridItemSpan(row = 1, column = 1) }
7875
)
7976
Box(
8077
modifier = Modifier
81-
.row(2)
82-
.column(2)
78+
.position(row = 2, column = 2)
8379
.size(100.dp)
8480
.background(Color.Yellow)
8581
.span { BoxGridItemSpan(row = 1, column = 1) }
@@ -106,16 +102,14 @@ class BoxGridSpanTest {
106102
)
107103
Box(
108104
modifier = Modifier
109-
.row(1)
110-
.column(1)
105+
.position(row = 1, column = 1)
111106
.size(100.dp)
112107
.background(Color.Green)
113108
.span { BoxGridItemSpan(row = 2, column = 2) }
114109
)
115110
Box(
116111
modifier = Modifier
117-
.row(2)
118-
.column(2)
112+
.position(row = 2, column = 2)
119113
.size(100.dp)
120114
.background(Color.Yellow)
121115
.span { BoxGridItemSpan(column = 2) }
@@ -142,16 +136,14 @@ class BoxGridSpanTest {
142136
)
143137
Box(
144138
modifier = Modifier
145-
.row(1)
146-
.column(1)
139+
.position(row = 1, column = 1)
147140
.size(100.dp)
148141
.background(Color.Green)
149142
.span { BoxGridItemSpan(row = maxRowSpan + 1, column = maxColumnSpan + 1) }
150143
)
151144
Box(
152145
modifier = Modifier
153-
.row(2)
154-
.column(2)
146+
.position(row = 2, column = 2)
155147
.size(100.dp)
156148
.background(Color.Yellow)
157149
.span { BoxGridItemSpan(column = 2) }
@@ -178,15 +170,13 @@ class BoxGridSpanTest {
178170
)
179171
Box(
180172
modifier = Modifier
181-
.row(1)
182-
.column(1)
173+
.position(row = 1, column = 1)
183174
.size(100.dp)
184175
.background(Color.Green)
185176
)
186177
Box(
187178
modifier = Modifier
188-
.row(2)
189-
.column(2)
179+
.position(row = 2, column = 2)
190180
.size(100.dp)
191181
.background(Color.Yellow)
192182
)
@@ -211,16 +201,14 @@ class BoxGridSpanTest {
211201
)
212202
Box(
213203
modifier = Modifier
214-
.row(1)
215-
.column(1)
204+
.position(row = 1, column = 1)
216205
.size(100.dp)
217206
.background(Color.Green)
218207
.span { BoxGridItemSpan(row = maxCurrentRowSpan) }
219208
)
220209
Box(
221210
modifier = Modifier
222-
.row(2)
223-
.column(2)
211+
.position(row = 2, column = 2)
224212
.size(100.dp)
225213
.background(Color.Yellow)
226214
.span { BoxGridItemSpan(column = maxCurrentColumnSpan) }

0 commit comments

Comments
 (0)