Add Arrangement.BottomReversed to reverse column direction
This commit is contained in:
parent
28647f3bcc
commit
93bd62545d
40
ui/src/main/java/de/mm20/launcher2/ui/layout/Arrangement.kt
Normal file
40
ui/src/main/java/de/mm20/launcher2/ui/layout/Arrangement.kt
Normal file
@ -0,0 +1,40 @@
|
||||
package de.mm20.launcher2.ui.layout
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.runtime.Stable
|
||||
import androidx.compose.ui.unit.Density
|
||||
|
||||
@Stable
|
||||
val Arrangement.BottomReversed: Arrangement.Vertical
|
||||
get() = object : Arrangement.Vertical {
|
||||
override fun Density.arrange(
|
||||
totalSize: Int,
|
||||
sizes: IntArray,
|
||||
outPositions: IntArray
|
||||
) = placeRightOrBottom(totalSize, sizes, outPositions, reverseInput = true)
|
||||
|
||||
override fun toString() = "Arrangement#BottomReversed"
|
||||
}
|
||||
|
||||
internal fun placeRightOrBottom(
|
||||
totalSize: Int,
|
||||
size: IntArray,
|
||||
outPosition: IntArray,
|
||||
reverseInput: Boolean
|
||||
) {
|
||||
val consumedSize = size.fold(0) { a, b -> a + b }
|
||||
var current = totalSize - consumedSize
|
||||
size.forEachIndexed(reverseInput) { index, it ->
|
||||
outPosition[index] = current
|
||||
current += it
|
||||
}
|
||||
}
|
||||
private inline fun IntArray.forEachIndexed(reversed: Boolean, action: (Int, Int) -> Unit) {
|
||||
if (!reversed) {
|
||||
forEachIndexed(action)
|
||||
} else {
|
||||
for (i in (size - 1) downTo 0) {
|
||||
action(i, get(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user