hex/ios/HEXShared/FullKeyboard/KeyboardLaunchWork.swift
Ryan Vogel c5a74dbdfe iOS keyboard: nothing heavy before the keys' first frame
TestFlight build 56 (AD4gTl68aP78, iPhone 15 Pro Max, iOS 27.2): switching to Hex
showed only empty panels, then iOS went back to the previous keyboard. Until a
keyboard's first frame iOS shows an empty panel, and it drops a keyboard that is
slow to draw or ends while starting. The keyboard did device-only blocking work on
that path:

- viewDidLoad called AVAudioSession setAllowHapticsAndSystemSoundsDuringRecording
  synchronously on the main thread (an audio-server round trip). It now runs once
  per process on KeyboardLocalDictation's audio-session queue, before any
  dictation's session work on that same serial queue.
- viewDidAppear queued the text checker warm-up, Apple Speech's language listing
  and the capture page's WKWebView with DispatchQueue.main.async, which the run
  loop can service before the first Core Animation commit. They now run from
  KeyboardLaunchWork: the text checker and lexicon 0.3 s after appearing, then the
  optional work (Speech and Translation language lists, capture page) at 0.6 s,
  only while iOS reports 32 MB of headroom (or none, as in the simulator) and never
  after a memory warning. Both are cancelled when the keyboard hides. Translation's
  language list no longer starts from viewDidLoad's first bridge sync.

Not reproduced in the iOS 27.2 simulator on 2ef08aa4 or d23acdf8 (French alone,
English, English+Spanish, French+English; Full Access on and off; Safari and Hex
Settings > Keyboard), so the device cause is not confirmed. Release peak footprint
there was 30-31 MB for every language; French loads no typing model.

Verified: xcodebuild test on an iPhone 15 Pro Max iOS 27.2 simulator, 524 tests,
0 failures, 1 skipped (new KeyboardBridgeTests case for the launch budget); the
keyboard comes up in Safari and in Hex's own test field with French, and the
capture page is still prepared after the first frame (probe log).
2026-11-21 08:28:20 -05:00

25 lines
1.5 KiB
Swift

import Foundation
/// What a keyboard process leaves until its keys are on screen. Until a keyboard's first
/// frame iOS shows only an empty panel, and it goes back to the previous keyboard when one is
/// slow to draw or ends while it starts (TestFlight build 56: "a strange rectangle", then the
/// original keyboard). So the text checker and supplementary lexicon wait for that frame,
/// and the optional work after them (Apple Speech's and Translation's language lists, the
/// capture page in-keyboard dictation preloads) also waits for memory to allow it.
nonisolated enum KeyboardLaunchWork {
/// After the keyboard appears: its first frame is on screen by then.
static let afterFirstFrame: TimeInterval = 0.3
/// The optional work, a turn of its own after the text checker's.
static let optionalWorkDelay: TimeInterval = 0.6
/// Memory left below iOS's limit for the keyboard that the optional work needs: what the
/// first in-keyboard dictation needs (`KeyboardLocalDictation.startHeadroomNeeded`), as
/// the capture page is that dictation's start made early.
static let optionalHeadroomNeeded = 32 * 1_048_576
/// Never after a memory warning in this process; otherwise with enough headroom where
/// iOS says (`os_proc_available_memory`, 0 where it doesn't, as in the simulator).
static func allowsOptionalWork(headroom: Int, memoryWarned: Bool) -> Bool {
guard !memoryWarned else { return false }
return headroom == 0 || headroom >= optionalHeadroomNeeded
}
}