Skip to content

Native iOS text input#2602

Merged
mazunin-v-jb merged 4 commits intojb-mainfrom
v.mazunin/native-ios-text-input
Mar 3, 2026
Merged

Native iOS text input#2602
mazunin-v-jb merged 4 commits intojb-mainfrom
v.mazunin/native-ios-text-input

Conversation

@mazunin-v-jb
Copy link

@mazunin-v-jb mazunin-v-jb commented Nov 27, 2025

This change introduces an opt-in Native iOS Text Input (NITI) mode for Compose TextField on iOS, enabled via PlatformImeOptions.usingNativeTextInput(enabled) in the iOS source set

With the flag set to true, Compose text fields use UIKit-native text editing and interaction behavior, details in the release notes

Fixes:
https://youtrack.jetbrains.com/issue/CMP-8208/Implement-iOS-native-text-input
https://youtrack.jetbrains.com/issue/CMP-332

Key changes

  • Added a new PlatformImeOptions flag: usingNativeTextInput
  • Split iOS text input and context menu logic into "NITI vs non-NITI" code paths in:
    • UIKitTextInputService.ios.kt
    • IntermediateTextInputUIView.ios.kt
    • ContextMenu.ios.kt
  • When NITI is enabled, Compose-drawn caret and selection handles are suppressed, so UIKit becomes the single source of truth for text controls UI
  • Replaced setNeedsRedraw with an explicit draw() call in updateView closure in UIKitTextInputService to eliminate a race condition between UIKit UITextInput queries and TextLayoutResult updates. This could leave iOS with stale text field state, which in NITI mode resulted in incorrect caret positioning and a crash. The fix applies to both NITI and non-NITI scenarios (non-NITI impact was largely not user-visible)
  • Implemented selection-aware context menu item updates (caret vs selection):
    • Legacy context menu: updates via LaunchedEffect (through startNotifyingAboutContextMenuItems).
    • New context menu (isNewContextMenu = true): updates via NitiContextMenuUpdaterElement.
  • Extracted existing Objective-C helpers into separate files:
    • CMPEditMenuCustomAction
    • CMPTextInputStringTokenizer
    • Introduced an additional CMPTextInputView to ensure correct UIKit text input behavior in NITI mode.
    • CMPEditMenuView now inherits from CMPTextInputView to unify UIKit text input handling for edit menu interactions

Added API

  • UIKitNativeTextInputContext
    • Used to pass required data between ui and foundation modules
  • LocalNativeTextInputContext
    • CompositionLocal for accessing UIKitNativeTextInputContext
  • PlatformTextInputMethodRequest.textUnclippingOffsetInRoot
    • Used to compute a correct IntermediateTextInputUIView size/position when extra margins are applied and/or when the text field needs to be scrollable

Testing

Manual

  1. In a separate project
    Create an expect/actual for KeyboardOptions

Common source set:

@ExperimentalComposeUiApi
expect fun nativeKeyboardOptionsUseNativeInputHandling(enabled: Boolean): KeyboardOptions

iOS source set:

@ExperimentalComposeUiApi
actual fun nativeKeyboardOptionsUseNativeInputHandling(enabled: Boolean): KeyboardOptions {
    return KeyboardOptions(
        platformImeOptions = PlatformImeOptions {
            useNativeInputHandling(enabled)
        }
    )
}

Then use it on any TextField in common code (BasicTextField(TextFieldState) as an example):

BasicTextField(
    state,
    keyboardOptions = nativeKeyboardOptionsUseNativeInputHandling(true)
)
  1. In Compose Demo project
    Build an iOS application, go iOS-specific features -> NITI Examples

Automated

Release Notes

