feat(keyboard): show dictation progress as a fill on the microphone capsule

This commit is contained in:
2026-10-20 20:05:21 +02:00
parent be56d2210c
commit 1ddbd28371
7 changed files with 296 additions and 1 deletions
@@ -67,6 +67,7 @@ final class KeyboardFlowCoordinator {
private var currentUtteranceId: UUID?
private var currentStartDeadlineAt: TimeInterval?
private var currentUtteranceRequest: FlowUtteranceRequest?
private let polishLatencyCalibration = DictationPolishLatencyCalibration()
private var audioPrimeID: UUID?
private var audioPrimeCancelTask: Task<Void, Never>?
private var editHostConfirmed = false
@@ -471,6 +472,7 @@ final class KeyboardFlowCoordinator {
if state.lastTranscript.isEmpty {
state.lastTranscript = ExtL10n.string("keyboard.flow.transcribing")
}
beginDictationProgressIfNeeded()
startFlowResultWatchdog()
traceState("adoptHostBusy.processing", extra: "session=\(sessionId)")
}
@@ -801,6 +803,7 @@ final class KeyboardFlowCoordinator {
finishLocalCancel()
return
}
state.dictationProgress = nil
let inserted = registerCancelledUtterance(utteranceID, kind: kind)
if !inserted, isAwaitingFlowResult {
state.phase = .processing
@@ -1101,6 +1104,7 @@ final class KeyboardFlowCoordinator {
onAIRecognitionStarted(currentUtteranceId)
} else if currentUtteranceRequest?.isEdit != true {
state.lastTranscript = ExtL10n.string("keyboard.flow.transcribing")
beginDictationProgressIfNeeded()
}
startFlowResultWatchdog()
recomputeMicVoiceAvailability()
@@ -2152,6 +2156,8 @@ final class KeyboardFlowCoordinator {
state.lastTranscript = partial
if result.resolvedUtteranceMode == .aiQuestion {
onAITranscript(partial, result.utteranceId, result.status)
} else if result.status == .rawReady {
markDictationPolishing(characterCount: partial.count)
}
}
default:
@@ -2159,6 +2165,43 @@ final class KeyboardFlowCoordinator {
}
}
// MARK: - Dictation progress
/// Starts the microphone progress fill for a dictation wait. Keeps an
/// existing timeline for the same utterance so re-adoption does not rewind.
private func beginDictationProgressIfNeeded() {
guard let utteranceID = currentUtteranceId,
currentUtteranceRequest?.isAIQuestion != true,
currentUtteranceRequest?.isEdit != true,
state.dictationProgress?.utteranceID != utteranceID else { return }
state.dictationProgress = DictationProgressTimeline(
utteranceID: utteranceID,
transcribingStartedAt: Date().timeIntervalSince1970
)
}
private func markDictationPolishing(characterCount: Int) {
guard var timeline = state.dictationProgress,
timeline.polishingStartedAt == nil else { return }
timeline.markPolishing(
at: Date().timeIntervalSince1970,
characterCount: characterCount,
expectedSeconds: polishLatencyCalibration.expectedSeconds(
forCharacterCount: characterCount
)
)
state.dictationProgress = timeline
}
private func recordDictationPolishLatency() {
guard let timeline = state.dictationProgress,
let polishStart = timeline.polishingStartedAt else { return }
polishLatencyCalibration.record(
observedSeconds: Date().timeIntervalSince1970 - polishStart,
characterCount: timeline.polishCharacterCount
)
}
private func startFlowResultWatchdog(
purpose: ResultWatchdogPurpose = .delivery
) {
@@ -2212,6 +2255,7 @@ final class KeyboardFlowCoordinator {
self.completeAIResult(result)
return
}
self.recordDictationPolishLatency()
self.textInserter.handleFlowTranscript(
TranscriptionDelivery(
text: text,
+36
View File
@@ -1048,6 +1048,9 @@ struct AIKeyboardView: View {
.regular.tint(microphoneTint).interactive(),
in: Capsule()
)
if let timeline = activeDictationProgress {
dictationProgressFill(timeline)
}
microphoneContent
}
.frame(width: primaryButtonWidth, height: primaryButtonHeight)
@@ -1096,11 +1099,41 @@ struct AIKeyboardView: View {
}
}
/// Dictation wait rendered as a fill; AI phases keep their own spinner.
private var activeDictationProgress: DictationProgressTimeline? {
guard state.phase == .processing, !state.aiSession.isBusy else { return nil }
return state.dictationProgress
}
/// Leading-edge fill in the brand colour. The real result swaps the
/// capsule to the solid accent success state, which reads as reaching 100 %.
private func dictationProgressFill(_ timeline: DictationProgressTimeline) -> some View {
TimelineView(.animation) { context in
let fraction = DictationProgressEstimator.fraction(
for: timeline,
at: context.date.timeIntervalSince1970
)
GeometryReader { proxy in
Rectangle()
.fill(palette.accent)
.frame(width: proxy.size.width * fraction)
}
}
.clipShape(Capsule())
.allowsHitTesting(false)
.accessibilityHidden(true)
}
@ViewBuilder
private var ordinaryMicrophoneContent: some View {
switch state.phase {
case .recording:
waveform(color: OSGColor.recordingWaveform)
case .processing where activeDictationProgress != nil:
Image(systemName: "ellipsis")
.font(.system(size: 25, weight: .bold))
.foregroundStyle(OSGColor.fixedLightContent)
.symbolEffect(.variableColor.iterative, options: .repeating)
case .requestingPermissions, .processing:
ProgressView().tint(OSGColor.fixedLightContent)
case .error:
@@ -1146,6 +1179,9 @@ struct AIKeyboardView: View {
switch state.phase {
case .recording:
return palette.recordRed
case .processing where activeDictationProgress != nil:
// Unfilled track of the progress fill.
return palette.accent.opacity(0.35)
case .requestingPermissions, .processing:
return palette.recordRed.opacity(0.72)
case .error, .denied: