JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.73k stars 1.14k forks source link

BasicTextField and BasicText cannot select styled ranges from annotated string #2135

Open b-camphart opened 2 years ago

b-camphart commented 2 years ago

Summary

When using a BasicTextField, if the supplied AnnotatedString contains a SpanStyle that does not cover the entire text and the TextFieldValue has a selection that starts within the SpanStyle range and ends outside it, the application stops rendering.

Code to reproduce

fun main() = application {
    Window(onCloseRequest = ::exitApplication) {

        // "abcd", 'b' will be bold, 'bc' will be selected
        val (value, setValue) = remember { mutableStateOf(TextFieldValue(AnnotatedString("abcd", spanStyles = listOf(
            AnnotatedString.Range(SpanStyle(fontWeight = FontWeight.Bold), 1, 2)
        )), selection = TextRange(1, 3))) }

        BasicTextField(
            value = value,
            onValueChange = setValue,
        )
    }
}

Result

The application opens and the window can still be moved, but the window is black. If this is done after the application has been running, all future rendering will stop, but composition logic is still being performed.

Environment

b-camphart commented 2 years ago

Additional notes:

b-camphart commented 2 years ago

Additionally, this applies to the BasicText component too. If you run the following test:

@Composable
fun TextFieldTest() {

    val value = AnnotatedString(
        "abcd", spanStyles = listOf(
            AnnotatedString.Range(SpanStyle(fontWeight = FontWeight.Bold), 1, 2)
        )
    )

    SelectionContainer {
        BasicText(text = value)
    }

}

fun main() {
    singleWindowApplication {
        TextFieldTest()
    }
}

You will be unable to select the bolded text and, if you try, the selection no longer responds appropriately.

noe commented 1 year ago

Maybe related to this compose issue: https://issuetracker.google.com/issues/135556699