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 LazyGridScrollbarAdapter to allow for items with unknown row/column at any position in visibleItemsInfo #1707

Conversation

m-sasha
Copy link
Member

@m-sasha m-sasha commented Dec 8, 2024

LazyGridLayoutInfo.visibleItemsInfo appears to allow items with "unknown" row or column to appear at the end of the list, which our current implementation of LazyGridScrollbarAdapter did not expect.

Note that behavior was apparently removed in the latest code merged from upstream, because the bug reproduces in 1.7.1, but not in jb-main tip.

Nevertheless, I think it's worthwhile (and not difficult) to work correctly in this case, which this PR implements.

Fixes https://youtrack.jetbrains.com/issue/CMP-7173

Testing

Only tested the code manually, as it's not possible to directly create a LazyGridState with the required state, and the LazyVerticalGrid implementation in jb-main does not reproduce it (meaning the test will succeed even before the fix).

  • Tested manually on 1.7.1 (commit 6d27653db67c353e15d10292db44d79e8c9a5f7c) to verify that the fix does indeed work.
  • Tested manually on jb-main to verify that scrollbar still works correctly with LazyVerticalGrid.

Code for testing:

fun main() = singleWindowApplication {
    Box {
        val list = remember { mutableStateListOf<Int>() }
        Row {
            val state = rememberLazyGridState()
            LazyVerticalGrid(
                modifier = Modifier.weight(1f).fillMaxHeight(),
                columns = GridCells.Fixed(2),
                state = state,
                verticalArrangement = Arrangement.spacedBy(8.dp),
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                contentPadding = PaddingValues(bottom = 10.dp)
            ) {
                items(list, key = { it }) {
                    Text(
                        it.toString(),
                        Modifier.animateItem().padding(12.dp).clip(RoundedCornerShape(10.dp))
                            .background(Color.LightGray).padding(50.dp)
                    )
                }
            }
            VerticalScrollbar(rememberScrollbarAdapter(state))
        }
        Column(Modifier.align(Alignment.BottomEnd).padding(20.dp)) {
            Button({ list.add(0, list.size) }) { Text("+") }
            Button({ list.removeFirst() }) { Text("-") }
        }
    }
}

Press the "+" button until the window is full of items. Pressing it again should crash the app with:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot round NaN value.
	at kotlin.math.MathKt__MathJVMKt.roundToInt(MathJVM.kt:619)
	at androidx.compose.foundation.Scrollbar_desktopKt.getThumbPixelRange(Scrollbar.skiko.kt:790)
	at androidx.compose.foundation.Scrollbar_desktopKt.access$getThumbPixelRange(Scrollbar.skiko.kt:1)
	at androidx.compose.foundation.Scrollbar_desktopKt$verticalMeasurePolicy$1.measure-3p2s80s(Scrollbar.skiko.kt:804)
	at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:135)
...

Release Notes

Fixes - Desktop

  • Fixed rare crash when using a scrollbar for lazy grid with animated enter/exit items.

…lumn at any position in `visibleItemsInfo`.
@m-sasha m-sasha requested a review from igordmn December 8, 2024 14:59
@m-sasha m-sasha merged commit 9f59218 into jb-main Dec 9, 2024
7 checks passed
@m-sasha m-sasha deleted the m-sasha/fix-LazyGridScrollbarAdapter-for-unknown-lines-at-end branch December 9, 2024 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants