|
| 1 | +package com.aliucord.manager.ui.util |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.runtime.Immutable |
| 5 | +import androidx.compose.runtime.Stable |
| 6 | +import androidx.compose.ui.unit.* |
| 7 | +import kotlin.math.min |
| 8 | + |
| 9 | +/** |
| 10 | + * Arranges all the elements with [spacing] except for the last element which is |
| 11 | + * spaced to the very bottom of the viewable component. |
| 12 | + * |
| 13 | + * ref: https://stackoverflow.com/a/69196765/13964629 |
| 14 | + */ |
| 15 | +@Immutable |
| 16 | +class ArrangementLastAtBottom( |
| 17 | + override val spacing: Dp = 0.dp, |
| 18 | +) : Arrangement.Vertical { |
| 19 | + override fun Density.arrange( |
| 20 | + totalSize: Int, |
| 21 | + sizes: IntArray, |
| 22 | + outPositions: IntArray, |
| 23 | + ) { |
| 24 | + if (sizes.isEmpty()) return |
| 25 | + |
| 26 | + val spacingPx = spacing.roundToPx() |
| 27 | + var occupied = 0 |
| 28 | + var lastSpace = 0 |
| 29 | + |
| 30 | + sizes.forEachIndexed { index, size -> |
| 31 | + if (index == sizes.lastIndex) { |
| 32 | + outPositions[index] = totalSize - size |
| 33 | + } else { |
| 34 | + outPositions[index] = min(occupied, totalSize - size) |
| 35 | + } |
| 36 | + |
| 37 | + lastSpace = min(spacingPx, totalSize - outPositions[index] - size) |
| 38 | + occupied = outPositions[index] + size + lastSpace |
| 39 | + } |
| 40 | + |
| 41 | + occupied -= lastSpace |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Arranges all the elements vertically with [space] except for the last element |
| 47 | + * which is drawn at the very bottom of the viewable bounds of the component. |
| 48 | + */ |
| 49 | +@Stable |
| 50 | +@Suppress("UnusedReceiverParameter") |
| 51 | +fun Arrangement.spacedByLastAtBottom(space: Dp) = |
| 52 | + ArrangementLastAtBottom(space) |
0 commit comments