hex/ios/HEXShared/FullKeyboard/FullKeyboardEngine+Trackpad.swift
Ryan Vogel 50e3464b25
iOS: a quick space tap that drifts still types a space, and space keeps its place in a rollover (#2233)
* iOS: a quick space tap that drifts still types a space, as on Apple's

TestFlight build 56 (iPhone Duo, iOS 27.3): "seems like I'm missing the
space key or it's not registering a tap". With one keyboard language a
space-bar touch that slid 10 pt (trackpadActivationDistance) started the
cursor trackpad at once, so letting go typed no space. Real HID taps on
the iPhone 17 Pro simulator (iOS 27.2) in Messages, "hi" + space + "o":

- build 56 (2ef08aa4) and origin/dev (6f0b20c1): taps of 60, 100 and
  150 ms drifting 12, 16, 24 or -16 pt typed no space (15 of 15 lost;
  8 pt drifts typed one). On dev the -16 pt ones also moved the cursor.
- Apple's keyboard, same field and gestures: every one typed a space,
  and so did slides of 40 pt in 150 ms. Its trackpad starts only from a
  still hold.

Removed: the slide that started the trackpad with one language. Only the
hold (trackpadHoldDelay, unchanged) starts it now; with two or more
languages a slide is still a language swipe. The dictation bar's space
bar (KeyboardSurface) keeps its own 10 pt slide; not changed here.

Verified: the same HID probes on this build type a space in all 15
drift cases and the 40 to 80 pt slides; holding still 0.6 s still starts
the trackpad (no space) and a hold then a 20 pt slide moves the cursor.
xcodebuild test (HEXMobile, iPhone 17 Pro, iOS 27.2): 657 tests, the one
that asserted the old quick-slide trackpad updated and rerun green.
Updated FullKeyboardEngineTests cases, ios/README.md and typing-logs.md.

* iOS: space keeps its place in a rollover

Found while triaging the build 56 space-key report: a key pressed while
the space bar was still held, and let go first, typed before the space.
Rollover committed only held characters and accent pickers, so space
waited for its own lift and "a", space, "b" typed fast came out "ab ",
which reads as a lost space between words. A held space is now typed when
the next key goes down, as a held letter is, and its own lift then types
nothing; a space already typed this way can't become the trackpad or a
language swipe.

Not verified against Apple's keyboard: simulator HID touches are one
finger, so overlapping touches can't be played to it. The order follows
Hex's own letter rollover rule.

Verified: FullKeyboardEngineTests.testRolloverWithTheSpaceBarKeepsOrder
failed on origin/dev ("ab ") and passes here; xcodebuild test (HEXMobile,
iPhone 17 Pro, iOS 27.2), 657 tests, all pass with this and the previous
commit together.
2026-11-21 14:36:17 -05:00

121 lines
6.3 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 {
/// 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
}
}