hex/ios/HEXShared/FullKeyboard/HexKeyGesture.swift
Ryan Vogel 55e46f515f iOS: push to talk on the Hex key and the dictation bar's Dictate
A tester asked to hold to talk instead of tapping Dictate and then Done.

Rule: holding still for 0.35 s (HexKeyGesture.holdDuration, the Hex key's
existing hold) starts dictation at once, and lifting the finger finishes it
as Done does. A quick tap keeps tap to start, Done to finish. On the Hex key,
sliding more than 10 pt before the hold fires still opens the style menu;
once dictation has started, moving changes nothing. Push to talk applies
only when dictation runs in the keyboard (warm session or in-keyboard
dictation); a hold that opens Hex stays a tap.

- HexKeyGesture: a fired hold's touch-up is now .release (was ignored).
  Changed: before, a hold started dictation and the release did nothing.
- HexKeyControl calls onHoldRelease; FullKeyboardView finishes the dictation
  the hold started (model.onStopTap, logged as dictation_stop from
  hex_key_release). The recording fade keeps a still-tracking Hex key in
  the window at alpha 0 instead of hiding it, so its touch keeps arriving.
- KeyboardSurfaceView: the well tracks the press with a simultaneous
  DragGesture on the same state machine (the Dictate button stops taking
  touches once recording shows), starts with the light haptic
  (onKeyFeedback: Haptics setting and Full Access), and finishes on release;
  the button's own touch-up action skips a press the hold took.
- Accessibility hints mention holding; README and docs/typing-logs.md
  describe it.

Verified: xcodebuild test on the iPhone 18 Pro simulator, 213 tests pass (1
skipped as before), including the updated HexKeyGesture tests (hold then
release finishes; moving after the hold is ignored). The dictation bar
renders as before (--snap-state keyboard-warm). Real touches (hold timing,
the bar's buttons with the well's gesture, the Hex key's touch surviving the
recording panel) were not driven on the simulator and need a physical
iPhone.
2026-11-20 08:18:20 -05:00

73 lines
2 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.
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
}
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
}
mutating func cancel() {
state = .idle
}
}