diff --git a/mobile/src/platform/keyboard-close-blur.web.test.ts b/mobile/src/platform/keyboard-close-blur.web.test.ts index 9bf8b349de91..62b14b1021f7 100644 --- a/mobile/src/platform/keyboard-close-blur.web.test.ts +++ b/mobile/src/platform/keyboard-close-blur.web.test.ts @@ -25,6 +25,8 @@ function shellReports(height: number): void { } } +const ANDROID = 'Mozilla/5.0 (Linux; Android 16) AppleWebKit/537.36' + function focusedField(userAgent: string): HTMLInputElement { Object.defineProperty(navigator, 'userAgent', { configurable: true, value: userAgent }) const field = document.createElement('input') @@ -35,6 +37,7 @@ function focusedField(userAgent: string): HTMLInputElement { describe('a keyboard the phone closes', () => { beforeEach(() => { + vi.useFakeTimers({ toFake: ['setTimeout', 'performance'] }) publishShellKeyboardSource({ read: () => shellHeight, subscribe: (listener) => { @@ -45,6 +48,7 @@ describe('a keyboard the phone closes', () => { }) afterEach(() => { + vi.useRealTimers() shellListeners.clear() shellHeight = 0 publishShellKeyboardSource(null) @@ -52,7 +56,7 @@ describe('a keyboard the phone closes', () => { }) it("ends its field's focus on Android, so the next touch cannot raise it again", () => { - const field = focusedField('Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36') + const field = focusedField(ANDROID) startBlurringOnKeyboardClose() shellReports(300) expect(document.activeElement).toBe(field) @@ -62,6 +66,50 @@ describe('a keyboard the phone closes', () => { expect(document.activeElement).not.toBe(field) }) + // The second case is tapping the underlined word after Back: that tap both focuses the field and opens the menu. + it.each([ + ['already focused', false], + ['focused by the tap', true] + ] as const)( + 'keeps focus and reopens the keyboard when Chromium hides it for a tapped autocorrected word in a field %s', + (_case, tapFocuses) => { + const field = focusedField(ANDROID) + if (tapFocuses) { + field.blur() + } + startBlurringOnKeyboardClose() + + field.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })) + TextInputState.focusTextInput(field) + shellReports(300) + const focusSessions = vi.fn() + field.addEventListener('focus', focusSessions) + vi.advanceTimersByTime(400) + shellReports(0) + vi.advanceTimersByTime(100) + + expect(document.activeElement).toBe(field) + expect(focusSessions).toHaveBeenCalledOnce() + } + ) + + it.each([ + ['a touch elsewhere', 'body', 400], + ['a Back long after a touch on the field', 'field', 5000] + ] as const)('still ends focus on a close after %s', (_case, touched, waitMs) => { + const field = focusedField(ANDROID) + startBlurringOnKeyboardClose() + shellReports(300) + + const target = touched === 'field' ? field : document.body + target.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })) + vi.advanceTimersByTime(waitMs) + shellReports(0) + vi.advanceTimersByTime(100) + + expect(document.activeElement).not.toBe(field) + }) + it('leaves focus alone on iOS, where only a blur closes the keyboard', () => { const field = focusedField('Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X)') startBlurringOnKeyboardClose() diff --git a/mobile/src/platform/keyboard-close-blur.web.ts b/mobile/src/platform/keyboard-close-blur.web.ts index f38e56dd2a1f..2611184750e4 100644 --- a/mobile/src/platform/keyboard-close-blur.web.ts +++ b/mobile/src/platform/keyboard-close-blur.web.ts @@ -2,12 +2,48 @@ import { Keyboard } from 'react-native' import { hostOs } from './host-os.web' import { subscribeSoftKeyboard } from './keyboard-occlusion.web' +// Chromium hides the IME itself to show a tapped autocorrected word's menu; Back comes with no touch. +const FIELD_TAP_HIDE_WINDOW_MS = 1000 +const KEYBOARD_REOPEN_DELAY_MS = 50 + +// Android raises the IME only for a new focus session, as for the terminal live input. +function reopenKeyboard(field: HTMLElement): void { + if (document.activeElement === field) { + field.blur() + field.focus() + } +} + // Android Back hides the IME without blurring; blur now so the next terminal touch cannot reopen it. export function startBlurringOnKeyboardClose(): void { - if (hostOs() === 'android') { - subscribeSoftKeyboard( - () => {}, - () => Keyboard.dismiss() - ) + if (hostOs() !== 'android') { + return } + let lastTouch: { target: Node; at: number } | null = null + document.addEventListener( + 'pointerdown', + (event) => { + lastTouch = + event.target instanceof Node ? { target: event.target, at: performance.now() } : null + }, + { capture: true } + ) + subscribeSoftKeyboard( + () => {}, + () => { + const field = document.activeElement + const touch = lastTouch + lastTouch = null + if ( + field instanceof HTMLElement && + touch !== null && + field.contains(touch.target) && + performance.now() - touch.at <= FIELD_TAP_HIDE_WINDOW_MS + ) { + setTimeout(() => reopenKeyboard(field), KEYBOARD_REOPEN_DELAY_MS) + } else { + Keyboard.dismiss() + } + } + ) }