Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the scroll bar thumb jumping when at the end of the LazyStaggeredList with more than one lane and improve smothness of the scroll #1553

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ internal fun itemVisibilityPercentage(
viewportEndOffset: Int,
): Float {
if (itemSize == 0) return 0f
// TODO: Workaround due to issue b/353143657, monitor if compose team will agree it's an issue
// and fix it, if so, below safeguard probably can be removed
if (
isItemOutOfViewport(
itemSize = itemSize,
itemStartOffset = itemStartOffset,
viewportStartOffset = viewportStartOffset,
viewportEndOffset = viewportEndOffset,
)
) {
return 0f
}

val itemEnd = itemStartOffset + itemSize
val startOffset = when {
itemStartOffset > viewportStartOffset -> 0
Expand All @@ -86,3 +99,28 @@ internal fun itemVisibilityPercentage(
val size = itemSize.toFloat()
return (size - startOffset - endOffset) / size
}

private fun isItemOutOfViewport(
itemSize: Int,
itemStartOffset: Int,
viewportStartOffset: Int,
viewportEndOffset: Int,
) = isItemBeforeViewport(
itemSize = itemSize,
itemStartOffset = itemStartOffset,
viewportStartOffset = viewportStartOffset,
) || isItemAfterViewport(
itemStartOffset = itemStartOffset,
viewportEndOffset = viewportEndOffset,
)

private fun isItemBeforeViewport(
itemSize: Int,
itemStartOffset: Int,
viewportStartOffset: Int,
) = (viewportStartOffset - itemStartOffset) > itemSize

private fun isItemAfterViewport(
itemStartOffset: Int,
viewportEndOffset: Int,
) = itemStartOffset > viewportEndOffset
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ fun LazyGridState.scrollbarState(

val firstIndex = min(
a = interpolateFirstItemIndex(
visibleItems = visibleItemsInfo,
visibleItems = when (layoutInfo.orientation) {
Orientation.Vertical -> layoutInfo.visibleItemsInfo.filter { it.row == 0 }
Orientation.Horizontal -> layoutInfo.visibleItemsInfo.filter { it.column == 0 }
},
itemSize = { layoutInfo.orientation.valueOf(it.size) },
offset = { layoutInfo.orientation.valueOf(it.offset) },
nextItemOnMainAxis = { first ->
Expand Down Expand Up @@ -189,7 +192,7 @@ fun LazyStaggeredGridState.scrollbarState(

val firstIndex = min(
a = interpolateFirstItemIndex(
visibleItems = visibleItemsInfo,
visibleItems = layoutInfo.visibleItemsInfo.filter { it.lane == 0 },
itemSize = { layoutInfo.orientation.valueOf(it.size) },
offset = { layoutInfo.orientation.valueOf(it.offset) },
nextItemOnMainAxis = { first ->
Expand Down