Features - iOS

  • Adds an opt-in Native iOS Text Input mode for Compose BasicTextField (both TextFieldValue and TextFieldState) enabled via PlatformImeOptions.useNativeInputHandling(enabled) in iOS source set
  • With the native iOS text input flag set to true, BasicTextField uses native UIKit editing and interaction, including:
    • Native caret placement and movement (tap-to-place caret, spacebar caret movement, “ghost caret”)
    • Native magnifier
    • Native selection gestures (double-tap word selection, triple-tap paragraph selection) and iOS-like selection UI (region + handles)
    • Native iOS context menu behavior is supported for both the legacy and the new context menu (isNewContextMenu = true) configurations.
    • Native context menu behavior when interacting with the caret, selection region, or selection handles
    • System iOS text actions in the text field context menu (e.g. Translate, Look Up, Share), provided by UIKit and not available without this mode
    • Autocorrect / typo replacement support
    • Autofill support for text fields, including filling from saved passwords one field at a time

@mazunin-v-jb mazunin-v-jb self-assigned this Nov 27, 2025
@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch from dd397ec to c0251cd Compare November 27, 2025 14:33
@mazunin-v-jb mazunin-v-jb marked this pull request as draft November 27, 2025 14:33
@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch from 7b04420 to 78ec253 Compare December 15, 2025 12:00
@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch from a2d64d8 to e1a2e1f Compare January 12, 2026 12:39
@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch from c4fb706 to d3380e7 Compare January 26, 2026 16:11
@mazunin-v-jb mazunin-v-jb changed the title (WIP) Native iOS text input Native iOS text input Jan 26, 2026
@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch 2 times, most recently from 4a3c226 to 2fbdaaf Compare February 2, 2026 09:45
@mazunin-v-jb mazunin-v-jb marked this pull request as ready for review February 2, 2026 10:51
@mazunin-v-jb
Copy link
Author

mazunin-v-jb commented Feb 2, 2026

Folks, I think this PR is ready for review, I'm only going to add automated tests for that soon and do review / small refactor fixes

@ASalavei ASalavei closed this Feb 2, 2026
@ASalavei ASalavei deleted the v.mazunin/native-ios-text-input branch February 2, 2026 16:03
@ASalavei ASalavei restored the v.mazunin/native-ios-text-input branch February 2, 2026 16:04
@ASalavei ASalavei reopened this Feb 2, 2026
@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch from 7294c0c to ea67b61 Compare February 17, 2026 15:58
@MatkovIvan MatkovIvan dismissed their stale review February 18, 2026 09:18

The blockers from my feedback above were resolved

