mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS keyboard: Spatial autocorrect experiment (tap + bigram decoder), off by default
What: a noisy-channel word decoder (TypingDecoder) behind Settings ›
Experiments › Spatial autocorrect (experiments.spatialAutocorrect, off
by default; it needs the full keyboard with Autocorrect on). When on, for a layout whose
model is bundled (English, Spanish), it replaces UITextChecker's one-edit
guesses on space and in the strip:
- each tap is scored against every letter with a 2D Gaussian around the
key (sigma 0.29 x 0.26 of the key pitch), moved by Learn how I type's
average tap offsets for letters with 20+ taps;
- words come from a beam search over a memory-mapped, sorted word list
with substitutions, one insertion, deletion or transposition, free
apostrophes ("dont" -> "don't") and accented letters read from their
base key (é from e, +1 nat);
- a bigram language model on the word before (stupid backoff) weighs
them, and a missed space is tried by splitting the taps in two (or
treating a bottom-row tap as the space);
- what was typed is replaced only when the best reading beats it by 2
nats, or 7 when it is itself a word the model, UITextChecker, learned
words or the shorthand list know.
Text replacements, learned fixes, the English contraction/typo table and
words you kept still come first. FullKeyboardEngine now records each
letter's tap point for the word being typed (cleared on anything else).
scripts/build-typing-model.py writes the model format (v2: per-language
alphabet, sorted words, u8 costs, sorted bigram keys); models are kept
out of git (.gitignore). The English prototype model is built from
SymSpell's frequency lists (MIT; Google Books Ngram CC BY 3.0 and SCOWL),
the Spanish one from Tatoeba sentence counts (CC BY 2.0 FR). Typing logs
now record the setting.
Why: Record typing logs show short words and non-words left uncorrected
("tk" for "to"), missed spaces with a typo inside, and o/i taps landing
low on k/j. The UITextChecker path skips 1-2 letter words, never uses
the word before, and can't split a word that also has a substitution.
Verified (anomastudio, iPhone 18 Pro simulator, iOS 27.2): benchmark
harness (kept outside the repo) typing every tap through
FullKeyboardEngine. Synthetic held-out English, 13,870 words: output
word error rate 15.0% today vs 6.7% (6.3% with tap offsets); typos fixed
58% vs 83%; correct words wrongly changed 89 vs 97 of 9,203. Labelled
real typing logs, 172 words: 14.5% vs 4.7%. Spanish held-out, 13,301
words: 26.8% vs 8.1% (Spanish model and test from the same corpus, so
optimistic). Per letter p50 1.0 ms, p95 2.5 ms; mapping the model adds
no measurable phys_footprint. Full HEXMobileTests suite: 221 tests,
0 failures (1 skipped, as before); Release generic-iOS build with
CODE_SIGNING_ALLOWED=NO succeeds with both models bundled. No new tests.
* iOS keyboard: Spatial autocorrect models from Google Books Ngram v3
What: the English and Spanish models (HEXKeyboard/TypingModel/en.hextm,
es.hextm, 3.6 MB each) are now built from Google Books Ngram v3 (CC BY
3.0) word and word-pair counts summed over 1980-2019, keeping only
words SCOWL (sizes up to 70, lowercase, ASCII) or RLA-ES (LibreOffice's
es_ES and es_MX dictionaries, MPL 1.1) accept, and are committed.
- English: 86,133 words, 417,480 word pairs; Spanish: 110,000 words,
326,366 pairs. Pairs are the ones that tell most over the single-word
model (count x log of P(b|a) over 0.4 P(b)), as many as fit.
- Format v3: pairs grouped by first word (u32 row starts, u32 second
word, u8 cost), 5 bytes a pair instead of 9, so ~70% more pairs fit
in the same size. TypingDecoderModel reads v3 only.
- scripts/build-typing-model.py downloads, counts (each ngram file
deleted once counted, every stage cached for a resumable rerun) and
writes a model: python build-typing-model.py <en|es> <workdir> <out>.
- Tuning for the new models (dev sentences): English leans more on the
word before (lmWeight 1.5; Parameters.tuned(for:)); an accent-only
change ("esta" -> "está") needs a 2-nat margin instead of 7 (ñ for n
isn't one: "ano"/"año"); an accented letter also reads from its base
key when it has its own (ñ from n), whichever is cheaper.
- Settings › Acknowledgements (new row and sheet, AcknowledgementsView)
and ios/README.md credit Google Books Ngram (CC BY 3.0), SCOWL with its
and WordNet's notices, and RLA-ES (MPL 1.1).
- AutocorrectEngineTests: decoder ranking cases on the bundled models
("need t^o" -> "need to", a missed space with a slip -> "see how",
correct words kept, Spanish "aqui manana" -> "aquí mañana", "esta"
kept).
Why: Ryan's licence decision: CC BY with credit, no share-alike, no
non-commercial or GPL/LGPL data, and not SymSpell's word-pair list
(unverified provenance). The Spanish model was built from Tatoeba,
the same corpus as its test set.
Removed: the SymSpell- and Tatoeba-built prototype models and the v2
format (pairs as sorted u64 keys); models are no longer gitignored.
Verified (Mac Studio, iPhone 18 Pro simulator, iOS 27.2; benchmark
harness outside the repo; word error rate on words that aren't names):
English held-out test, 13,870 words: today's engine 13.0%, decoder
3.6% (3.4% with tap offsets), correct words broken 4 -> 2 of 7,716;
labelled real typing logs, 172 words: 14.5% -> 4.1%; the shared
example sentence decodes exactly; Spanish held-out Tatoeba test (no
source shared with the model), 13,301 words: 25.7% -> 8.0%, broken
16 -> 20 of 7,246. Per letter p50 1.0 ms, p95 2.6 ms.
AutocorrectEngineTests 19 passed.
* iOS keyboard: Spatial autocorrect knows your own words
What: the decoder also searches TypingDecoder.UserWords, a small sorted
in-memory list of your words, beside the model: Contacts names and
other UILexicon terms, one-word text replacement outputs, words you
kept twice (LearnedWordsStore) and typo fixes Learn how I type learned.
Each scores as a moderately common word (userWordCost 11 nats) and
comes back as you spell it ("Zorbix", not "zorbix"); typed exactly,
one counts as a word, so it needs the larger margin to change.
AutocorrectEngine rebuilds the list only when a source changes
(revision bumped by loadLexiconEntries, applyLearning, suppressAndKeep)
or the model changes. The search is now generic over TypingDecoderWords
(the model or your words); the model's own narrow moved to it.
Why: a name, brand or word you use that the model lacks could only be
left alone, never reached from a typo.
Verified (Mac Studio, iPhone 18 Pro simulator, iOS 27.2):
AutocorrectEngineTests 20 passed, including a new case: with "Zorbix"
in the lexicon, a slip "zorbiz" becomes "Zorbix" and "zorbix" typed
right stays. Benchmark unchanged (it has no user words).
* iOS keyboard: an accent or tilde is the cheapest autocorrect fix
What: AutocorrectEngine.singleEditKeyboardCost now scores a one-letter
substitution that only adds or changes an accent, diaeresis or tilde
(e/é, u/ü, n/ñ) at 0.15, below every other edit, instead of 1.15 (the
cost of two far-apart keys). New helper sameLetterIgnoringAccents.
Valid words are still never rewritten, so "esta" stays "esta".
Why: Spanish typed without accents was corrected into the wrong word,
because the accented guess cost more than any insertion, deletion or
neighbouring key: "aqui" -> "caqui", "podia" -> "odia", "manana" ->
"manaba". Now they become "aquí", "podía", "mañana".
Verified (Mac Studio, iPhone 18 Pro simulator, iOS 27.2, the
benchmark harness kept outside the repo, today's engine with Spatial
autocorrect off): Spanish held-out Tatoeba test, 13,301 words, output
word error rate 26.8% -> 24.7%, words changed into another wrong word
1,015 -> 743, correct words broken 16 -> 16 of 7,246 (names excluded).
English test (13,870 words) unchanged at 15.0%.
* iOS keyboard: fix two-letter typos toward very common two-letter words
What: cautiousReplacement fixes a two-letter non-word (UITextChecker
doesn't know it, not ALL CAPS, no apostrophe) when one neighbouring key
or one swap turns it into a very common two-letter word of the layout's
language (KeyboardLanguage.commonTwoLetterWords, commonest first: "tk"
-> "to", "ot" -> "to", Spanish "nl" -> "no", "ee" -> "de"). English and
Spanish have lists; Norwegian and German have none yet and keep
leaving two-letter words alone. The step numbering in the comments
moves by one.
Why: autocorrect skipped every word under three letters, and the
commonest words are two letters: typing logs show "tk" for "to" left
uncorrected again and again (the o key's taps land low, on k).
Verified (Mac Studio, iPhone 18 Pro simulator, iOS 27.2, benchmark
harness outside the repo, today's engine): English held-out test,
13,870 words, output word error rate 15.0% -> 13.2%; labelled real
typing logs, 172 words, 14.5% -> 10.5%; Spanish test 24.7% -> 22.9%;
correct words broken unchanged (4 of 7,716 English, 16 of 7,246
Spanish, names excluded).
745 lines
32 KiB
Swift
745 lines
32 KiB
Swift
import SwiftUI
|
||
import UIKit
|
||
|
||
@MainActor
|
||
final class KeyboardViewController: UIInputViewController {
|
||
private let bridge = KeyboardBridge()
|
||
private let viewModel = KeyboardViewModel()
|
||
private let localDictation = KeyboardLocalDictation()
|
||
private var pollTimer: Timer?
|
||
private var coldStartJobID = UUID().uuidString
|
||
private var lastURLRefreshBucket: Int = 0
|
||
private var lastFullAccessNote: TimeInterval = 0
|
||
private var surface: UIView?
|
||
private var fullKeyboard: FullKeyboardView?
|
||
private var heightConstraint: NSLayoutConstraint?
|
||
private var pollInterval: TimeInterval = 0
|
||
private var didHandOffCurrentField = false
|
||
private var didWarmLexicon = false
|
||
/// Learn how I type: what becomes of each dictation Hex inserts.
|
||
private let dictationEdits = DictationEditTracker()
|
||
private lazy var documentTarget = DocumentProxyTextTarget { [weak self] in self?.textDocumentProxy }
|
||
/// Record typing logs: the process's logger while the experiment is on.
|
||
private var typingLog: TypingLogger?
|
||
/// The field the current typing log session is for.
|
||
private var typingLogTraits: TypingLogTraits?
|
||
/// Follows dictations for typing logs' outcomes, apart from Learn how I type's tracker.
|
||
private let logDictationEdits = DictationEditTracker()
|
||
private var pendingHostContext: DispatchWorkItem?
|
||
private var isShown = false
|
||
/// The dictation bar's key haptic; the full keyboard has its own generator.
|
||
private lazy var barHaptics = UIImpactFeedbackGenerator(style: .light)
|
||
|
||
/// Haptics need Full Access, and Settings › Keyboard › Haptics covers both keyboards.
|
||
private var allowsKeyHaptics: Bool {
|
||
hasFullAccess && FullKeyboardSettings().haptics
|
||
}
|
||
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
configureView()
|
||
syncFromBridge(animated: false)
|
||
}
|
||
|
||
override func viewWillAppear(_ animated: Bool) {
|
||
super.viewWillAppear(animated)
|
||
coldStartJobID = UUID().uuidString
|
||
didHandOffCurrentField = false
|
||
// The keyboard may come back for another field: settle on where the dictation was last seen.
|
||
dictationEdits.finish(nil)
|
||
mountSurface()
|
||
fullKeyboard?.prepareForField()
|
||
isShown = true
|
||
beginTypingLogSession(trigger: "appear")
|
||
handOffNumericPadIfNeeded()
|
||
syncFromBridge(animated: false)
|
||
schedulePoll(force: true)
|
||
}
|
||
|
||
override func viewDidAppear(_ animated: Bool) {
|
||
super.viewDidAppear(animated)
|
||
warmFullKeyboardLexiconIfNeeded()
|
||
listDictationLanguagesIfNeeded()
|
||
}
|
||
|
||
override func viewWillDisappear(_ animated: Bool) {
|
||
super.viewWillDisappear(animated)
|
||
if dictationEdits.isTracking { dictationEdits.finish(editContext) }
|
||
TypingJournal.sharedIfEnabled()?.save()
|
||
isShown = false
|
||
endTypingLogSession(reason: "disappear", settleDictation: editContext)
|
||
pollTimer?.invalidate()
|
||
pollTimer = nil
|
||
}
|
||
|
||
override func didReceiveMemoryWarning() {
|
||
super.didReceiveMemoryWarning()
|
||
fullKeyboard?.handleMemoryWarning()
|
||
typingLog?.flush()
|
||
}
|
||
|
||
override func viewWillLayoutSubviews() {
|
||
super.viewWillLayoutSubviews()
|
||
if let fullKeyboard, heightConstraint?.constant != fullKeyboard.preferredHeight {
|
||
heightConstraint?.constant = fullKeyboard.preferredHeight
|
||
preferredContentSize.height = fullKeyboard.preferredHeight
|
||
}
|
||
}
|
||
|
||
override func textWillChange(_ textInput: UITextInput?) {
|
||
super.textWillChange(textInput)
|
||
if KeyboardFieldRouting.route(for: textDocumentProxy.keyboardType) != .handOff {
|
||
didHandOffCurrentField = false
|
||
}
|
||
}
|
||
|
||
override func textDidChange(_ textInput: UITextInput?) {
|
||
super.textDidChange(textInput)
|
||
updateKeyboardTraits()
|
||
followTypingLogField()
|
||
fullKeyboard?.textDidChange()
|
||
observeDictationEdits(fromKeys: false)
|
||
noteHostTextChange()
|
||
handOffNumericPadIfNeeded()
|
||
}
|
||
|
||
// MARK: - Record typing logs
|
||
|
||
/// Starts a session for the field the keyboard is in, while the experiment is on. A
|
||
/// field that must not be logged gets a `skipped` line and nothing else.
|
||
private func beginTypingLogSession(trigger: String) {
|
||
guard let log = TypingLogger.sharedIfEnabled() else {
|
||
typingLog = nil
|
||
return
|
||
}
|
||
typingLog = log
|
||
log.contextSource = { [weak self] in
|
||
self?.typingLogContext ?? TypingLogContext(before: nil, after: nil, hasText: false)
|
||
}
|
||
let traits = TypingLogTraits(textDocumentProxy, document: textDocumentProxy.documentIdentifier)
|
||
typingLogTraits = traits
|
||
log.beginSession(typingLogSessionInfo(trigger: trigger, traits: traits))
|
||
attachTypingLog()
|
||
}
|
||
|
||
/// `settleDictation` is the text the last dictation's outcome is judged on (nil: where
|
||
/// it was last seen, for a field the keyboard already left).
|
||
private func endTypingLogSession(reason: String, settleDictation context: DictationEditTracker.Context?) {
|
||
pendingHostContext?.cancel()
|
||
pendingHostContext = nil
|
||
guard let log = typingLog else { return }
|
||
if logDictationEdits.isTracking { logDictationEdits.finish(context) }
|
||
fullKeyboard?.typingLog = nil
|
||
log.endSession(reason: reason)
|
||
typingLogTraits = nil
|
||
}
|
||
|
||
private func attachTypingLog() {
|
||
guard let fullKeyboard else { return }
|
||
let log = typingLog?.isRecording == true ? typingLog : nil
|
||
guard fullKeyboard.typingLog !== log || log != nil else { return }
|
||
fullKeyboard.typingLog = log
|
||
fullKeyboard.typingLogSessionBegan()
|
||
}
|
||
|
||
/// Turned on or off while the keyboard is shown.
|
||
private func syncTypingLogEnabled() {
|
||
let enabled = TypingLogSettings().isEnabled
|
||
if enabled, typingLog == nil, isShown {
|
||
beginTypingLogSession(trigger: "enabled")
|
||
} else if !enabled, typingLog != nil {
|
||
endTypingLogSession(reason: "disabled", settleDictation: nil)
|
||
typingLog = nil
|
||
_ = TypingLogger.sharedIfEnabled()
|
||
}
|
||
}
|
||
|
||
/// Another field, or the same field with new traits: a new session (which may be skipped).
|
||
private func followTypingLogField() {
|
||
guard typingLog != nil, isShown else { return }
|
||
let traits = TypingLogTraits(textDocumentProxy, document: textDocumentProxy.documentIdentifier)
|
||
guard traits.signature != typingLogTraits?.signature else { return }
|
||
endTypingLogSession(reason: "field_changed", settleDictation: nil)
|
||
beginTypingLogSession(trigger: "field")
|
||
}
|
||
|
||
/// The host changed the text or moved the cursor. Hosts call this in bursts (and some
|
||
/// after Hex's own edits), so the context is written once things settle, at once when
|
||
/// the field was emptied.
|
||
private func noteHostTextChange() {
|
||
guard let log = typingLog, log.isRecording else { return }
|
||
pendingHostContext?.cancel()
|
||
guard textDocumentProxy.hasText else {
|
||
pendingHostContext = nil
|
||
log.context("text_did_change")
|
||
return
|
||
}
|
||
let work = DispatchWorkItem { [weak self] in
|
||
MainActor.assumeIsolated {
|
||
self?.pendingHostContext = nil
|
||
self?.typingLog?.context("text_did_change")
|
||
}
|
||
}
|
||
pendingHostContext = work
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: work)
|
||
}
|
||
|
||
private var typingLogContext: TypingLogContext {
|
||
TypingLogContext(
|
||
before: textDocumentProxy.documentContextBeforeInput,
|
||
after: textDocumentProxy.documentContextAfterInput,
|
||
hasText: textDocumentProxy.hasText
|
||
)
|
||
}
|
||
|
||
private func typingLogSessionInfo(trigger: String, traits: TypingLogTraits) -> TypingLogSessionInfo {
|
||
let bundle = Bundle.main.infoDictionary
|
||
let orientation = fullKeyboard?.orientation ?? FullKeyboardView.orientation(for: traitCollection)
|
||
let dark = textDocumentProxy.keyboardAppearance == .dark || traitCollection.userInterfaceStyle == .dark
|
||
return TypingLogSessionInfo(
|
||
trigger: trigger,
|
||
surface: fullKeyboard == nil ? "bar" : "full",
|
||
traits: traits,
|
||
bounds: view.bounds.size,
|
||
safeArea: view.safeAreaInsets,
|
||
orientation: orientation == .landscape ? "landscape" : "portrait",
|
||
appearance: dark ? "dark" : "light",
|
||
device: TypingLogSessionInfo.deviceModel,
|
||
system: "\(UIDevice.current.systemName) \(UIDevice.current.systemVersion)",
|
||
version: bundle?["CFBundleShortVersionString"] as? String ?? "",
|
||
build: bundle?["CFBundleVersion"] as? String ?? "",
|
||
locale: Locale.current.identifier,
|
||
languages: Array(Locale.preferredLanguages.prefix(4)),
|
||
keyboardLanguage: primaryLanguage,
|
||
inputMode: textDocumentProxy.documentInputMode?.primaryLanguage,
|
||
fullAccess: hasFullAccess,
|
||
settings: [
|
||
TypingLogField("autocorrect", .bool(FullKeyboardSettings().autocorrect)),
|
||
TypingLogField("spatialAutocorrect", .bool(FullKeyboardSettings().spatialAutocorrect)),
|
||
TypingLogField("key_popups", .bool(FullKeyboardSettings().keyPopups)),
|
||
TypingLogField("haptics", .bool(FullKeyboardSettings().haptics)),
|
||
TypingLogField("learnHowIType", .bool(TypingJournalSettings().isEnabled)),
|
||
TypingLogField("cleanupTagger", .bool(CleanupTaggerSettings().isEnabled)),
|
||
TypingLogField("hexKeyPlacement", .string(HexKeyPlacement.current.rawValue)),
|
||
TypingLogField("keyboardLayout", .string(fullKeyboard?.engine.language.rawValue ?? "")),
|
||
TypingLogField("dictationLanguage", .string(LanguageSettings().dictationLanguage ?? "automatic")),
|
||
TypingLogField("style", .string(bridge.style.rawValue)),
|
||
]
|
||
)
|
||
}
|
||
|
||
// MARK: - Learn how I type
|
||
|
||
/// Dictations are followed only while Learn how I type is on, in plain text fields
|
||
/// that allow autocorrect.
|
||
private var learnsFromDictation: Bool {
|
||
guard TypingJournalSettings().isEnabled,
|
||
case .keys(let kind, _) = KeyboardFieldRouting.route(for: textDocumentProxy.keyboardType)
|
||
else { return false }
|
||
return TypingJournal.allows(field: kind, target: documentTarget)
|
||
}
|
||
|
||
private var editContext: DictationEditTracker.Context {
|
||
DictationEditTracker.Context(
|
||
before: textDocumentProxy.documentContextBeforeInput,
|
||
after: textDocumentProxy.documentContextAfterInput,
|
||
hasText: textDocumentProxy.hasText
|
||
)
|
||
}
|
||
|
||
private func observeDictationEdits(fromKeys: Bool) {
|
||
guard dictationEdits.isTracking || logDictationEdits.isTracking else { return }
|
||
let context = editContext
|
||
if logDictationEdits.isTracking { logDictationEdits.observe(context, fromKeys: fromKeys) }
|
||
if dictationEdits.isTracking { dictationEdits.observe(context, fromKeys: fromKeys) }
|
||
}
|
||
|
||
/// A return key labelled Send ends the dictation's edit window: what is there is sent.
|
||
private func returnKeyWillInsert() {
|
||
guard textDocumentProxy.returnKeyType == .send else { return }
|
||
if logDictationEdits.isTracking { logDictationEdits.finish(editContext) }
|
||
guard dictationEdits.isTracking else { return }
|
||
dictationEdits.finish(editContext)
|
||
}
|
||
|
||
private func handOffNumericPadIfNeeded() {
|
||
guard fullKeyboard != nil else { return }
|
||
if KeyboardFieldRouting.route(for: textDocumentProxy.keyboardType) == .handOff {
|
||
guard !didHandOffCurrentField else { return }
|
||
didHandOffCurrentField = true
|
||
if needsInputModeSwitchKey {
|
||
advanceToNextInputMode()
|
||
}
|
||
} else {
|
||
didHandOffCurrentField = false
|
||
}
|
||
}
|
||
|
||
private func warmFullKeyboardLexiconIfNeeded() {
|
||
guard let fullKeyboard else { return }
|
||
fullKeyboard.warmUpTextCheckerAsync()
|
||
guard !didWarmLexicon else { return }
|
||
didWarmLexicon = true
|
||
// UIKit calls back on its lexicon-request queue. A main-actor closure here trips
|
||
// Swift's runtime isolation check (EXC_BREAKPOINT, TestFlight build 3), so the
|
||
// callback is nonisolated and hops to the main actor itself.
|
||
requestSupplementaryLexicon { @Sendable [weak self] lexicon in
|
||
Task { @MainActor in
|
||
self?.fullKeyboard?.loadLexicon(lexicon)
|
||
}
|
||
}
|
||
}
|
||
|
||
private func configureView() {
|
||
view.backgroundColor = .clear
|
||
|
||
viewModel.inputViewController = self
|
||
dictationEdits.onResult = { result in
|
||
TypingJournal.sharedIfEnabled()?.recordDictation(result)
|
||
}
|
||
logDictationEdits.onResult = { [weak self] result in
|
||
self?.typingLog?.dictationOutcome(result)
|
||
}
|
||
localDictation.corrections = {
|
||
TypingJournal.sharedIfEnabled()?.dictationCorrections() ?? []
|
||
}
|
||
viewModel.onDictateTap = { [weak self] in self?.startWarmDictation() }
|
||
viewModel.onKeyFeedback = { [weak self] in
|
||
guard let self, self.allowsKeyHaptics else { return }
|
||
self.barHaptics.impactOccurred(intensity: 0.65)
|
||
self.barHaptics.prepare()
|
||
}
|
||
viewModel.onStopTap = { [weak self] in
|
||
guard let self else { return }
|
||
if self.localDictation.isActive {
|
||
self.localDictation.stop()
|
||
// Loading the cleanup model while recording starts ran older iPhones out of
|
||
// keyboard memory, so it loads after the microphone closes.
|
||
CleanupTagger.prepareIfEnabled(
|
||
style: self.bridge.style,
|
||
language: LanguageSettings().effectiveDictationLanguage
|
||
)
|
||
} else {
|
||
self.stopWarmDictation()
|
||
}
|
||
}
|
||
viewModel.onCancelTap = { [weak self] in
|
||
guard let self else { return }
|
||
if self.localDictation.isActive { self.localDictation.cancel() } else { self.cancelWarmDictation() }
|
||
}
|
||
syncLocalDictationAvailability()
|
||
viewModel.onStyleSelected = { [weak self] style in
|
||
guard let self else { return }
|
||
self.bridge.style = style
|
||
if self.viewModel.style != style {
|
||
self.viewModel.style = style
|
||
}
|
||
self.refreshColdStartURL(now: Date().timeIntervalSince1970, force: true)
|
||
}
|
||
viewModel.onLanguageSelected = { [weak self] language in
|
||
LanguageSettings().chooseDictationLanguage(language)
|
||
self?.languageSignature = nil
|
||
self?.syncLanguageState()
|
||
}
|
||
viewModel.onPasteTranslationSelected = { [weak self] translated in
|
||
DictationTranslationSettings().pastesTranslation = translated
|
||
self?.languageSignature = nil
|
||
self?.syncLanguageState()
|
||
}
|
||
viewModel.onSpace = { [weak self] in
|
||
self?.typingLog?.edit("insert", [
|
||
TypingLogField("inserted", .string(" ")),
|
||
TypingLogField("src", .string("bar")),
|
||
])
|
||
self?.textDocumentProxy.insertText(" ")
|
||
self?.observeDictationEdits(fromKeys: true)
|
||
self?.typingLog?.context("space")
|
||
}
|
||
viewModel.onDelete = { [weak self] in
|
||
if let self, let log = self.typingLog {
|
||
let deleted = self.textDocumentProxy.documentContextBeforeInput?.last.map { String($0) }
|
||
log.edit("delete", [
|
||
deleted.map { TypingLogField("deleted", .string($0)) } ?? TypingLogField("deletedCount", .int(1)),
|
||
TypingLogField("src", .string("bar")),
|
||
])
|
||
}
|
||
self?.textDocumentProxy.deleteBackward()
|
||
self?.observeDictationEdits(fromKeys: true)
|
||
}
|
||
viewModel.onReturn = { [weak self] in
|
||
if let self {
|
||
self.typingLog?.edit("return", [
|
||
TypingLogField("inserted", .string("\n")),
|
||
TypingLogField("returnKeyType", .string(TypingLogTraits.name(self.textDocumentProxy.returnKeyType ?? .default))),
|
||
TypingLogField("send", .bool(self.textDocumentProxy.returnKeyType == .send)),
|
||
TypingLogField("src", .string("bar")),
|
||
])
|
||
}
|
||
self?.returnKeyWillInsert()
|
||
self?.textDocumentProxy.insertText("\n")
|
||
self?.observeDictationEdits(fromKeys: true)
|
||
self?.typingLog?.context("return")
|
||
}
|
||
viewModel.onOpenURL = { [weak self] url in
|
||
guard let self else { return }
|
||
ContainingAppOpener.open(url, from: self)
|
||
}
|
||
|
||
let heightConstraint = view.heightAnchor.constraint(equalToConstant: 140)
|
||
heightConstraint.priority = UILayoutPriority(999)
|
||
heightConstraint.isActive = true
|
||
self.heightConstraint = heightConstraint
|
||
mountSurface()
|
||
}
|
||
|
||
/// The dictation bar, or the full keyboard when its experiment is on (iPhone only).
|
||
/// Only switches while idle.
|
||
private func mountSurface() {
|
||
let wantsFull = FullKeyboardSettings().isEnabled && UIDevice.current.userInterfaceIdiom == .phone
|
||
guard surface == nil || wantsFull != (fullKeyboard != nil) else { return }
|
||
guard surface == nil || viewModel.stagePhase == .idle || viewModel.stagePhase == .failed else { return }
|
||
|
||
children.forEach { child in
|
||
child.willMove(toParent: nil)
|
||
child.removeFromParent()
|
||
}
|
||
surface?.removeFromSuperview()
|
||
fullKeyboard = nil
|
||
didWarmLexicon = false
|
||
|
||
let mounted: UIView
|
||
if wantsFull {
|
||
let keyboard = FullKeyboardView(
|
||
model: viewModel,
|
||
target: DocumentProxyTextTarget { [weak self] in self?.textDocumentProxy }
|
||
)
|
||
keyboard.hostController = self
|
||
keyboard.onPreferredHeightChange = { [weak self] in
|
||
self?.view.setNeedsLayout()
|
||
self?.view.layoutIfNeeded()
|
||
}
|
||
keyboard.allowsHaptics = { [weak self] in
|
||
self?.allowsKeyHaptics ?? false
|
||
}
|
||
keyboard.onTextEdit = { [weak self] in self?.observeDictationEdits(fromKeys: true) }
|
||
keyboard.onUndoDictation = { [weak self] in
|
||
self?.dictationEdits.didUndo()
|
||
self?.logDictationEdits.didUndo()
|
||
}
|
||
keyboard.onReturnKey = { [weak self] in self?.returnKeyWillInsert() }
|
||
fullKeyboard = keyboard
|
||
mounted = keyboard
|
||
heightConstraint?.constant = keyboard.preferredHeight
|
||
} else {
|
||
let hosting = UIHostingController(
|
||
rootView: KeyboardSurfaceView(model: viewModel, defersDotScopeUntilActive: true)
|
||
)
|
||
hosting.view.backgroundColor = .clear
|
||
addChild(hosting)
|
||
hosting.didMove(toParent: self)
|
||
mounted = hosting.view
|
||
heightConstraint?.constant = 140
|
||
}
|
||
preferredContentSize.height = heightConstraint?.constant ?? 140
|
||
mounted.translatesAutoresizingMaskIntoConstraints = false
|
||
view.addSubview(mounted)
|
||
NSLayoutConstraint.activate([
|
||
mounted.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||
mounted.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||
mounted.topAnchor.constraint(equalTo: view.topAnchor),
|
||
mounted.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
||
])
|
||
surface = mounted
|
||
attachTypingLog()
|
||
schedulePoll(force: false)
|
||
}
|
||
|
||
/// The dictation bar polls at 25 Hz; the full keyboard at 4 Hz, and 30 Hz while recording.
|
||
private func schedulePoll(force: Bool) {
|
||
let busy = viewModel.stagePhase == .recording || viewModel.stagePhase == .transcribing
|
||
|| viewModel.pendingCommand != nil
|
||
let interval: TimeInterval = fullKeyboard == nil ? 0.04 : (busy ? 1.0 / 30 : 0.25)
|
||
guard force || (pollTimer != nil && interval != pollInterval) else { return }
|
||
pollInterval = interval
|
||
pollTimer?.invalidate()
|
||
pollTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
|
||
MainActor.assumeIsolated {
|
||
self?.syncFromBridge(animated: true)
|
||
}
|
||
}
|
||
}
|
||
|
||
private func syncFromBridge(animated: Bool) {
|
||
let now = Date().timeIntervalSince1970
|
||
updateKeyboardTraits()
|
||
fullKeyboard?.syncSettings()
|
||
syncTypingLogEnabled()
|
||
syncLocalDictationAvailability()
|
||
|
||
#if targetEnvironment(simulator)
|
||
let simSnap = bridge.snapshot()
|
||
let effectiveFullAccess = hasFullAccess || simSnap.isAvailable(at: now)
|
||
#else
|
||
let effectiveFullAccess = hasFullAccess
|
||
#endif
|
||
|
||
guard effectiveFullAccess else {
|
||
viewModel.applyBridgeState(
|
||
hasFullAccess: false,
|
||
snapshot: .offline,
|
||
isModelInstalled: false,
|
||
style: viewModel.style,
|
||
now: now,
|
||
clearPending: true,
|
||
animated: animated
|
||
)
|
||
return
|
||
}
|
||
|
||
if now - lastFullAccessNote >= 5 {
|
||
lastFullAccessNote = now
|
||
bridge.fullAccessSeenAt = now
|
||
}
|
||
|
||
if dictationEdits.isTracking {
|
||
dictationEdits.tick { editContext }
|
||
}
|
||
if logDictationEdits.isTracking {
|
||
logDictationEdits.tick { editContext }
|
||
}
|
||
|
||
let localSnapshot = localDictation.snapshot(now: now)
|
||
let snap = localSnapshot ?? bridge.snapshot()
|
||
var insertedResult = false
|
||
|
||
if snap.hasDeliverableResult(at: now),
|
||
let resultID = snap.resultID,
|
||
let rawTranscript = snap.transcript,
|
||
bridge.lastInsertedResultID != resultID {
|
||
let context = editContext
|
||
let insertion = TranscriptFormatter.insertionText(
|
||
for: rawTranscript,
|
||
beforeContext: context.before,
|
||
afterContext: context.after,
|
||
isWord: dictationWordCheck()
|
||
)
|
||
if learnsFromDictation {
|
||
dictationEdits.willInsert(heard: snap.heard, inserted: insertion, context: context)
|
||
} else {
|
||
dictationEdits.finish(context)
|
||
}
|
||
if let log = typingLog, log.isRecording {
|
||
// A translation can't be compared with what was heard.
|
||
let heard = snap.stages?.translated == nil ? (snap.heard ?? snap.stages?.raw) : nil
|
||
logDictationEdits.willInsert(heard: heard, inserted: insertion, context: context)
|
||
} else if logDictationEdits.isTracking {
|
||
logDictationEdits.finish(context)
|
||
}
|
||
textDocumentProxy.insertText(insertion)
|
||
fullKeyboard?.didInsertDictation(insertion)
|
||
typingLog?.dictation(
|
||
source: localSnapshot == nil ? "app" : "keyboard",
|
||
resultID: resultID,
|
||
style: bridge.style.rawValue,
|
||
stages: snap.stages,
|
||
delivered: rawTranscript,
|
||
inserted: insertion
|
||
)
|
||
observeDictationEdits(fromKeys: true)
|
||
bridge.lastInsertedResultID = resultID
|
||
localDictation.markDelivered(resultID: resultID)
|
||
insertedResult = true
|
||
coldStartJobID = UUID().uuidString
|
||
}
|
||
|
||
syncLanguageState()
|
||
|
||
viewModel.applyBridgeState(
|
||
hasFullAccess: true,
|
||
snapshot: snap,
|
||
isModelInstalled: bridge.isModelInstalled,
|
||
style: bridge.style,
|
||
now: now,
|
||
clearPending: insertedResult,
|
||
animated: animated
|
||
)
|
||
|
||
refreshColdStartURL(now: now, force: false)
|
||
schedulePoll(force: false)
|
||
}
|
||
|
||
// MARK: - In-keyboard dictation
|
||
|
||
/// Whether the Dictate and Hex keys dictate in the keyboard; nil until first decided.
|
||
private var localDictationAvailable: Bool?
|
||
private var lastAvailabilityNote: TimeInterval = 0
|
||
|
||
/// In-keyboard dictation (iOS 26) needs memory for its capture web view and recording
|
||
/// screen, and iOS ends a keyboard that passes its limit (older iPhones, many apps open).
|
||
/// While the keyboard is short of it, `onLocalDictateTap` is nil, so Dictate and the Hex
|
||
/// key take the cold start that opens Hex, as before iOS 26 (a hold stays a tap there).
|
||
private func syncLocalDictationAvailability() {
|
||
guard KeyboardLocalDictation.isSupported else { return }
|
||
let available = localDictation.isActive || localDictation.hasMemoryToStart
|
||
guard available != localDictationAvailable else { return }
|
||
// Noted at most every 5 s, as the footprint can hover at the line.
|
||
let now = Date().timeIntervalSince1970
|
||
if localDictationAvailable != nil || !available, now - lastAvailabilityNote >= 5 {
|
||
lastAvailabilityNote = now
|
||
let change = available ? "memory back, dictating in the keyboard" : "short of memory, Dictate opens Hex"
|
||
KeyboardProbeLog.shared.write("\(change) \(KeyboardProbeLog.memory())")
|
||
}
|
||
localDictationAvailable = available
|
||
viewModel.onLocalDictateTap = available ? { [weak self] in
|
||
guard let self else { return }
|
||
self.localDictation.start(in: self.view, allowBluetoothMic: self.bridge.allowBluetoothMic)
|
||
} : nil
|
||
}
|
||
|
||
// MARK: - Dictation language
|
||
|
||
/// The dictation language's dictionary, for lowercasing a dictation that continues a
|
||
/// sentence; nil (case kept) when the iPhone has no dictionary for it.
|
||
private func dictationWordCheck() -> ((String) -> Bool)? {
|
||
let settings = LanguageSettings()
|
||
let language = settings.resolvedDictationLanguage
|
||
?? settings.effectiveDictationLanguage
|
||
?? Locale.preferredLanguages.first
|
||
?? "en-US"
|
||
guard TranscriptFormatter.lowercasesContinuations(in: language),
|
||
let code = KeyboardLanguage.spellCheckLanguage(
|
||
forDictation: language,
|
||
available: UITextChecker.availableLanguages
|
||
) else { return nil }
|
||
let checker = SystemTextChecker(language: code)
|
||
return { checker.isWordValid($0) }
|
||
}
|
||
|
||
/// What `syncLanguageState` last read, so the 25 Hz poll only works out the choices on a change.
|
||
private var languageSignature: String?
|
||
/// Apple Speech's languages are listed once per keyboard process.
|
||
private static var didListDictationLanguages = false
|
||
|
||
/// Settings › Dictation language, what Automatic picks, and the switch's choices, plus
|
||
/// Translate what I say's target and paste switch, from the App Group (iOS 26, where
|
||
/// dictation is Apple Speech).
|
||
private func syncLanguageState() {
|
||
guard KeyboardLocalDictation.isSupported else { return }
|
||
let settings = LanguageSettings()
|
||
let explicit = settings.dictationLanguage
|
||
let automatic = settings.automaticDictationLanguage
|
||
let recent = settings.recentDictationLanguages
|
||
let supported = settings.supportedDictationLanguages
|
||
let translation = DictationTranslationSettings()
|
||
let target = translation.isEnabled ? translation.targetLanguage : nil
|
||
let pastesTranslation = translation.pastesTranslation
|
||
let signature = [
|
||
explicit ?? "", automatic ?? "", recent.joined(separator: ","), "\(supported.count)",
|
||
target ?? "", "\(pastesTranslation)",
|
||
].joined(separator: "|")
|
||
guard signature != languageSignature else { return }
|
||
languageSignature = signature
|
||
let choices = DictationLanguageSwitch.choices(
|
||
current: explicit,
|
||
recent: recent,
|
||
preferred: Locale.preferredLanguages,
|
||
supported: supported
|
||
)
|
||
if viewModel.dictationLanguage != explicit { viewModel.dictationLanguage = explicit }
|
||
if viewModel.automaticLanguage != automatic { viewModel.automaticLanguage = automatic }
|
||
if viewModel.languageChoices != choices { viewModel.languageChoices = choices }
|
||
if viewModel.translationTarget != target { viewModel.translationTarget = target }
|
||
if viewModel.pastesTranslation != pastesTranslation { viewModel.pastesTranslation = pastesTranslation }
|
||
}
|
||
|
||
/// Asks Apple Speech which languages it has and what Automatic picks, so the switch can
|
||
/// offer them before Hex has listed them.
|
||
private func listDictationLanguagesIfNeeded() {
|
||
guard #available(iOS 26.0, *), hasFullAccess, !Self.didListDictationLanguages else { return }
|
||
Self.didListDictationLanguages = true
|
||
Task { [weak self] in
|
||
_ = await AppleSpeechLanguages.options()
|
||
_ = await AppleSpeechLanguages.automatic()
|
||
self?.languageSignature = nil
|
||
self?.syncLanguageState()
|
||
}
|
||
}
|
||
|
||
private func refreshColdStartURL(now: TimeInterval, force: Bool) {
|
||
let bucket = Int(now / 2.0)
|
||
guard force || bucket != lastURLRefreshBucket else { return }
|
||
lastURLRefreshBucket = bucket
|
||
mountSurface()
|
||
|
||
let request: KeyboardLaunchRequest
|
||
if bridge.isModelInstalled {
|
||
request = .startRecording(
|
||
jobID: coldStartJobID,
|
||
requestedAt: now,
|
||
style: bridge.style
|
||
)
|
||
} else {
|
||
request = .startSession(requestedAt: now)
|
||
}
|
||
viewModel.coldStartURL = request.url
|
||
}
|
||
|
||
private func updateKeyboardTraits() {
|
||
let globe = needsInputModeSwitchKey
|
||
if viewModel.needsGlobeKey != globe {
|
||
viewModel.needsGlobeKey = globe
|
||
}
|
||
// No interface style override: iOS already gives the keyboard the field's appearance
|
||
// (a field that asks for a dark keyboard gets dark traits in light mode, like the system
|
||
// keyboard), while the document proxy's `keyboardAppearance` can go stale.
|
||
let nextReturn: String = switch textDocumentProxy.returnKeyType {
|
||
case .send: "Send"
|
||
case .google, .yahoo, .search: "Search"
|
||
case .go: "Go"
|
||
case .done: "Done"
|
||
case .join: "Join"
|
||
case .next: "Next"
|
||
case .route: "Route"
|
||
case .continue: "Continue"
|
||
case .emergencyCall: "SOS"
|
||
default: "Return"
|
||
}
|
||
if viewModel.returnKeyLabel != nextReturn {
|
||
viewModel.returnKeyLabel = nextReturn
|
||
}
|
||
}
|
||
|
||
private func startWarmDictation() {
|
||
let now = Date().timeIntervalSince1970
|
||
guard viewModel.hasFullAccess, viewModel.currentSnapshot.isAvailable(at: now) else { return }
|
||
let jobID = UUID().uuidString
|
||
viewModel.setPendingCommand(
|
||
KeyboardViewModel.PendingCommand(kind: .start, jobID: jobID, sentAt: now),
|
||
now: now,
|
||
animated: true
|
||
)
|
||
bridge.send(.start, jobID: jobID, style: viewModel.style, sentAt: now)
|
||
}
|
||
|
||
private func stopWarmDictation() {
|
||
let now = Date().timeIntervalSince1970
|
||
guard let jobID = viewModel.currentSnapshot.jobID ?? viewModel.pendingCommand?.jobID else { return }
|
||
viewModel.setPendingCommand(
|
||
KeyboardViewModel.PendingCommand(kind: .stop, jobID: jobID, sentAt: now),
|
||
now: now,
|
||
animated: true
|
||
)
|
||
bridge.send(.stop, jobID: jobID, style: viewModel.style, sentAt: now)
|
||
}
|
||
|
||
private func cancelWarmDictation() {
|
||
let now = Date().timeIntervalSince1970
|
||
guard let jobID = viewModel.currentSnapshot.jobID ?? viewModel.pendingCommand?.jobID else { return }
|
||
viewModel.setPendingCommand(
|
||
KeyboardViewModel.PendingCommand(kind: .cancel, jobID: jobID, sentAt: now),
|
||
now: now,
|
||
animated: true
|
||
)
|
||
bridge.send(.cancel, jobID: jobID, style: viewModel.style, sentAt: now)
|
||
}
|
||
}
|