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.
68 lines
3 KiB
Swift
68 lines
3 KiB
Swift
import Foundation
|
||
|
||
/// The Czech layout's ´ and ˇ keys, as the system Czech keyboard was observed to apply them
|
||
/// (iOS 27.2 simulator, real touches in Messages, every letter followed by each key, black box).
|
||
/// Neither is a dead key: each accents the letter right before it, as Greek's tonos key does,
|
||
/// and after anything else (another letter, an accented one, a space, nothing) types itself.
|
||
/// ´ gives á ć é í ĺ ń ó ŕ ś ú ý ź; ˇ gives č ď ě ľ ň ř š ť ž, and ů after u; the ring ˚, held on
|
||
/// ˇ, gives ů and å. Capitals take theirs (Á, Č, Ů). Delete takes the accented letter whole.
|
||
nonisolated enum CzechTyping {
|
||
static let acuteKey = "\u{00B4}"
|
||
static let caronKey = "\u{02C7}"
|
||
static let ringKey = "\u{02DA}"
|
||
|
||
private static let marks: [String: [Character: Character]] = [
|
||
acuteKey: [
|
||
"a": "á", "c": "ć", "e": "é", "i": "í", "l": "ĺ", "n": "ń", "o": "ó", "r": "ŕ", "s": "ś",
|
||
"u": "ú", "y": "ý", "z": "ź",
|
||
],
|
||
caronKey: [
|
||
"c": "č", "d": "ď", "e": "ě", "l": "ľ", "n": "ň", "r": "ř", "s": "š", "t": "ť", "z": "ž",
|
||
"u": "ů",
|
||
],
|
||
ringKey: ["a": "å", "u": "ů"],
|
||
]
|
||
|
||
/// The letter `before` ends with, accented by the mark `text` types after it; nil when
|
||
/// the key types the mark itself.
|
||
static func accenting(_ text: String, after before: String?) -> String? {
|
||
guard let table = marks[text], let last = before?.last else { return nil }
|
||
let lower = Character(last.lowercased())
|
||
guard let accented = table[lower] else { return nil }
|
||
return last.isUppercase ? accented.uppercased() : String(accented)
|
||
}
|
||
}
|
||
|
||
@MainActor
|
||
extension FullKeyboardEngine {
|
||
/// The layout's own keys' typing rules: Greek's tonos key and final sigma (`GreekTyping`),
|
||
/// Czech's ´ and ˇ (`CzechTyping`). True when the key already did all it does; else
|
||
/// `text` is what to type.
|
||
func typeLayoutRules(_ text: inout String) -> Bool {
|
||
switch layout.language {
|
||
case .greek: typeGreek(&text)
|
||
case .czech: typeCzech(text)
|
||
default: false
|
||
}
|
||
}
|
||
|
||
/// Czech: a held ˇ's ˇ or ˚ accents the letter before it, as a tapped ˇ does. True when it
|
||
/// did; else the held key's choice goes in as it is.
|
||
func typeHeldMark(_ chosen: String) -> Bool {
|
||
guard layout.language == .czech else { return false }
|
||
var mark = chosen
|
||
return typeLayoutRules(&mark)
|
||
}
|
||
|
||
/// ´, ˇ or ˚ after a letter it accents: puts the accented letter in its place. False
|
||
/// otherwise, and the mark is typed as itself.
|
||
private func typeCzech(_ text: String) -> Bool {
|
||
guard let accented = CzechTyping.accenting(text, after: target.documentContextBeforeInput) else { return false }
|
||
rememberTypingContext(before: textBefore.map { String($0.dropLast(1)) }, inserted: accented)
|
||
target.deleteBackward()
|
||
target.insertText(accented)
|
||
didEdit()
|
||
logState("typed")
|
||
return true
|
||
}
|
||
}
|