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.
126 lines
6.5 KiB
Swift
126 lines
6.5 KiB
Swift
import UIKit
|
|
|
|
/// The space bar's trackpad, as Apple's moves it (`TrackpadCursor`): held, it moves the cursor
|
|
/// through the text in two dimensions, faster the faster the finger.
|
|
@MainActor
|
|
extension FullKeyboardEngine {
|
|
/// Whether the space bar's trackpad is moving the cursor now.
|
|
var isTrackpadActive: Bool {
|
|
touches.values.contains(where: \.isTrackpad)
|
|
}
|
|
|
|
/// The space bar held `trackpadHoldDelay` without sliding: the trackpad starts where the
|
|
/// finger is, as Apple's does, so letting go types no space (a press held to move the
|
|
/// cursor used to type one beside the space already there).
|
|
func holdSpace(forTouch id: Int) {
|
|
guard var touch = touches[id], touch.key.kind == .space, !touch.committed, !touch.isTrackpad, !touch.isLanguageSwipe else { return }
|
|
startTrackpad(&touch, id: id, hold: true, time: nil)
|
|
}
|
|
|
|
/// A space bar touch moved to `point`: true once it's the keys' to keep (a language swipe,
|
|
/// or the trackpad moving the cursor), false while it may still be a tap. Only the hold
|
|
/// starts the trackpad, as on Apple's: a quick tap that drifted 10 pt or more used to start
|
|
/// it with one language and type no space (TestFlight build 56, iPhone Duo), where Apple's
|
|
/// types one (iOS 27.2: taps of 60 to 150 ms sliding 8 to 24 pt, slides of 40 pt in 150 ms).
|
|
func moveSpaceBar(_ touch: inout ActiveTouch, id: Int, to point: CGPoint, time: TimeInterval?) -> Bool {
|
|
if touch.isLanguageSwipe { return true }
|
|
if !touch.isTrackpad, !touch.committed, switchesLanguage, abs(point.x - touch.startPoint.x) >= Self.trackpadActivationDistance {
|
|
touch.isLanguageSwipe = true
|
|
return true
|
|
}
|
|
guard touch.isTrackpad else { return false }
|
|
let offset = trackpadStep(&touch, to: point, time: time ?? clock())
|
|
moveCursor(&touch, id: id, by: offset)
|
|
return true
|
|
}
|
|
|
|
private func startTrackpad(_ touch: inout ActiveTouch, id: Int, hold: Bool, time: TimeInterval?) {
|
|
touch.isTrackpad = true
|
|
touch.trackpadPath = [TrackpadCursor.Sample(time: time ?? clock(), point: touch.point)]
|
|
touch.trackpad = TrackpadCursor.State(
|
|
before: target.documentContextBeforeInput ?? "",
|
|
after: target.documentContextAfterInput ?? "",
|
|
wrapWidth: TrackpadCursor.wrapWidth(forWidth: width),
|
|
lineHeight: Self.trackpadFont().lineHeight,
|
|
width: Self.trackpadWidth
|
|
)
|
|
trackpadUpdated(touch, id: id, started: true)
|
|
var fields = [TypingLogField("id", .int(id))]
|
|
if hold { fields.append(TypingLogField("hold", .bool(true))) }
|
|
typingLog?.ui("trackpad_start", fields)
|
|
onChange?()
|
|
}
|
|
|
|
/// The finger moved to `point` at `time`: the UTF-16 offset to move the cursor by. The
|
|
/// finger's speed sets how far the point goes (`TrackpadCursor.gain(speed:)`); with no time
|
|
/// between moves (a test's clock) it goes as far as the finger.
|
|
private func trackpadStep(_ touch: inout ActiveTouch, to point: CGPoint, time: TimeInterval) -> Int {
|
|
let last = touch.trackpadPath.last ?? TrackpadCursor.Sample(time: time, point: point)
|
|
let sample = TrackpadCursor.Sample(time: time, point: point)
|
|
touch.trackpadPath = time > last.time ? touch.trackpadPath.suffix(7) + [sample] : [sample]
|
|
guard var state = touch.trackpad else { return 0 }
|
|
let gain = TrackpadCursor.gain(speed: TrackpadCursor.speed(along: touch.trackpadPath))
|
|
let dx = point.x - last.point.x, dy = point.y - last.point.y
|
|
let vertical = touch.trackpadPath.count > 1 ? TrackpadCursor.isVertical(touch.trackpadPath) : abs(dy) > abs(dx)
|
|
var offset = state.move(by: CGVector(dx: dx * gain, dy: dy * gain), at: time, vertical: vertical)
|
|
if state.nearsEdge, catchUp(&state) { offset += state.settle(at: time) }
|
|
touch.trackpad = state
|
|
return offset
|
|
}
|
|
|
|
/// Adds what the field shows beyond the trackpad's copy of the text, if it does.
|
|
private func catchUp(_ state: inout TrackpadCursor.State) -> Bool {
|
|
state.catchUp(before: target.documentContextBeforeInput ?? "", after: target.documentContextAfterInput ?? "", width: Self.trackpadWidth)
|
|
}
|
|
|
|
/// The field changed: while the trackpad is on that's mostly the host following its moves.
|
|
/// It may show more of the text, or that the cursor is at the text's start or a line's end,
|
|
/// so the cursor goes where the point is once more.
|
|
func trackpadFieldChanged() {
|
|
guard let (id, found) = touches.first(where: { $0.value.isTrackpad }), var state = found.trackpad else { return }
|
|
var touch = found
|
|
if state.nearsEdge { _ = catchUp(&state) }
|
|
let offset = state.settle(at: clock())
|
|
touch.trackpad = state
|
|
moveCursor(&touch, id: id, by: offset)
|
|
}
|
|
|
|
/// The trackpad finger lifts: the cursor goes where the point is, among the rows known.
|
|
func liftTrackpad(_ touch: inout ActiveTouch) {
|
|
guard var state = touch.trackpad else { return }
|
|
let offset = state.settle(at: clock(), lifting: true)
|
|
touch.trackpad = state
|
|
log(&touch, offset)
|
|
if offset != 0 { trackpadMovedCursor(by: offset) }
|
|
}
|
|
|
|
private func moveCursor(_ touch: inout ActiveTouch, id: Int, by offset: Int) {
|
|
log(&touch, offset)
|
|
trackpadUpdated(touch, id: id)
|
|
if offset != 0 { trackpadMovedCursor(by: offset) }
|
|
}
|
|
|
|
private func log(_ touch: inout ActiveTouch, _ offset: Int) {
|
|
guard offset != 0 else { return }
|
|
touch.trackpadOffset += offset
|
|
typingLog?.edit("cursor", [TypingLogField("offset", .int(offset))])
|
|
}
|
|
|
|
private static var trackpadWidths: (font: UIFont, widths: [Character: CGFloat]) = (.systemFont(ofSize: 17), [:])
|
|
|
|
/// Body text, as most fields show it (17 pt, larger with Dynamic Type): the font the
|
|
/// trackpad measures characters in (`trackpadWidth`).
|
|
static func trackpadFont() -> UIFont {
|
|
let font = UIFont.preferredFont(forTextStyle: .body)
|
|
if font != trackpadWidths.font { trackpadWidths = (font, [:]) }
|
|
return font
|
|
}
|
|
|
|
/// A character's width in `trackpadFont()`, measured once.
|
|
static func trackpadWidth(_ character: Character) -> CGFloat {
|
|
if let width = trackpadWidths.widths[character] { return width }
|
|
let width = character.isNewline ? 0 : (String(character) as NSString).size(withAttributes: [.font: trackpadWidths.font]).width
|
|
trackpadWidths.widths[character] = width
|
|
return width
|
|
}
|
|
}
|