|
|
@ -24,8 +24,8 @@ import androidx.compose.foundation.lazy.grid.LazyGridState
|
|
|
|
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridItemInfo
|
|
|
|
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridItemInfo
|
|
|
|
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridState
|
|
|
|
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridState
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
import androidx.compose.runtime.State
|
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
|
|
import androidx.compose.runtime.produceState
|
|
|
|
import androidx.compose.runtime.remember
|
|
|
|
import androidx.compose.runtime.snapshotFlow
|
|
|
|
import androidx.compose.runtime.snapshotFlow
|
|
|
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
|
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
|
|
import kotlinx.coroutines.flow.filterNotNull
|
|
|
|
import kotlinx.coroutines.flow.filterNotNull
|
|
|
@ -41,57 +41,57 @@ import kotlin.math.min
|
|
|
|
fun LazyListState.scrollbarState(
|
|
|
|
fun LazyListState.scrollbarState(
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemIndex: (LazyListItemInfo) -> Int = LazyListItemInfo::index,
|
|
|
|
itemIndex: (LazyListItemInfo) -> Int = LazyListItemInfo::index,
|
|
|
|
): State<ScrollbarState> = produceState(
|
|
|
|
): ScrollbarState {
|
|
|
|
initialValue = ScrollbarState.FULL,
|
|
|
|
val state = remember { ScrollbarState() }
|
|
|
|
key1 = this,
|
|
|
|
LaunchedEffect(this, itemsAvailable) {
|
|
|
|
key2 = itemsAvailable,
|
|
|
|
snapshotFlow {
|
|
|
|
) {
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
snapshotFlow {
|
|
|
|
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
val firstIndex = min(
|
|
|
|
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
val firstIndex = min(
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
itemSize = { it.size },
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
offset = { it.offset },
|
|
|
|
itemSize = { it.size },
|
|
|
|
nextItemOnMainAxis = { first -> visibleItemsInfo.find { it != first } },
|
|
|
|
offset = { it.offset },
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
nextItemOnMainAxis = { first -> visibleItemsInfo.find { it != first } },
|
|
|
|
),
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
)
|
|
|
|
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
itemSize = itemInfo.size,
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
itemStartOffset = itemInfo.offset,
|
|
|
|
itemSize = itemInfo.size,
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
itemStartOffset = itemInfo.offset,
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
)
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
scrollbarStateValue(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = when {
|
|
|
|
|
|
|
|
layoutInfo.reverseLayout -> 1f - thumbTravelPercent
|
|
|
|
|
|
|
|
else -> thumbTravelPercent
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.filterNotNull()
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
.distinctUntilChanged()
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
.collect { state.onScroll(it) }
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
ScrollbarState(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = when {
|
|
|
|
|
|
|
|
layoutInfo.reverseLayout -> 1f - thumbTravelPercent
|
|
|
|
|
|
|
|
else -> thumbTravelPercent
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.filterNotNull()
|
|
|
|
return state
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
|
|
|
.collect { value = it }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -104,67 +104,67 @@ fun LazyListState.scrollbarState(
|
|
|
|
fun LazyGridState.scrollbarState(
|
|
|
|
fun LazyGridState.scrollbarState(
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemIndex: (LazyGridItemInfo) -> Int = LazyGridItemInfo::index,
|
|
|
|
itemIndex: (LazyGridItemInfo) -> Int = LazyGridItemInfo::index,
|
|
|
|
): State<ScrollbarState> = produceState(
|
|
|
|
): ScrollbarState {
|
|
|
|
initialValue = ScrollbarState.FULL,
|
|
|
|
val state = remember { ScrollbarState() }
|
|
|
|
key1 = this,
|
|
|
|
LaunchedEffect(this, itemsAvailable) {
|
|
|
|
key2 = itemsAvailable,
|
|
|
|
snapshotFlow {
|
|
|
|
) {
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
snapshotFlow {
|
|
|
|
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
val firstIndex = min(
|
|
|
|
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
val firstIndex = min(
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
itemSize = { layoutInfo.orientation.valueOf(it.size) },
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
offset = { layoutInfo.orientation.valueOf(it.offset) },
|
|
|
|
itemSize = { layoutInfo.orientation.valueOf(it.size) },
|
|
|
|
nextItemOnMainAxis = { first ->
|
|
|
|
offset = { layoutInfo.orientation.valueOf(it.offset) },
|
|
|
|
when (layoutInfo.orientation) {
|
|
|
|
nextItemOnMainAxis = { first ->
|
|
|
|
Orientation.Vertical -> visibleItemsInfo.find {
|
|
|
|
when (layoutInfo.orientation) {
|
|
|
|
it != first && it.row != first.row
|
|
|
|
Orientation.Vertical -> visibleItemsInfo.find {
|
|
|
|
}
|
|
|
|
it != first && it.row != first.row
|
|
|
|
|
|
|
|
}
|
|
|
|
Orientation.Horizontal -> visibleItemsInfo.find {
|
|
|
|
|
|
|
|
it != first && it.column != first.column
|
|
|
|
Orientation.Horizontal -> visibleItemsInfo.find {
|
|
|
|
}
|
|
|
|
it != first && it.column != first.column
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
|
|
|
|
itemSize = layoutInfo.orientation.valueOf(itemInfo.size),
|
|
|
|
|
|
|
|
itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),
|
|
|
|
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
scrollbarStateValue(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = when {
|
|
|
|
|
|
|
|
layoutInfo.reverseLayout -> 1f - thumbTravelPercent
|
|
|
|
|
|
|
|
else -> thumbTravelPercent
|
|
|
|
},
|
|
|
|
},
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
|
|
|
|
itemSize = layoutInfo.orientation.valueOf(itemInfo.size),
|
|
|
|
|
|
|
|
itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),
|
|
|
|
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.filterNotNull()
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
.distinctUntilChanged()
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
.collect { state.onScroll(it) }
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
ScrollbarState(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = when {
|
|
|
|
|
|
|
|
layoutInfo.reverseLayout -> 1f - thumbTravelPercent
|
|
|
|
|
|
|
|
else -> thumbTravelPercent
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.filterNotNull()
|
|
|
|
return state
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
|
|
|
.collect { value = it }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -178,56 +178,56 @@ fun LazyGridState.scrollbarState(
|
|
|
|
fun LazyStaggeredGridState.scrollbarState(
|
|
|
|
fun LazyStaggeredGridState.scrollbarState(
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemIndex: (LazyStaggeredGridItemInfo) -> Int = LazyStaggeredGridItemInfo::index,
|
|
|
|
itemIndex: (LazyStaggeredGridItemInfo) -> Int = LazyStaggeredGridItemInfo::index,
|
|
|
|
): State<ScrollbarState> = produceState(
|
|
|
|
): ScrollbarState {
|
|
|
|
initialValue = ScrollbarState.FULL,
|
|
|
|
val state = remember { ScrollbarState() }
|
|
|
|
key1 = this,
|
|
|
|
LaunchedEffect(this, itemsAvailable) {
|
|
|
|
key2 = itemsAvailable,
|
|
|
|
snapshotFlow {
|
|
|
|
) {
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
snapshotFlow {
|
|
|
|
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
val firstIndex = min(
|
|
|
|
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
val firstIndex = min(
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
itemSize = { layoutInfo.orientation.valueOf(it.size) },
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
offset = { layoutInfo.orientation.valueOf(it.offset) },
|
|
|
|
itemSize = { layoutInfo.orientation.valueOf(it.size) },
|
|
|
|
nextItemOnMainAxis = { first ->
|
|
|
|
offset = { layoutInfo.orientation.valueOf(it.offset) },
|
|
|
|
visibleItemsInfo.find { it != first && it.lane == first.lane }
|
|
|
|
nextItemOnMainAxis = { first ->
|
|
|
|
},
|
|
|
|
visibleItemsInfo.find { it != first && it.lane == first.lane }
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
)
|
|
|
|
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
itemSize = layoutInfo.orientation.valueOf(itemInfo.size),
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),
|
|
|
|
itemSize = layoutInfo.orientation.valueOf(itemInfo.size),
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
)
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
scrollbarStateValue(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = thumbTravelPercent,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.filterNotNull()
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
.distinctUntilChanged()
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
.collect { state.onScroll(it) }
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
ScrollbarState(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = thumbTravelPercent,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.filterNotNull()
|
|
|
|
return state
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
|
|
|
.collect { value = it }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private inline fun <T> List<T>.floatSumOf(selector: (T) -> Float): Float {
|
|
|
|
private inline fun <T> List<T>.floatSumOf(selector: (T) -> Float): Float {
|
|
|
|