Skip to content

fix(mobile): keep the cursor and keyboard when tapping an autocorrected word on Android - #36751

Closed
Lenibi wants to merge 4 commits into
stablyai:mainfrom
Lenibi:fix/android-autocorrect-caret
Closed

Lenibi wants to merge 4 commits into
stablyai:mainfrom
Lenibi:fix/android-autocorrect-caret

Conversation

@Lenibi

@Lenibi Lenibi commented Oct 31, 2026 •

Copy link
Copy Markdown

ELI5

On Android, when Samsung Keyboard autocorrects a word and you tap it to fix it, the keyboard closes and Orca throws away the cursor. Now Orca keeps the cursor where you tapped and brings the keyboard straight back, so you can just backspace and retype that part of the word.

What Changed

  • Before: you mean "npx", typo "run the nlx tests", and autocorrect makes it "Run the next tests". Tap the word, the keyboard closes, the chat box drops, and there is no cursor. Tapping again does nothing. You have to tap after the word and backspace through it.
  • After: tap the word, the autocorrect menu still opens, the keyboard comes back right away, and the cursor is where you tapped. Backspace, type "px", done.
  • Mechanism (all in keyboard-close-blur.web.ts):
    1. It records the last pointerdown and whether it landed on a text field (an input or textarea).
    2. On an Android keyboard close within 1 s of a touch on the field, it does not blur. Instead, 50 ms later, it blurs and refocuses the same field and restores the selection. Android only reopens the keyboard for a new focus session, which is the same thing the terminal live input already does.
    3. Any other keyboard close, like Back, still calls Keyboard.dismiss() as before.

Why

This is Orca-side, not a keyboard bug: plain Chrome on the same phone, with the same keyboard and the same tap, keeps the cursor. Orca loses it because of its own blur.

That blur from #32038 is still needed: after Back, a still-focused field lets the next terminal touch reopen the keyboard. But the hook treats every keyboard close as Back, and Chromium closes the keyboard itself to show the autocorrect menu. "Was the field just touched?" tells them apart (Back has no touch).

Alternatives tried or considered:

  • Only keep focus, don't reopen the keyboard. The cursor stays, but the keyboard stays closed, so you still can't type. It matches plain Chrome, but it doesn't fix the real annoyance.
  • Reopen on the next tap instead. Doesn't work: the tap that closes Chromium's menu never reaches the page. No pointer, click or selectionchange event fires (checked with an on-screen event log on the device).
  • spellcheck=false on the field. Tested: the underline and menu still appear.
  • Only blur for the terminal input. Needs a terminal-field check across the session dock. It's more coupling, and the chat box still wouldn't get its keyboard back.

Why a 1 s time window and not something cleaner:

  • Back can't be detected directly: the keyboard consumes it before the page sees anything.
  • A plain "the field was the last thing touched" flag (no timer) breaks a real case: tap the box, wait, then press Back, and the keyboard would wrongly come back.
  • On the test phone the autocorrect menu closes the keyboard about 370 ms after the tap, so 1 s leaves room on slower devices while a later Back still blurs. Happy to move or tune the constants if you prefer.

Each alternative was tried first in a plain Chrome test page on the same phone; only the refocus brought the keyboard back with the cursor in place.

Linked Issue

Fixes #36747

Visual Proof

Before (left, 1.4.245) vs After (right, this PR). Same phone, same steps, in sync.

CARETFIX_V3_nlx_BEFORE_AFTER_2026-10-31.mp4

