hex/ios/HEXShared/FullKeyboard/HexKeyGesture.swift
Ryan Vogel 66a038201b Dictate with a tap on the Hex key and the bar's Hex mark
The full keyboard's Hex key ignored taps and needed a 0.35 s hold; a tap now dictates too (sliding still opens the style menu). The dictation bar's Hex mark opened Hex through the cold-start handoff; with no warm session it now starts in-keyboard dictation instead. Removes the stray-tap-does-nothing guard because Ryan wants the key to act like a dictation button.

Verified HexKeyGestureTests (5 passed) in the iPhone 18 Pro simulator and installed on the iPhone 17 Pro.
2026-11-19 11:42:17 -05:00

69 lines
1.8 KiB
Swift

import CoreGraphics
import Foundation
/// The full keyboard's Hex key as a pure state machine. A tap, or a still hold of
/// `holdDuration`, dictates; moving more than `slop` before either opens the style menu,
/// and the release picks the row under the finger. Once the hold fires, moves and the
/// release 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
}
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 .idle, .fired: .ignored
}
state = .idle
return output
}
mutating func cancel() {
state = .idle
}
}