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
16.2k stars 1.17k forks source link

Incorrect text cursor direction for Arabic digits #2772

Closed hfeky closed 1 month ago

hfeky commented 1 year ago

Bug

I'm developing a desktop app that only supports Arabic language. When I try to enter Arabic digits in a TextField, it reverses the text cursor direction when I use the keyboard arrow keys, which confuses the user; left arrow key moves the text cursor to the right, and the right arrow key moves the text cursor to the left. Contrarily, Arabic letters work fine as expected.

Example

val textInput = remember { mutableStateOf(text) }

BasicTextField(
    value = textInput.value.toArabic(),
    onValueChange = {
        textInput.value = it.filter { char ->
            char.isDigit()
        }
    },
    textStyle = TextStyle(
        textAlign = TextAlign.Center,
        textDirection = TextDirection.Rtl // This also doesn't make it work.
    ),
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
)

toArabic() Function

fun String.toArabic(): String {
    val stringBuilder = StringBuilder()
    this.forEach {
        if (it.code in 48..57) {
            stringBuilder.append(it + 1584)
        } else {
            stringBuilder.append(it)
        }
    }
    return stringBuilder.toString()
}

In case it matters, I also have the following wrapped around all other composables for RTL layout direction.

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
    // Draw screen.
}

Affected Platform

Not sure if it also affects other platforms or not, but it affects at least Desktop.

Versions

Steps To Reproduce

  1. Add any TextField composable.
  2. Type Arabic digits such as "١٢٣٤".
  3. Click on left arrow key.
  4. See the issue.

Expected Behavior

The text cursor should move to the left.

Actual Behavior

The text cursor moves to the right.

igordmn commented 1 year ago

Thanks!

@eymar, we should probably revert Arrow Left/Right for an RTL text.

hfeky commented 1 year ago

@igordmn The keyboard arrow keys are already reverted for RTL text. It works fine for Arabic letters but just not for Arabic digits; it behaves as if it identifies Arabic digits text direction as RTL while it should actually be identified as LTR.

hfeky commented 1 year ago

@eymar I was trying to debug the character directionality of Arabic digits like this:

println(Character.getDirectionality('١')) // This returned Character.DIRECTIONALITY_ARABIC_NUMBER.
println(Character.getDirectionality('1')) // This returned Character.DIRECTIONALITY_EUROPEAN_NUMBER.

So the question here is are characters whose directionality is Character.DIRECTIONALITY_ARABIC_NUMBER classified as LTR text in Compose?

okushnikov commented 2 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.