import QuartzCore import UIKit /// The text field the keys type into. `UITextDocumentProxy` in the keyboard; a fake in tests. @MainActor protocol KeyboardTextTarget: AnyObject { var documentContextBeforeInput: String? { get } var hasText: Bool { get } var autocapitalizationType: UITextAutocapitalizationType? { get } var autocorrectionType: UITextAutocorrectionType? { get } var keyboardType: UIKeyboardType? { get } var returnKeyType: UIReturnKeyType? { get } var enablesReturnKeyAutomatically: Bool? { get } var keyboardAppearance: UIKeyboardAppearance? { get } var textContentType: UITextContentType? { get } func insertText(_ text: String) func deleteBackward() func adjustTextPosition(byCharacterOffset offset: Int) } extension KeyboardTextTarget { var autocorrectionType: UITextAutocorrectionType? { .default } var enablesReturnKeyAutomatically: Bool? { false } var textContentType: UITextContentType? { nil } func adjustTextPosition(byCharacterOffset offset: Int) {} } @MainActor final class DocumentProxyTextTarget: KeyboardTextTarget { private let proxy: () -> UITextDocumentProxy? init(_ proxy: @escaping () -> UITextDocumentProxy?) { self.proxy = proxy } var documentContextBeforeInput: String? { proxy()?.documentContextBeforeInput } var hasText: Bool { proxy()?.hasText ?? false } var autocapitalizationType: UITextAutocapitalizationType? { proxy()?.autocapitalizationType } var autocorrectionType: UITextAutocorrectionType? { proxy()?.autocorrectionType } var keyboardType: UIKeyboardType? { proxy()?.keyboardType } var returnKeyType: UIReturnKeyType? { proxy()?.returnKeyType } var enablesReturnKeyAutomatically: Bool? { proxy()?.enablesReturnKeyAutomatically } var keyboardAppearance: UIKeyboardAppearance? { proxy()?.keyboardAppearance } var textContentType: UITextContentType? { proxy()?.textContentType ?? nil } func insertText(_ text: String) { proxy()?.insertText(text) } func deleteBackward() { proxy()?.deleteBackward() } func adjustTextPosition(byCharacterOffset offset: Int) { proxy()?.adjustTextPosition(byCharacterOffset: offset) } } /// Everything the keys do, apart from drawing: multitouch with rollover, shift, pages, /// long-press accents, spacebar cursor trackpad, cautious autocorrect, double-space period, /// auto-capitalization and accelerating delete. Touches are identified by any stable integer /// so tests can drive it without `UITouch`. @MainActor final class FullKeyboardEngine { struct ActiveTouch: Equatable { var key: KeyboardKey var startPoint: CGPoint var point: CGPoint /// A character that already went in because another key went down after it. var committed: Bool /// The page a 123/ABC press came from; releasing over a character returns there. var returnLayer: KeyboardLayer? /// True once a spacebar press crosses `trackpadActivationDistance`. var isTrackpad = false var trackpadAnchorX: CGFloat = 0 /// Characters the trackpad has moved the cursor, for Record typing logs. var trackpadOffset = 0 } static let accentHoldDelay: TimeInterval = 0.40 static let accentSlop: CGFloat = 10 static let trackpadActivationDistance: CGFloat = 10 static let trackpadPointsPerCharacter: CGFloat = 7.5 private(set) var shift = ShiftStateMachine() private(set) var layerState = KeyboardLayerState() private(set) var field: KeyboardFieldKind = .text private(set) var touches: [Int: ActiveTouch] = [:] /// Touch ids in the order they went down. private var touchOrder: [Int] = [] private var lastSpaceAt: TimeInterval? /// Where each letter of the word being typed was tapped (nil for one that came another /// way, such as an accent), for Spatial autocorrect. private var tokenTaps: [CGPoint?] = [] /// Spatial autocorrect: where you tend to tap each letter, from its centre (Learn how I type). var tapOffsets: [Character: CGVector] = [:] { didSet { if tapOffsets != oldValue { syncDecoderKeys() } } } private(set) var activeAccentMenu: AccentMenu? private var activeAccentTouchID: Int? private(set) var suggestions: [KeyboardSuggestion] = [] let target: KeyboardTextTarget let autocorrect: AutocorrectEngine /// Learn how I type (Experiments); nil while off. var journal: TypingJournal? { didSet { syncLearning() } } private var learning: TypingJournal? { guard let journal, TypingJournal.allows(field: field, target: target) else { return nil } return journal } /// Record typing logs (Experiments); nil while it is off, in fields it skips, and in the /// app's fixtures. Every call through it is skipped (arguments too) while nil. var typingLog: TypingLogger? var width: CGFloat = 375 { didSet { if width != oldValue { rebuildLayout() } } } var orientation: KeyboardOrientation = .portrait { didSet { if orientation != oldValue { rebuildLayout() } } } var showsGlobe = false { didSet { if showsGlobe != oldValue { rebuildLayout() } } } /// Experiments › Emoji, in fields that take emoji. var showsEmoji = false { didSet { if showsEmoji != oldValue { rebuildLayout() } } } var horizontalInset: CGFloat = 0 { didSet { if horizontalInset != oldValue { rebuildLayout() } } } /// The letters, accents and spelling dictionary (Settings › Keyboard layout). var language: KeyboardLanguage = .english { didSet { guard language != oldValue else { return } cancelAllTouches() autocorrect.setLanguage(language) rebuildLayout(cause: "language") refreshSuggestions() } } private(set) var layout = KeyboardLayout(width: 375) var clock: () -> TimeInterval = { CACurrentMediaTime() } /// Called whenever something visible changed (page, shift, pressed keys, suggestions, accents). var onChange: (() -> Void)? /// Called after the keys typed or deleted anything. var onEdit: (() -> Void)? /// Called when an accent cell or spacebar trackpad step ticks, for selection haptics. var onSelectionStep: (() -> Void)? /// Called just before the return key types its line break, so a send can be noticed. var onReturn: (() -> Void)? /// Called when the emoji key is let go. var onEmojiKey: (() -> Void)? init(target: KeyboardTextTarget, autocorrect: AutocorrectEngine? = nil) { self.target = target self.autocorrect = autocorrect ?? AutocorrectEngine() rebuildLayout() } var isUppercase: Bool { layerState.layer == .letters && shift.isUppercase } var isTrackpadActive: Bool { touches.values.contains(where: \.isTrackpad) } /// Settings › Key pop-ups. Off: pressed keys only darken, as with iOS's Character Preview off. var showsKeyPopups = true /// The key whose balloon shows: the newest character still held (hidden while an accent menu is up). var previewKey: KeyboardKey? { guard activeAccentMenu == nil else { return nil } for id in touchOrder.reversed() { if let touch = touches[id], touch.key.character != nil, !touch.committed { return touch.key } } return nil } var pressedKinds: Set { Set(touches.values.map(\.key.kind)) } func displayLabel(for key: KeyboardKey) -> String { guard let character = key.character else { return key.label } return isUppercase && layerState.layer == .letters ? character.uppercased() : character } // MARK: - Field /// Call when the keyboard appears or the field changes. func configureForField() { switch KeyboardFieldRouting.route(for: target.keyboardType) { case .keys(let kind, let initialLayer): field = kind layerState.reset(to: initialLayer) case .handOff: field = .text layerState.reset(to: .numbers) } shift.reset() lastSpaceAt = nil tokenTaps.removeAll() autocorrect.clearPendingUndo() cancelAllTouches() rebuildLayout(cause: "field") refreshAutoCapitalization() refreshSuggestions() } /// Call after the text or the cursor changed outside these keys. func textDidChange() { lastSpaceAt = nil tokenTaps.removeAll() autocorrect.clearPendingUndo() refreshAutoCapitalization() refreshSuggestions() } func refreshAutoCapitalization() { let before = shift shift.refreshAutoCapitalization( AutoCapitalization.shouldCapitalize( mode: AutoCapitalization.Mode(target.autocapitalizationType), before: target.documentContextBeforeInput, hasText: target.hasText ) ) if shift != before { logState("auto") onChange?() } } func refreshSuggestions() { syncLearning() autocorrect.decoderTaps = currentTokenTaps let next = autocorrect.suggestions( beforeInput: target.documentContextBeforeInput, field: field, autocorrectionType: target.autocorrectionType ) if next != suggestions { suggestions = next typingLog?.suggestionsChanged(next) onChange?() } } func acceptSuggestion(_ suggestion: KeyboardSuggestion) { if let log = typingLog { let token = AutocorrectEngine.currentToken(in: target.documentContextBeforeInput) ?? "" log.edit("suggestion", [ TypingLogField("slot", suggestions.firstIndex(of: suggestion).map { .int($0) } ?? .null), TypingLogField("text", .string(suggestion.displayText)), TypingLogField("deleted", .string(token)), TypingLogField("inserted", .string(suggestion.replacement + " ")), TypingLogField("candidates", TypingLogger.candidates(suggestions)), ]) } if let journal = learning { let token = AutocorrectEngine.currentToken(in: target.documentContextBeforeInput) if let token, !token.isEmpty, token != suggestion.replacement { journal.record(.picked, typed: token, final: suggestion.replacement) } else { journal.didFinish(word: token) // Tapping what you typed over the autocorrection space would apply turns it down. if suggestion.kind == .keepLiteral, let token, let autoFix = suggestions.first(where: \.willAutoReplace) { journal.record(.keptTyped, typed: autoFix.replacement, final: token) } } } if let token = AutocorrectEngine.currentToken(in: target.documentContextBeforeInput) { for _ in 0.. Bool { guard activeAccentMenu == nil, let touch = touches[id], !touch.committed, let base = touch.key.character else { return false } let dx = touch.point.x - touch.startPoint.x let dy = touch.point.y - touch.startPoint.y guard (dx * dx + dy * dy).squareRoot() <= Self.accentSlop else { return false } guard let options = AccentCatalog.options(for: base, uppercase: isUppercase, language: language), options.count > 1 else { return false } activeAccentMenu = AccentMenu.build( for: touch.key.frame, rawOptions: options, in: layout.width, horizontalInset: layout.horizontalInset ) activeAccentTouchID = id typingLog?.ui("accent_open", [ TypingLogField("id", .int(id)), TypingLogField("key", .string(TypingLogger.keyID(touch.key.kind))), TypingLogField("options", .strings(activeAccentMenu?.options ?? [])), ]) onChange?() return true } func touchMoved(id: Int, to point: CGPoint) { guard var touch = touches[id] else { return } touch.point = point // 1. Active long-press accent picker: horizontal drag selects the alternate character. if activeAccentTouchID == id, var menu = activeAccentMenu { let nextIndex = menu.index(for: point.x) touches[id] = touch if nextIndex != menu.selectedIndex { menu.selectedIndex = nextIndex activeAccentMenu = menu onSelectionStep?() onChange?() } return } // 2. Spacebar cursor trackpad: horizontal drag >= 10 pt slides the text caret. if touch.key.kind == .space { if !touch.isTrackpad, abs(point.x - touch.startPoint.x) >= Self.trackpadActivationDistance { touch.isTrackpad = true touch.trackpadAnchorX = point.x lastSpaceAt = nil autocorrect.clearPendingUndo() touches[id] = touch typingLog?.ui("trackpad_start", [TypingLogField("id", .int(id))]) onSelectionStep?() onChange?() return } if touch.isTrackpad { let delta = point.x - touch.trackpadAnchorX let rawSteps = Int((delta / Self.trackpadPointsPerCharacter).rounded(.towardZero)) if rawSteps != 0 { touch.trackpadAnchorX += CGFloat(rawSteps) * Self.trackpadPointsPerCharacter touch.trackpadOffset += rawSteps touches[id] = touch typingLog?.edit("cursor", [TypingLogField("offset", .int(rawSteps))]) tokenTaps.removeAll() target.adjustTextPosition(byCharacterOffset: rawSteps) onSelectionStep?() refreshAutoCapitalization() refreshSuggestions() return } touches[id] = touch return } } // 3. Sliding across character keys updates the active key and re-anchors the hold timer. if touch.key.character != nil, !touch.committed, let under = layout.hitTest(point), under.character != nil, under.kind != touch.key.kind { touch.key = under touch.startPoint = point touches[id] = touch onChange?() return } touches[id] = touch } func touchEnded(id: Int, at point: CGPoint) { guard var touch = touches.removeValue(forKey: id) else { return } touchOrder.removeAll { $0 == id } touch.point = point if activeAccentTouchID == id, let menu = activeAccentMenu { let index = menu.index(for: point.x) let chosen = menu.options[index] activeAccentMenu = nil activeAccentTouchID = nil if !touch.committed { typingLog?.ui("accent_pick", [ TypingLogField("id", .int(id)), TypingLogField("index", .int(index)), TypingLogField("chosen", .string(chosen)), ]) commitAccentSelection(chosen) } onChange?() return } switch touch.key.kind { case .character: if !touch.committed { commitCharacter(touch.key, at: touch.startPoint) } case .shift: shift.shiftUp() logState("shift_key") case .space: if touch.isTrackpad { typingLog?.ui("trackpad_end", [ TypingLogField("id", .int(id)), TypingLogField("offset", .int(touch.trackpadOffset)), ]) typingLog?.context("trackpad") } else { typeSpace() } case .returnKey: if target.enablesReturnKeyAutomatically == true && !target.hasText { typingLog?.edit("return", [ TypingLogField("returnKeyType", .string(TypingLogTraits.name(target.returnKeyType ?? .default))), TypingLogField("ignored", .bool(true)), ]) break } learning?.didFinish(word: AutocorrectEngine.currentToken(in: target.documentContextBeforeInput)) autocorrect.clearPendingUndo() typingLog?.edit("return", [ TypingLogField("inserted", .string("\n")), TypingLogField("returnKeyType", .string(TypingLogTraits.name(target.returnKeyType ?? .default))), TypingLogField("send", .bool(target.returnKeyType == .send)), ]) onReturn?() insert("\n", source: nil) typingLog?.context("return") case .layerSwitch: // Hold 123, slide to a key, let go: type it and go back. if let returnLayer = touch.returnLayer, let under = layout.hitTest(point), let character = under.character, under.kind != touch.key.kind { autocorrect.clearPendingUndo() insert(character, source: "slide") layerState.switchTo(returnLayer) rebuildLayout(cause: "layer_slide") refreshAutoCapitalization() } case .emoji: onEmojiKey?() case .delete, .hex, .globe: break } onChange?() } func touchCancelled(id: Int) { if activeAccentTouchID == id { activeAccentMenu = nil activeAccentTouchID = nil typingLog?.ui("accent_cancel", [TypingLogField("id", .int(id))]) } guard touches.removeValue(forKey: id) != nil else { return } touchOrder.removeAll { $0 == id } onChange?() } func cancelAllTouches() { let hadTouches = !touches.isEmpty || activeAccentMenu != nil touches.removeAll() touchOrder.removeAll() activeAccentMenu = nil activeAccentTouchID = nil if shift.isHeld { shift.shiftUp() logState("cancel") } if hadTouches { onChange?() } } // MARK: - Delete /// The delete key repeats after 360 ms, accelerates at 850 ms, and removes whole words after 1.4 s. static let repeatDelay: TimeInterval = 0.36 static let repeatInterval: TimeInterval = 0.075 static let fastRepeatAfter: TimeInterval = 0.85 static let wordDeleteAfter: TimeInterval = 1.4 func repeatDelete(heldFor: TimeInterval) { autocorrect.clearPendingUndo() if heldFor >= Self.wordDeleteAfter { deleteWordBackward() } else if heldFor >= Self.fastRepeatAfter { deleteBackward(allowAutocorrectUndo: false, source: "repeat_fast") deleteBackward(allowAutocorrectUndo: false, source: "repeat_fast") } else { deleteBackward(allowAutocorrectUndo: false, source: "repeat") } } func deleteBackward(allowAutocorrectUndo: Bool = true, source: String = "key") { if allowAutocorrectUndo, let undo = autocorrect.consumePendingUndo(), let before = target.documentContextBeforeInput, before.hasSuffix(undo.insertedText) { typingLog?.edit("autocorrect_undo", [ TypingLogField("deleted", .string(undo.insertedText)), TypingLogField("inserted", .string(undo.original)), ]) for _ in 0.. before.startIndex, before[before.index(before: index)].isWhitespace { index = before.index(before: index) count += 1 } while index > before.startIndex, !before[before.index(before: index)].isWhitespace { index = before.index(before: index) count += 1 } tokenTaps.removeAll() typingLog?.edit("delete_word", [ TypingLogField("deleted", .string(String(before.suffix(max(1, count))))), TypingLogField("src", .string("repeat_word")), ]) for _ in 0..