hex/ios/HEXShared/FullKeyboard/SwipeTyping.swift
Ryan Vogel b719d45e53
iOS: swipe reads the letters under the path, and the emoji bar scrubs (#2189)
* iOS: swipe guesses read the letters under the path, from where the finger turned and paused

X tester (Brandon McConnell): "The swipe keyboard guesses don't appear to account
for letters 'under' the swipe."

SwipeDecoder already matched the whole path SHARK2-style (location by point and
by dynamic time warping, normalised shape, start and end keys, language model),
but on the path resampled to 32 evenly spaced points with the touches' times
thrown away. A straight stroke runs over the keys between its ends, so words
that differ only by a key on that line drew the same ideal path: "tired"/"tied"
(i to e over r), "wire"/"write", "tire"/"tie", "greet"/"get", "pit"/"put", and
the language model alone chose between them.

The decoder now also reads Evidence from the raw path: points where it turns
sharply (angle over 0.2 pitch each side) and, from the touches' timestamps,
where the finger slowed (time per pitch against the swipe's median). The best
80 words are scored on an edit distance between their keys and that evidence,
in order: a turn or pause no key of the word explains costs by its strength, a
key the word turns at without a turn or pause near it costs, and a key it goes
straight through without a pause on it costs (only when the path has times).
SwipeTyping keeps each touch's UITouch.timestamp (KeyboardKeyPlaneView passes
it) and hands it to decode and the preview; paths without times, or whose
times are all equal (tests), read turns only. Parameters were tuned on the dev
sentences and on mined dev sets of common words that share a path, with
fingers that stop at every key, keep 60% or 90% of their speed through a
straight-on key, or hesitate between keys; lmWeight 1.1 -> 1.0 and
locationWeight 40 -> 30 were retuned with them.

Benchmark (experiment 2026-11-20-hex-swipe-typing, held-out test sentences,
390 pt, top-1 before -> after): English typical 94.9 -> 97.1, neat 97.5 ->
98.9, sloppy 76.8 -> 81.2, clean 98.1 -> 99.3, 60 Hz 94.9 -> 97.1, Duo split
94.2 -> 95.1; Spanish typical 90.9 -> 94.2, sloppy 77.3 -> 82.5, split 90.5 ->
91.8. Fingers that barely slow on straight-on keys (flow 0.9): 94.9 -> 95.9 and
90.9 -> 93.0; with a mid-stroke hesitation in 30% of words: 94.7 -> 96.0 and
91.0 -> 93.8; no times at all: 94.9 -> 94.9 and 90.9 -> 91.7. Held-out words
that share a path (mined, 5 swipes each): English 85.1 -> 96.0, Spanish 81.2
-> 94.5; the hand-picked set with Brandon's examples (tired/tried/tire,
great/grant/greet, though/through/tough, where/were/wire, ...) 75.1 -> 89.2.
Decode on an M5 Max: median 0.86 -> 0.91 ms, p99 2.72 -> 2.85 ms, max 3.96 ->
4.34 ms (English); Spanish unchanged.

Live on an iPhone 17 Pro simulator (iOS 27.2, Release, HID touch paths):
tired with a pause on r -> "Tired", the same path without -> "tied", "wire"
with a pause on r and "write" with pauses on r and t read as themselves.

Kept: #2173's withoutStrayCapital casing and #2159's swipe spacing (untouched;
the full suite passes).

Tests: SwipeDecoderTests testAPauseOnALetterTheStrokeCrossesReadsIt and
testAPauseOffTheWordsKeysCountsAgainstIt (SwipePath.timed), and
FullKeyboardEngineTests testASwipeReadsWhereTheFingerPaused (the plane hands
the times through). Full HEXMobileTests: 530 run, 0 failed, 1 skipped (iPhone
17 sim, iOS 27.2).

* iOS: slide along the emoji categories to scrub the grid, and the whole band under the grid takes them

