hex/ios/HEXShared/FullKeyboard/HexKeyGesture.swift
Ryan Vogel 4db6d91c7c iOS keyboard: start in-keyboard dictation on the Hex key's touch-down
A tap only started capture when the finger lifted (~100-150 ms) and push to
talk only after the 0.35 s hold timer, so the first words were lost even
with the capture page ready.

What changed
- The full keyboard's Hex key reports touch-down (onPress) and a press that
  becomes a slide to the style menu or is cancelled by the system before it
  is a tap or a hold (onPressAbandoned; HexKeyGesture.cancel() now returns
  .abandon for a pending press, .ignored once the hold fired).
- KeyboardViewController arms in-keyboard dictation at touch-down, with the
  same checks the Hex key uses to pick it (Full Access, no warm session,
  in-keyboard dictation available) and only while idle or failed.
- KeyboardLocalDictation.arm opens the microphone at once in a new .armed
  phase that the UI doesn't see (no snapshot). start() keeps it as the
  dictation (a tap's lift or the hold), so tap and push to talk behave as
  before, only listening from touch-down. discard() stops it, deletes its
  audio and transcribes nothing: on the menu, a cancelled touch, the
  keyboard hiding, a warm session taking over, or 3 s without a tap or hold.
  A failure while armed is shown only if the touch is kept.
- The capture page's hexStart now notices a hexStop that came while it
  awaited getUserMedia or the audio context (a generation count) and
  releases what it opened instead of going on; before, a stop during
  getUserMedia left the microphone open until Swift stopped it again on
  "started". A page that is idle after cancel() or discard() keeps
  listening for its process ending, and closes a stray "started".
- README: listening from touch-down, the dot during a slide, and what only
  an iPhone can measure.

Measured (anomastudio, iPhone 13 simulator, iOS 27.2, Release, Hex keyboard
in Messages, 100 ms AXe taps and 1.5 s holds; touch-down to first audio,
median and range; "before" is a8f9b707 with only the timing log):
                       before                prepare + touch-down
  tap, first          862 (849-961)          227 (146-401)
  tap, second         154 (150-157)           15 (10-18)
  hold, first        1160 (1143-1161)        322 (157-354)
  hold, second        374 (373-377)           17 (12-20)
What is left on a first dictation is WebKit's: the page's process is
suspended after its load (WebKit drops its assertion) and resuming it to
run hexStart took 0-240 ms in the simulator; a tap's recording strip also
builds its Metal scope on the main thread while WebKit waits for it.
Memory (phys_footprint, simulator, no GPU memory): idle 27.4-27.6 MB before,
31.7-32.2 MB after; lifetime peak by the first dictation 40.2-40.8 MB
before, 39.0-42.0 MB after (the page load's own peak is now at keyboard
open).

Verified live in Messages: WebKit's CoreAudioCaptureSource::start and
captureStateChanged 0 -> 2048 (what drives the orange dot) appear only
after touch-down, never when the page loads; a slide right after
touch-down logs "discarded after 118-149 ms", the capture source ends 144 ms
after it started, and nothing is transcribed; a tap is kept at ~140 ms and
Done/cancel work; a hold is kept at ~363 ms and the release stops it
(frames written, transcription attempted). The simulator shows no orange
dot even while recording, and a Home press reaches SpringBoard after the
hold fires, so the system-cancelled touch is covered by the new
HexKeyControl test rather than live.
2026-11-20 15:04:03 -05:00

81 lines
2.5 KiB
Swift

import CoreGraphics
import Foundation
/// The full keyboard's Hex key as a pure state machine, also the dictation bar's push to
/// talk. A tap dictates (Done finishes it); a still hold of `holdDuration` dictates while
/// held, and releasing finishes it (push to talk). Moving more than `slop` before either
/// opens the style menu, and the release picks the row under the finger. Once the hold
/// fires, moves are ignored. In-keyboard dictation starts listening at touch-down; a tap or
/// hold keeps that capture, and the menu or a cancelled press (`.abandon`) drops it.
nonisolated struct HexKeyGesture: Equatable, Sendable {
static let holdDuration: TimeInterval = 0.35
static let slop: CGFloat = 10
enum State: Equatable, Sendable {
case idle
case pressing
case menu
case fired
}
enum Output: Equatable, Sendable {
case ignored
case tap
case hold
case openMenu
case dragMenu
case releaseMenu
/// The finger lifted after a hold: finish the dictation it started.
case release
/// The system cancelled a press before it was a tap or a hold: nothing dictates.
case abandon
}
private(set) var state: State = .idle
private var origin: CGPoint = .zero
mutating func touchDown(at point: CGPoint) {
state = .pressing
origin = point
}
mutating func touchMoved(to point: CGPoint) -> Output {
switch state {
case .pressing:
guard hypot(point.x - origin.x, point.y - origin.y) > Self.slop else { return .ignored }
state = .menu
return .openMenu
case .menu:
return .dragMenu
case .idle, .fired:
return .ignored
}
}
/// The hold timer started at touch-down ran out.
mutating func holdElapsed() -> Output {
guard state == .pressing else { return .ignored }
state = .fired
return .hold
}
mutating func touchUp() -> Output {
let output: Output = switch state {
case .menu: .releaseMenu
case .pressing: .tap
case .fired: .release
case .idle: .ignored
}
state = .idle
return output
}
/// A hold that already fired keeps dictating (Done finishes it); a press still pending is
/// `.abandon`.
@discardableResult
mutating func cancel() -> Output {
let output: Output = state == .pressing ? .abandon : .ignored
state = .idle
return output
}
}