hex/ios/HEXShared/FullKeyboard/FullKeyboardTouch.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

61 lines
3.1 KiB
Swift

import QuartzCore
import UIKit
/// The full keyboard's touches, as data: one finger's key, where it started and where it is,
/// and the state that makes a press an accent picker, a slide, a trackpad or a language
/// swipe. Kept beside the engine, like `SwipedWord`, so the engine's own file stays readable.
@MainActor
extension FullKeyboardEngine {
/// One finger on the keys.
struct ActiveTouch: Equatable {
var key: KeyboardKey
var startPoint: CGPoint
var point: CGPoint
/// A character (or space) that already went in because another key went down after it.
var committed: Bool
/// The page a 123/ABC press came from; releasing over a character returns there.
var returnLayer: KeyboardLayer?
/// A 123, #+= or shift press slid onto this character key: it shows the key's pop-up,
/// and letting go types it (`slideTarget`), as on Apple's keyboard.
var slideKey: KeyboardKey?
/// True once such a press slid onto a character key, so letting go back on the page
/// key is a slide taken back rather than a tap.
var hasSlid = false
/// True once a spacebar press is held `trackpadHoldDelay` (`holdSpace`).
var isTrackpad = false
/// True once a spacebar press slides before its hold (`holdSpace`) while it switches
/// language: letting go far enough along switches instead of typing a space.
var isLanguageSwipe = false
/// The trackpad finger's last few moves, for its speed (`TrackpadCursor.speed(along:)`).
var trackpadPath: [TrackpadCursor.Sample] = []
/// The text the trackpad moves the cursor through (`TrackpadCursor.State`).
var trackpad: TrackpadCursor.State?
/// UTF-16 units the trackpad has moved the cursor, for Record typing logs.
var trackpadOffset = 0
/// True when the likelier next letter claimed this touch from the key under it; a
/// small move then keeps it rather than sliding back (`letterReach`).
var claimed = false
/// Where the finger went down, before a strip touch moved onto row 1 (Adaptive touch).
var downPoint: CGPoint = .zero
}
/// A press that types the character key it slides onto: 123, ABC and #+= (with a page to
/// go back to), and shift.
static func slidesToType(_ touch: ActiveTouch) -> Bool {
touch.returnLayer != nil || touch.key.kind == .shift
}
/// A language swipe's direction, or nil when it was too short or more up than along.
static func languageSwipeStep(from start: CGPoint, to end: CGPoint) -> Int? {
let dx = end.x - start.x
guard abs(dx) >= languageSwipeDistance, abs(dx) >= abs(end.y - start.y) else { return nil }
return dx < 0 ? 1 : -1
}
/// How far `point` is from `rect`: zero inside it.
static func distance(from point: CGPoint, to rect: CGRect) -> CGFloat {
let dx = max(rect.minX - point.x, 0, point.x - rect.maxX)
let dy = max(rect.minY - point.y, 0, point.y - rect.maxY)
return (dx * dx + dy * dy).squareRoot()
}
}