hex/ios/HEXShared/TypingLog/TypingLogger.swift
Ryan Vogel 52b9e475a6
iOS: the full keyboard's top bar matches the system keyboard (#2135)
* iOS: the full keyboard's top bar matches Apple's predictive bar

Kit's App Store screenshot (dark mode) showed the suggestions sitting low
with a large empty band between the keyboard panel's top edge and the
words. Measured black-box against Apple's English keyboard in the same
Messages field (iOS 27.2 simulator, iPhone 17 Pro, SE 3rd gen, 17 Pro
Max): iOS 26 draws a band of its own above every third-party keyboard
(16.3 pt on the 17 Pro, 12.5 pt on the SE, 13.3 pt on the Pro Max) that
no extension can draw into (checked with a throwaway build drawing at
negative y: the hosted window is exactly the keyboard's height). Hex
then added Apple's whole 44 pt bar and 8 pt gap below it, so its keycaps
started 68 pt below the panel edge where Apple's start 48-52 pt, and its
words sat 38 pt down where Apple's sit 20-24 pt.

What changed:
- The phone strip is the 27 pt of Apple's bar below iOS's band, then the
  same 8 pt gap: row 1 starts 35 pt down (Apple's keycaps are 35.0-35.5 pt
  below the extension's top on all three iPhones), so the panel edge to
  row 1 now measures 51.3 pt against Apple's 51.7 pt on the 17 Pro. The
  keyboard is 253 pt (172 pt landscape) instead of 270 (189); keys do not
  move on screen, only the panel's top edge comes down 17 pt. The Duo
  keeps its 44 pt bar (KeyboardMetrics.insetStripHeight).
- Slots are thirds of the keyboard as Apple's are (dividers end at 134
  and 268 pt on a 402 pt iPhone), with the Hex key's room taken from the
  slot beside it (KeyboardLayout.stripSlotFrames), instead of thirds of
  the strip left of the Hex key. Typing logs record the real slot frames.
- Dividers are Apple's 1 x 24 pt (were 1 px x 22 pt), ending on the
  boundary, in Apple's measured ink.
- Words are Apple's muted ink (dark 0.58 white, light 0.67 black) instead
  of full white/black; the literal word no longer has its own alpha.
- A pending autocorrection is Apple's full-slot capsule with white words
  in regular weight (was a 10 pt-radius pill inset 4 pt, medium weight).
  The capsule is 26 pt tall, not Apple's 38 pt: Apple's top 11 pt falls
  in iOS's band.
- The Hex key, recording bar (Dot Scope well, X and check) and Undo /
  Paste chips use the strip's pill height (26 pt, 4 pt-inset 36 pt on the
  Duo); the Hex mark keeps its 19 pt. The Full Access hint's two lines
  fit 27 pt (13 / 11.5 pt).
- #2100's rule is unchanged: a fast tap on the strip's lower half (below
  its text line, now 13.5 pt down) types the row-1 key below; its tests
  pass as written.

Kept on purpose: words are centred in the 27 pt Hex can draw, about
5.5 pt lower than Apple's relative to the panel edge, so the pill has
room around them; Apple lets long words run past their slot at 17 pt,
Hex still shrinks them to 15 pt and truncates, so nothing runs under
the Hex key. Which suggestions appear (#2125) is untouched.

Verified: xcodebuild test on the iPhone 17 Pro Max simulator, 383 tests,
0 failures, 1 skipped, with updated KeyboardLayoutTests (heights, strip
metrics, row 1 at 35 pt) and a new case for Apple's thirds with the Hex
key on either side. Side-by-side screenshots of Apple's keyboard and
Hex in Messages (empty, typing, literal in quotes, pending correction,
long word, next word) on the 17 Pro and SE in light and dark, and the
recording bar, Full Access hint and Hex key on the left before and after.

* iOS: the top bar's light-mode autocorrection pill matches Apple's

The first pass guessed an opaque white capsule with black words for a
pending autocorrection in light mode. Apple's, measured in Messages on the
iPhone 17 Pro (iOS 27.2 simulator), is a faint white tint, (235,237,240)
on the (226,227,232) platter, under near-black (44,46,49) words; dark mode
was already measured and is unchanged.

Verified: rebuilt and compared the pending-correction state against
Apple's keyboard in the same field in light mode; xcodebuild test.

* iOS: the top bar offers three words in an empty field and between words

Ryan's screenshot of the current build in an empty field (dark mode, shift
on) showed an empty bar with two dividers and nothing in the slots, where
Apple's keyboard shows three sentence starters (I | The | I'm). After a
word, Hex often showed one prediction in the middle with empty slots
beside it, where Apple's fills all three.

