forEachChangeReversed

Function

Common
@ExperimentalFoundationApi
inline fun ChangeList.forEachChangeReversed(
    block: (range: TextRange, originalRange: TextRange) -> Unit
)

Iterates over all the changes in this ChangeList in reverse order.

Changes are iterated by index, so block should not perform any new changes before the current one or changes may be skipped. block may make non-overlapping changes after the current one safely, such changes will not be visited.

Code Examples

BasicTextFieldChangeReverseIterationSample

@Composable
fun BasicTextFieldChangeReverseIterationSample() {
    // Make a text field behave in "insert mode" – inserted text overwrites the text ahead of it
    // instead of being inserted.
    BasicTextField(
        state = rememberTextFieldState(),
        inputTransformation = {
            changes.forEachChangeReversed { range, originalRange ->
                if (!range.collapsed && originalRange.collapsed) {
                    // New text was inserted, delete the text ahead of it.
                    delete(
                        range.end.coerceAtMost(length),
                        (range.end + range.length).coerceAtMost(length),
                    )
                }
            }
        },
    )
}