|
|
@ -1,5 +1,5 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright 2021 The Android Open Source Project
|
|
|
|
* Copyright 2023 The Android Open Source Project
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@ -21,7 +21,14 @@ import androidx.compose.foundation.lazy.LazyListItemInfo
|
|
|
|
import androidx.compose.foundation.lazy.LazyListState
|
|
|
|
import androidx.compose.foundation.lazy.LazyListState
|
|
|
|
import androidx.compose.foundation.lazy.grid.LazyGridItemInfo
|
|
|
|
import androidx.compose.foundation.lazy.grid.LazyGridItemInfo
|
|
|
|
import androidx.compose.foundation.lazy.grid.LazyGridState
|
|
|
|
import androidx.compose.foundation.lazy.grid.LazyGridState
|
|
|
|
|
|
|
|
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridItemInfo
|
|
|
|
|
|
|
|
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridState
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
|
|
|
|
import androidx.compose.runtime.produceState
|
|
|
|
|
|
|
|
import androidx.compose.runtime.snapshotFlow
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.filterNotNull
|
|
|
|
|
|
|
|
import kotlin.math.min
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Calculates a [ScrollbarState] driven by the changes in a [LazyListState].
|
|
|
|
* Calculates a [ScrollbarState] driven by the changes in a [LazyListState].
|
|
|
@ -33,29 +40,58 @@ import androidx.compose.runtime.Composable
|
|
|
|
fun LazyListState.scrollbarState(
|
|
|
|
fun LazyListState.scrollbarState(
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemIndex: (LazyListItemInfo) -> Int = LazyListItemInfo::index,
|
|
|
|
itemIndex: (LazyListItemInfo) -> Int = LazyListItemInfo::index,
|
|
|
|
): ScrollbarState =
|
|
|
|
): ScrollbarState = produceState(
|
|
|
|
scrollbarState(
|
|
|
|
initialValue = ScrollbarState.FULL,
|
|
|
|
itemsAvailable = itemsAvailable,
|
|
|
|
key1 = this,
|
|
|
|
visibleItems = { layoutInfo.visibleItemsInfo },
|
|
|
|
key2 = itemsAvailable,
|
|
|
|
firstVisibleItemIndex = { visibleItems ->
|
|
|
|
) {
|
|
|
|
interpolateFirstItemIndex(
|
|
|
|
snapshotFlow {
|
|
|
|
visibleItems = visibleItems,
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val firstIndex = min(
|
|
|
|
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
itemSize = { it.size },
|
|
|
|
itemSize = { it.size },
|
|
|
|
offset = { it.offset },
|
|
|
|
offset = { it.offset },
|
|
|
|
nextItemOnMainAxis = { first -> visibleItems.find { it != first } },
|
|
|
|
nextItemOnMainAxis = { first -> visibleItemsInfo.find { it != first } },
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
},
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
itemPercentVisible = itemPercentVisible@{ itemInfo ->
|
|
|
|
)
|
|
|
|
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
itemSize = itemInfo.size,
|
|
|
|
itemSize = itemInfo.size,
|
|
|
|
itemStartOffset = itemInfo.offset,
|
|
|
|
itemStartOffset = itemInfo.offset,
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
reverseLayout = { layoutInfo.reverseLayout },
|
|
|
|
|
|
|
|
)
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
ScrollbarState(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = when {
|
|
|
|
|
|
|
|
layoutInfo.reverseLayout -> 1f - thumbTravelPercent
|
|
|
|
|
|
|
|
else -> thumbTravelPercent
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.filterNotNull()
|
|
|
|
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
|
|
|
.collect { value = it }
|
|
|
|
|
|
|
|
}.value
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Calculates a [ScrollbarState] driven by the changes in a [LazyGridState]
|
|
|
|
* Calculates a [ScrollbarState] driven by the changes in a [LazyGridState]
|
|
|
@ -67,38 +103,136 @@ fun LazyListState.scrollbarState(
|
|
|
|
fun LazyGridState.scrollbarState(
|
|
|
|
fun LazyGridState.scrollbarState(
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemsAvailable: Int,
|
|
|
|
itemIndex: (LazyGridItemInfo) -> Int = LazyGridItemInfo::index,
|
|
|
|
itemIndex: (LazyGridItemInfo) -> Int = LazyGridItemInfo::index,
|
|
|
|
): ScrollbarState =
|
|
|
|
): ScrollbarState = produceState(
|
|
|
|
scrollbarState(
|
|
|
|
initialValue = ScrollbarState.FULL,
|
|
|
|
itemsAvailable = itemsAvailable,
|
|
|
|
key1 = this,
|
|
|
|
visibleItems = { layoutInfo.visibleItemsInfo },
|
|
|
|
key2 = itemsAvailable,
|
|
|
|
firstVisibleItemIndex = { visibleItems ->
|
|
|
|
) {
|
|
|
|
interpolateFirstItemIndex(
|
|
|
|
snapshotFlow {
|
|
|
|
visibleItems = visibleItems,
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
itemSize = {
|
|
|
|
|
|
|
|
layoutInfo.orientation.valueOf(it.size)
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
},
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val firstIndex = min(
|
|
|
|
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
|
|
|
|
itemSize = { layoutInfo.orientation.valueOf(it.size) },
|
|
|
|
offset = { layoutInfo.orientation.valueOf(it.offset) },
|
|
|
|
offset = { layoutInfo.orientation.valueOf(it.offset) },
|
|
|
|
nextItemOnMainAxis = { first ->
|
|
|
|
nextItemOnMainAxis = { first ->
|
|
|
|
when (layoutInfo.orientation) {
|
|
|
|
when (layoutInfo.orientation) {
|
|
|
|
Orientation.Vertical -> visibleItems.find {
|
|
|
|
Orientation.Vertical -> visibleItemsInfo.find {
|
|
|
|
it != first && it.row != first.row
|
|
|
|
it != first && it.row != first.row
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Orientation.Horizontal -> visibleItems.find {
|
|
|
|
Orientation.Horizontal -> visibleItemsInfo.find {
|
|
|
|
it != first && it.column != first.column
|
|
|
|
it != first && it.column != first.column
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
itemPercentVisible = itemPercentVisible@{ itemInfo ->
|
|
|
|
|
|
|
|
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
ScrollbarState(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = when {
|
|
|
|
|
|
|
|
layoutInfo.reverseLayout -> 1f - thumbTravelPercent
|
|
|
|
|
|
|
|
else -> thumbTravelPercent
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.filterNotNull()
|
|
|
|
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
|
|
|
.collect { value = it }
|
|
|
|
|
|
|
|
}.value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Remembers a [ScrollbarState] driven by the changes in a [LazyStaggeredGridState]
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param itemsAvailable the total amount of items available to scroll in the staggered grid.
|
|
|
|
|
|
|
|
* @param itemIndex a lookup function for index of an item in the staggered grid relative
|
|
|
|
|
|
|
|
* to [itemsAvailable].
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
|
|
|
fun LazyStaggeredGridState.scrollbarState(
|
|
|
|
|
|
|
|
itemsAvailable: Int,
|
|
|
|
|
|
|
|
itemIndex: (LazyStaggeredGridItemInfo) -> Int = LazyStaggeredGridItemInfo::index,
|
|
|
|
|
|
|
|
): ScrollbarState = produceState(
|
|
|
|
|
|
|
|
initialValue = ScrollbarState.FULL,
|
|
|
|
|
|
|
|
key1 = this,
|
|
|
|
|
|
|
|
key2 = itemsAvailable,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
snapshotFlow {
|
|
|
|
|
|
|
|
if (itemsAvailable == 0) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val visibleItemsInfo = layoutInfo.visibleItemsInfo
|
|
|
|
|
|
|
|
if (visibleItemsInfo.isEmpty()) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val firstIndex = min(
|
|
|
|
|
|
|
|
a = interpolateFirstItemIndex(
|
|
|
|
|
|
|
|
visibleItems = visibleItemsInfo,
|
|
|
|
|
|
|
|
itemSize = { layoutInfo.orientation.valueOf(it.size) },
|
|
|
|
|
|
|
|
offset = { layoutInfo.orientation.valueOf(it.offset) },
|
|
|
|
|
|
|
|
nextItemOnMainAxis = { first ->
|
|
|
|
|
|
|
|
visibleItemsInfo.find { it != first && it.lane == first.lane }
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
itemIndex = itemIndex,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
b = itemsAvailable.toFloat(),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
if (firstIndex.isNaN()) return@snapshotFlow null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val itemsVisible = visibleItemsInfo.floatSumOf { itemInfo ->
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
itemVisibilityPercentage(
|
|
|
|
itemSize = layoutInfo.orientation.valueOf(itemInfo.size),
|
|
|
|
itemSize = layoutInfo.orientation.valueOf(itemInfo.size),
|
|
|
|
itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),
|
|
|
|
itemStartOffset = layoutInfo.orientation.valueOf(itemInfo.offset),
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
viewportStartOffset = layoutInfo.viewportStartOffset,
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
viewportEndOffset = layoutInfo.viewportEndOffset,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
reverseLayout = { layoutInfo.reverseLayout },
|
|
|
|
|
|
|
|
)
|
|
|
|
val thumbTravelPercent = min(
|
|
|
|
|
|
|
|
a = firstIndex / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
val thumbSizePercent = min(
|
|
|
|
|
|
|
|
a = itemsVisible / itemsAvailable,
|
|
|
|
|
|
|
|
b = 1f,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
ScrollbarState(
|
|
|
|
|
|
|
|
thumbSizePercent = thumbSizePercent,
|
|
|
|
|
|
|
|
thumbMovedPercent = thumbTravelPercent,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.filterNotNull()
|
|
|
|
|
|
|
|
.distinctUntilChanged()
|
|
|
|
|
|
|
|
.collect { value = it }
|
|
|
|
|
|
|
|
}.value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private inline fun <T> List<T>.floatSumOf(selector: (T) -> Float): Float {
|
|
|
|
|
|
|
|
var sum = 0f
|
|
|
|
|
|
|
|
for (element in this) {
|
|
|
|
|
|
|
|
sum += selector(element)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum
|
|
|
|
|
|
|
|
}
|
|
|
|