@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch from cc1cd03 to b19d9aa Compare March 2, 2026 12:52
- fixed no-op context menu items with isNewContextMenu flag enabled
- renamed defaultCursor back to cursor to minimize changes with AOSP
- small review fixes
- removed @composable annotations from CoreTextField expect/actuals, refactored to `composed` in iOS actuals
- fixed objc project after rebasing, sorted imports in CMPUIKitUtils.h alphabetically
- remove debug background
- fixed if condition in firstSelectionRectForRange
- Refactored TextInsets to use dp and toPlatformInsets to use pixels, updated the related logic in the IntermediateTextInputUIView.ios.kt and UIKitTextInputService.ios.kt
- optimized unclippedTextOffsetInRoot calculation in LegacyPlatformTextInputServiceAdapter.skiko.kt
- removed @internalComposeUIApi from UIKitTextInputService
- review fix
- omit the `Modifier.` in the CoreTextField actuals, updated android actuals
- renamed textFieldCursor, textFieldDraw functions to meet the lint rules of androidx
- review fixes: updated year, updated android implementation, added comma in the examples, renamed jsWasm to web
- refactored CMPEditMenuCustomAction initWithTitle
- renamed forgotten textUnclippingOffsetInRoot to unclippedTextOffsetInRoot
- refactored cursor drawing logic on BTF2:
changed signature of TextFieldCoreModifierNode.drawCursor, moved cursor drawing logic into TextFieldCoreModifierNode.drawDefaultCursor, removed unnecessary private drawCursor method
- minor review fixes
- added extension parameter to the TextFieldCoreModifierNode.drawDefaultCursor
- moved CATransaction.flush() call to the end of updateView closure
- removed unused targetRect parameter
- renamed methods of UIKitNativeTextInputContext
- refactored condition in internal actual fun TextFieldCoreModifierNode.drawCursor(
- once again renamed niti to the native text input
- renamed `textUnclippingOffsetInRoot` to the `unclippedTextOffsetInRoot`
- refactored a drawSelectionHighlight for BTF2, removed redunant drawSelection
- wrapped isNativeTextInputProvider() call into snapshotFlow
- applied fixes of wrapping context.usingNativeTextInput() in the snapshotFlow in the ContextMenu.ios.kt to avoid race conditions
- splitted updateView closure into NITI and non-NITI, reverted wrapping updateView for non-NITI path into mainScope (coroutineScope)
- finally wrapped usingNativeTextInput to the mutableState
- fixed non-optional casting to the IntermediateTextPosition in the characterOffsetOfPosition method
- refactored updating tint color logic with passing null if current LocalTextSelectionColors is the default one
- renamed ContextMenuCustomAction to the UIKitNativeTextInputContextMenuCustomAction, updated API dump
- fixed typo in the actuals of Modifier.textFieldCursorModifier(
- reverted TextFieldDelegate to the jb-main state
- updated NITITextFields examples
- forwarded selection color to the UIKitTextInputService if it differs from default one, removed updateCursorThickness method (it was redundant), added documentation for the UIKitNativeTextInputContext
- changed the way of disabling compose cursor and selection area in BTF1 for native text input in iOS
- changed data class ContextMenuCustomAction to class
- use remember in ContextMenuArea to avoid recreating of lambdas, modify update method of NativeTextInputContextMenuUpdaterNode to avoid extra restarts
- used MainScope in MemoryLeaksTest (due to changes in IntermediateTextInputUIView) and properly canceled it in tearDown
- upd api dump
- reverted TextToolbarStatus in UIKitTextInputService.ios.kt, split logic there with `usingNativeTextInput`
- fixed doc: added parameter drawSelectionHighlight
- removed `platform` prefix from expect/actuals
- changing extension class of functions drawPlatformCursor and drawPlatformSelection from CompositionLocalConsumerModifierNode
to the TextFieldCoreModifierNode, removed extension class from drawDefaultCursor
- passed CoroutineContext from the ComposeSceneMediator.ios.kt to the UIKitTextInputService.ios.kt and IntermediateTextInputUIView.ios.kt
- fixed bug with incorrect menu positioning in both cases
- small review fixes
- added task mention for adoption NITI in the SelectionContainer
- hidden CMPEditMenuCustomAction from public exposure, introduced Kotlin wrapper for that
- still renaming
- continue renaming niti to nativeTextInput, review fixes
- try to fix `testRefocusByTapKeyboardSizeNotChanges` test
- regenerated api dump
- renamed forgotten `usingNativeInputHandling` places to `usingNativeTextInput`
- renamed `usingNativeInput` and similar places to `usingNativeTextInput`
- documented textUnclippingOffsetInRoot, added comments about scroll synchronization, changed IntermediateTextScrollView to lazy init
- moved default empty UIKitNativeTextInputContext implementation to LocalNativeTextInputContext
- changed @ExperimentalComposeUiApi to the @InternalComposeUiApi for the UIKitNativeTextInputContext
- renamed UIKitNativeTextInputContext to UIKitNativeTextInputContext.ios
- fixed some todo's in IntermediateTextInputUIView.ios.kt
- renamed uikit actuals to ios
- removed outdated todos
- cleaned up showMenu logic in UIKitTextInputService.ios.kt
- restored accidentally deleted workaround with dictation
- corrected the year and the title in NITITextFields
- moved NITI Examples from TextFields examples to the iOS-specific features and moved them from common to iOS source set, updated NITI Examples deleted supplementary expect/actual in PlatfromImeHelpers, reverted  TextFields.kt to the jb-main state
- fixing issue with multiplied text scroll views after switching focus
- provided default Skiko implementation of selection handles for macOS
- moved incorrectly created actuals for desktop from jvm to desktop sourceset
- updated .api file
- provide default value for using native input in ios instrumented tests
- provided default empty implementation for UIKitNativeTextInputContext
- update api dump
- fix in skiko RootNodeOwnerTest, fixed formatting in SelectionHandles.jsWasm.kt
- fixed redrawer crash with non-niti scenario
- rewrote context menu updating logic for NITI (changed to "update by selection change")
- fixed incorrect renaming after rebase
- deleted debug prints
- fixed crashes during text input in NITI which had been caused by the late update of TextLayoutResult, reverted hack with checking MultiParagraph text length
- Fixed crash with text autocorrection
- Fix cursor width, fix context menu appearance by cursor tap
- Fix tint color
- Fix text field cursor activation
- fixed generated comments in IOSSkikoInput.uikit.kt
- clean up IntermediateTextInputUIView.uikit.kt
- removed unnecessary imports from IntermediateTextInputUIView.uikit.kt, removed unnecessary method from IOSSkikoInput.uikit.kt
- reverted observeSelectionChanges in commonMain part of BTF2, which seem unnecessary
- reverted accidental commit of debug flags
- remove unnecessary macos actuals, added one universal macos actual
- fixed CMPUtils header
- reverting unnecessary changes
- xcodeproj fixes
- Fixed lack of items in the context menu with NITI
- Forwarded cursor / selection colors in TF1 / TF2
- Disabled Compose cursor in NITI mode in both TFs
- Created expect / actual for the Compose text selection highlight in TF1, hidden it under NITI flag in uikit sourceset
- Renamed UIKitTextContextMenuHandler.kt into more appropriate UIKitNativeTextInputContext
- Created expect / actual for altering drawing selection rect on different platforms
- fixed incorrect positioning of context menu
- disabled back newContextMenu flag, rewrote showEditMenu logic in UIKitTextInputService.uikit.kt with using NITI toggle, created a signle entry point for context menu, removed some comments
- context menu without NITI fixes + added existing obj-c files to the project which were forgotten to add during the merge
- merge fixes after commit with refactoring InputViews.uikit.kt
- Reverted pointerInput handling in TF1,2, reverted view hierarchy, hidden almost everything related to the NITI under the flag, added some TODOs with bad fixes
- Added feature flag for NITI to platform ime options
- added NITI tests screens to the example app
- Forwarded custom actions to the IntermediateTextInputUIView.uikit.kt
- Enabled native context menu, added public API for updating context menu state (foundation -> ui), extracted all UITextInput methods from objc file to separate class, temporarily disabled current context menu
- Extracted UITextInput methods from CMPEditMenuView to CMPTextInputView, fixed NewContextMenuApi menu
- (wip) commented CMPEditMenu methods calls after rebase with new context menu api on ios
- (wip) turned on native selection rects (LTR only!) + fixed appearing of the text editing menu by tapping on the selection rects + more appropriate naming + fixed imports
- (wip) disabled custom iOS tap handlers in TF1,2
- (wip) fixed native text views positioning in TF1
- (wip) disabled compose selection handles
- (wip) fixed scroll positioning in BTF2
- (wip) fixed touch forwarding
- (wip) fixed positioning of ScrollView and TextView issue
- (wip) removed unnecessary comments
- (wip) fixed sizing of TextScrollView and TextView
- (wip) transfered changes from previous niti branch
- (wip) rebase fixes
@mazunin-v-jb mazunin-v-jb force-pushed the v.mazunin/native-ios-text-input branch from a397126 to 5a0f7e3 Compare March 3, 2026 12:00
@igordmn igordmn requested review from igordmn and removed request for igordmn March 3, 2026 12:13
Copy link
Collaborator

@igordmn igordmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM regarding commonMain changes.

@igordmn igordmn self-requested a review March 3, 2026 12:15
@igordmn igordmn dismissed their stale review March 3, 2026 12:16

LGTM regarding commonMain changes

@igordmn igordmn removed their request for review March 3, 2026 12:16
Copy link

@svastven svastven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mazunin-v-jb mazunin-v-jb merged commit 1f26b52 into jb-main Mar 3, 2026
25 checks passed
@mazunin-v-jb mazunin-v-jb deleted the v.mazunin/native-ios-text-input branch March 3, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants