mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
A host reports textDidChange for the keys' own inserts, and some report it after the insert has finished, with a proxy that still returns the text from before it. textDidChange cleared swipedWord and read auto-capitalization from that report, so "This is a test" swiped quickly came out "This Isa test". A swipe now records the text it left before and after the cursor, and textDidChange keeps it while the host's update is that echo or a lagging read, still clearing it for a change made elsewhere, a cursor move, a selection, a cleared field or another field. A short-lived record of Hex's own insert (typingContext, bounded by clock, with the text after the cursor too) gives word boundaries and casing what Hex typed while the host catches up, in the host's own straight-quote form. Paths that move text around the keys (a delete slide, an autocorrection or text replacement, the strip's revert, a double-space period, a smart dash, a held space, Czech and Greek accents, a paste, a dictation, an undo) record the text they leave, so the next key reads the corrected text and not the old one; anything that deletes or replaces text still reads the host's own report. Delete and a strip pick decide the held space before forgetting the record, as that decision deletes nothing; a picked reading records the word it puts in, not the one the host still reports. The delete key's repeat moves to DeleteWordSwipe.swift, and the key labels and trackpad state to their extensions, to keep the engine under the file-size cap. Tests: a host echo after each swipe, a proxy that lags one insert, reports nil or empty, the lifetime bound, an edit or cursor move from elsewhere (casing too), a delete slide right after a swipe, an autocorrection on space in a stale host before a swipe, a field cleared after a swipe, a smart-quote swipe's echo, a delete after a pick and a picked swipe reading, each with a host still reporting the old text.
65 lines
3.2 KiB
Swift
65 lines
3.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), 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 }
|
|
guard let adaptive = adaptiveTouch?.offsets(for: layout),
|
|
let personal = TypingDecoder.Keys(layout: layout, offsets: adaptive) 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)
|
|
}
|
|
|
|
/// `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.
|
|
func displayLabel(for key: KeyboardKey) -> String {
|
|
guard let character = key.character else { return key.label }
|
|
return isUppercase && layerState.layer == .letters ? character.uppercased() : character
|
|
}
|
|
|
|
/// 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 }
|
|
return keycapsUppercase ? character.uppercased() : character
|
|
}
|
|
}
|