compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

Bug: Resizing LazyVerticalGrid crashes in combination with item span > 1

Open JuBan1 opened this issue 2 years ago • 0 comments

Platform: Linux Desktop JVM Compose Version: 1.2.0-alpha01-dev755

I'm using a LazyVerticalGrid that has full-width items. Upon resizing the window (and therefore the Grid) I can reliably produce a crash within a few seconds of trying.

Steps to reproduce:

  • Use the code below.
  • Start the application
  • Resize the window by dragging it around. (See attached video.)

Notes:

I have played around with it a little, and identified a few things about this crash:

  • It only happens when items with a non-default span are used. If I remove the header items or set their span={GridItemSpan1)} the crash stops happening.
  • It seems it is important that there are enough items to fill the whole viewport.
  • It's important to use GridCells.Adaptive. The crash won't usually happen using GridCells.Fixed(num). However, I can produce the crash by using Fixed(num) and varying the num, for example via BoxWithContraints { num = maxWidth/200.dp; ... }.

Video: simplescreenrecorder-2022-08-31_20.05.19.zip

Stack trace:

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: List is empty.
	at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:214)
	at androidx.compose.foundation.lazy.grid.LazyGridMeasureKt.measureLazyGrid-zIfe3eg(LazyGridMeasure.kt:196)
	at androidx.compose.foundation.lazy.grid.LazyGridKt$rememberLazyGridMeasurePolicy$1$1.invoke-0kLqBqw(LazyGrid.kt:340)
	at androidx.compose.foundation.lazy.grid.LazyGridKt$rememberLazyGridMeasurePolicy$1$1.invoke(LazyGrid.kt:190)
	at androidx.compose.foundation.lazy.layout.LazyLayoutKt$LazyLayout$2$1.invoke-0kLqBqw(LazyLayout.kt:74)
	at androidx.compose.foundation.lazy.layout.LazyLayoutKt$LazyLayout$2$1.invoke(LazyLayout.kt:70)
	at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$createMeasurePolicy$1.measure-3p2s80s(SubcomposeLayout.kt:590)
	and much more

Code to reproduce:

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.grid.*
import androidx.compose.material.Text
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState

fun main() = application {

    val windowState = rememberWindowState(size = DpSize(800.dp, 1000.dp))

    val items = (0..100).toList()
    Window(onCloseRequest = ::exitApplication, state = windowState) {

        LazyVerticalGrid(
            columns = GridCells.Adaptive(200.dp),
        ) {
            items.chunked(5).forEach {
                item("header-${it.first()}", span = { GridItemSpan(maxLineSpan) }) {
                    Text("Full width header", Modifier.fillMaxSize().background(Color.Blue))
                }
                items(it, key = { it }) { item ->
                    Text("Item $item)", Modifier.fillMaxSize().background(Color.Red))
                }
            }
        }
    }
}

JuBan1 avatar Aug 31 '22 18:08 JuBan1