hex/ios/HEXMobile/ContentView.swift
Ryan Vogel 9427d069bf
iOS: onboarding follows the desktop flow (#2136)
* iOS: launch screen in the app's first-frame black

The launch screen's LaunchBackground was a dark teal (sRGB 7,17,20, shown
as about 2,17,19) for the ~1.5 s before the app's first frame, which is
HexTheme.bgDeep (0x0A0B0D). It now uses bgDeep's exact components, so
launch hands over to the app without a colour jump.

Verified: built for Simulator; fresh-install launch recorded.

* iOS: onboarding follows the desktop's pages, with the polish fixes

Dax found the iPhone onboarding annoying; Ryan and Dax asked for the
desktop's flow. The three pages (Welcome, Set up Hex, Try the keyboard)
become the desktop's six, in its order and copy style:

1. Setting up Hex: the speech model starts downloading as onboarding
   opens (whichever engine transcriberFactory picks). Before, only home's
   "Set up" started a Parakeet download, so onboarding said "You're all
   set" while home said "Finish setup · 1 step". A launch that found
   Parakeet ended mid-load stays failed (ParakeetLoadGuard) until Try
   again. Moves on after a beat; a failed start holds it with Try again.
2. With Hex: a short demo, a reply dictated with the Hex key. Skip.
3. Let Hex hear you: Microphone and Hex keyboard rows with a one-line
   detail that stays (steady heights) and one-line copy ("With Full
   Access", no mid-phrase wrap). A done row shows only its check (the
   Keyboard row used to keep its Open Settings pill). The pill and the
   page's button say Continue before iOS's prompt (App Review 5.1.1(iv)),
   Continue never needs the microphone, and the page moves on once both
   rows are done. No Skip.
4. Almost ready: only while the model downloads, with its progress;
   Continue without a model only after a failure or 30 s without progress.
5. Your turn: the practice. The field keeps the keyboard up (the old
   page unfocused it ~150 ms after the first letter) and counts once a
   dictation arrives (lastInsertedResultID changes) or three words are
   typed with Full Access. An empty dictation says "Hex didn't hear any
   words. Try again a little louder." on this page only; the keyboard
   notes it in the App Group (keyboard.lastEmptyDictationAt) and still
   shows nothing itself. Skip.
6. Hex is ready: only once every step (model included) is done, else
   "Almost ready" with what's left. Start using Hex never waits.

The download's pill (language and percent) sits in the top bar until the
model is ready; Skip shows on the demo and the practice only. The page
counter and Back are gone, as on the desktop. Saved pages from earlier
builds resume on their new equivalents. Snapshot scenes are renamed
onboarding-setup/-demo/-permissions(-done)/-waiting/-failed/-try(-done,
-empty,-off)/-ready, plus onboarding-tour. zh-Hans strings added.

Verified: xcodebuild test on an iOS 27.2 simulator (385 tests, 0
failures, 1 skipped), including new KeyboardBridgeTests for the empty
dictation note, the practice rule, and saved-page migration; English and
Chinese snapshots of every page; a fresh-install recording.

* iOS onboarding: the practice field takes focus when Hex comes back

Coming back from Settings with both permission rows done moves straight
on to Your turn, but the page opened before Hex was active again, so its
field never took focus and the keyboard stayed down. The page now
focuses the field again when the scene becomes active (unless it already
has focus).

Verified: fresh-install recording on an iOS 27.2 simulator: back from
Settings, Your turn opens with the keyboard up.

* iOS: Ready waits for the model the Hex key itself dictates with

On iOS 26 the keyboard transcribes inside itself with Apple Speech,
whatever Hex uses. When Hex dictates with Parakeet (an English iPhone
without SpeechTranscriber, or a cached Parakeet model), onboarding and
home said Ready while the keyboard's Apple Speech model wasn't there,
and the first Hex key tap said "Open Hex to download English."

KeyboardSpeechPreparation (owned by ContentView, started at launch)
prepares the keyboard's engine:
- Hex dictates with Apple Speech: Hex's model is the same one, so it
  follows Hex's.
- Otherwise it resolves the keyboard's language (AppleSpeechLanguages,
  as the keyboard does) and installs it with AppleSpeechAssets.install,
  the keyboard's own call, which also clears keyboardNeedsHex.
- Apple has no model for it on this iPhone, or Speech lists nothing: it
  marks keyboardNeedsHex, so the Hex key opens Hex (which dictates with
  its own model) from the first tap instead of after a failed one.
  Re-marked at each launch, as the mark lasts a day.
- A failed download (connection) shows on onboarding with Try again.

Why not have the keyboard use Hex's Parakeet: it can't load within a
keyboard extension's memory limit, and handing every dictation to Hex
leaves the app being typed in each time. In-keyboard Apple Speech stays
the path, with the handoff only where Apple has no model.

Onboarding's model stage combines both (OnboardingModel.combine): ready
only when both are, a failure first, fractions averaged only when the
keyboard actually downloads. Home's status line and the setup sheet's
Speech model row count it the same way.

Verified: xcodebuild test, iOS 27.2 simulator: 386 tests, 0 failures,
1 skipped (new: the combine rules). In the simulator (no Apple Speech
assets: "not subscribed to assistant.en_US") it marks the handoff and
onboarding goes on to Parakeet's Ready; a real iPhone must confirm the
Apple Speech download path.

* iOS onboarding: the desktop's motion, the pill flying between places

The desktop onboarding's motion (onboarding_kit/landing.rs, pill.rs):
- Each page's parts roll in 8 pt with a fade on a 0.34 s zero-bounce
  spring, 0.05 s apart (onboardingStagger), and the leaving page lifts
  away 8 pt on 0.26 s. Replaces the old 28 pt sideways page slide.
- The download pill flies from under Setting up Hex to the top bar and
  stays there across pages (matchedGeometryEffect), on the desktop's
  0.42 s flight spring.
- Reduce Motion: parts fade without moving or staggering, the pill
  cross-fades, pages fade.
Stills (--snap-state) show every part at once.

"Watch another one" stays off: on the desktop it fills a long download
on Almost ready; on iPhone the demo is ~6 s and moves on by itself, so a
replay control would only add a label.

Verified: xcodebuild test (386, 0 failures, 1 skipped); fresh-install
recording on an iOS 27.2 simulator, frames checked at each page change.

* iOS onboarding: steady progress and an untruncated pill in flight

Two things the recording showed:
- In the simulator the keyboard's Apple Speech download starts, then
  can't be had and hands off to Hex; that short attempt still counted as
  half of onboarding's progress, so the percent jumped from 0% to 50%.
  A handoff no longer counts as a download.
- The pill flew with its frame, so the small pill's language truncated
  ("Engli… 2%") for about half a second after landing. It now flies by
  position only; each pill keeps its own size.

Verified: fresh-install recording on an iOS 27.2 simulator, frames at
15 fps around the Setting up Hex to demo change, and the percent climbing
from 0% in steps.
2026-11-21 03:27:38 +00:00

801 lines
32 KiB
Swift

