TextInputSession

Class

Common

Deprecated Use PlatformTextInputModifierNode instead.

class TextInputSession(
    private val textInputService: TextInputService,
    private val platformTextInputService: PlatformTextInputService,
)

Represents a input session for interactions between a soft keyboard and editable text.

This session may be closed at any time by TextInputService or by calling dispose, after which isOpen will return false and all further calls will have no effect.

Properties

Common
val isOpen: Boolean

If this session is currently open.

A session may be closed at any time by TextInputService or by calling dispose.

Functions

fun dispose()

Close this input session.

All further calls to this object will have no effect, and isOpen will return false.

Note, TextInputService may also close this input session at any time without calling dispose. Calling dispose after this session has been closed has no effect.

fun notifyFocusedRect(rect: Rect): Boolean

Notify the focused rectangle to the system.

The system can ignore this information or use it to for additional functionality.

For example, desktop systems show a popup near the focused input area (for some languages).

If the session is not open, no action will be performed.

Parameters

rectthe rectangle that describes the boundaries on the screen that requires focus

Returns

false if this session expired and no action was performed
fun updateTextLayoutResult(
        textFieldValue: TextFieldValue,
        offsetMapping: OffsetMapping,
        textLayoutResult: TextLayoutResult,
        textFieldToRootTransform: (Matrix) -> Unit,
        innerTextFieldBounds: Rect,
        decorationBoxBounds: Rect,
    ) = ensureOpenSession {
        platformTextInputService.updateTextLayoutResult(
            textFieldValue,
            offsetMapping,
            textLayoutResult,
            textFieldToRootTransform,
            innerTextFieldBounds,
            decorationBoxBounds,
        )
    }

Notify the input service of layout and position changes.

Parameters

textFieldValuethe text field's TextFieldValue
offsetMappingthe offset mapping for the visual transformation
textLayoutResultthe text field's TextLayoutResult
textFieldToRootTransformfunction that modifies a matrix to be a transformation matrix from local coordinates to the root composable coordinates
innerTextFieldBoundsvisible bounds of the text field in text layout coordinates, or an empty rectangle if the text field is not visible
decorationBoxBoundsvisible bounds of the decoration box in text layout coordinates, or an empty rectangle if the decoration box is not visible
fun updateState(oldValue: TextFieldValue?, newValue: TextFieldValue): Boolean

Notify IME about the new TextFieldValue and latest state of the editing buffer. oldValue is the state of the buffer before the changes applied by the newValue.

oldValue represents the changes that was requested by IME on the buffer, and newValue is the final state of the editing buffer that was requested by the application. In cases where oldValue is not equal to newValue, it would mean the IME suggested value is rejected, and the IME connection will be restarted with the newValue.

If the session is not open, action will be performed.

Parameters

oldValuethe value that was requested by IME on the buffer
newValuefinal state of the editing buffer that was requested by the application

Returns

false if this session expired and no action was performed
fun showSoftwareKeyboard(): Boolean

Request showing onscreen keyboard.

This call will have no effect if this session is not open.

This should be used instead of TextInputService.showSoftwareKeyboard when implementing a new editable text composable to show the keyboard in response to events related to that composable.

There is no guarantee that the keyboard will be shown. The software keyboard or system service may silently ignore this request.

Returns

false if this session expired and no action was performed
fun hideSoftwareKeyboard(): Boolean

Hide onscreen keyboard for a specific TextInputSession.

This call will have no effect if this session is not open.

This should be used instead of TextInputService.showSoftwareKeyboard when implementing a new editable text composable to hide the keyboard in response to events related to that composable.

Returns

false if this session expired and no action was performed