hex/ios/HEXShared/FullKeyboard/Engine/FullKeyboardEngine+TypingConfig.swift
Ryan Vogel 47a2e08f03 ios: typing config: compile the active config once per generation
The keyboard re-read active.json through a full TypingConfigReview (JSON parse, decode, every
check) whenever it built a letters layout it hadn't just built, to find that layout's aim and
zones, and compiled the zone map again each time. TypingConfigActive now compiles the file once
per typingConfig.generation into what the keys use and keeps it for the life of the process: the
words, and each key block's context, aim and zones as plain values, read and clamped by the same
two helpers the review uses (TypingConfigReview.aim / .zone). A layout only picks the first block
whose context matches (zones dropped when the hit model differs, as the review's
needsCheckingAgain), and the compiled zone map is reused while the keys' geometry is the same.

Changed behaviour: hasZones (which stops Adaptive touch recording) now counts only zones that
parse, in the blocks a reader looks at; a file whose only zones are unreadable no longer pauses
it. keys(on:) returns the block or nil instead of a TypingConfigReview.Applied.

Verified (Studio, iPhone 17 Pro sim, iOS 27.2, Full Access on): a tap 3.35 pt inside r's area
types "E" with the key-off fixture's zones and "R" with no config or a words/aim-only config, on
the app built from this tree; memory and suite results are in the PR thread.
2026-11-23 12:47:08 -05:00

43 lines
2 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import CoreGraphics
/// Settings › Experiments › Typing configs: what the active config (`typingConfig`) changes in
/// the keys (`ios/TypingConfig/package/reference/typing-config.md`, "Who wins"). Its words go to
/// autocorrect on top of Learn how I type's, in memory only; its zones move what a tap presses
/// on the letters page; its aim replaces the decoder's centre for the letters it names. With no
/// config every one of these is a no-op.
@MainActor
extension FullKeyboardEngine {
func typingConfigChanged() {
rebuildLayout(cause: "typing_config")
syncLearning()
syncAdaptiveField()
}
/// `layout` with the active config's zones, when it has a key block for these keys.
func zoned(_ layout: KeyboardLayout) -> KeyboardLayout {
TypingConfigActive.record(layout)
guard let config = typingConfig, let zones = config.keys(on: layout)?.zones, !zones.isEmpty else { return layout }
return layout.applying(config.zoneMap(zones, on: layout))
}
/// The active config's aim for these keys, in points from each letter's keycap centre.
func typingConfigAim() -> [Character: CGVector] {
guard let aim = typingConfig?.keys(on: layout)?.aim, !aim.isEmpty else { return [:] }
var out: [Character: CGVector] = [:]
for (letter, aim) in aim {
guard let key = layout.keys.first(where: { $0.character == String(letter) }) else { continue }
out[letter] = CGVector(dx: aim.dx * key.frame.width, dy: aim.dy * key.frame.height)
}
return out
}
/// `learning` with the active config's words.
func withTypingConfig(_ learning: TypingLearning) -> TypingLearning {
typingConfig.map { learning.adding($0.words) } ?? learning
}
/// How far `point` is from `key`'s touch area, zones included, for the next-letter odds.
func touchDistance(from point: CGPoint, to key: KeyboardKey) -> CGFloat {
key.zoneRects?.map { Self.distance(from: point, to: $0) }.min() ?? Self.distance(from: point, to: key.hitFrame)
}
}