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
14.85k stars 1.08k forks source link

Cursor goes to start when BasicTextField gets empty when using TextAlign.Center #4743

Closed mahozad closed 2 weeks ago

mahozad commented 2 weeks ago

Describe the bug When removing all the text in a basic text field, the cursor (blinking vertical line) goes to the start of text field instead of staying at center.

Screenshot (59)

Affected platforms

Versions

To Reproduce


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

@OptIn(ExperimentalMaterialApi::class)
@Composable
private fun MyCustomTextField() {
    var text by remember { mutableStateOf("1.2.3.4") }
    val interactionSource = remember(::MutableInteractionSource)
    Box(contentAlignment = Alignment.CenterEnd, modifier = Modifier.width(500.dp)) {
        BasicTextField(
            value = text,
            onValueChange = { text = it },
            cursorBrush = SolidColor(MaterialTheme.colors.onSurface),
            textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center), // <<<<<<<<<<<< -------------
            interactionSource = interactionSource,
            modifier = Modifier
                .fillMaxWidth()
                .height(36.dp)
                .background(Color.LightGray)
        ) {
            CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
                TextFieldDefaults.TextFieldDecorationBox(
                    value = text,
                    innerTextField = it,
                    singleLine = true,
                    enabled = true,
                    placeholder = {
                        Text(
                            text = "hint",
                            style = LocalTextStyle.current.copy(textAlign = TextAlign.Center),
                            fontSize = 9.sp,
                            modifier = Modifier.fillMaxWidth()
                        )
                    },
                    visualTransformation = VisualTransformation.None,
                    interactionSource = interactionSource,
                    contentPadding = TextFieldDefaults.textFieldWithoutLabelPadding(top = 0.dp, bottom = 0.dp)
                )
            }
        }
    }
}

Expected behavior The cursor should stay at center or there should be an option to customize its behavior.

igordmn commented 2 weeks ago

Thanks! It was reported before: https://github.com/JetBrains/compose-multiplatform/issues/4611