X tester (Brandon McConnell): "The hitbox size for the emoji category buttons
in the emoji picker is really small, making it difficult to quickly navigate the
emoji picker", and "should be able to swipe through these emoji categories,
which is currently unsupported."

Black box, Apple's emoji keyboard (iPhone 17 simulator, iOS 27.2, Safari,
HID touch paths): the grid scrolls sideways, sections left to right, emoji
filling each column top to bottom. A tap on a category jumps the grid to its
start when the finger lifts (a held finger moves nothing). A finger sliding
along the bar scrubs the grid live and continuously: each icon's slot runs
through its own category (its left edge at the category's start, its right
edge at the next one's), the grid staying where the finger lifts. Scrolling
the grid by hand moves the bar's chip. While scrubbing Apple's bar shows a
capsule behind every icon and hides the chip; Hex keeps #2142's chip and moves
it with the finger. The simulator has no haptics, so whether Apple's bar ticks
is unknown; Hex gives the light selection tick it gives the skin tones, per new
category, when haptics are allowed.

EmojiCategoryBar now tracks the touch itself instead of one UIButton per
category: a tap selects on lift, a move past 8 pt starts a scrub that reports
the category under the finger and how far across its slot, and its buttons
only draw and speak (VoiceOver activates them through accessibilityActivate).
EmojiPanelView's band routing (#2142) now hands every touch under the grid
between ABC and delete to the bar, so each category takes the full height to
the panel's bottom edge and the full width of its slot (32.6 pt on a 402 pt
iPhone); it maps a scrub onto the grid (scrub(toSection:fraction:)), and while
a finger scrubs the grid follows the bar rather than the other way round.

Live (iPhone 17 Pro simulator, Release, Hex keyboard in a Safari field): taps
at y 797 (under the icons, at the panel's bottom edge) and y 760 (between the
grid and the icons) open Food and drink and Travel and places; sliding from
Frequently used to Symbols moves the grid through every category with the
chip under the finger. Memory (footprint, one run): tapping every category
peaked at 44.5 MB and scrolling past every emoji by hand at 44.4; one slide
across five categories peaked at 46.0, three slides back and forth across the
bar at 47.5 and ten at 48.3, back to 42-43 MB resident after. Scrubbing
redraws a screen of emoji per move, so its peak sits 1.5-4 MB over browsing;
throttling it to 30 or 15 Hz only took off about 1 MB, so it isn't.

Tests: EmojiSuggesterTests testTheBarsKeysTakeTheWholeBandUnderTheGrid now
checks the bar takes every point of every slot from the grid to the panel's
edge (it checked the buttons), and testSlidingAlongTheBarScrubsTheGrid covers
tap on lift, a small move staying a tap, the scrub through every category with
one tick each, the grid moving within a category, the end of the bar at the end
of the grid, and the chip following a hand scroll. Full HEXMobileTests: 530
run, 0 failed, 1 skipped.

* iOS: a swipe that comes back to its key types its letter, and "this is a test" swiped stays as typed

TestFlight (build 56): "Swipe keyboard doesn't handle next word capitalization
correctly. Also forgets to add a space sometimes. For example try swiping 'this
is a test'. Example: This Isa test."

Reproduced on this branch (rebased on origin/dev 83717ce6) with HID touch paths
in a Safari field (iPhone 17 Pro simulator, iOS 27.2, Release, Hex keyboard):
swipe "this", swipe "is", tap "a", swipe "test" types "This is a test", so
#2159 (the space a swiped word keeps pending before a tapped letter) fixed
"Isa" and #2173 (withoutStrayCapital) the stray "Is"; shift is on only at the
sentence start. A small slide on "a" (8 pt) is still a tap and types "a" too.