- An empty field, and a new sentence (after ". ", "! ", "? " or a new
  line), offers three common first words from a short hand-written list
  per language (the typing model has no start-of-sentence row): English
  I / The / I'm, Spanish Yo / El / La, and so on for every layout,
  Ukrainian included. They take a capital when shift would turn itself on
  (never with Auto-capitalization off; "I" and "I'm" keep theirs), pass
  never-suggest, and are tap-only like #2125's predictions.
- After a word and a space, the model's next words keep #2125's ranking
  for the slots they fill; when it has fewer than three, the language's
  commonest words fill the rest, so no slot stays empty.
- After a comma (or ; : ) ” ’ —) and a space, three words that start a
  clause (English I / but / and). After a handle, a path, a number or a
  second space the bar stays empty, as #2125 had it.
- With Emoji (on by default since #2129) a finished word's emoji no longer
  replaces the predictions: one emoji takes the centre with next words on
  either side ("pizza ": and | 🍕 | hut); two or three fill from the left.
- Picking a starter in an empty field or after a new line inserts it (the
  queued-tap guard now checks that no word was started, not that the text
  ends in a space).

Verified: xcodebuild test on the iPhone 17 Pro simulator (411 tests,
0 failures, 1 skipped), with a new FullKeyboardEngineTests case for the
starters, capitals, the fill and the comma, and #2125's boundary test
updated for ". " and a new line. Held-out tap benchmark (2,000 English
and 2,000 Spanish test sentences, Spatial autocorrect, freshly erased
simulators): finished text is unchanged within the benchmark's own noise
(two runs of the same build differ in 21 English and 32 Spanish sentences,
from the system spell checker); English words broken 528 and 529 before,
528 after, fixed 3,147/3,148 before, 3,147 after; Spanish broken 419/416
before, 417 after, fixed 3,744 before, 3,745 after. Side-by-side shots
against Apple's keyboard in Messages: empty field, after ". ", after
"hi ", after a comma, light and dark, iPhone 17 Pro, SE and 17 Pro Max.

* iOS: the top bar matches Apple's in landscape and on the Duo's cover screen

The 27 pt strip assumed the band iOS draws above a third-party keyboard,
which the iPhone has in portrait. Measured against Apple's keyboard
(iOS 27.2 simulator):

- Landscape (iPhone 17 Pro Max): iOS draws no band, and Apple's keycaps
  start 50 pt below the panel's edge where Hex's started 35 pt down. The
  landscape strip is now 42 pt plus the 8 pt gap (row 1 at 50 pt, keyboard
  187 pt, was 172 pt).
- The Duo folded and upright: the band is 8.7 pt and Apple's keycaps start
  48.7 pt below the edge where Hex's started 52.7 pt down (its 44 pt bar
  sat right on the keys). It is now 32 pt plus the 8 pt gap, so the panel
  edge to row 1 measures 48.7 pt for both. The unfolded postures and the
  cover in landscape keep the 44 pt bar (not measured against the band).
- Pills (an autocorrection, a pressed word, the Hex key, the recording
  bar) are the strip less half a point above and below, at most 36 pt.

Keys do not move on screen; only the panel's top edge does. iPhone
portrait keeps the 27 + 8 pt contract (51.3 pt panel edge to row 1 on
the 17 Pro against Apple's 51.7; 48.67 pt on the 17 Pro Max, the same as
Apple's).

Verified: KeyboardLayoutTests updated for the new heights and metrics;
xcodebuild test (411 tests, 0 failures, 1 skipped); screenshots of the
hosted keyboard next to Apple's in landscape and on the Duo's cover.

* iOS: top-bar dividers, Logging mark and micro-animations follow Apple's

Measured on Apple's keyboard in the iOS 27.2 simulator, recorded at
60 fps with simctl io recordVideo and timed frame by frame:

- A changed word swaps letter by letter: letters the old and new word
  start with stay, the old word's other letters fade from its end and the
  new word's from its start, each in 0.1 s, staggered over 0.2 s (Apple:
  0.14-0.23 s). A word leaving an emptied slot fades the same way. The
  pending-correction capsule and the dividers change at once, as Apple's
  do, and so does a pressed word's capsule on touch down and release.
  Reduce Motion swaps at once. Before, every change was an instant redraw.