Testing

  • Real device: Galaxy S24+ (Android 16, Samsung Keyboard), Android app 0.0.61 against desktop 1.4.245 on Windows, with this change built into the mobile page.

    • Autocorrected word ("nlx" to "next", "npx" to "next", "readme" to "reader"): tap it, the menu shows, the keyboard comes back with the cursor where tapped, and backspace/typing edits in place. Picking the original word from the menu still works.
    • Chat box: keyboard open, Back: the keyboard closes and stays closed. Tap the box: it opens normally.
    • Terminal: Back, then scroll the terminal twice: the keyboard stays closed (the fix(mobile): keep the android keyboard closed when you scroll or swipe the terminal #32038 case).
  • Vitest keyboard-close-blur.web.test.ts, 5 new cases:

    • a hide right after a touch on the field refocuses it with the caret kept (fails on main)
    • a touch outside then a hide still blurs
    • Back 5 s after a touch still blurs
    • a quick Back after a touch on the terminal, with nothing focused, still blurs (fails on the first version)
    • a touch that itself focuses the field still refocuses it, as when an autocorrected word is tapped after Back

    7/7 pass. src/platform + web-entry suites: 226 pass.

  • oxlint and oxfmt clean on touched files. Typecheck shows no new errors.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

I used Claude (Opus 5.5) to trace the cause, build debug pages and write the change and tests. I reproduced the bug, directed the device testing and checked the fix on my own phone.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Android only (hostOs() === 'android'); iOS and desktop untouched.

  • The 1 s and 50 ms values were tuned on one device. The keyboard close comes about 370 ms after the tap on a Galaxy S24+.
  • The refocus fires one extra blur and focus event on the field. That's harmless for the chat composer.
  • The keyboard briefly flickers before it comes back. Chromium hides it to show this menu (ChatGPT's site in Chrome does the same) and the page can't prevent that. Native fields like Discord's avoid it, but matching them would need native code and an app release.
  • The first key press after the menu only closes Chromium's menu. That's Chromium's popup behaviour, and Chrome does the same.
  • Not tested with Gboard.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred). Lint + mobile tests ran locally; full build left to CI.

…d word on Android

Chromium hides the soft keyboard to show the suggestion menu of a tapped
autocorrected word. The Android blur-on-keyboard-close hook treated that
like Back and blurred the composer, so the caret could never be placed
inside the word. When the hide follows a touch on the focused field within
1 s, keep focus and reopen the keyboard 50 ms later (blur, focus, restore
the selection). Back (no touch) still blurs.
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2640b7f1-da24-4c40-ab5c-d0773987ef68
Commits

Reviewing files that changed from the base of the PR and between 36354ae and 348d8e9.

Files selected for processing: 1
  • mobile/src/platform/keyboard-close-blur.web.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Walkthrough

On Android, keyboard closure now uses the timing and target of the latest pointerdown event to decide whether to dismiss the keyboard or refocus the active field after 50 ms. The refocus path restores the selection when both selection positions are available. New tests check retained focus and caret position after a recent field touch, and focus loss after an outside touch or a five-second delay. Additional tests cover a touch that focuses the field and a quick keyboard close after an outside touch when no field was focused.

Severity of issue fixed: Medium

Merge Risk: Low · up to 348d8

The Android behavior is likely mergeable, but the initial-focus test does not verify keyboard reopening and warrants follow-up.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title states the main change: on Android, keep the cursor and keyboard when tapping an autocorrected word. The changeset adds pointer-touch tracking to keyboard-close-blur.web.ts that refocuses a …
Description check Passed The description follows the repository template closely. It has an ELI5 section, a What Changed section with before and after behavior and the mechanism, and a Why section that explains alternatives. …
Linked Issues check Passed Issue [#36747] requires the Android Samsung Keyboard autocorrect flow to preserve the chat cursor after the keyboard closes. keyboard-close-blur.web.ts tracks recent text-field touches, refocuses th…
Out of Scope Changes check Passed The pull request changes Android keyboard-close handling and its focused tests. Touch tracking, delayed refocus, selection restoration, and timer cleanup directly support [#36747] or preserve the exis…
Full details: Title check

Explanation

The title states the main change: on Android, keep the cursor and keyboard when tapping an autocorrected word. The changeset adds pointer-touch tracking to keyboard-close-blur.web.ts that refocuses a recently touched text field and restores its selection instead of dismissing the keyboard. The title matches this behavior and is scoped to mobile and Android. It is specific and within the length guidance.

Full details: Description check

Explanation

The description follows the repository template closely. It has an ELI5 section, a What Changed section with before and after behavior and the mechanism, and a Why section that explains alternatives. It links issue #36747 with a Fixes reference. It has Visual Proof (a before and after video link), a Testing section with device details and five new Vitest cases, an AI Disclosure, the agent skill upstream boundary checkbox, Notes, and a Checklist. Minor gaps: the final checklist item (pnpm build) is unchecked and the author notes the full build is left to CI. The Visual Proof section uses a video link and does not show the full before and after tabbing format, but it is present.

Full details: Linked Issues check

Explanation

Issue [#36747] requires the Android Samsung Keyboard autocorrect flow to preserve the chat cursor after the keyboard closes. keyboard-close-blur.web.ts tracks recent text-field touches, refocuses the field after a quick Android keyboard close, and restores the selection. The added tests cover caret preservation, delayed Back, outside touches, and touches that focus the field.

Full details: Out of Scope Changes check

Explanation

The pull request changes Android keyboard-close handling and its focused tests. Touch tracking, delayed refocus, selection restoration, and timer cleanup directly support [#36747] or preserve the existing Back behavior. No unrelated product changes are identified.

Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Nitpick comments: 1
mobile/src/platform/keyboard-close-blur.web.test.ts: 1

70-87: Trivial · Functional Correctness · Quick win

Assert the selection restoration call.

The timer ordering and focus assertions cover the delayed refocus. The collapsed-selection assertion does not cover the explicit restoration in reopenKeyboard. Spy on setSelectionRange after the initial setup and assert the call after advancing the timer.

Suggested fix
     field.value = 'reader here'
     field.setSelectionRange(3, 3)
+    const setSelectionRange = vi.spyOn(field, 'setSelectionRange')
     let focusSessions = 0
     field.addEventListener('focus', () => focusSessions++)
@@
     expect(document.activeElement).toBe(field)
     expect(focusSessions).toBe(1)
+    expect(setSelectionRange).toHaveBeenLastCalledWith(3, 3)
     expect([field.selectionStart, field.selectionEnd]).toEqual([3, 3])

Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f90e6468-26df-4c27-9a98-755bdf8e861e
Commits

Reviewing files that changed from the base of the PR and between 10e35fd and aa58f70.

Files selected for processing: 2
  • mobile/src/platform/keyboard-close-blur.web.test.ts
  • mobile/src/platform/keyboard-close-blur.web.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

…t refocus

document.activeElement is the body when nothing is focused, so any touch
passed body.contains(target), and a quick Back after tapping the terminal
reopened the keyboard. Ask whether the touch landed on an input or
textarea instead. A touch that focuses a field still counts, so tapping an
autocorrected word after Back keeps the caret. Also assert the selection
restore.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Follow-up Comments

Nitpick Assert that the touch-first-focus path starts a new focus session.

134-134: Trivial · Maintainability & Code Quality · Quick win

Assert that the touch-first-focus path starts a new focus session.

The current assertion passes from the initial TextInputState.focusTextInput(field) call even if the delayed refocus never runs. Add the listener after that initial focus so only the delayed field.focus() increments it.

Suggested test assertion
     touch(field)
     TextInputState.focusTextInput(field)
+    let focusSessions = 0
+    field.addEventListener('focus', () => focusSessions++)
     shellReports(300)
     shellReports(0)
     vi.advanceTimersByTime(50)

     expect(document.activeElement).toBe(field)
+    expect(focusSessions).toBe(1)

Open the source location


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 04228983-cf51-4c16-b58a-0a7f47d82a96
Commits

Reviewing files that changed from the base of the PR and between aa58f70 and 36354ae.

Files selected for processing: 2
  • mobile/src/platform/keyboard-close-blur.web.test.ts
  • mobile/src/platform/keyboard-close-blur.web.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@AmoabaKelvin

Copy link
Copy Markdown
Collaborator

Thanks for digging into this and for the clear report. We landed the fix in #36947, so I'm closing this one.

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.

[Bug]: Android (Samsung Keyboard): tapping an autocorrected word closes the keyboard and loses the cursor in the chat box

2 participants