import SwiftUI
import UIKit
private struct AppRecordingClockLabel: View {
let dictation: DictationController
var body: some View {
Text(HexTheme.formatClock(TimeInterval(dictation.recordingWholeSeconds)))
.font(.system(size: 13, weight: .medium))
.monospacedDigit()
.foregroundStyle(HexTheme.textBase)
}
}
private struct AppWarmRemainingLabel: View {
let dictation: DictationController
var body: some View {
Text(dictation.keyboardSessionDisplayRemaining)
.font(.system(size: 13, weight: .medium))
.monospacedDigit()
.foregroundStyle(HexTheme.textMuted)
}
}
struct ContentView: View {
private enum AppStageMode: Equatable {
case idle
case recording
case transcribing
case preparing
case permission
case failed
}
@Environment(\.openURL) private var openURL
@Environment(\.scenePhase) private var scenePhase
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@Bindable var dictation: DictationController
@Binding var openedFromKeyboard: Bool
@Binding var showSetup: Bool
@State private var setupMonitor = KeyboardSetupMonitor()
@State private var keyboardSpeech = KeyboardSpeechPreparation()
@State private var onboardingPage = OnboardingProgress.launchPage
@State private var settingsPath = SettingsSnapshotFixture.initialPath
@State private var showAcknowledgements = false
private static let stageWellHeight: CGFloat = 48
private static let stageControlHeight: CGFloat = 36
private static let stageInset: CGFloat = 6
private static let stageCancelWidth: CGFloat = 36
private static let stageDoneWidth: CGFloat = 70
private static var simulatesHomeButtonDevice: Bool {
let args = ProcessInfo.processInfo.arguments
guard let idx = args.firstIndex(of: "--snap-state"), idx + 1 < args.count else { return false }
return args[idx + 1] == "handoff-no-home-bar"
}
var body: some View {
ZStack {
HexTheme.bgDeep
.ignoresSafeArea()
if openedFromKeyboard {
keyboardHandoffView
.transition(.opacity)
} else if onboardingPage != nil {
OnboardingView(dictation: dictation, monitor: setupMonitor, keyboardSpeech: keyboardSpeech, page: $onboardingPage)
.transition(.opacity)
} else {
standaloneView
.transition(.opacity)
}
}
.preferredColorScheme(.dark)
.task { keyboardSpeech.start { dictation.engineName } }
.animation(HexMotion.reducedFade, value: openedFromKeyboard)
.animation(HexMotion.reducedFade, value: onboardingPage == nil)
}
private var appStageMode: AppStageMode {
switch dictation.phase {
case .recording:
.recording
case .transcribing:
.transcribing
case .preparingModel:
.preparing
case .requestingPermission:
.permission
case .failed:
.failed
case .modelRequired, .ready:
.idle
}
}
private var handoffScopePhase: DotScopePhase {
switch dictation.phase {
case .recording:
.recording
case .transcribing:
.processing
default:
.hidden
}
}
private var stageScopePhase: DotScopePhase {
switch dictation.phase {
case .recording:
.recording
case .transcribing:
.processing
case .preparingModel, .requestingPermission:
.hidden
case .failed:
.failed
case .modelRequired, .ready:
.idle
}
}
private var setupStepsLeft: Int {
// The Hex key's own model counts: Ready means dictation works in other apps.
setupMonitor.stepsLeft(modelReady: dictation.isModelReady && keyboardSpeech.isReady)
}
private var hasTranscript: Bool {
!dictation.transcript.isEmpty
}
// MARK: - Standalone App
private var standaloneView: some View {
NavigationStack(path: $settingsPath) {
homeView
.navigationTitle("Hex")
.toolbar(.hidden, for: .navigationBar)
.navigationDestination(for: SettingsPage.self) { page in
settingsPage(page)
}
}
.tint(HexTheme.textBase)
.sheet(isPresented: $showSetup) {
SetupSheet(dictation: dictation, monitor: setupMonitor, keyboardSpeech: keyboardSpeech)
}
.task {
while !Task.isCancelled {
setupMonitor.refresh()
try? await Task.sleep(for: .seconds(1))
}
}
.onChange(of: scenePhase) { _, phase in
if phase == .active {
setupMonitor.refresh()
}
}
}
/// Fits one screen: the header and its status line, Dictate, and the four pages.
private var homeView: some View {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
VStack(alignment: .leading, spacing: 2) {
headerRow
SetupStatusRow(stepsLeft: setupStepsLeft) {
showSetup = true
}
}
stageBar
if hasTranscript {
transcriptCard
.transition(.opacity.combined(with: .scale(scale: 0.98)))
}
if case .failed(let message) = dictation.phase {
errorBanner(message, offer: dictation.languageOffer)
.transition(.opacity.combined(with: .scale(scale: 0.98)))
} else if let notice = dictation.languageNotice {
noticeBanner(notice)
.transition(.opacity.combined(with: .scale(scale: 0.98)))
}
SettingsCard {
ForEach(Array(SettingsPage.allCases.enumerated()), id: \.element) { index, page in
if index > 0 {
SettingsDivider(leading: 50)
}
NavigationLink(value: page) {
SettingsLinkLabel(title: page.title, icon: page.icon)
}
.buttonStyle(.plain)
}
}
.padding(.top, 8)
SettingsCard {
Button {
if let url = URL(string: UIApplication.openSettingsURLString) {
openURL(url)
}
} label: {
SettingsLinkLabel(title: String(localized: "Keyboard settings"), icon: "gearshape", trailingIcon: "arrow.up.right")
}
.buttonStyle(.plain)
SettingsDivider(leading: 50)
Button {
OnboardingProgress.save(.setup)
onboardingPage = .setup
} label: {
SettingsLinkLabel(title: String(localized: "Run setup again"), icon: "arrow.counterclockwise")
}
.buttonStyle(.plain)
}
// The privacy policy link App Review 5.1.1(i) asks for, with support and the
// licence notices.
SettingsCard {
Button {
SafariSheet.present(HexLinks.privacyPolicy)
} label: {
SettingsLinkLabel(title: String(localized: "Privacy policy"), icon: "hand.raised", trailingIcon: "arrow.up.right")
}
.buttonStyle(.plain)
SettingsDivider(leading: 50)
Button {
SafariSheet.present(HexLinks.support)
} label: {
SettingsLinkLabel(title: String(localized: "Support"), icon: "questionmark.circle", trailingIcon: "arrow.up.right")
}
.buttonStyle(.plain)
SettingsDivider(leading: 50)
Button {
showAcknowledgements = true
} label: {
SettingsLinkLabel(title: String(localized: "Acknowledgements"), icon: "doc.text")
}
.buttonStyle(.plain)
.sheet(isPresented: $showAcknowledgements) {
AcknowledgementsView()
}
}
}
.frame(maxWidth: 500)
.padding(.horizontal, 20)
.padding(.top, 14)
.padding(.bottom, 28)
.frame(maxWidth: .infinity)
.animation(HexMotion.stageAnimation(reduceMotion: reduceMotion), value: appStageMode)
.animation(HexMotion.settleAnimation(reduceMotion: reduceMotion), value: dictation.keyboardSessionEnabled)
.animation(HexMotion.settleAnimation(reduceMotion: reduceMotion), value: hasTranscript)
.animation(HexMotion.settleAnimation(reduceMotion: reduceMotion), value: setupStepsLeft)
}
.scrollBounceBehavior(.basedOnSize)
.scrollDismissesKeyboard(.interactively)
.background(HexTheme.bgDeep.ignoresSafeArea())
}
@ViewBuilder
private func settingsPage(_ page: SettingsPage) -> some View {
switch page {
case .dictation:
DictationSettingsPage(dictation: dictation)
case .keyboard:
KeyboardSettingsPage()
case .privacy:
PrivacySettingsPage()
case .experiments:
ExperimentsSettingsPage()
}
}
private var headerRow: some View {
let showRecordingPill = appStageMode == .recording
let showWarmPill = appStageMode != .recording
&& appStageMode != .transcribing
&& dictation.keyboardSessionEnabled
return HStack(alignment: .center, spacing: 9) {
HexMarkView(
size: 20,
color: showRecordingPill ? HexTheme.warmPearl : HexTheme.textBase
)
Text("Hex")
.font(.system(size: 17, weight: .semibold))
.tracking(-0.2)
.foregroundStyle(HexTheme.textBase)
Spacer()
ZStack(alignment: .trailing) {
HStack(spacing: 6) {
HexStatusDot(color: HexTheme.danger, size: 6.5)
AppRecordingClockLabel(dictation: dictation)
}
.padding(.horizontal, 10)
.frame(height: 28)
.background(Color.white.opacity(0.07), in: Capsule())
.hexTrayReveal(showRecordingPill, reduceMotion: reduceMotion, offsetY: 2)
Button {
withAnimation(HexMotion.settleAnimation(reduceMotion: reduceMotion)) {
dictation.endWarmSession()
}
} label: {
HStack(spacing: 6) {
HexStatusDot(color: HexTheme.warmMic, size: 6)
AppWarmRemainingLabel(dictation: dictation)
}
.padding(.horizontal, 10)
.frame(height: 28)
.background(Color.white.opacity(0.07), in: Capsule())
}
.buttonStyle(.plain)
.accessibilityLabel("End warm microphone session")
.hexTrayReveal(showWarmPill, reduceMotion: reduceMotion, offsetY: 2)
}
}
.frame(height: 30)
}
private var stageBar: some View {
let mode = appStageMode
let showScope = stageScopePhase != .hidden
let showRecordingControls = mode == .recording
let showIdleTrigger = mode == .idle || mode == .failed
let showPreparing = mode == .preparing
let showPermission = mode == .permission
let idleWidth: CGFloat = (dictation.phase == .modelRequired || mode == .failed) ? 90 : 96
let leadInset: Float = showRecordingControls
? Float(Self.stageInset + Self.stageCancelWidth + 6)
: 0.0
let trailInset: Float = if showRecordingControls {
Float(Self.stageInset + Self.stageDoneWidth + 6)
} else if showIdleTrigger {
Float(Self.stageInset + idleWidth + 6)
} else {
0.0
}
return ZStack {
HexRecessedWell(isDark: true)
// Permanently mounted so phosphor trails and inset springs never reset mid-session.
DotScopeView(
phase: stageScopePhase,
tuning: .bar(leadingInset: leadInset, trailingInset: trailInset, fade: 28.0, spacing: 3.5),
drawPlate: false,
loudnessProvider: { [weak dictation] in dictation?.loudnessDB ?? -120.0 }
)
.clipShape(Capsule())
.opacity(showScope ? 1.0 : 0.0)
.allowsHitTesting(false)
HStack(spacing: 10) {
Button {
withAnimation(HexMotion.stageAnimation(reduceMotion: reduceMotion)) {
dictation.cancelCurrentJob()
}
} label: {
Image(systemName: "xmark")
.font(.system(size: 13, weight: .semibold))
.foregroundStyle(HexTheme.textMuted)
.frame(width: Self.stageCancelWidth, height: Self.stageControlHeight)
.background(Color.white.opacity(0.085), in: Circle())
.frame(minWidth: 44, minHeight: Self.stageWellHeight, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel("Cancel")
.hexControlReveal(
showRecordingControls,
reduceMotion: reduceMotion,
scale: 0.90,
blur: 2.5,
anchor: .leading
)
Spacer(minLength: 0)
Button {
withAnimation(HexMotion.stageAnimation(reduceMotion: reduceMotion)) {
dictation.finishRecording()
}
} label: {
Text("Done")
.font(.system(size: 15, weight: .semibold))
.foregroundStyle(HexTheme.textBase)
.frame(width: Self.stageDoneWidth, height: Self.stageControlHeight)
.background(Color.white.opacity(0.15), in: Capsule())
.frame(minHeight: Self.stageWellHeight, alignment: .trailing)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.hexControlReveal(
showRecordingControls,
reduceMotion: reduceMotion,
scale: 0.90,
blur: 2.5,
anchor: .trailing
)
}
.padding(.horizontal, Self.stageInset)
HStack(spacing: 12) {
Text("Preparing model…")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
ProgressView(value: dictation.modelPreparation.fractionCompleted)
.tint(HexTheme.textBase)
}
.padding(.horizontal, 18)
.hexControlReveal(showPreparing, reduceMotion: reduceMotion, scale: 0.96, blur: 2.0)
Text("Allow microphone access…")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
.hexControlReveal(showPermission, reduceMotion: reduceMotion, scale: 0.96, blur: 2.0)
Button {
withAnimation(HexMotion.stageAnimation(reduceMotion: reduceMotion)) {
dictation.performPrimaryAction()
}
} label: {
HStack(spacing: 0) {
Spacer(minLength: 0)
HStack(spacing: 6) {
Image(systemName: primaryActionIcon)
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
Text(primaryActionLabel)
.font(.system(size: 15, weight: .medium))
.foregroundStyle(HexTheme.textBase)
}
.frame(width: idleWidth, height: Self.stageControlHeight)
.background(Color.white.opacity(0.09), in: Capsule())
}
.padding(.horizontal, Self.stageInset)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.contentShape(Capsule())
}
.buttonStyle(.plain)
.hexControlReveal(
showIdleTrigger,
reduceMotion: reduceMotion,
scale: 0.92,
blur: 2.5,
anchor: .trailing
)
}
.frame(height: Self.stageWellHeight)
.clipShape(Capsule())
}
private var transcriptCard: some View {
VStack(alignment: .leading, spacing: 12) {
Text(dictation.transcript)
.font(.system(size: 17))
.lineSpacing(3)
.foregroundStyle(HexTheme.textBase)
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
HStack(spacing: 6) {
Spacer()
Button(dictation.copied ? String(localized: "Copied") : String(localized: "Copy")) {
dictation.copyTranscript()
}
.font(.system(size: 13, weight: .medium))
.foregroundStyle(dictation.copied ? HexTheme.success : HexTheme.textBase)
.padding(.horizontal, 12)
.frame(height: 28)
.background(Color.white.opacity(0.09), in: Capsule())
Button("Clear") {
withAnimation(HexMotion.settleAnimation(reduceMotion: reduceMotion)) {
dictation.clearTranscript()
}
}
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
.padding(.horizontal, 12)
.frame(height: 28)
.background(Color.white.opacity(0.055), in: Capsule())
}
.buttonStyle(.plain)
}
.padding(16)
.background(
HexTheme.bgBase,
in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous)
)
}
// MARK: - Cold-Start Handoff Screen (unified bar + home-indicator swipe cue)
private var keyboardHandoffView: some View {
GeometryReader { geo in
let bottomInset: CGFloat = Self.simulatesHomeButtonDevice ? 0 : geo.safeAreaInsets.bottom
let hasHomeIndicator = bottomInset > 0
let mode = appStageMode
let showScope = handoffScopePhase != .hidden
let showRecordingControls = mode == .recording
let showFailedRetry = mode == .failed
let showPreparing = mode == .preparing
let status = handoffStatusLine
let showHomeSwipeCue = hasHomeIndicator && (showRecordingControls || dictation.keyboardSessionEnabled)
let handoffWellHeight: CGFloat = 52
let handoffControlHeight: CGFloat = 38
let handoffInset: CGFloat = 7
let handoffCancelWidth: CGFloat = 38
let handoffDoneWidth: CGFloat = 74
let leadInset: Float = showRecordingControls
? Float(handoffInset + handoffCancelWidth + 6)
: 0.0
let trailInset: Float = showRecordingControls
? Float(handoffInset + handoffDoneWidth + 6)
: 0.0
ZStack(alignment: .bottom) {
VStack(spacing: 0) {
HStack(alignment: .center, spacing: 9) {
HexMarkView(
size: 20,
color: showRecordingControls ? HexTheme.warmPearl : HexTheme.textBase
)
Text("Hex")
.font(.system(size: 17, weight: .semibold))
.tracking(-0.2)
.foregroundStyle(HexTheme.textBase)
Spacer()
Button("Settings") {
openedFromKeyboard = false
}
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
.padding(.horizontal, 12)
.frame(height: 30)
.background(Color.white.opacity(0.07), in: Capsule())
.buttonStyle(.plain)
}
Spacer()
// Centered unified dictation stage platter
VStack(spacing: 14) {
ZStack {
HStack(spacing: 6) {
HexStatusDot(color: HexTheme.danger, size: 6.5)
AppRecordingClockLabel(dictation: dictation)
}
.padding(.horizontal, 11)
.frame(height: 26)
.background(Color.white.opacity(0.06), in: Capsule())
.hexTrayReveal(showRecordingControls, reduceMotion: reduceMotion, offsetY: 2)
Text(status ?? " ")
.font(.system(size: 15, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
.multilineTextAlignment(.center)
.lineLimit(2)
.hexTrayReveal(!showRecordingControls && status != nil, reduceMotion: reduceMotion, offsetY: 2)
}
.frame(minHeight: 28)
ZStack {
ZStack {
HexRecessedWell(isDark: true)
DotScopeView(
phase: handoffScopePhase,
tuning: .bar(leadingInset: leadInset, trailingInset: trailInset, fade: 30.0, spacing: 3.5),
drawPlate: false,
loudnessProvider: { [weak dictation] in dictation?.loudnessDB ?? -120.0 }
)
.clipShape(Capsule())
.allowsHitTesting(false)
HStack(spacing: 10) {
Button {
withAnimation(HexMotion.stageAnimation(reduceMotion: reduceMotion)) {
dictation.cancelCurrentJob()
}
} label: {
Image(systemName: "xmark")
.font(.system(size: 13, weight: .semibold))
.foregroundStyle(HexTheme.textMuted)
.frame(width: handoffCancelWidth, height: handoffControlHeight)
.background(Color.white.opacity(0.085), in: Circle())
.frame(minWidth: 44, minHeight: handoffWellHeight, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel("Cancel")
.hexControlReveal(
showRecordingControls,
reduceMotion: reduceMotion,
scale: 0.90,
blur: 2.5,
anchor: .leading
)
Spacer(minLength: 0)
Button {
withAnimation(HexMotion.stageAnimation(reduceMotion: reduceMotion)) {
dictation.finishRecording()
}
} label: {
Text("Done")
.font(.system(size: 15, weight: .semibold))
.foregroundStyle(HexTheme.textBase)
.frame(width: handoffDoneWidth, height: handoffControlHeight)
.background(Color.white.opacity(0.15), in: Capsule())
.frame(minHeight: handoffWellHeight, alignment: .trailing)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.hexControlReveal(
showRecordingControls,
reduceMotion: reduceMotion,
scale: 0.90,
blur: 2.5,
anchor: .trailing
)
}
.padding(.horizontal, handoffInset)
}
.frame(maxWidth: .infinity)
.frame(height: handoffWellHeight)
.clipShape(Capsule())
.hexControlReveal(showScope, reduceMotion: reduceMotion, scale: 0.96, blur: 0.0)
VStack(spacing: 12) {
ProgressView(value: dictation.modelPreparation.fractionCompleted)
.tint(HexTheme.textBase)
.frame(maxWidth: 200)
.hexControlReveal(showPreparing, reduceMotion: reduceMotion, scale: 0.96, blur: 2.0)
Button("Try again") {
withAnimation(HexMotion.stageAnimation(reduceMotion: reduceMotion)) {
dictation.prepareModel()
}
}
.font(.system(size: 15, weight: .semibold))
.foregroundStyle(HexTheme.textBase)
.padding(.horizontal, 18)
.frame(height: 36)
.background(Color.white.opacity(0.15), in: Capsule())
.buttonStyle(.plain)
.hexControlReveal(showFailedRetry, reduceMotion: reduceMotion, scale: 0.94, blur: 2.5)
}
.hexControlReveal(showPreparing || showFailedRetry, reduceMotion: reduceMotion, scale: 0.96, blur: 2.5)
}
.frame(minHeight: handoffWellHeight)
}
.frame(maxWidth: .infinity)
Spacer()
}
.frame(maxWidth: 500)
.padding(.horizontal, 20)
.padding(.top, 14)
.frame(maxWidth: .infinity, maxHeight: .infinity)
// Full-bottom-width Home-Indicator return cue (21 pt flush with screen bottom => center 10.5 pt
// above bottom edge, aligned dead-on the iOS Home Indicator pill across the full screen width).
if hasHomeIndicator {
VStack(spacing: 3) {
Text("Swipe bar to return")
.font(.system(size: 11.5, weight: .medium))
.tracking(0.1)
.foregroundStyle(HexTheme.textFaint)
.opacity(showHomeSwipeCue ? 1 : 0)
DotSwipeCueView(isActive: showHomeSwipeCue)
.frame(width: geo.size.width, height: 21)
}
.allowsHitTesting(false)
.offset(y: bottomInset)
}
}
.animation(HexMotion.stageAnimation(reduceMotion: reduceMotion), value: mode)
.animation(HexMotion.settleAnimation(reduceMotion: reduceMotion), value: showHomeSwipeCue)
}
.persistentSystemOverlays(.hidden)
}
// MARK: - Pieces
/// `offer`: a language (BCP 47) that works here, when the one chosen doesn't yet.
private func errorBanner(_ message: String, offer: String? = nil) -> some View {
HStack(spacing: 10) {
Image(systemName: "exclamationmark.circle.fill")
.font(.system(size: 15))
.foregroundStyle(HexTheme.danger)
Text(message)
.font(.system(size: 13))
.foregroundStyle(HexTheme.textBase)
Spacer(minLength: 0)
if let offer {
Button("Use \(SpeechModelUnavailable.interfaceName(offer))") {
dictation.useOfferedLanguage()
}
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textBase)
.padding(.horizontal, 12)
.frame(height: 28)
.background(Color.white.opacity(0.09), in: Capsule())
.buttonStyle(.plain)
.fixedSize()
}
}
.padding(14)
.background(
HexTheme.dangerSurface,
in: RoundedRectangle(cornerRadius: 14, style: .continuous)
)
}
private func noticeBanner(_ message: String) -> some View {
HStack(spacing: 10) {
Image(systemName: "info.circle.fill")
.font(.system(size: 15))
.foregroundStyle(HexTheme.textMuted)
Text(message)
.font(.system(size: 13))
.foregroundStyle(HexTheme.textBase)
Spacer(minLength: 0)
}
.padding(14)
.background(
Color.white.opacity(0.055),
in: RoundedRectangle(cornerRadius: 14, style: .continuous)
)
}
private var primaryActionLabel: String {
switch dictation.phase {
case .modelRequired, .failed:
String(localized: "Set up")
default:
String(localized: "Dictate")
}
}
private var primaryActionIcon: String {
switch dictation.phase {
case .modelRequired, .failed:
"arrow.down"
default:
"mic.fill"
}
}
private var handoffStatusLine: String? {
if let notice = dictation.handoffNotice {
return notice
}
switch dictation.phase {
case .failed(let message):
return message
case .requestingPermission:
return String(localized: "Allow microphone access")
case .preparingModel:
return String(localized: "Preparing model…")
case .transcribing:
return String(localized: "Transcribing…")
case .ready where dictation.keyboardSessionEnabled:
return String(localized: "Ready")
default:
return nil
}
}
}
#Preview {
ContentView(
dictation: DictationController.shared,
openedFromKeyboard: .constant(false),
showSetup: .constant(false)
)
}