hex/ios/HEXShared/FullKeyboard/Engine/FullKeyboardEngine+Keys.swift
Ryan Vogel edc54a24ef ios: the keyboard reads a typing config (Settings › Experiments › Typing configs)
With experiments.typingConfigs on and Full Access, FullKeyboardView.syncSettings loads
TypingConfigs/active.json through TypingConfigActive.current. It reads the file again only
when typingConfig.generation changes, through the same TypingConfigReview the app uses. A
missing or unreadable file is no config.
- Words: TypingLearning.adding layers the config's words on top of Learn how I type's learning
  in memory, so nothing is written into what Hex learned and removing the config removes
  exactly its words. What the person taught Hex themselves wins.
  - Kept words count as their own and are offered.
  - neverCorrectTo blocks space corrections, next-word revisions and merges into the word.
  - Fixes are applied (learned-typo rules) or only offered.
  - Dictation fixes join the keyboard's dictation corrections.
- Zones: rebuildLayout compiles the matching key block's zones (KeyboardLayout.zoneMap) into
  per-key zoneRects. KeyboardKey.takes, hitTest and the next-letter odds' distances use them.
- Aim: syncDecoderKeys replaces the decoder's centre for the letters the config names, over
  Learn how I type's average or Adaptive touch's centre; the baseline keys stay Learn how I
  type's.
- Adaptive touch records no taps while a config has zones.
- Typing logs: layout lines record each key's gapHitFrame (the space bar's band of the gap,
  which the logs lacked) and zoneRects. The hash includes zoneRects, and a typing_config
  layout cause is added.
- Edits to the files at their size limit (FullKeyboardEngine, AutocorrectEngine,
  KeyboardViewController) are line-for-line, apart from one stored property in
  FullKeyboardEngine. Three private methods became internal for the new extension.
- docs/typing-configs.md is new; typing-logs.md documents the new layout fields.

Verified on the Studio (iPhone 17 Pro, iOS 27.2):
- With no config, the iOS typing gate (scripts/typing-gate.py run, en es de fr pl,
  TEST_RUNNER_SWIFT_DETERMINISTIC_HASHING=1) gives byte-identical output-<lang>.json to the
  merge base 73ee886d in all five languages.
- FullKeyboardEngineTests: 161 of 161 pass, including two new cases in
  FullKeyboardEngineTests+AdaptiveTouch.swift. The first: a zone gives the space bar b's lowest
  3 pt, aim moves o's decoder centre by 0.2 keycap with the baseline kept, and removing the
  config restores the layout. The second: a kept word is left alone, neverCorrectTo blocks
  "hte" -> "the", and removing the config leaves TypingLearning() as it was.
- TypingLogTests, KeyboardLayoutTests, AutocorrectEngineTests, DictationLearningTests and
  LanguageLearningTests: 196 of 196 pass.
- code-shape.py --check passes; the largest file is 1,498 lines.
2026-11-23 12:47:08 -05:00

132 lines
7.2 KiB
Swift

