KeyboardWarmup.swift (1476B)
1 import UIKit 2 3 /// Pre-pays the ~400ms `RemoteTextInput` / autocorrect subsystem spin-up that 4 /// otherwise happens on the first real text field focus after a cold launch. 5 /// Creates an offscreen `UITextField`, makes it first responder, then resigns 6 /// immediately — enough to trigger the keyboard process to initialize without 7 /// visibly animating anything. 8 @MainActor 9 enum KeyboardWarmup { 10 private static var didWarm = false 11 12 static func prime() { 13 guard !didWarm else { return } 14 guard let window = keyWindow() else { return } 15 didWarm = true 16 17 PerfSampler.shared.measure("Keyboard.warmup") { 18 let field = UITextField(frame: CGRect(x: -100, y: -100, width: 1, height: 1)) 19 field.autocorrectionType = .default 20 field.autocapitalizationType = .sentences 21 window.addSubview(field) 22 field.becomeFirstResponder() 23 field.resignFirstResponder() 24 field.removeFromSuperview() 25 } 26 } 27 28 private static func keyWindow() -> UIWindow? { 29 for case let scene as UIWindowScene in UIApplication.shared.connectedScenes { 30 if let key = scene.windows.first(where: \.isKeyWindow) { 31 return key 32 } 33 } 34 for case let scene as UIWindowScene in UIApplication.shared.connectedScenes { 35 if let first = scene.windows.first { 36 return first 37 } 38 } 39 return nil 40 } 41 }