hex/ios/HEXShared/FullKeyboard/TypingConfig/TypingConfigContext.swift
Ryan Vogel ccf81bb240 ios: typing config shared model: the file, one review of it, active.json and its settings
What the phone lane builds on, so there is one parser (HEXShared/FullKeyboard/TypingConfig/):
- TypingConfigFile: the Codable model of a format-0 file (envelope, words, aim, zones, evidence,
  note). Lists decode leniently: an entry that doesn't parse is kept as nil and named, and the
  rest is read.
- TypingConfigReview(data⌨️checksEvidence:): what Hex would do with a raw file on one
  keyboard.
  - Every part gets an Item: applied, clamped(from:to:), shortened, or skipped(Reason).
  - Reason is an ordered enum: notAConfig, tooLarge, newerFormat, unknownField, invalidValue,
    tooMany, duplicate, notEnoughEvidence, forAnotherKeyboard, needsCheckingAgain,
    keyNotOnKeyboard, edgeNotAllowed, twoZonesClaimPoint, keyTooSmall.
  - It also returns each key block's ContextCheck (made for, keyboard, differences, hit model),
    the SHA-256 of the bytes, and Applied (what the keyboard will use).
  - compiled(switchedOn:) produces active.json's content.
- KeyboardZoneMap / KeyboardLayout.zoneMap: zones compiled to disjoint per-key rectangles.
  Overlapping zones and zones that leave a keycap's centre region uncovered are skipped.
- TypingConfigFile.Context(layout:) and differences(from:): the context rules of the reference.
- TypingConfigSettings (experiments.typingConfigs, typingConfig.generation) and
  TypingConfigStore: writeActive (atomic, bumps the generation), removeActive, and activeData
  (nil when off or without Full Access).
- The generator also emits each part's evidence minimums.

The reference now says the phone doesn't recompute a file's id (number printing differs
between languages); history keys on the bytes' hash. Nothing in the keyboard reads a config
yet: no behaviour change.

Verified on the Studio (iPhone 17 Pro, iOS 27.2) with three new cases in
FullKeyboardEngineTests+AdaptiveTouch.swift: every reason above except tooMany, each with the
path it names; that a zone moves where a tap counts while every point keeps exactly one key
(1.5 pt grid); and compile, write, read back without evidence, then remove.
build-typing-config.py --check passes.
2026-11-23 12:47:08 -05:00

53 lines
2.4 KiB
Swift

import CoreGraphics
/// The keyboard a typing config's key block is for, read from a live layout, and how two
/// compare (`reference/typing-config.md`, "Context").
nonisolated extension TypingConfigFile.Context {
init(layout: KeyboardLayout) {
let m = layout.metrics
self.init(
width: Double(layout.width),
orientation: layout.orientation == .portrait ? "portrait" : "landscape",
posture: Self.name(layout.posture),
language: layout.language.rawValue,
letters: layout.letters.rawValue,
split: layout.isSplit,
hexPlacement: layout.hexPlacement.rawValue,
numberRow: layout.hasNumberRow,
keyHeight: Double(m.keyHeight),
rowPitch: Double(m.keyHeight + m.rowGap),
letterWidth: Double(layout.letterWidth),
hitModel: KeyboardZone.hitModel
)
}
static func name(_ posture: KeyboardPosture) -> String {
switch posture {
case .phone: "phone"
case .cover: "cover"
case .unfoldedWide: "unfoldedWide"
case .unfoldedTall: "unfoldedTall"
}
}
/// The fields that keep this block off `live`, in the order the context lists them; empty
/// when it applies there. `hitModel` is `TypingConfigReview.ContextCheck.hitModelMatches`.
func differences(from live: Self) -> [String] {
let round = Double(TypingConfigLimits.contextWidthRound)
let tolerance = Double(TypingConfigLimits.contextGeometryTolerance)
func near(_ a: Double, _ b: Double) -> Bool { b > 0 && abs(a - b) <= tolerance * b }
var out: [String] = []
if (width / round).rounded() != (live.width / round).rounded() { out.append("width") }
if orientation != live.orientation { out.append("orientation") }
if posture != live.posture { out.append("posture") }
if language != live.language { out.append("language") }
if letters != live.letters { out.append("letters") }
if split != live.split { out.append("split") }
if hexPlacement != live.hexPlacement { out.append("hexPlacement") }
if numberRow != live.numberRow { out.append("numberRow") }
if !near(keyHeight, live.keyHeight) { out.append("keyHeight") }
if !near(rowPitch, live.rowPitch) { out.append("rowPitch") }
if !near(letterWidth, live.letterWidth) { out.append("letterWidth") }
return out
}
}