import QuartzCore
import UIKit
/// What the engine hands Spatial autocorrect and Record typing logs when the keys or the
/// field change: the letter keys to read taps against, and the keys' and shift's state.
@MainActor
extension FullKeyboardEngine {
/// The letter keys Spatial autocorrect scores taps against: this layout's, moved by
/// `tapOffsets`; with Adaptive touch on, by its personal centres instead (none while it
/// has too little evidence), and for a typing config's letters by its aim, with the others
/// beside them as the baseline no correction may go past (`AutocorrectEngine.decoderBaselineKeys`). Off the letters page the last
/// letters page's stay: a closing mark from the 123 page still reads the word's taps
/// ("ofthe." -> "of the.").
func syncDecoderKeys() {
guard let keys = TypingDecoder.Keys(layout: layout, offsets: tapOffsets) else { return }
// A typing config's aim replaces what Hex learned for the letters it names.
let aim = typingConfigAim()
guard let adaptive = adaptiveTouch?.offsets(for: layout) ?? (aim.isEmpty ? nil : tapOffsets),
let personal = TypingDecoder.Keys(layout: layout, offsets: adaptive.merging(aim) { _, config in config }) else {
autocorrect.decoderKeys = keys
autocorrect.decoderBaselineKeys = nil
return
}
autocorrect.decoderKeys = personal
autocorrect.decoderBaselineKeys = personal == keys ? nil : keys
}
/// Hands the layout and shift to typing logs, which writes a line when either changed.
func logState(_ cause: String) {
typingLog?.keyboardState(layout: layout, shift: shift, cause: cause)
}
/// Whether a finger going down at `point`, in the strip above the keys, belongs to the
/// keys: in the strip's lower half while you type (a key is down, or one went down or came
/// up within `stripTouchGuard`) it is a row-1 key aimed high, not a pick. Nobody reads a
/// new suggestion and taps it that fast; Record typing logs showed such picks were the e
/// below the left slot, which kept "th" and typed a space where "the" was meant.
func takesStripTouch(at point: CGPoint) -> Bool {
let strip = layout.stripFrame
guard point.y < layout.touchAreaFrame.minY, point.y >= strip.midY,
point.x >= strip.minX, point.x < strip.maxX else { return false }
if !touches.isEmpty { return true }
guard let last = lastKeyTouchAt else { return false }
let elapsed = clock() - last
return elapsed >= 0 && elapsed < Self.stripTouchGuard
}
/// `point`, or for one above the keys (a strip touch `takesStripTouch(at:)` gave them)
/// the same spot on row 1's top edge.
func pointOnKeys(_ point: CGPoint) -> CGPoint {
let top = layout.touchAreaFrame.minY
return point.y < top ? CGPoint(x: point.x, y: top) : point
}
/// The kind of key a finger going down at `point` lands on, or nil off the keys: what
/// `touchBegan(id:at:)` will press, up to the next-letter odds choosing a neighbouring
/// letter. Cheap and free of side effects, so the key haptic can fire before any typing.
func keyKind(at point: CGPoint) -> KeyKind? {
layout.hitTest(pointOnKeys(point))?.kind
}
/// The kinds of every key a finger holds down now, its slide's key too.
var pressedKinds: Set<KeyKind> {
Set(touches.values.flatMap { touch in [touch.key.kind] + (touch.slideKey.map { [$0.kind] } ?? []) })
}
/// A key's label as the keys read it: upper case on the letters page with shift on, and the
/// Dvorak punctuation keys' `<`, `>` and `:` with a shift you pressed (the docked iPad draws
/// Apple's own pairs).
func displayLabel(for key: KeyboardKey) -> String {
guard let character = key.character else { return key.label }
if typesFrenchApostrophe(character) { return SmartPunctuation.apostrophe }
guard isUppercase, layerState.layer == .letters else { return character }
if let shifted = key.shifted, layout.metrics.pad?.isDocked != true, shift.isUserShift { return shifted }
return layout.language == .korean ? HangulComposer.shifted(character) : character.uppercased()
}
/// Whether the letter keys read as capitals: as `isUppercase`, except while a press that
/// turns shift on is still down (`ShiftStateMachine.pressShowsOff`).
var keycapsUppercase: Bool { isUppercase && !shift.pressShowsOff }
/// A key's label as its keycap shows it (`keycapsUppercase`); pop-ups use `displayLabel`.
func keycapLabel(for key: KeyboardKey) -> String {
guard let character = key.character else { return key.label }
if typesFrenchApostrophe(character) { return SmartPunctuation.apostrophe }
guard keycapsUppercase else { return character }
if let shifted = key.shifted, layout.metrics.pad?.isDocked != true, shift.isUserShift { return shifted }
return layout.language == .korean ? HangulComposer.shifted(character) : character.uppercased()
}
/// The key a touch going down at `point` presses: the one under it, or a neighbouring
/// letter within `letterReach` points that is enough likelier to come next in the word.
/// The number row's digits are never letters: a digit's tap stays a digit, and no digit
/// claims a row-1 letter's.
func key(at point: CGPoint) -> KeyboardKey? {
let point = pointOnKeys(point)
guard let key = layout.hitTest(point) else { return nil }
guard key.character != nil, !key.isNumberRow, let model = letterOdds, layerState.layer == .letters,
field.takesWords, autocorrect.isEnabled, target.autocorrectionType != .no,
shift.state != .locked, model.language == language.rawValue else { return key }
let reach = letterReach
let near = layout.keys.filter {
$0.character != nil && !$0.isNumberRow && $0.kind != key.kind && touchDistance(from: point, to: $0) < reach
}
guard !near.isEmpty, let continuations = continuationMass(model) else { return key }
// An accented letter without a key of its own is typed from its base letter's (é from e).
var mass = [Double](repeating: 0, count: continuations.count)
let keyed = Set(layout.keys.compactMap { key in
key.character.flatMap { $0.count == 1 ? model.codes[Character($0)] : nil }
})
for (code, m) in continuations.enumerated() where m > 0 {
mass[keyed.contains(UInt8(code)) ? code : Int(model.baseCodes[code])] += m
}
let total = mass.reduce(0, +)
func odds(_ k: KeyboardKey) -> Double? {
guard let c = k.character, c.count == 1, let code = model.codes[Character(c)] else { return nil }
return log(mass[Int(code)] + total * 1e-4)
}
guard let own = odds(key) else { return key }
var best = key
var bestScore = own
for other in near {
guard let theirs = odds(other) else { continue }
let score = theirs - letterOddsPerPoint * Double(touchDistance(from: point, to: other))
if score > bestScore {
best = other
bestScore = score
}
}
return best
}
}