mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS keyboard: log each step from the Hex key's touch-down to the first audio
In-keyboard dictation can lose the first words: nothing said how long the
microphone takes to open after the Hex key, or where that time goes.
What changed
- HexKeyControl reports touch-down (onPress, from the touch's own timestamp)
through KeyboardViewModel.onHexKeyPress, before it is a tap, hold or slide.
- KeyboardLocalDictation keeps a CaptureTimeline per start and, at the first
audio buffer, writes one probe-log line: the touch-down, the start call,
the web view made, its page loaded, hexStart evaluated, the device list,
getUserMedia, the stream kept, the audio context running, "started" and
the first buffer, all in ms after touch-down. The page reports its own
steps with Date.now(). "first audio" is the first buffer less its 4,096
samples.
- A warm Hex session (Keep mic warm) start logs when Hex's recorder ran
(the snapshot's recordingStartedAt), when the command was sent and when
the keyboard saw it, from touch-down.
No behaviour changes.
Measured (anomastudio, iPhone 13 simulator, iOS 27.2, Release, Hex keyboard
in Messages, AXe taps of 100 ms and holds of 1.5 s; median of 5 tap and 3
hold runs, first dictation in a fresh keyboard process, then a second):
tap, first dictation 862 ms (849-961)
tap, second 154 ms (150-157)
hold, first 1160 ms (1143-1161)
hold, second 374 ms (373-377)
In a first dictation the web view's page takes 620-690 ms to load and
getUserMedia ~50 ms; a tap starts ~150 ms after touch-down (the lift) and a
hold ~370 ms (the 350 ms hold timer).
Verified: Release build for the simulator; the log lines above come from it.
* iOS keyboard: load the capture page when the keyboard opens, not on the Hex key
The first in-keyboard dictation after the keyboard opened waited 620-690 ms
for its capture web view's page to load before getUserMedia could even be
asked, so the first words were lost.
What changed
- KeyboardLocalDictation.prepare(in:) makes the capture web view and loads
its page ahead of time. It asks for nothing else: no getUserMedia, no
audio context, no audio session change, so the microphone stays closed
and no orange dot shows until a dictation starts. It is skipped below the
32 MB a first dictation needs (#2045's guard; that dictation opens Hex
instead) and when neither the keyboard nor Hex has the microphone allowed.
- KeyboardViewController calls it a main-queue turn after viewDidAppear (so
the keyboard's first frame never waits for it), only with Full Access, with
in-keyboard dictation available and no warm Hex session (which takes the
Hex key instead).
- A capture page whose content process ends while nothing records is now
dropped quietly and made again by the next start. Before, the last
dictation's handler still ran and showed a microphone error while idle;
a prepared page lives the whole keyboard session, so this matters now.
A recording that loses its page still fails as before.
- The probe log notes "capture prepared" (the main-thread time to make the
web view) and "capture page loaded", with memory.
Measured (anomastudio, iPhone 13 simulator, iOS 27.2, Release, Hex keyboard
in Messages, 100 ms AXe taps, 3 runs, touch-down to first audio):
first dictation 862 ms (849-961) before -> 282 ms (262-309)
second dictation 154 ms (150-157) before -> 144 ms (144-148)
Memory (phys_footprint, simulator, no GPU memory counted):
idle 3 s after the keyboard shows 27.4-27.6 MB -> 31.8-32.0 MB
lifetime peak by the first audio 40.2-40.8 MB -> 40.7-41.2 MB
The page load itself peaks at 39.0-40.7 MB while idle. Making the web view
takes 46-112 ms of the main thread once per keyboard, right after it shows.
Verified: Release simulator build and the runs above; the probe log shows
no getUserMedia or capture start until the Hex key is pressed.
* 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.
1163 lines
44 KiB
Swift
1163 lines
44 KiB
Swift
import Combine
|
||
import SwiftUI
|
||
import UIKit
|
||
|
||
@MainActor
|
||
final class KeyboardClockModel: ObservableObject {
|
||
@Published private(set) var recordingText: String = "0:00"
|
||
@Published private(set) var warmRemainingText: String = "15m"
|
||
|
||
func update(recordingElapsed: TimeInterval?, warmRemaining: TimeInterval?) {
|
||
if let recordingElapsed {
|
||
let formatted = HexTheme.formatClock(recordingElapsed)
|
||
if formatted != recordingText {
|
||
recordingText = formatted
|
||
}
|
||
}
|
||
if let warmRemaining, warmRemaining > 0 {
|
||
let formatted = HexTheme.formatRemaining(warmRemaining)
|
||
if formatted != warmRemainingText {
|
||
warmRemainingText = formatted
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
@MainActor
|
||
final class KeyboardViewModel: ObservableObject {
|
||
enum PickerTray: Equatable {
|
||
case none
|
||
case style
|
||
case language
|
||
}
|
||
|
||
enum StagePhase: Equatable {
|
||
case idle
|
||
case recording
|
||
case transcribing
|
||
case failed
|
||
}
|
||
|
||
struct PendingCommand: Equatable {
|
||
let kind: KeyboardCommandKind
|
||
let jobID: String
|
||
let sentAt: TimeInterval
|
||
}
|
||
|
||
static let commandAckTimeout: TimeInterval = 1.6
|
||
|
||
let clock = KeyboardClockModel()
|
||
|
||
@Published var hasFullAccess = true
|
||
@Published var needsGlobeKey = true
|
||
@Published var returnKeyLabel = "Return"
|
||
@Published var isModelInstalled = false
|
||
@Published private(set) var isWarmAvailable = false
|
||
@Published private(set) var stagePhase: StagePhase = .idle
|
||
@Published private(set) var statusMessage: String?
|
||
@Published var style: DictationStyle = .polished
|
||
@Published var activeTray: PickerTray = .none
|
||
/// Settings › Dictation language (nil for Automatic), what Automatic resolves to, and the
|
||
/// languages the keyboard's quick switch offers. Empty choices hide the switch.
|
||
@Published var dictationLanguage: String?
|
||
@Published var automaticLanguage: String?
|
||
@Published var languageChoices: [String] = []
|
||
/// Translate what I say's target (BCP 47) while it is on, else nil, and the switch that
|
||
/// picks translated or as spoken for the next dictations.
|
||
@Published var translationTarget: String?
|
||
@Published var pastesTranslation = true
|
||
/// Record typing logs is recording this field: both keyboards show a small mark the
|
||
/// whole time, so logging is never hidden.
|
||
@Published var isLoggingTyping = false
|
||
|
||
// Non-published live telemetry read directly by `DotScopeHostView` / button tap handlers
|
||
// so 25–30 Hz polling never invalidates SwiftUI views mid-spring.
|
||
var liveLoudnessDB: Float = -62.0
|
||
var currentTimestamp: TimeInterval = Date().timeIntervalSince1970
|
||
private(set) var currentSnapshot: KeyboardSnapshot = .offline
|
||
var coldStartURL: URL = KeyboardLaunchRequest.startSession(requestedAt: nil).url
|
||
private(set) var pendingCommand: PendingCommand?
|
||
|
||
var onDictateTap: (() -> Void)?
|
||
var onLocalDictateTap: (() -> Void)?
|
||
/// The finger landed on the full keyboard's Hex key (wall clock), before it is a tap,
|
||
/// a hold or a slide.
|
||
var onHexKeyPress: ((TimeInterval) -> Void)?
|
||
/// That press became a slide (the style menu) or was cancelled before it was a tap or a hold.
|
||
var onHexKeyAbandon: (() -> Void)?
|
||
var onStopTap: (() -> Void)?
|
||
var onCancelTap: (() -> Void)?
|
||
var onStyleSelected: ((DictationStyle) -> Void)?
|
||
/// A language picked in the keyboard's switch; nil is Automatic.
|
||
var onLanguageSelected: ((String?) -> Void)?
|
||
/// Paste translated (true) or as spoken (false), picked where you dictate.
|
||
var onPasteTranslationSelected: ((Bool) -> Void)?
|
||
var onSpace: (() -> Void)?
|
||
var onDelete: (() -> Void)?
|
||
var onReturn: (() -> Void)?
|
||
var onOpenURL: ((URL) -> Void)?
|
||
/// The dictation bar's light haptic for each press; the keyboard allows it with Full
|
||
/// Access and Experiments › Haptics, as the full keyboard's keys.
|
||
var onKeyFeedback: (() -> Void)?
|
||
weak var inputViewController: UIInputViewController?
|
||
|
||
/// The language dictation uses now, when known.
|
||
var effectiveLanguage: String? {
|
||
dictationLanguage ?? automaticLanguage
|
||
}
|
||
|
||
/// Its own short name ("Norsk", "English"), for the switch and the space bar.
|
||
var languageLabel: String? {
|
||
effectiveLanguage.map(DictationLanguageSwitch.nativeName(for:))
|
||
}
|
||
|
||
var showsLanguageSwitch: Bool {
|
||
hasFullAccess && !languageChoices.isEmpty
|
||
}
|
||
|
||
/// "Paste in English" or "Paste in Spanish", when Translate what I say is on and you
|
||
/// dictate in another language than its target.
|
||
var pasteChoice: TranslationPasteChoice? {
|
||
guard hasFullAccess, let translationTarget else { return nil }
|
||
return TranslationPasteChoice(target: translationTarget, source: effectiveLanguage)
|
||
}
|
||
|
||
/// The language label with where dictations go while they paste translated:
|
||
/// "Español → English".
|
||
var languageSummary: String? {
|
||
guard let languageLabel else { return nil }
|
||
guard pastesTranslation, let target = pasteChoice?.target else { return languageLabel }
|
||
return "\(languageLabel) → \(DictationLanguageSwitch.nativeName(for: target))"
|
||
}
|
||
|
||
var dotScopePhase: DotScopePhase {
|
||
switch stagePhase {
|
||
case .idle:
|
||
return .idle
|
||
case .recording:
|
||
return .recording
|
||
case .transcribing:
|
||
return .processing
|
||
case .failed:
|
||
return .failed
|
||
}
|
||
}
|
||
|
||
func setActiveTray(_ tray: PickerTray, reduceMotion: Bool) {
|
||
guard activeTray != tray else { return }
|
||
withAnimation(HexMotion.trayAnimation(reduceMotion: reduceMotion)) {
|
||
activeTray = tray
|
||
}
|
||
}
|
||
|
||
func setPendingCommand(_ command: PendingCommand?, now: TimeInterval, animated: Bool) {
|
||
currentTimestamp = now
|
||
pendingCommand = command
|
||
reconcileVisualState(
|
||
hasFullAccess: hasFullAccess,
|
||
snapshot: currentSnapshot,
|
||
isModelInstalled: isModelInstalled,
|
||
style: style,
|
||
now: now,
|
||
animated: animated
|
||
)
|
||
}
|
||
|
||
func applyBridgeState(
|
||
hasFullAccess newFullAccess: Bool,
|
||
snapshot newSnapshot: KeyboardSnapshot,
|
||
isModelInstalled newModelInstalled: Bool,
|
||
style newStyle: DictationStyle,
|
||
now: TimeInterval,
|
||
clearPending: Bool = false,
|
||
animated: Bool
|
||
) {
|
||
currentTimestamp = now
|
||
currentSnapshot = newSnapshot
|
||
liveLoudnessDB = newSnapshot.state == .recording ? newSnapshot.effectiveLoudnessDB : -62.0
|
||
|
||
if clearPending {
|
||
pendingCommand = nil
|
||
} else if let pending = pendingCommand {
|
||
let expired = now - pending.sentAt >= Self.commandAckTimeout
|
||
let acknowledged = (pending.kind == .start && newSnapshot.state == .recording)
|
||
|| (pending.kind == .stop && newSnapshot.state != .recording)
|
||
|| (pending.kind == .cancel && newSnapshot.state != .recording)
|
||
if expired || acknowledged {
|
||
pendingCommand = nil
|
||
}
|
||
}
|
||
|
||
let recordingElapsed: TimeInterval? = if newSnapshot.state == .recording {
|
||
newSnapshot.recordingElapsed(at: now)
|
||
} else if let pending = pendingCommand, pending.kind == .start {
|
||
max(0, now - pending.sentAt)
|
||
} else {
|
||
nil
|
||
}
|
||
let warmRemaining: TimeInterval? = newSnapshot.isAvailable(at: now)
|
||
? max(0, newSnapshot.expiresAt - now)
|
||
: nil
|
||
clock.update(recordingElapsed: recordingElapsed, warmRemaining: warmRemaining)
|
||
|
||
reconcileVisualState(
|
||
hasFullAccess: newFullAccess,
|
||
snapshot: newSnapshot,
|
||
isModelInstalled: newModelInstalled,
|
||
style: newStyle,
|
||
now: now,
|
||
animated: animated
|
||
)
|
||
}
|
||
|
||
/// Convenience for deterministic preview/snapshot fixtures (`KeyboardPreviewHost`).
|
||
func configureFixture(
|
||
snapshot: KeyboardSnapshot,
|
||
now: TimeInterval,
|
||
hasFullAccess: Bool = true,
|
||
isModelInstalled: Bool = true,
|
||
style: DictationStyle = .polished,
|
||
activeTray: PickerTray = .none
|
||
) {
|
||
self.activeTray = activeTray
|
||
applyBridgeState(
|
||
hasFullAccess: hasFullAccess,
|
||
snapshot: snapshot,
|
||
isModelInstalled: isModelInstalled,
|
||
style: style,
|
||
now: now,
|
||
clearPending: true,
|
||
animated: false
|
||
)
|
||
}
|
||
|
||
private func reconcileVisualState(
|
||
hasFullAccess newFullAccess: Bool,
|
||
snapshot snap: KeyboardSnapshot,
|
||
isModelInstalled newModelInstalled: Bool,
|
||
style newStyle: DictationStyle,
|
||
now: TimeInterval,
|
||
animated: Bool
|
||
) {
|
||
let nextWarm = newFullAccess && snap.isAvailable(at: now)
|
||
let nextStage = computeStagePhase(snapshot: snap, now: now, hasFullAccess: newFullAccess)
|
||
let trimmedMsg = snap.message?.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
let nextMessage = (trimmedMsg?.isEmpty == false) ? trimmedMsg : nil
|
||
|
||
let changed = hasFullAccess != newFullAccess
|
||
|| isWarmAvailable != nextWarm
|
||
|| isModelInstalled != newModelInstalled
|
||
|| stagePhase != nextStage
|
||
|| statusMessage != nextMessage
|
||
|| style != newStyle
|
||
|
||
guard changed else { return }
|
||
|
||
let commit = {
|
||
self.hasFullAccess = newFullAccess
|
||
self.isWarmAvailable = nextWarm
|
||
self.isModelInstalled = newModelInstalled
|
||
self.stagePhase = nextStage
|
||
self.statusMessage = nextMessage
|
||
self.style = newStyle
|
||
}
|
||
|
||
if animated {
|
||
withAnimation(HexMotion.stageAnimation(reduceMotion: UIAccessibility.isReduceMotionEnabled)) {
|
||
commit()
|
||
}
|
||
} else {
|
||
commit()
|
||
}
|
||
}
|
||
|
||
private func computeStagePhase(
|
||
snapshot snap: KeyboardSnapshot,
|
||
now: TimeInterval,
|
||
hasFullAccess: Bool
|
||
) -> StagePhase {
|
||
guard hasFullAccess else { return .idle }
|
||
|
||
if let pending = pendingCommand, now - pending.sentAt < Self.commandAckTimeout {
|
||
switch pending.kind {
|
||
case .cancel:
|
||
return .idle
|
||
case .stop where snap.state == .recording:
|
||
return .transcribing
|
||
case .start where snap.state != .recording:
|
||
return .recording
|
||
default:
|
||
break
|
||
}
|
||
}
|
||
|
||
switch snap.state {
|
||
case .recording:
|
||
return .recording
|
||
case .transcribing:
|
||
return .transcribing
|
||
case .failed:
|
||
return .failed
|
||
case .offline, .ready:
|
||
return .idle
|
||
}
|
||
}
|
||
}
|
||
|
||
private struct GlobeKeyRepresentable: UIViewRepresentable {
|
||
weak var controller: UIInputViewController?
|
||
let isDark: Bool
|
||
|
||
func makeUIView(context: Context) -> UIButton {
|
||
let button = UIButton(type: .system)
|
||
var config = UIButton.Configuration.plain()
|
||
config.image = UIImage(
|
||
systemName: "globe",
|
||
withConfiguration: UIImage.SymbolConfiguration(pointSize: 15, weight: .medium)
|
||
)
|
||
config.baseForegroundColor = isDark
|
||
? UIColor(white: 0.92, alpha: 1)
|
||
: UIColor(red: 0x14 / 255.0, green: 0x16 / 255.0, blue: 0x1B / 255.0, alpha: 1)
|
||
button.configuration = config
|
||
button.accessibilityLabel = "Next keyboard"
|
||
if let controller {
|
||
button.addTarget(
|
||
controller,
|
||
action: #selector(UIInputViewController.handleInputModeList(from:with:)),
|
||
for: .allTouchEvents
|
||
)
|
||
}
|
||
return button
|
||
}
|
||
|
||
func updateUIView(_ uiView: UIButton, context: Context) {
|
||
uiView.configuration?.baseForegroundColor = isDark
|
||
? UIColor(white: 0.92, alpha: 1)
|
||
: UIColor(red: 0x14 / 255.0, green: 0x16 / 255.0, blue: 0x1B / 255.0, alpha: 1)
|
||
}
|
||
}
|
||
|
||
private struct KeyboardChromePalette {
|
||
let isDark: Bool
|
||
let isHosted: Bool
|
||
|
||
var textBase: Color {
|
||
isDark
|
||
? HexTheme.textBase
|
||
: Color(red: 0x14 / 255.0, green: 0x16 / 255.0, blue: 0x1B / 255.0)
|
||
}
|
||
|
||
var textMuted: Color {
|
||
isDark
|
||
? HexTheme.textMuted
|
||
: Color(red: 0x4E / 255.0, green: 0x54 / 255.0, blue: 0x61 / 255.0)
|
||
}
|
||
|
||
var chipIdleFill: Color {
|
||
isDark ? Color.white.opacity(0.07) : Color.black.opacity(0.055)
|
||
}
|
||
|
||
var chipActiveFill: Color {
|
||
isDark ? Color.white.opacity(0.16) : Color.black.opacity(0.12)
|
||
}
|
||
|
||
var topBarPressedFill: Color {
|
||
isDark ? Color.white.opacity(0.09) : Color.black.opacity(0.07)
|
||
}
|
||
|
||
var controlSubtleFill: Color {
|
||
isDark ? Color.white.opacity(0.085) : Color.black.opacity(0.065)
|
||
}
|
||
|
||
var controlProminentFill: Color {
|
||
isDark ? Color.white.opacity(0.15) : Color.black.opacity(0.11)
|
||
}
|
||
|
||
var idleTriggerFill: Color {
|
||
isDark ? Color.white.opacity(0.09) : Color.black.opacity(0.07)
|
||
}
|
||
|
||
var keyFill: Color {
|
||
if !isHosted { return Color.white.opacity(0.085) }
|
||
return isDark ? Color.white.opacity(0.105) : Color.white
|
||
}
|
||
|
||
var keyPressedFill: Color {
|
||
if !isHosted { return Color.white.opacity(0.16) }
|
||
return isDark ? Color.white.opacity(0.19) : Color(white: 0.88)
|
||
}
|
||
|
||
var keyShadow: Color {
|
||
Color.black.opacity(isDark ? 0.24 : 0.14)
|
||
}
|
||
}
|
||
|
||
private struct KeyboardKeyButtonStyle: ButtonStyle {
|
||
let palette: KeyboardChromePalette
|
||
|
||
func makeBody(configuration: Configuration) -> some View {
|
||
configuration.label
|
||
.background(
|
||
configuration.isPressed ? palette.keyPressedFill : palette.keyFill,
|
||
in: RoundedRectangle(cornerRadius: HexTheme.controlRadius, style: .continuous)
|
||
)
|
||
.shadow(color: palette.keyShadow, radius: 0.5, x: 0, y: 1)
|
||
}
|
||
}
|
||
|
||
private struct TopBarTriggerButtonStyle: ButtonStyle {
|
||
let palette: KeyboardChromePalette
|
||
|
||
func makeBody(configuration: Configuration) -> some View {
|
||
configuration.label
|
||
.background(
|
||
configuration.isPressed ? palette.topBarPressedFill : Color.clear,
|
||
in: Capsule()
|
||
)
|
||
}
|
||
}
|
||
|
||
private struct RepeatDeleteKey: View {
|
||
let palette: KeyboardChromePalette
|
||
let onDelete: () -> Void
|
||
@State private var isPressing = false
|
||
@State private var repeatTask: Task<Void, Never>?
|
||
|
||
var body: some View {
|
||
Image(systemName: "delete.left")
|
||
.font(.system(size: 15, weight: .medium))
|
||
.foregroundStyle(palette.textBase)
|
||
.frame(width: 52, height: 42)
|
||
.background(
|
||
isPressing ? palette.keyPressedFill : palette.keyFill,
|
||
in: RoundedRectangle(cornerRadius: HexTheme.controlRadius, style: .continuous)
|
||
)
|
||
.shadow(color: palette.keyShadow, radius: 0.5, x: 0, y: 1)
|
||
.contentShape(RoundedRectangle(cornerRadius: HexTheme.controlRadius, style: .continuous))
|
||
.gesture(
|
||
DragGesture(minimumDistance: 0)
|
||
.onChanged { _ in
|
||
guard !isPressing else { return }
|
||
isPressing = true
|
||
onDelete()
|
||
repeatTask = Task { @MainActor in
|
||
try? await Task.sleep(for: .milliseconds(360))
|
||
while !Task.isCancelled {
|
||
onDelete()
|
||
try? await Task.sleep(for: .milliseconds(75))
|
||
}
|
||
}
|
||
}
|
||
.onEnded { _ in
|
||
isPressing = false
|
||
repeatTask?.cancel()
|
||
repeatTask = nil
|
||
}
|
||
)
|
||
.accessibilityLabel("Delete")
|
||
}
|
||
}
|
||
|
||
private struct KeyboardRecordingClockText: View {
|
||
@ObservedObject var clock: KeyboardClockModel
|
||
let color: Color
|
||
|
||
var body: some View {
|
||
Text(clock.recordingText)
|
||
.font(.system(size: 13, weight: .medium))
|
||
.monospacedDigit()
|
||
.contentTransition(.numericText())
|
||
.foregroundStyle(color)
|
||
.animation(.easeOut(duration: 0.16), value: clock.recordingText)
|
||
}
|
||
}
|
||
|
||
private struct KeyboardWarmRemainingText: View {
|
||
@ObservedObject var clock: KeyboardClockModel
|
||
let color: Color
|
||
|
||
var body: some View {
|
||
Text(clock.warmRemainingText)
|
||
.font(.system(size: 13, weight: .medium))
|
||
.monospacedDigit()
|
||
.contentTransition(.numericText())
|
||
.foregroundStyle(color)
|
||
}
|
||
}
|
||
|
||
extension View {
|
||
/// iOS 26's soft scroll-edge effect blurs the whole content of a short
|
||
/// scroll view (the 26 pt return-app tray), so turn it off there.
|
||
/// Earlier versions have no edge effect and are left unchanged.
|
||
@ViewBuilder
|
||
func hexScrollEdgeEffectHidden() -> some View {
|
||
if #available(iOS 26.0, *) {
|
||
scrollEdgeEffectHidden(true, for: .all)
|
||
} else {
|
||
self
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Record typing logs is recording: a dot and "Logging", flat and small, in the dictation
|
||
/// bar's top row (the full keyboard draws `TypingLogMarkView`).
|
||
struct TypingLogMark: View {
|
||
let textColor: Color
|
||
|
||
var body: some View {
|
||
HStack(spacing: 4) {
|
||
// Muted, not orange: an orange dot would read as iOS's microphone indicator.
|
||
Circle()
|
||
.fill(textColor)
|
||
.frame(width: 5, height: 5)
|
||
Text(TypingLogMarkView.text)
|
||
.font(.system(size: 11, weight: .medium))
|
||
.foregroundStyle(textColor)
|
||
}
|
||
.accessibilityElement(children: .ignore)
|
||
.accessibilityLabel(TypingLogMarkView.accessibilityText)
|
||
}
|
||
}
|
||
|
||
enum KeyboardSetupCopy {
|
||
static let fullAccessPath = "Allow Full Access: Settings › Apps › Hex › Keyboards"
|
||
}
|
||
|
||
/// Metal-free stand-in for the resting Dot Scope bar. Mounting it instead of
|
||
/// `DotScopeView` keeps the keyboard extension's launch path off the GPU: no
|
||
/// device, command queue, render pipelines, `CAMetalLayer`, display link, or
|
||
/// idle simulation steps before the first frame.
|
||
private struct KeyboardRestingDotBar: View {
|
||
let isDark: Bool
|
||
let leadingInset: CGFloat
|
||
let trailingInset: CGFloat
|
||
|
||
private static let pitch: CGFloat = 3.5
|
||
private static let rows = 11
|
||
private static let fade: CGFloat = 26
|
||
private static let braidCycles: Double = 2.1
|
||
private static let restingAmplitude: Double = 0.9
|
||
|
||
var body: some View {
|
||
let lead = Double(max(0, leadingInset))
|
||
let trail = Double(max(0, trailingInset))
|
||
let tint: (red: Double, green: Double, blue: Double) = isDark
|
||
? (red: 0.93, green: 0.91, blue: 0.86)
|
||
: (red: 0.55, green: 0.46, blue: 0.33)
|
||
return Canvas { context, size in
|
||
let pitch = Double(Self.pitch)
|
||
let firstX = lead + 3
|
||
let lastX = Double(size.width) - trail - 3
|
||
guard lastX - firstX > pitch * 2, size.height > 8 else { return }
|
||
|
||
let columns = max(3, Int(((lastX - firstX) / pitch).rounded(.down)) + 1)
|
||
let midY = Double(size.height) * 0.5
|
||
let halfRows = Double(Self.rows - 1) * 0.5
|
||
|
||
for column in 0..<columns {
|
||
let x = firstX + Double(column) * pitch
|
||
let xn = Double(column) / Double(max(1, columns - 1))
|
||
let fadeIn = min(1.0, max(0.0, (x - lead) / Double(Self.fade)))
|
||
let fadeOut = min(1.0, max(0.0, (Double(size.width) - trail - x) / Double(Self.fade)))
|
||
let edge = min(fadeIn, fadeOut)
|
||
guard edge > 0.004 else { continue }
|
||
|
||
// The live scope flattens to a faint mirrored braid at rest.
|
||
let wobble = sin(2.0 * .pi * Self.braidCycles * xn - 0.6) * Self.restingAmplitude
|
||
let upper = midY - wobble
|
||
let lower = midY + wobble
|
||
|
||
for row in 0..<Self.rows {
|
||
let y = midY + (Double(row) - halfRows) * pitch
|
||
let litUpper = exp(-pow(y - upper, 2) / 2.2)
|
||
let litLower = exp(-pow(y - lower, 2) / 2.2)
|
||
let power = min(1.0, 1.0 - (1.0 - litUpper) * (1.0 - litLower))
|
||
let alpha = (0.05 + 0.40 * power) * edge
|
||
guard alpha > 0.01 else { continue }
|
||
let radius = (0.16 + 0.21 * power) * pitch
|
||
context.fill(
|
||
Path(
|
||
ellipseIn: CGRect(
|
||
x: x - radius,
|
||
y: y - radius,
|
||
width: radius * 2,
|
||
height: radius * 2
|
||
)
|
||
),
|
||
with: .color(Color(red: tint.red, green: tint.green, blue: tint.blue, opacity: alpha))
|
||
)
|
||
}
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.accessibilityHidden(true)
|
||
}
|
||
}
|
||
|
||
struct KeyboardSurfaceView: View {
|
||
@Environment(\.colorScheme) private var colorScheme
|
||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||
@ObservedObject var model: KeyboardViewModel
|
||
/// The keyboard extension sets this so its process never builds the Metal
|
||
/// scope before a stage needs it. The app's docked keyboard preview keeps
|
||
/// the live scope mounted at rest.
|
||
var defersDotScopeUntilActive = false
|
||
|
||
/// Push to talk on the well: the press, on the Hex key's state machine.
|
||
@State private var hold = HexKeyGesture()
|
||
@State private var holdTimer: Task<Void, Never>?
|
||
@State private var pressBegan = false
|
||
/// This press's hold started the dictation, so the Dictate button's tap action skips it.
|
||
@State private var holdTookPress = false
|
||
@GestureState private var isPressingWell = false
|
||
|
||
private static let wellHeight: CGFloat = 44
|
||
private static let controlHeight: CGFloat = 34
|
||
private static let wellInset: CGFloat = 5
|
||
private static let cancelWidth: CGFloat = 34
|
||
private static let doneWidth: CGFloat = 66
|
||
|
||
private var isHostedExtension: Bool {
|
||
model.inputViewController != nil
|
||
}
|
||
|
||
private var palette: KeyboardChromePalette {
|
||
KeyboardChromePalette(
|
||
isDark: !isHostedExtension || colorScheme == .dark,
|
||
isHosted: isHostedExtension
|
||
)
|
||
}
|
||
|
||
private var isStageActive: Bool {
|
||
model.stagePhase == .recording || model.stagePhase == .transcribing
|
||
}
|
||
|
||
var body: some View {
|
||
VStack(spacing: 8) {
|
||
topBar
|
||
primaryStage
|
||
utilityRow
|
||
}
|
||
.padding(.horizontal, 10)
|
||
.padding(.top, isHostedExtension ? 4 : 6)
|
||
.padding(.bottom, 6)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.background(isHostedExtension ? Color.clear : HexTheme.bgDeep)
|
||
.preferredColorScheme(isHostedExtension ? nil : .dark)
|
||
}
|
||
|
||
// MARK: - Top Bar (26 pt, permanently mounted ZStack)
|
||
|
||
private var topBar: some View {
|
||
let p = palette
|
||
return ZStack {
|
||
defaultTopBar(palette: p)
|
||
.hexTrayReveal(model.activeTray == .none, reduceMotion: reduceMotion, offsetY: -3)
|
||
|
||
styleTrayBar(palette: p)
|
||
.hexTrayReveal(model.activeTray == .style, reduceMotion: reduceMotion, offsetY: 3.5)
|
||
|
||
languageTrayBar(palette: p)
|
||
.hexTrayReveal(model.activeTray == .language, reduceMotion: reduceMotion, offsetY: 3.5)
|
||
}
|
||
.frame(height: 26)
|
||
}
|
||
|
||
private func defaultTopBar(palette p: KeyboardChromePalette) -> some View {
|
||
HStack(spacing: 8) {
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
if model.hasFullAccess, !model.isWarmAvailable, let local = model.onLocalDictateTap {
|
||
local()
|
||
} else {
|
||
let target = model.hasFullAccess ? model.coldStartURL : KeyboardLaunchRequest.setup.url
|
||
model.onOpenURL?(target)
|
||
}
|
||
} label: {
|
||
HexMarkView(
|
||
size: 16,
|
||
color: isStageActive && p.isDark ? HexTheme.warmPearl : p.textBase
|
||
)
|
||
.frame(width: 22, height: 26, alignment: .leading)
|
||
.contentShape(Rectangle())
|
||
}
|
||
.buttonStyle(.plain)
|
||
.accessibilityLabel("Open Hex")
|
||
|
||
if model.isLoggingTyping {
|
||
TypingLogMark(textColor: p.textMuted)
|
||
}
|
||
|
||
leadingAccessory(palette: p)
|
||
|
||
Spacer(minLength: 4)
|
||
|
||
if model.showsLanguageSwitch {
|
||
languageTrigger(palette: p)
|
||
}
|
||
|
||
styleTrigger(palette: p)
|
||
.hexTrayReveal(model.hasFullAccess, reduceMotion: reduceMotion, offsetY: 0)
|
||
}
|
||
.frame(height: 26)
|
||
}
|
||
|
||
private func styleTrayBar(palette p: KeyboardChromePalette) -> some View {
|
||
HStack(spacing: 4) {
|
||
ForEach(DictationStyle.allCases, id: \.self) { candidate in
|
||
let active = candidate == model.style
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
model.onStyleSelected?(candidate)
|
||
model.setActiveTray(.none, reduceMotion: reduceMotion)
|
||
} label: {
|
||
Text(candidate.label)
|
||
.font(.system(size: 13, weight: .medium))
|
||
.foregroundStyle(active ? p.textBase : p.textMuted)
|
||
.padding(.horizontal, 11)
|
||
.frame(height: 26)
|
||
.background(active ? p.chipActiveFill : p.chipIdleFill, in: Capsule())
|
||
}
|
||
.buttonStyle(.plain)
|
||
}
|
||
Spacer(minLength: 4)
|
||
closeTrayButton(palette: p)
|
||
}
|
||
.frame(height: 26)
|
||
}
|
||
|
||
private func leadingAccessory(palette p: KeyboardChromePalette) -> some View {
|
||
let showNoAccess = !model.hasFullAccess
|
||
let showRecordingClock = model.hasFullAccess && model.stagePhase == .recording
|
||
let showWarmStatus = model.hasFullAccess
|
||
&& model.isWarmAvailable
|
||
&& model.stagePhase != .recording
|
||
&& model.stagePhase != .transcribing
|
||
|
||
return ZStack(alignment: .leading) {
|
||
HStack(spacing: 6) {
|
||
HexStatusDot(color: HexTheme.warmMic, size: 6)
|
||
Text(KeyboardSetupCopy.fullAccessPath)
|
||
.font(.system(size: 13, weight: .regular))
|
||
.foregroundStyle(p.textMuted)
|
||
.lineLimit(1)
|
||
.minimumScaleFactor(0.78)
|
||
}
|
||
.accessibilityElement(children: .combine)
|
||
.accessibilityLabel("Full Access is off. \(KeyboardSetupCopy.fullAccessPath)")
|
||
.hexTrayReveal(showNoAccess, reduceMotion: reduceMotion, offsetY: 2)
|
||
|
||
HStack(spacing: 6) {
|
||
HexStatusDot(color: HexTheme.danger, size: 6.5)
|
||
KeyboardRecordingClockText(clock: model.clock, color: p.textBase)
|
||
}
|
||
.hexTrayReveal(showRecordingClock, reduceMotion: reduceMotion, offsetY: 2)
|
||
|
||
Group {
|
||
if let message = model.statusMessage {
|
||
HStack(spacing: 6) {
|
||
if model.stagePhase == .failed {
|
||
HexStatusDot(color: HexTheme.danger, size: 6)
|
||
}
|
||
// A failure gets two smaller lines: it has to be read in full.
|
||
let failed = model.stagePhase == .failed
|
||
Text(message)
|
||
.font(.system(size: failed ? 11 : 13, weight: .regular))
|
||
.foregroundStyle(p.textMuted)
|
||
.lineLimit(failed ? 2 : 1)
|
||
.minimumScaleFactor(0.85)
|
||
}
|
||
} else {
|
||
HStack(spacing: 5) {
|
||
HexStatusDot(color: HexTheme.warmMic, size: 6)
|
||
KeyboardWarmRemainingText(clock: model.clock, color: p.textMuted)
|
||
}
|
||
}
|
||
}
|
||
.hexTrayReveal(showWarmStatus, reduceMotion: reduceMotion, offsetY: 2)
|
||
}
|
||
}
|
||
|
||
private func styleTrigger(palette p: KeyboardChromePalette) -> some View {
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
model.setActiveTray(.style, reduceMotion: reduceMotion)
|
||
} label: {
|
||
HStack(spacing: 4) {
|
||
Text(model.style.label)
|
||
.font(.system(size: 13, weight: .medium))
|
||
Image(systemName: "chevron.down")
|
||
.font(.system(size: 10, weight: .medium))
|
||
.foregroundStyle(p.textMuted)
|
||
}
|
||
.foregroundStyle(p.textMuted)
|
||
.padding(.horizontal, 8)
|
||
.frame(height: 26)
|
||
.contentShape(Capsule())
|
||
}
|
||
.buttonStyle(TopBarTriggerButtonStyle(palette: p))
|
||
.accessibilityLabel("Style: \(model.style.label)")
|
||
}
|
||
|
||
private func languageTrigger(palette p: KeyboardChromePalette) -> some View {
|
||
let label = model.languageSummary ?? "Automatic"
|
||
return Button {
|
||
model.onKeyFeedback?()
|
||
model.setActiveTray(.language, reduceMotion: reduceMotion)
|
||
} label: {
|
||
HStack(spacing: 4) {
|
||
Text(label)
|
||
.font(.system(size: 13, weight: .medium))
|
||
.lineLimit(1)
|
||
Image(systemName: "chevron.down")
|
||
.font(.system(size: 10, weight: .medium))
|
||
.foregroundStyle(p.textMuted)
|
||
}
|
||
.foregroundStyle(p.textMuted)
|
||
.padding(.horizontal, 8)
|
||
.frame(height: 26)
|
||
.contentShape(Capsule())
|
||
}
|
||
.buttonStyle(TopBarTriggerButtonStyle(palette: p))
|
||
// "Español → English" before the status beside the Hex mark, which is short whenever
|
||
// the switch shows (it needs Full Access).
|
||
.layoutPriority(1)
|
||
.accessibilityLabel("Dictation language: \(label)")
|
||
}
|
||
|
||
private func languageTrayBar(palette p: KeyboardChromePalette) -> some View {
|
||
let names = DictationLanguageSwitch.nativeNames(for: model.languageChoices)
|
||
return HStack(spacing: 4) {
|
||
ScrollView(.horizontal, showsIndicators: false) {
|
||
HStack(spacing: 4) {
|
||
languageChip(id: nil, label: "Auto", palette: p)
|
||
ForEach(model.languageChoices, id: \.self) { id in
|
||
languageChip(id: id, label: names[id] ?? id, palette: p)
|
||
}
|
||
}
|
||
.padding(.leading, 2)
|
||
.padding(.trailing, 10)
|
||
}
|
||
.hexScrollEdgeEffectHidden()
|
||
.hexHorizontalEdgeFade(leading: 4, trailing: 14)
|
||
|
||
if let choice = model.pasteChoice {
|
||
pasteTranslationChip(choice, palette: p)
|
||
}
|
||
closeTrayButton(palette: p)
|
||
}
|
||
.frame(height: 26)
|
||
}
|
||
|
||
/// Lit while dictations paste translated; a tap switches to as spoken and back.
|
||
private func pasteTranslationChip(_ choice: TranslationPasteChoice, palette p: KeyboardChromePalette) -> some View {
|
||
let active = model.pastesTranslation
|
||
return Button {
|
||
model.onKeyFeedback?()
|
||
model.onPasteTranslationSelected?(!active)
|
||
model.setActiveTray(.none, reduceMotion: reduceMotion)
|
||
} label: {
|
||
Text(choice.translatedTitle)
|
||
.font(.system(size: 13, weight: .medium))
|
||
.foregroundStyle(active ? p.textBase : p.textMuted)
|
||
.lineLimit(1)
|
||
.padding(.horizontal, 10)
|
||
.frame(height: 26)
|
||
.background(active ? p.chipActiveFill : p.chipIdleFill, in: Capsule())
|
||
}
|
||
.buttonStyle(.plain)
|
||
.accessibilityLabel(choice.translatedTitle)
|
||
.accessibilityValue(active ? "On" : "Off. \(choice.spokenTitle)")
|
||
.accessibilityAddTraits(active ? .isSelected : [])
|
||
}
|
||
|
||
private func languageChip(id: String?, label: String, palette p: KeyboardChromePalette) -> some View {
|
||
let active = model.dictationLanguage == id
|
||
return Button {
|
||
model.onKeyFeedback?()
|
||
model.onLanguageSelected?(id)
|
||
model.setActiveTray(.none, reduceMotion: reduceMotion)
|
||
} label: {
|
||
Text(label)
|
||
.font(.system(size: 13, weight: .medium))
|
||
.foregroundStyle(active ? p.textBase : p.textMuted)
|
||
.padding(.horizontal, 10)
|
||
.frame(height: 26)
|
||
.background(active ? p.chipActiveFill : p.chipIdleFill, in: Capsule())
|
||
}
|
||
.buttonStyle(.plain)
|
||
.accessibilityLabel(id == nil ? "Automatic language" : label)
|
||
}
|
||
|
||
private func closeTrayButton(palette p: KeyboardChromePalette) -> some View {
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
model.setActiveTray(.none, reduceMotion: reduceMotion)
|
||
} label: {
|
||
Image(systemName: "xmark")
|
||
.font(.system(size: 11, weight: .medium))
|
||
.foregroundStyle(p.textMuted)
|
||
.frame(width: 26, height: 26)
|
||
.background(p.chipIdleFill, in: Circle())
|
||
}
|
||
.buttonStyle(.plain)
|
||
.accessibilityLabel("Close")
|
||
}
|
||
|
||
// MARK: - Unified 44 pt Recessed Stage Well (all controls permanently mounted)
|
||
|
||
private var idleTriggerSpec: (icon: String, title: String, width: CGFloat) {
|
||
if !model.hasFullAccess {
|
||
return ("arrow.up.forward.app.fill", "Set up Hex", 112)
|
||
}
|
||
if !model.isModelInstalled {
|
||
return ("mic.fill", "Set up", 88)
|
||
}
|
||
return ("mic.fill", "Dictate", 94)
|
||
}
|
||
|
||
private var primaryStage: some View {
|
||
let p = palette
|
||
let showRecordingControls = model.stagePhase == .recording
|
||
let showIdleTrigger = model.stagePhase == .idle || model.stagePhase == .failed
|
||
let spec = idleTriggerSpec
|
||
|
||
let leadInset: Float = showRecordingControls
|
||
? Float(Self.wellInset + Self.cancelWidth + 6)
|
||
: 0.0
|
||
let trailInset: Float = if showRecordingControls {
|
||
Float(Self.wellInset + Self.doneWidth + 6)
|
||
} else if showIdleTrigger {
|
||
Float(Self.wellInset + spec.width + 6)
|
||
} else {
|
||
0.0
|
||
}
|
||
|
||
return ZStack {
|
||
HexRecessedWell(isDark: p.isDark)
|
||
|
||
if defersDotScopeUntilActive && !isStageActive {
|
||
KeyboardRestingDotBar(
|
||
isDark: p.isDark,
|
||
leadingInset: CGFloat(leadInset),
|
||
trailingInset: CGFloat(trailInset)
|
||
)
|
||
.clipShape(Capsule())
|
||
.allowsHitTesting(false)
|
||
} else {
|
||
DotScopeView(
|
||
phase: model.dotScopePhase,
|
||
tuning: .bar(leadingInset: leadInset, trailingInset: trailInset, fade: 28.0, spacing: 3.5),
|
||
drawPlate: false,
|
||
loudnessProvider: { [weak model] in model?.liveLoudnessDB ?? -62.0 }
|
||
)
|
||
.clipShape(Capsule())
|
||
.allowsHitTesting(false)
|
||
}
|
||
|
||
// Recording peer controls (`✕` and `Done`), always mounted so enter/exit springs stay continuous.
|
||
HStack(spacing: 8) {
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
model.onCancelTap?()
|
||
} label: {
|
||
Image(systemName: "xmark")
|
||
.font(.system(size: 13, weight: .semibold))
|
||
.foregroundStyle(p.textMuted)
|
||
.frame(width: Self.cancelWidth, height: Self.controlHeight)
|
||
.background(p.controlSubtleFill, in: Circle())
|
||
.frame(minWidth: 44, minHeight: Self.wellHeight, alignment: .leading)
|
||
.contentShape(Rectangle())
|
||
}
|
||
.buttonStyle(.plain)
|
||
.accessibilityLabel("Cancel dictation")
|
||
.hexControlReveal(
|
||
showRecordingControls,
|
||
reduceMotion: reduceMotion,
|
||
scale: 0.90,
|
||
blur: 2.5,
|
||
anchor: .leading
|
||
)
|
||
|
||
Spacer(minLength: 0)
|
||
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
model.onStopTap?()
|
||
} label: {
|
||
Text("Done")
|
||
.font(.system(size: 15, weight: .semibold))
|
||
.foregroundStyle(p.textBase)
|
||
.frame(width: Self.doneWidth, height: Self.controlHeight)
|
||
.background(p.controlProminentFill, in: Capsule())
|
||
.frame(minHeight: Self.wellHeight, alignment: .trailing)
|
||
.contentShape(Rectangle())
|
||
}
|
||
.buttonStyle(.plain)
|
||
.hexControlReveal(
|
||
showRecordingControls,
|
||
reduceMotion: reduceMotion,
|
||
scale: 0.90,
|
||
blur: 2.5,
|
||
anchor: .trailing
|
||
)
|
||
}
|
||
.padding(.horizontal, Self.wellInset)
|
||
|
||
// Single permanently mounted idle trigger (never swapped across 3 Button branches).
|
||
Button {
|
||
guard !holdTookPress else { return }
|
||
model.onKeyFeedback?()
|
||
handleIdleStageTap()
|
||
} label: {
|
||
HStack(spacing: 0) {
|
||
Spacer(minLength: 0)
|
||
HStack(spacing: 6) {
|
||
Image(systemName: spec.icon)
|
||
.font(.system(size: 13, weight: .medium))
|
||
.foregroundStyle(p.textMuted)
|
||
Text(spec.title)
|
||
.font(.system(size: 15, weight: .medium))
|
||
.foregroundStyle(p.textBase)
|
||
}
|
||
.frame(width: spec.width, height: Self.controlHeight)
|
||
.background(p.idleTriggerFill, in: Capsule())
|
||
}
|
||
.padding(.horizontal, Self.wellInset)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.contentShape(Capsule())
|
||
}
|
||
.buttonStyle(.plain)
|
||
.accessibilityLabel(spec.title)
|
||
.accessibilityHint(canHoldToTalk ? "Hold to talk, release to finish" : "")
|
||
.hexControlReveal(
|
||
showIdleTrigger,
|
||
reduceMotion: reduceMotion,
|
||
scale: 0.92,
|
||
blur: 2.5,
|
||
anchor: .trailing
|
||
)
|
||
}
|
||
.frame(height: Self.wellHeight)
|
||
.clipShape(Capsule())
|
||
// On the well, not the Dictate button: the button stops taking touches once recording shows.
|
||
.simultaneousGesture(pushToTalk)
|
||
.onChange(of: isPressingWell) { _, pressing in
|
||
guard !pressing else { return }
|
||
// A cancelled press never reaches onEnded; an ended one already reset.
|
||
DispatchQueue.main.async {
|
||
guard pressBegan else { return }
|
||
holdTimer?.cancel()
|
||
hold.cancel()
|
||
pressBegan = false
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The well dictates in the keyboard (a warm session or in-keyboard dictation), so a
|
||
/// hold there can be push to talk; opening Hex or setup stays a tap.
|
||
private var canHoldToTalk: Bool {
|
||
guard model.hasFullAccess, model.stagePhase == .idle || model.stagePhase == .failed else { return false }
|
||
return (model.isWarmAvailable && model.stagePhase != .failed) || model.onLocalDictateTap != nil
|
||
}
|
||
|
||
/// Hold still on Dictate for `HexKeyGesture.holdDuration`: dictation starts with a light
|
||
/// tap, and lifting the finger finishes it as Done does. A quick tap keeps tap to start.
|
||
private var pushToTalk: some Gesture {
|
||
DragGesture(minimumDistance: 0)
|
||
.updating($isPressingWell) { _, pressing, _ in pressing = true }
|
||
.onChanged { value in
|
||
guard pressBegan else {
|
||
pressBegan = true
|
||
holdTookPress = false
|
||
guard canHoldToTalk else { return }
|
||
hold.touchDown(at: value.startLocation)
|
||
holdTimer?.cancel()
|
||
holdTimer = Task { @MainActor in
|
||
try? await Task.sleep(for: .seconds(HexKeyGesture.holdDuration))
|
||
guard !Task.isCancelled, hold.holdElapsed() == .hold else { return }
|
||
holdTookPress = true
|
||
model.onKeyFeedback?()
|
||
handleIdleStageTap()
|
||
}
|
||
return
|
||
}
|
||
if hold.touchMoved(to: value.location) == .openMenu {
|
||
// Moved away: not a hold.
|
||
holdTimer?.cancel()
|
||
}
|
||
}
|
||
.onEnded { _ in
|
||
holdTimer?.cancel()
|
||
holdTimer = nil
|
||
pressBegan = false
|
||
if hold.touchUp() == .release {
|
||
model.onStopTap?()
|
||
// After the button's own touch-up action, which this press must not repeat.
|
||
DispatchQueue.main.async { holdTookPress = false }
|
||
}
|
||
}
|
||
}
|
||
|
||
private func handleIdleStageTap() {
|
||
if !model.hasFullAccess {
|
||
model.onOpenURL?(KeyboardLaunchRequest.setup.url)
|
||
} else if !model.isWarmAvailable || model.stagePhase == .failed {
|
||
if let local = model.onLocalDictateTap {
|
||
local()
|
||
} else {
|
||
model.onOpenURL?(model.coldStartURL)
|
||
}
|
||
} else {
|
||
model.onDictateTap?()
|
||
}
|
||
}
|
||
|
||
// MARK: - Bottom Utility Key Row (42 pt, shadow-grounded keycaps with zero strokes)
|
||
|
||
private var utilityRow: some View {
|
||
let p = palette
|
||
return HStack(spacing: 6) {
|
||
if model.needsGlobeKey {
|
||
GlobeKeyRepresentable(controller: model.inputViewController, isDark: p.isDark)
|
||
.frame(width: 44, height: 42)
|
||
.background(
|
||
p.keyFill,
|
||
in: RoundedRectangle(cornerRadius: HexTheme.controlRadius, style: .continuous)
|
||
)
|
||
.shadow(color: p.keyShadow, radius: 0.5, x: 0, y: 1)
|
||
}
|
||
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
model.onSpace?()
|
||
} label: {
|
||
Text("space")
|
||
.font(.system(size: 15, weight: .regular))
|
||
.foregroundStyle(p.textMuted)
|
||
.frame(maxWidth: .infinity)
|
||
.frame(height: 42)
|
||
.contentShape(RoundedRectangle(cornerRadius: HexTheme.controlRadius, style: .continuous))
|
||
}
|
||
.buttonStyle(KeyboardKeyButtonStyle(palette: p))
|
||
|
||
RepeatDeleteKey(palette: p) {
|
||
model.onKeyFeedback?()
|
||
model.onDelete?()
|
||
}
|
||
|
||
Button {
|
||
model.onKeyFeedback?()
|
||
model.onReturn?()
|
||
} label: {
|
||
Image(systemName: "return")
|
||
.font(.system(size: 15, weight: .medium))
|
||
.foregroundStyle(p.textBase)
|
||
.frame(width: 56, height: 42)
|
||
.contentShape(RoundedRectangle(cornerRadius: HexTheme.controlRadius, style: .continuous))
|
||
}
|
||
.buttonStyle(KeyboardKeyButtonStyle(palette: p))
|
||
.accessibilityLabel(model.returnKeyLabel)
|
||
}
|
||
}
|
||
}
|