import HexKeyboardCore import UIKit /// Narrow seam over `UITextChecker` so unit tests can run deterministically or against UIKit's dictionary. @MainActor protocol TextChecking: AnyObject { func isWordValid(_ word: String) -> Bool func guesses(for word: String) -> [String] func completions(for prefix: String) -> [String] /// Switches the dictionary (a `UITextChecker` language code such as `nb`). func useLanguage(_ code: String) } extension TextChecking { func useLanguage(_ code: String) {} } @MainActor final class SystemTextChecker: TextChecking { private var checker: UITextChecker? private var language: String init(language: String = "en_US") { self.language = language } /// Hex's own words for the language Spatial autocorrect reads (`AutocorrectEngine.useOwnWords`): /// while set, they alone say what is a word, and `UITextChecker`'s guesses and completions /// count only while it is in that language (iOS may have no dictionary for it). var ownWords: HexOwnWords? func useLanguage(_ code: String) { language = code } /// Warms `UITextChecker` after the keyboard's first frame commits so cold open stays < 12 ms. func warmUp() { guard checker == nil else { return } let instance = UITextChecker() _ = instance.rangeOfMisspelledWord( in: "hello", range: NSRange(location: 0, length: 5), startingAt: 0, wrap: false, language: language ) checker = instance } private var activeChecker: UITextChecker { if let checker { return checker } let instance = UITextChecker() checker = instance return instance } func isWordValid(_ word: String) -> Bool { guard let ownWords else { return systemKnows(word) } guard ownWords.knows(word) else { return false } // A word the books capitalise only part of the time ("tom", 94%: also a word) is a name // where this language's own dictionary knows it only capitalised, as without Hex's lists; // never from a dictionary that takes any letters for a word. guard ownWords.mayBeName(word), speaksOwnLanguage, systemIsSound, let first = word.first else { return true } return !(systemKnows(first.uppercased() + word.dropFirst()) && !systemKnows(word)) } /// Whether `UITextChecker`'s dictionary for its language tells words from other letters, once /// per language (the iOS 27.2 simulator's `nb_NO` and `pl_PL` take "qzxjvkq"). private var systemIsSound: Bool { if let known = soundness[language] { return known } let sound = !systemKnows("qzxjvkq") soundness[language] = sound return sound } private var soundness: [String: Bool] = [:] private func systemKnows(_ word: String) -> Bool { let ns = word as NSString let range = NSRange(location: 0, length: ns.length) guard range.length > 0 else { return true } let misspelled = activeChecker.rangeOfMisspelledWord( in: word, range: range, startingAt: 0, wrap: false, language: language ) return misspelled.location == NSNotFound } private var speaksOwnLanguage: Bool { ownWords.map { language.hasPrefix($0.language) } ?? true } func guesses(for word: String) -> [String] { guard speaksOwnLanguage else { return [] } let ns = word as NSString let range = NSRange(location: 0, length: ns.length) guard range.length > 0 else { return [] } return activeChecker.guesses(forWordRange: range, in: word, language: language) ?? [] } func completions(for prefix: String) -> [String] { guard speaksOwnLanguage else { return [] } let ns = prefix as NSString let range = NSRange(location: 0, length: ns.length) guard range.length > 0 else { return [] } return activeChecker.completions(forPartialWordRange: range, in: prefix, language: language) ?? [] } } /// A `TextChecking` as autocorrect's core asks it (`HkTextChecker`): on the main thread, /// during the core call that needs it, in the order the Swift engine asked. The core holds it /// until it releases it. nonisolated enum KeyboardCoreChecker { private final class Box: @unchecked Sendable { let checker: any TextChecking init(_ checker: any TextChecking) { self.checker = checker } static func of(_ context: UnsafeMutableRawPointer?) -> Box { Unmanaged.fromOpaque(context!).takeUnretainedValue() } } @MainActor static func make(_ checker: any TextChecking) -> HkTextChecker { HkTextChecker( context: Unmanaged.passRetained(Box(checker)).toOpaque(), is_word_valid: { context, word in let (box, word) = (Box.of(context), String(cString: word!)) return MainActor.assumeIsolated { box.checker.isWordValid(word) } ? 1 : 0 }, guesses: { context, word, out in let (box, word) = (Box.of(context), String(cString: word!)) for guess in MainActor.assumeIsolated({ box.checker.guesses(for: word) }) { hk_strings_push(out, guess) } }, completions: { context, prefix, out in let (box, prefix) = (Box.of(context), String(cString: prefix!)) for completion in MainActor.assumeIsolated({ box.checker.completions(for: prefix) }) { hk_strings_push(out, completion) } }, use_language: { context, code in let (box, code) = (Box.of(context), String(cString: code!)) MainActor.assumeIsolated { box.checker.useLanguage(code) } }, release: { context in Unmanaged.fromOpaque(context!).release() } ) } }