hex/ios/HEXShared/FullKeyboard/HexKeyGesture.swift
Ryan Vogel 359b7c4d9e
iOS: dictate from the keyboard without opening Hex, plus suggestions, autocorrect, and Paste (#1999)
* feat(ios): match iOS 26 Liquid Glass keyboard geometry, palette, and Hex key controls

* feat(ios): add 3-slot suggestion strip, cautious autocorrect, and typing gestures

* test(ios): cover iOS 26 keyboard metrics, autocorrect, gestures, and hosted stage

* iOS: move Hex for iPhone to Anomaly's Apple team

(cherry picked from commit 09061502ff)

* Cut Hex Mobile TestFlight betas with a dedicated release tag

Connect the Anomaly app record and pin the mobile package graph. Cut ios-beta prereleases from dev at the scheduled windows or on dispatch, skipping unchanged mobile inputs instead of creating desktop releases or allocating build numbers locally. Prepare the generated project for the Cloud archive after clone.

Verified actionlint, duplicate and desktop-only skips, strict SwiftPM resolution, and an unsigned Release device archive containing the app, keyboard, and widgets. First Xcode Cloud onboarding and signed distribution are still pending.

* Dictate inside the iOS keyboard without opening Hex (probe)

When no warm session is running, the keyboard now records and transcribes on its own instead of opening Hex. iOS refuses to record from a keyboard extension directly (audiomxd: extension lacks the record-audio entitlement), so capture runs through getUserMedia in a hidden WKWebView, whose media process records under Hex's microphone permission. PCM is streamed to Swift, written to a WAV file, and transcribed with on-device Apple Speech (SpeechTranscriber), downloading its assets from the keyboard on first use. Steps and memory are logged to the App Group.

Replaces the cold-start handoff that opened Hex and returned to the host app. Not gated yet: needs an Experiments toggle before it ships. Verified on an iPhone 17 Pro (iOS 27.2) with Hex quit: dictation in Mail/Notes inserted text; the keyboard's footprint was about 58 MB with 118 MB headroom. Unsigned simulator tests were not run.

* Regenerate the keyboard Info.plist with its microphone and speech descriptions

XcodeGen writes these from project.yml; the previous commit changed project.yml without the generated plist. Verified by building for the device.

* Start in-keyboard dictation from the full keyboard's Hex key

Holding the Hex key without a warm session opened Hex through the cold-start handoff; it now records and transcribes inside the keyboard when that path is available, and only falls back to opening Hex otherwise. Verified it builds and installs on the iPhone 17 Pro.

* 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.

* Offer Paste and one-time-code help in the full keyboard's strip

When something was copied in the last 3 minutes and not yet pasted from the keyboard, the empty strip shows the system Paste button (UIPasteControl), which hands the clipboard over only on tap, so iOS never shows its paste prompt; nothing reads the clipboard before then. In one-time-code fields the strip also offers switching keyboards, because iOS gives codes from Messages and Mail only to its own keyboard. Copies seen and pasted are tracked by changeCount in the App Group. UIPasteControl's configuration is immutable, so a palette change replaces the control (setting colors on it raised NSInternalInconsistencyException in FullKeyboardEngineTests).

Verified the full HEXMobileTests suite passes on the iPhone 18 Pro simulator and the build installs on the iPhone 17 Pro; the Paste button itself is not yet tried on the device.

* Describe in-keyboard dictation in the iOS README

The status paragraph said the keyboard never touches the microphone; it now records through WebKit and transcribes with Apple Speech when no warm session runs. The cold-start handoff remains only before iOS 26.

---------

Co-authored-by: Kit Langton <kit.langton@gmail.com>
2026-11-19 12:20:12 -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
}
}