PlatformTextInputModifierNode

Interface

Common
interface PlatformTextInputModifierNode : DelegatableNode

A modifier node that can connect to the platform's text input IME system. To initiate a text input session, call establishTextInputSession.

Code Examples

platformTextInputModifierNodeSample

@Suppress("UnusedReceiverParameter")
fun platformTextInputModifierNodeSample() {
    class PlatformTextInputModifierNodeSample :
        Modifier.Node(), FocusEventModifierNode, PlatformTextInputModifierNode {
        private var focusedJob: Job? = null
        override fun onFocusEvent(focusState: FocusState) {
            focusedJob?.cancel()
            focusedJob =
                if (focusState.isFocused) {
                    // establishTextInputSession is a suspend function, so it must be called from a
                    // coroutine. Launching it into this modifier node's coroutine scope ensures the
                    // session will automatically be torn down when the node is detached.
                    coroutineScope.launch {
                        // This will automatically cancel any currently-active session.
                        establishTextInputSession {
                            launch {
                                // TODO: Observe text field state, call into system to update it as
                                //  required by the platform.
                            }
                            // Call out to a platform-specific expect/actual function to create the
                            // platform-specific request.
                            val request: PlatformTextInputMethodRequest = createInputRequest()
                            startInputMethod(request)
                        }
                    }
                } else {
                    null
                }
        }
        // This would probably be an expect/actual function.
        fun PlatformTextInputSession.createInputRequest(): PlatformTextInputMethodRequest {
            TODO("Create platform-specific request")
        }
    }
}