- A pressed word gets the same capsule as a pending correction (Apple's
  measured identical: (115,115,115) dark, (235,237,240) light), not a
  lighter one.
- The Undo dictation, Paste and Code from Messages chips and the Full
  Access hint fade in and out over the same 0.2 s instead of popping.
- Dividers are drawn only between two filled slots, so a bar with
  nothing to show (a URL or email field, or a lone "Kq") has none, as
  Apple's has none in secure fields. Apple does draw them beside empty
  slots in URL fields and for a lone literal; Hex doesn't, by request.
- The Logging mark (Record typing logs) sat at the strip's top corner,
  over slot 1's word since the strip became 27 pt ("Logging" overlapped
  "ritsrdo" in the Italian scene). It now sits on the text line at the
  outer end, and that slot centres its word in the room left.

Verified: xcodebuild test (411 tests, 0 failures, 1 skipped); recordings
of Apple's bar and Hex's on the iPhone SE simulator (typing "hello", a
pending correction, a held and released pick, the Paste chip), and
before/after snapshot scenes for the Logging mark on either side.
2026-11-21 03:26:34 +00:00

551 lines
22 KiB
Swift

import CoreGraphics
import QuartzCore
import UIKit
/// Record typing logs, on the main thread: turns what the keyboard does into events, keeps
/// them in memory, and hands them to `TypingLogStore` every 2 s, when the keyboard goes
/// away, and on a memory warning. Exists only while the experiment is on.
@MainActor
final class TypingLogger {
static let flushInterval: TimeInterval = 2
static let maxBuffered = 1500
static let contextLimit = 200
/// A moving finger is written once it has gone this far and this long since its last line.
static let moveMinInterval: TimeInterval = 0.008
static let moveMinDistance: CGFloat = 2
private static var shared: TypingLogger?
/// The one logger this process writes while the experiment is on; nil while it is off.
static func sharedIfEnabled(settings: TypingLogSettings = TypingLogSettings()) -> TypingLogger? {
guard settings.isEnabled else {
shared?.endSession(reason: "disabled")
shared = nil
return nil
}
if let shared { return shared }
let logger = TypingLogger(store: TypingLogStore())
logger.store.prune()
shared = logger
return logger
}
let store: TypingLogStore
var wallClock: () -> TimeInterval = { Date().timeIntervalSince1970 }
var monoClock: () -> TimeInterval = { CACurrentMediaTime() }
/// The host's text around the cursor (`documentContextBeforeInput`/`AfterInput`).
var contextSource: (() -> TypingLogContext)?
private struct LayoutInputs: Equatable {
var width: CGFloat
var orientation: KeyboardOrientation
var layer: KeyboardLayer
var field: KeyboardFieldKind
var showsGlobe: Bool
var horizontalInset: CGFloat
var placement: HexKeyPlacement
init(_ layout: KeyboardLayout) {
width = layout.width
orientation = layout.orientation
layer = layout.layer
field = layout.field
showsGlobe = layout.showsGlobe
horizontalInset = layout.horizontalInset
placement = HexKeyPlacement.current
}
}
private struct Move {
var time: TimeInterval
var point: CGPoint
var key: String?
}
private struct Session {
let id: String
let startedAt: TimeInterval
let skipped: Bool
var seq = 0
var touches = 0
var edits = 0
var layoutInputs: LayoutInputs?
var layout: KeyboardLayout?
var layoutHash: String?
var shift: ShiftState?
var shiftHeld = false
var viewSize: CGSize = .zero
var safeArea: UIEdgeInsets = .zero
/// The view's size or safe area changed since the last `layout` line.
var geometryChanged = false
var context: TypingLogContext?
var shownCandidates: [KeyboardSuggestion]?
var candidates: [KeyboardSuggestion] = []
var moves: [Int: Move] = [:]
var dictations = 0
}
private var session: Session?
private var buffer: [TypingLogEvent] = []
private var timer: Timer?
/// The last dictation logged, which an outcome refers to.
private var lastDictation: (session: String, seq: Int)?
init(store: TypingLogStore) {
self.store = store
}
/// A session is open and its field may be logged.
var isRecording: Bool { session?.skipped == false }
var sessionID: String? { session?.id }
// MARK: - Sessions
/// The keyboard appeared, or moved to another field. Logs `session_start`, or one
/// `skipped` line and nothing else until the session ends when the field must not be logged.
func beginSession(_ info: TypingLogSessionInfo) {
if session != nil { endSession(reason: "replaced") }
let id = String(UUID().uuidString.replacingOccurrences(of: "-", with: "").prefix(12)).lowercased()
if let reason = info.traits.skipReason {
session = Session(id: id, startedAt: monoClock(), skipped: true)
emit("skipped", [
TypingLogField("reason", .string(reason)),
TypingLogField("trigger", .string(info.trigger)),
TypingLogField("surface", .string(info.surface)),
TypingLogField("keyboardType", .string(info.traits.keyboardType)),
TypingLogField("textContentType", .text(info.traits.textContentType)),
TypingLogField("secure", .bool(info.traits.isSecure)),
], force: true)
flush()
return
}
session = Session(id: id, startedAt: monoClock(), skipped: false)
emit("session_start", info.fields)
context("session_start", force: true)
startTimer()
}
func endSession(reason: String) {
guard let current = session else { return }
if !current.skipped {
emitPendingCandidates()
context("session_end")
emit("session_end", [
TypingLogField("reason", .string(reason)),
TypingLogField("durationMS", .ms((monoClock() - current.startedAt) * 1000)),
TypingLogField("touches", .int(current.touches)),
TypingLogField("edits", .int(current.edits)),
TypingLogField("dictations", .int(current.dictations)),
])
}
session = nil
timer?.invalidate()
timer = nil
flush()
store.close()
}
/// Hands buffered events to the writer.
func flush() {
guard !buffer.isEmpty else { return }
store.append(buffer)
buffer.removeAll(keepingCapacity: true)
}
private func startTimer() {
timer?.invalidate()
let timer = Timer(timeInterval: Self.flushInterval, repeats: true) { [weak self] _ in
MainActor.assumeIsolated { self?.flush() }
}
timer.tolerance = 0.5
RunLoop.main.add(timer, forMode: .common)
self.timer = timer
}
// MARK: - Keys
/// The layout or shift changed (or may have): writes a `layout` line when either did.
/// The key list goes in full the first time a layout appears in a day's file.
func keyboardState(layout: KeyboardLayout, shift: ShiftStateMachine, cause: String) {
guard var current = session, !current.skipped else { return }
let inputs = LayoutInputs(layout)
let layoutChanged = inputs != current.layoutInputs
guard layoutChanged || shift.state != current.shift || shift.isHeld != current.shiftHeld else { return }
if layoutChanged {
current.layoutInputs = inputs
current.layout = layout
current.layoutHash = Self.hash(layout)
}
current.shift = shift.state
current.shiftHeld = shift.isHeld
session = current
emitLayout(cause: cause)
}
/// The keyboard view's size or safe area, before the layout follows it. The next
/// `layout` line carries it; `commitGeometry` writes one if the layout didn't change.
func viewGeometry(size: CGSize, safeArea: UIEdgeInsets) {
guard var current = session, !current.skipped,
size != current.viewSize || safeArea != current.safeArea else { return }
current.viewSize = size
current.safeArea = safeArea
current.geometryChanged = true
session = current
}
func commitGeometry() {
guard let current = session, !current.skipped, current.geometryChanged, current.layout != nil else { return }
emitLayout(cause: "geometry")
}
private func emitLayout(cause: String) {
session?.geometryChanged = false
guard let current = session, let layout = current.layout, let hash = current.layoutHash else { return }
var fields = [
TypingLogField("hash", .string(hash)),
TypingLogField("cause", .string(cause)),
TypingLogField("layer", .string(Self.name(layout.layer))),
TypingLogField("shift", .string(Self.name(current.shift ?? .off))),
TypingLogField("field", .string(Self.name(layout.field))),
TypingLogField("orientation", .string(layout.orientation == .portrait ? "portrait" : "landscape")),
TypingLogField("width", .pt(layout.width)),
TypingLogField("height", .pt(layout.totalHeight)),
TypingLogField("view", .array([.pt(current.viewSize.width), .pt(current.viewSize.height)])),
TypingLogField("safeArea", TypingLogSessionInfo.insets(current.safeArea)),
TypingLogField("horizontalInset", .pt(layout.horizontalInset)),
TypingLogField("globe", .bool(layout.showsGlobe)),
TypingLogField("hexPlacement", .string(HexKeyPlacement.current.rawValue)),
TypingLogField("language", .string(layout.language.rawValue)),
]
if current.shiftHeld {
fields.insert(TypingLogField("shiftHeld", .bool(true)), at: 4)
}
emit("layout", fields, detail: TypingLogEvent.Detail(key: hash) { Self.layoutDetail(layout) })
}
nonisolated private static func layoutDetail(_ layout: KeyboardLayout) -> [TypingLogField] {
let m = layout.metrics
let strip = layout.stripFrame
let slots = layout.stripSlotFrames.map { TypingLogValue.rect($0) }
let keys = layout.keys.map { key in
TypingLogValue.object([
TypingLogField("id", .string(keyID(key.kind))),
TypingLogField("label", .string(key.label)),
TypingLogField("row", .int(key.row)),
TypingLogField("frame", .rect(key.frame)),
TypingLogField("hitFrame", .rect(key.hitFrame)),
])
}
return [
TypingLogField("letterWidth", .pt(layout.letterWidth)),
TypingLogField("metrics", .object([
TypingLogField("side", .pt(m.side)),
TypingLogField("gap", .pt(m.gap)),
TypingLogField("rowGap", .pt(m.rowGap)),
TypingLogField("keyHeight", .pt(m.keyHeight)),
TypingLogField("stripHeight", .pt(m.stripHeight)),
TypingLogField("keysBottom", .pt(m.keysBottom)),
TypingLogField("cornerRadius", .pt(m.cornerRadius)),
])),
TypingLogField("strip", .rect(strip)),
TypingLogField("slots", .array(slots)),
TypingLogField("keys", .array(keys)),
]
}
/// A finger on the keys. `key` is the key the engine holds it on (for `began`, the
/// key it is about to press); `under` is written when the key under the raw point differs.
func touch(_ phase: TypingLogTouchPhase, id: Int, sample: TypingLogTouchSample, key: KeyboardKey?, under: KeyboardKey? = nil) {
guard var current = session, !current.skipped else { return }
let keyName = key.map { Self.keyID($0.kind) }
switch phase {
case .began:
emitPendingCandidates()
current = session ?? current
current.touches += 1
current.moves[id] = Move(time: sample.timestamp, point: sample.point, key: keyName)
case .moved:
if let last = current.moves[id] {
let distance = hypot(sample.point.x - last.point.x, sample.point.y - last.point.y)
let slid = keyName != last.key
guard slid || (distance >= Self.moveMinDistance && sample.timestamp - last.time >= Self.moveMinInterval) else {
return
}
}
current.moves[id] = Move(time: sample.timestamp, point: sample.point, key: keyName)
case .ended, .cancelled:
current.moves.removeValue(forKey: id)
}
session = current
var fields = [
TypingLogField("phase", .string(phase.rawValue)),
TypingLogField("id", .int(id)),
TypingLogField("x", .pt(sample.point.x)),
TypingLogField("y", .pt(sample.point.y)),
TypingLogField("r", .pt(sample.majorRadius)),
TypingLogField("rt", .pt(sample.majorRadiusTolerance)),
TypingLogField("ts", .ms(sample.timestamp * 1000)),
TypingLogField("layout", .text(current.layoutHash)),
TypingLogField("key", .text(keyName)),
]
if sample.maximumPossibleForce > 0 {
fields.insert(TypingLogField("force", .number(Double(sample.force), places: 3)), at: 6)
fields.insert(TypingLogField("maxForce", .number(Double(sample.maximumPossibleForce), places: 3)), at: 7)
}
if let under, under.kind != key?.kind {
fields.append(TypingLogField("under", .string(Self.keyID(under.kind))))
} else if under == nil, phase != .moved, key != nil {
fields.append(TypingLogField("under", .null))
}
emit("touch", fields)
}
/// Something the keys did to the text.
func edit(_ op: String, _ fields: [TypingLogField] = []) {
guard session?.skipped == false else { return }
session?.edits += 1
emit("edit", [TypingLogField("op", .string(op))] + fields)
}
/// A tap on the strip, the Hex key or its menu, an accent picker, or the trackpad.
func ui(_ action: String, _ fields: [TypingLogField] = []) {
emit("ui", [TypingLogField("action", .string(action))] + fields)
}
/// The suggestions the strip shows. Written lazily: before the next touch goes down,
/// with a pick, or when the session ends, so a line is what was on screen.
func suggestionsChanged(_ suggestions: [KeyboardSuggestion]) {
guard session?.skipped == false else { return }
session?.candidates = suggestions
}
var shownSuggestions: [KeyboardSuggestion] { session?.candidates ?? [] }
private func emitPendingCandidates() {
guard let current = session, !current.skipped, current.candidates != current.shownCandidates else { return }
session?.shownCandidates = current.candidates
emit("candidates", [TypingLogField("items", Self.candidates(current.candidates))])
}
static func candidates(_ suggestions: [KeyboardSuggestion]) -> TypingLogValue {
.array(suggestions.enumerated().map { index, suggestion in
var fields = [
TypingLogField("slot", .int(index)),
TypingLogField("text", .string(suggestion.displayText)),
]
if suggestion.replacement != suggestion.displayText {
fields.append(TypingLogField("replacement", .string(suggestion.replacement)))
}
switch suggestion.kind {
case .keepLiteral:
fields.append(TypingLogField("kind", .string("literal")))
case .primary(let auto):
fields.append(TypingLogField("kind", .string("primary")))
fields.append(TypingLogField("auto", .bool(auto)))
case .secondary:
fields.append(TypingLogField("kind", .string("secondary")))
case .nextWord:
fields.append(TypingLogField("kind", .string("next_word")))
case .emoji:
fields.append(TypingLogField("kind", .string("emoji")))
case .swiped:
fields.append(TypingLogField("kind", .string("swiped")))
}
return .object(fields)
})
}
// MARK: - Text
/// The host's text around the cursor, after a word, a return, a dictation, or a change
/// from outside the keys. Identical context is written once. `cleared` marks the host
/// emptying a field that had text (most apps do that when they send).
func context(_ why: String, force: Bool = false) {
guard var current = session, !current.skipped, let read = contextSource?() else { return }
guard force || read != current.context else { return }
let previous = current.context
current.context = read
session = current
var fields = [
TypingLogField("why", .string(why)),
TypingLogField("before", .text(read.before.map { String($0.suffix(Self.contextLimit)) })),
TypingLogField("after", .text(read.after.map { String($0.prefix(Self.contextLimit)) })),
TypingLogField("beforeLength", read.before.map { .int($0.count) } ?? .null),
TypingLogField("afterLength", read.after.map { .int($0.count) } ?? .null),
TypingLogField("hasText", .bool(read.hasText)),
]
if let previous, !read.hasText,
previous.hasText || !(previous.before ?? "").isEmpty || !(previous.after ?? "").isEmpty {
fields.append(TypingLogField("cleared", .bool(true)))
}
emit("context", fields)
}
// MARK: - Dictation
/// Hex inserted a dictation's text.
func dictation(source: String, resultID: String?, style: String, stages: DictationStages?, delivered: String, inserted: String) {
guard var current = session, !current.skipped else { return }
current.dictations += 1
session = current
var fields = [
TypingLogField("source", .string(source)),
TypingLogField("resultID", .text(resultID)),
TypingLogField("style", .string(stages?.style ?? style)),
]
if let stages { fields += stages.fields }
fields += [
TypingLogField("text", .string(delivered)),
TypingLogField("inserted", .string(inserted)),
]
let seq = emit("dictation", fields)
lastDictation = seq.map { (current.id, $0) }
context("dictation")
}
/// What became of a dictation (`DictationEditTracker`): it always concerns the latest
/// `dictation` line written before it, which `dictation`/`dictationSession` name.
func dictationOutcome(_ result: DictationEditResult) {
var fields = [
TypingLogField("outcome", .string(result.outcome.rawValue)),
TypingLogField("words", .int(result.wordCount)),
TypingLogField("changed", .int(result.changedWordCount)),
]
if let lastDictation {
fields.insert(TypingLogField("dictationSession", .string(lastDictation.session)), at: 0)
fields.insert(TypingLogField("dictation", .int(lastDictation.seq)), at: 1)
}
fields.append(TypingLogField("pairs", .array(result.pairs.map { pair in
.object([
TypingLogField("kind", .string(pair.kind.rawValue)),
TypingLogField("heard", .string(pair.heard)),
TypingLogField("inserted", .string(pair.inserted)),
TypingLogField("meant", .string(pair.meant)),
TypingLogField("before", .text(pair.before)),
TypingLogField("after", .text(pair.after)),
])
})))
emit("dictation_outcome", fields)
}
// MARK: - Lines
@discardableResult
private func emit(_ type: String, _ fields: [TypingLogField], detail: TypingLogEvent.Detail? = nil, force: Bool = false) -> Int? {
guard var current = session, force || !current.skipped else { return nil }
current.seq += 1
let seq = current.seq
session = current
buffer.append(TypingLogEvent(
type: type,
wallMS: Int64((wallClock() * 1000).rounded()),
monoMS: monoClock() * 1000,
session: current.id,
seq: seq,
fields: fields,
detail: detail
))
if buffer.count >= Self.maxBuffered { flush() }
return seq
}
// MARK: - Names
/// A key's id in logs: the character itself for character keys (always one character),
/// otherwise a word: `shift`, `delete`, `space`, `return`, `hex`, `globe`, `layer:numbers`.
nonisolated static func keyID(_ kind: KeyKind) -> String {
switch kind {
case .character(let text): text
case .shift: "shift"
case .layerSwitch(let layer): "layer:\(name(layer))"
case .delete: "delete"
case .space: "space"
case .returnKey: "return"
case .hex: "hex"
case .globe: "globe"
case .emoji: "emoji"
}
}
nonisolated static func name(_ layer: KeyboardLayer) -> String {
switch layer {
case .letters: "letters"
case .numbers: "numbers"
case .symbols: "symbols"
}
}
nonisolated static func name(_ field: KeyboardFieldKind) -> String {
switch field {
case .text: "text"
case .url: "url"
case .email: "email"
}
}
nonisolated static func name(_ shift: ShiftState) -> String {
switch shift {
case .off: "off"
case .on: "on"
case .locked: "locked"
}
}
/// 64-bit FNV-1a over every key's kind and frames and the strip, as 16 hex digits.
nonisolated static func hash(_ layout: KeyboardLayout) -> String {
var hash: UInt64 = 0xcbf2_9ce4_8422_2325
func mix(_ text: String) {
for byte in text.utf8 {
hash ^= UInt64(byte)
hash = hash &* 0x0000_0100_0000_01b3
}
}
func mix(_ value: CGFloat) {
// Rounded to 1/100 pt so the hash is the same wherever the same frames come from.
var bits = UInt64(bitPattern: Int64((Double(value) * 100).rounded()))
for _ in 0..<8 {
hash ^= bits & 0xff
hash = hash &* 0x0000_0100_0000_01b3
bits >>= 8
}
}
func mix(_ rect: CGRect) {
mix(rect.minX)
mix(rect.minY)
mix(rect.width)
mix(rect.height)
}
mix(name(layout.layer))
mix(name(layout.field))
mix(layout.orientation == .portrait ? "p" : "l")
mix(layout.stripFrame)
for key in layout.keys {
mix(keyID(key.kind))
mix(key.frame)
mix(key.hitFrame)
}
return String(format: "%016llx", hash)
}
}
extension DictationStages {
var fields: [TypingLogField] {
var fields: [TypingLogField] = []
func add(_ key: String, _ value: String?) {
if let value { fields.append(TypingLogField(key, .string(value))) }
}
add("engine", engine)
add("locale", locale)
add("language", language)
if let recordedMS { fields.append(TypingLogField("recordedMS", .int(recordedMS))) }
if let transcribeMS { fields.append(TypingLogField("transcribeMS", .int(transcribeMS))) }
add("raw", raw)
add("fixed", fixed)
add("spelled", spelled)
add("tagged", tagged)
add("cleaned", cleaned)
add("translated", translated)
add("formatted", formatted)
return fields
}
}