hex/ios/HEXShared/FullKeyboard/FullKeyboardEngine+HostText.swift
Dax ee8e480bdd
iOS keyboard: a late host echo keeps the swipe's pending space (#2273)
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.
2026-11-21 19:14:42 -05:00

80 lines
4.2 KiB
Swift

import Foundation
/// What a host reports around Hex's own insert. A proxy can lag `insertText` — a web view's
/// keeps returning the text from before it, or none at all — and then deliver `textDidChange`
/// after the keys have moved on. That echo is not a change to the text, and until the host
/// catches up its report is not the text either: word boundaries and casing read
/// `typingContext` (what Hex typed), while anything that deletes or replaces text keeps
/// reading `textBefore` (what the host reports).
@MainActor
extension FullKeyboardEngine {
/// How long Hex takes a host that still reports the text from before its insert at its
/// word: long enough for an echo on the next runloop, short enough that a change made
/// elsewhere is never read as one. `clock` keeps it deterministic in tests.
static let typingContextLifetime: TimeInterval = 0.5
/// The text before the cursor Hex's own insert left, while the host may not report it yet.
struct ExpectedTypingContext {
/// What the host reported before the insert went in.
var reported: String?
/// What Hex expects the host to report after it.
var expected: String
/// The text after the cursor then: an insert at the cursor leaves it, a move changes it.
var after: String?
/// When Hex stops taking a host that still reports `reported` for merely behind.
var until: TimeInterval
}
/// Whether the host is behind Hex's insert: same text after the cursor, the field still
/// has text, nothing selected, and a report that is what the host had before the insert or
/// a step of the way to what Hex typed (a proxy catches up one insert at a time). Anything
/// else is a change made elsewhere: a cleared field, another field, a cursor move.
private var hostIsBehind: Bool {
guard let expected = expectedTypingContext, clock() <= expected.until,
target.hasText, (target.selectedText ?? "").isEmpty,
target.documentContextAfterInput == expected.after else { return false }
guard let reported = textBefore else { return true }
if reported == expected.expected { return false }
if reported == expected.reported { return true }
return reported.count >= (expected.reported?.count ?? 0) && expected.expected.hasPrefix(reported)
}
/// The text before the cursor as word boundaries and casing read it: the host's, unless it
/// still reports what it had before Hex's last insert, then what Hex typed.
var typingContext: String? {
guard hostIsBehind, let expected = expectedTypingContext else { return textBefore }
return expected.expected
}
/// Records what Hex's own insert of `inserted` after `before` will leave, read before it
/// goes in: the text a host that has caught up reports next, for `typingContext` while
/// it hasn't. Both are as `textBefore` reads them: quotes straight.
func rememberTypingContext(before: String?, inserted: String) {
expectedTypingContext = ExpectedTypingContext(
reported: textBefore,
expected: (before ?? "") + SmartPunctuation.plain(inserted),
after: target.documentContextAfterInput,
until: clock() + Self.typingContextLifetime
)
}
/// A delete, a cursor move, a pick or a field change: what Hex typed is no longer what the
/// host should report.
func forgetTypingContext() {
expectedTypingContext = nil
}
/// Whether the host's update is Hex's own edit (an echo, or a host still behind it) rather
/// than a change made elsewhere: a swipe's pending space then still belongs to the text.
func hostEchoed(_ swipe: SwipedWord) -> Bool {
if let selected = target.selectedText, !selected.isEmpty { return false }
return swipe.before == typingContext && swipe.after == target.documentContextAfterInput
}
/// The host caught up, or changed the text: either way Hex's record has done its job. A
/// host still reporting the text from before the insert keeps it for the next key.
func settleTypingContext() {
guard expectedTypingContext != nil, !hostIsBehind else { return }
expectedTypingContext = nil
}
}