Still broken: "a" slid a little past the swipe threshold (a key's pitch) and
back typed "as", because the decoder reads no one-letter word (Apple's
keyboard reads that path as "As" too; it is what the tester's "a" can be).
SwipeTyping now reads a swipe that lifts on the key it went down on, never more
than 1.6 key pitches from it, as a tap that slid: the swipe ends with nothing
typed and the key's letter goes in through the engine's own tap path (the
pending space, shift and autocorrect as a tap's), unless the decoder read a
word that starts and ends on that key ("lol" on l, "pop" on p). A slide from a
that lifts on s still reads "as".

Live after the fix: tap, 8 pt slide, and 45 pt out-and-back slide on "a" all
type "This is a test".

Tests: FullKeyboardEngineTests testThisIsATestSwipedWithASingleLetterBetween
(the sequence with "a" tapped, slid a little, and slid out and back, in a field
that capitalises sentences; then ". " and "it works" swiped starts the next
sentence with a capital). Full HEXMobileTests: 530 run, 0 failed, 1 skipped.
2026-11-21 08:47:45 -05:00

411 lines
17 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import QuartzCore
import UIKit
/// Settings › Keyboard › Swipe typing: slide from a letter through the letters of a word and lift,
/// and the word goes in, as with Apple's QuickPath and Gboard's glide typing. English and
/// Spanish layouts only (the languages with a typing model); every other layout keeps a drag
/// as a slide to another key.
///
/// A touch on a letter key becomes a swipe once it travels more than a key's pitch from
/// where it went down: the key no longer types, a light trail follows the finger, and the
/// strip shows what the path reads as so far. Lifting the finger types the best reading
/// (`FullKeyboardEngine.typeSwipedWord`, which spaces and capitalises it) and offers the
/// next three in the strip. Taps, holds for accents, and slides of a key or less keep
/// working as before, and so do delete, the space bar's trackpad, 123 and the Hex key, which
/// no swipe starts on.
@MainActor
final class SwipeTyping {
private(set) var decoder: SwipeDecoder
private unowned let engine: FullKeyboardEngine
/// The light trail behind the finger; the key plane holds it above its keys.
let trail = SwipeTrailView()
/// A touch's points so far and when each came (`UITouch.timestamp`): the decoder reads
/// where the finger paused from the times.
struct Stroke {
var points: [CGPoint] = []
var times: [TimeInterval] = []
mutating func add(_ point: CGPoint, at time: TimeInterval) {
points.append(point)
times.append(time)
}
}
/// Letter touches that may still become swipes: their strokes so far.
private var pending: [Int: Stroke] = [:]
/// The swipe under way.
private var active: (id: Int, stroke: Stroke)?
/// Bumped by every swipe's start and end, so a reading of an older one is dropped.
private var generation = 0
private var previewInFlight = false
private var lastPreviewAt: TimeInterval = 0
/// Decodes while the finger moves, for the strip, off the main thread.
private static let previewQueue = DispatchQueue(label: "anomaly.hex.swipe-preview", qos: .userInitiated)
static let previewInterval: TimeInterval = 0.07
/// Your own words (`AutocorrectEngine.ownWords`) as the decoder has them.
private var ownWordsRevision: Int?
/// How long the last swipe's decode took, for Record typing logs and probes.
private(set) var lastDecodeTime: TimeInterval?
/// Nats more a rare reading the dictionary knows only with a capital costs, unless it is
/// one of your own words: the model's lowercase list holds names and abbreviations ("gs",
/// "ut", "kent") that a loose swipe of a common word could read as, and they would go in
/// capitalised mid-sentence. Common ones ("john", "york", "english") cost nothing more.
var nameCost = 4.0
/// The model's cost (-ln p) past which such a word counts as rare: about its 2,500th word.
static let commonNameCost = 10.0
var clock: () -> TimeInterval = { CACurrentMediaTime() }
init(engine: FullKeyboardEngine, model: TypingDecoderModel) {
self.engine = engine
decoder = SwipeDecoder(model: model)
syncWords()
}
var model: TypingDecoderModel { decoder.model }
/// How far a letter touch travels before it is a swipe: a key's width and gap.
var threshold: CGFloat { engine.layout.swipePitch }
var isSwiping: Bool { active != nil }
/// Words never offered (slurs, and swear words unless Suggest swear words is on), the
/// language's contractions, and your own words; rebuilt only when one of them changed.
func syncWords() {
let autocorrect = engine.autocorrect
let revision = autocorrect.ownWordsRevision
guard revision != ownWordsRevision else { return }
ownWordsRevision = revision
let model = decoder.model
decoder.neverSuggest = TypingDecoder.NeverSuggest(
autocorrect.offensiveWords.blocked(allowSwearing: autocorrect.allowsSwearing), model: model
)
var extra = SwipeDecoder.ExtraWords()
if model.language == KeyboardLanguage.english.rawValue {
extra.add(AutocorrectEngine.englishContractions, cost: { SwipeDecoder.costOfContraction($0) }, model: model, contractions: true)
}
extra.add(autocorrect.ownWords, cost: SwipeDecoder.ownWordCost, model: model)
decoder.extraWords = extra
}
// MARK: - Touches (from `KeyboardKeyPlaneView`)
/// Before a touch goes down: a swipe still under way ends first, so its word goes in
/// before the new key's letter.
func touchWillBegin() {
if let swipe = active { finish(swipe.id, stroke: swipe.stroke) }
}
/// After the engine took a touch: a letter key's may become a swipe. `time` is the
/// touch's timestamp (now when nil).
func began(id: Int, at point: CGPoint, time: TimeInterval? = nil) {
guard engine.allowsSwipes, let touch = engine.touches[id], !touch.committed,
touch.key.character?.first?.isLetter == true else { return }
var stroke = Stroke()
stroke.add(point, at: time ?? clock())
pending[id] = stroke
}
/// True when the touch is a swipe (now or already): the plane leaves it to this.
func moved(id: Int, to point: CGPoint, time: TimeInterval? = nil) -> Bool {
if var swipe = active, swipe.id == id {
swipe.stroke.add(point, at: time ?? clock())
active = swipe
trail.add(point)
schedulePreview()
return true
}
guard var stroke = pending[id] else { return false }
stroke.add(point, at: time ?? clock())
pending[id] = stroke
guard let first = stroke.points.first, hypot(point.x - first.x, point.y - first.y) > threshold,
let touch = engine.touches[id], !touch.committed, engine.activeAccentMenu == nil,
engine.allowsSwipes else { return false }
pending[id] = nil
syncWords()
engine.beginSwipe(touchID: id)
active = (id, stroke)
generation += 1
lastPreviewAt = 0
trail.begin(stroke.points)
schedulePreview()
return true
}
/// Whether the touch is the swipe under way.
func owns(_ id: Int) -> Bool { active?.id == id }
/// A touch that never became a swipe ended.
func forget(_ id: Int) {
pending[id] = nil
}
/// The swipe's finger lifted: its word goes in.
func ended(id: Int, at point: CGPoint, time: TimeInterval? = nil) {
guard var swipe = active, swipe.id == id else { return }
swipe.stroke.add(point, at: time ?? clock())
finish(id, stroke: swipe.stroke)
}
/// The system cancelled the swipe's touch: nothing goes in.
func cancelled(id: Int) {
pending[id] = nil
guard let swipe = active, swipe.id == id else { return }
active = nil
generation += 1
trail.fade()
engine.endSwipe(typing: [], points: swipe.stroke.points.count, decodeTime: nil)
}
func cancelAll() {
pending.removeAll()
guard active != nil else { return }
active = nil
generation += 1
trail.clear()
engine.endSwipe(typing: [], points: 0, decodeTime: nil)
}
private func finish(_ id: Int, stroke: Stroke) {
active = nil
pending[id] = nil
generation += 1
trail.fade()
let started = clock()
let layout = engine.layout
let candidates = decoder.decode(
stroke.points.map(layout.joinedForSwipe), times: stroke.times, keys: engine.swipeKeys,
previous: engine.swipePreviousWord, limit: 8
)
let words = ranked(candidates)
let elapsed = clock() - started
lastDecodeTime = elapsed
if let letter = slidTapLetter(stroke), let start = stroke.points.first,
words.first.map({ !Self.word($0, startsAndEndsWith: letter) }) ?? true {
// A tap that slid: its letter goes in as a tap's would, after the space a swiped
// word before it keeps pending and with the capital shift gives it.
engine.endSwipe(typing: [], points: stroke.points.count, decodeTime: elapsed)
engine.touchBegan(id: id, at: start)
engine.touchEnded(id: id, at: start)
return
}
engine.endSwipe(typing: words, points: stroke.points.count, decodeTime: elapsed)
}
/// How far (key pitches) a swipe may go from where it went down and still be a tap
/// that slid, when it lifts on the key it started on.
static let slidTapReach: CGFloat = 1.6
/// The letter of a swipe that came back to the key it went down on without going more
/// than `slidTapReach` pitches from it ("a" dragged a little too far and back): the
/// decoder reads no word of one letter, so it read "as". Nil for any other swipe.
private func slidTapLetter(_ stroke: Stroke) -> String? {
guard let first = stroke.points.first, let last = stroke.points.last else { return nil }
let reach = threshold * Self.slidTapReach
guard stroke.points.allSatisfy({ hypot($0.x - first.x, $0.y - first.y) <= reach }) else { return nil }
let layout = engine.layout
guard let down = layout.hitTest(first), down == layout.hitTest(last),
let letter = down.character, letter.count == 1 else { return nil }
return letter.lowercased()
}
/// Whether `word` begins and ends on `letter`'s key ("lol" on l, "pop" on p): a word a
/// swipe that comes back to its key can mean.
static func word(_ word: String, startsAndEndsWith letter: String) -> Bool {
func base(_ character: Character?) -> String? {
character.map { String($0).lowercased().decomposedStringWithCanonicalMapping.unicodeScalars.first.map(String.init) ?? "" }
}
return word.count > 1 && base(word.first) == letter && base(word.last) == letter
}
/// The readings best first once a rare name or abbreviation costs `nameCost` more.
func ranked(_ candidates: [SwipeDecoder.Candidate]) -> [String] {
let model = decoder.model
func rareName(_ text: String) -> Bool {
guard engine.swipeReadsAsName(text) else { return false }
return model.index(of: text).map { model.unigramCost($0) > Self.commonNameCost } ?? true
}
return candidates
.map { ($0.text, $0.cost + (rareName($0.text) ? nameCost : 0)) }
.enumerated()
.sorted { $0.element.1 != $1.element.1 ? $0.element.1 < $1.element.1 : $0.offset < $1.offset }
.map(\.element.0)
}
// MARK: - The word so far
/// Reads the path so far off the main thread, at most every `previewInterval`, and shows
/// its best word in the strip while the swipe is still the same one.
private func schedulePreview() {
guard let swipe = active, !previewInFlight, swipe.stroke.points.count >= 3 else { return }
let now = clock()
guard now - lastPreviewAt >= Self.previewInterval else { return }
previewInFlight = true
lastPreviewAt = now
let decoder = decoder, keys = engine.swipeKeys, previous = engine.swipePreviousWord
let points = swipe.stroke.points.map(engine.layout.joinedForSwipe), times = swipe.stroke.times, generation = generation
Self.previewQueue.async { [weak self] in
let candidates = decoder.decode(points, times: times, keys: keys, previous: previous, limit: 3)
DispatchQueue.main.async {
MainActor.assumeIsolated {
guard let self else { return }
self.previewInFlight = false
guard self.generation == generation, self.active != nil else { return }
self.engine.showSwipePreview(self.ranked(candidates))
}
}
}
}
// MARK: - Spacing
/// Whether a swiped word needs a space before it, as Apple's keyboard adds one: unless
/// the text before the cursor is empty or ends in a space, a line break or opening
/// punctuation (a bracket, an opening quote, ¿ or ¡, or a mark that starts a word such
/// as @ # /). A straight quote opens when nothing or a space comes before it.
static func needsSpace(before: String?) -> Bool {
guard let before, let last = before.last else { return false }
if last.isWhitespace || last.isNewline || opening.contains(last) { return false }
if last == "\"" || last == "'" {
let previous = before.dropLast().last
return !(previous == nil || previous!.isWhitespace || previous!.isNewline || opening.contains(previous!))
}
return true
}
private static let opening: Set<Character> = ["(", "[", "{", "<", "«", "‹", "“", "‘", "„", "¿", "¡", "@", "#", "/"]
}
extension KeyboardLanguage {
/// Swipe typing reads only the layouts with a typing model; elsewhere a drag stays a
/// slide to another key.
var supportsSwipeTyping: Bool { self == .english || self == .spanish }
}
/// The light trail behind a swiping finger: the last `tail` seconds of its path, which
/// shortens behind the finger and fades when it lifts. A shape layer, so the keyboard draws
/// no bitmap of its own for it.
@MainActor
final class SwipeTrailView: UIView {
static let tail: TimeInterval = 0.45
static let width: CGFloat = 6
var palette = FullKeyboardPalette(isDark: false) {
didSet { if palette != oldValue { shape.strokeColor = Self.color(palette).cgColor } }
}
var clock: () -> TimeInterval = { CACurrentMediaTime() }
private let shape = CAShapeLayer()
private var points: [(point: CGPoint, at: TimeInterval)] = []
private var link: CADisplayLink?
override init(frame: CGRect) {
super.init(frame: frame)
isUserInteractionEnabled = false
isOpaque = false
backgroundColor = .clear
isAccessibilityElement = false
shape.fillColor = nil
shape.lineCap = .round
shape.lineJoin = .round
shape.lineWidth = Self.width
shape.strokeColor = Self.color(palette).cgColor
layer.addSublayer(shape)
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
static func color(_ palette: FullKeyboardPalette) -> UIColor {
palette.isDark ? UIColor(white: 1, alpha: 0.5) : palette.actionFill.withAlphaComponent(0.42)
}
override func layoutSubviews() {
super.layoutSubviews()
shape.frame = bounds
}
/// Off screen, the display link (which holds this view) stops.
override func didMoveToWindow() {
super.didMoveToWindow()
if window == nil { clear() }
}
var isShowing: Bool { shape.path != nil }
func begin(_ start: [CGPoint]) {
shape.removeAllAnimations()
shape.opacity = 1
let now = clock()
points = start.map { ($0, now) }
if link == nil, window != nil {
let link = CADisplayLink(target: self, selector: #selector(tick))
link.add(to: .main, forMode: .common)
self.link = link
}
redraw()
}
func add(_ point: CGPoint) {
points.append((point, clock()))
redraw()
}
/// The finger lifted: the trail fades out.
func fade() {
stopTicking()
guard shape.path != nil else { return }
CATransaction.begin()
CATransaction.setCompletionBlock { [weak self] in
MainActor.assumeIsolated {
guard let self, self.link == nil else { return }
self.clear()
}
}
let fade = CABasicAnimation(keyPath: "opacity")
fade.fromValue = 1
fade.toValue = 0
fade.duration = 0.2
shape.opacity = 0
shape.add(fade, forKey: "fade")
CATransaction.commit()
}
func clear() {
stopTicking()
points.removeAll()
CATransaction.begin()
CATransaction.setDisableActions(true)
shape.removeAllAnimations()
shape.path = nil
CATransaction.commit()
}
private func stopTicking() {
link?.invalidate()
link = nil
}
@objc private func tick() {
redraw()
}
private func redraw() {
// Points older than the tail drop off behind the finger, down to its last stretch.
if let old = points.lastIndex(where: { $0.at < clock() - Self.tail }) {
let drop = min(old, points.count - 2)
if drop > 0 { points.removeFirst(drop) }
}
let path = CGMutablePath()
if let first = points.first {
path.move(to: first.point)
for p in points.dropFirst() { path.addLine(to: p.point) }
}
CATransaction.begin()
CATransaction.setDisableActions(true)
shape.path = points.count >= 2 ? path : nil
CATransaction.commit()
}
}