Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@m-sasha
Copy link

@m-sasha m-sasha commented May 20, 2025

This is a cherry-pick of #2117 into release/1.8.

When determining the focusedRectInRoot, legacyTextInputServiceAdapterAndService needs to transform the position of the caret according to the offsetMapping.

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

Testing

Added a new test, and tested manually with

private const val TEXT_ORIGINAL = "XTest"
private const val TEXT_TRANSFORMED = "Test"

val visualTransformation = object : VisualTransformation {
    val offsetMapping = object : OffsetMapping {
        override fun originalToTransformed(offset: Int) = (offset - 1).coerceAtLeast(0)
        override fun transformedToOriginal(offset: Int) = offset + 1
    }
    override fun filter(text: AnnotatedString) = TransformedText(
        text = AnnotatedString(TEXT_TRANSFORMED),
        offsetMapping = offsetMapping,
    )
}

fun main() = singleWindowApplication {
    Box(
        modifier = Modifier.padding(8.dp),
    ) {
        BasicTextField(
            value = TEXT_ORIGINAL,
            onValueChange = {},
            textStyle = TextStyle(fontSize = 52.sp),
            visualTransformation = visualTransformation,
            modifier = Modifier
                .border(1.dp, Color.Black)
                .padding(4.dp)
        )
    }
}

This could be tested by QA

Release Notes

Fixes - Multiple Platforms

  • Fixed TextField(TextFieldValue) when used with a visual transformation with a non-identity offset mapping (potentially even crashing).

@m-sasha m-sasha requested a review from Kpotko May 20, 2025 09:59
@m-sasha m-sasha changed the title Properly transform caret position in legacyTextInputServiceAdapterAndService [Cherry Pick] Properly transform caret position in legacyTextInputServiceAdapterAndService May 20, 2025
@Kpotko Kpotko merged commit 18fe89b into release/1.8 May 20, 2025
12 checks passed
@Kpotko Kpotko deleted the m-sasha/fix-textfield-selection-with-offsetMapping-1.8-cherry-pick branch May 20, 2025 10:25
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.

3 participants