// A chat's input bar with a microphone — the composer every conversation on// this phone mounts, so a mic added to one is a mic added to all of them.//// The message thread (`/messages/[thread]`) and a session's chat// (`/sessions/[id]`) each drew their own field and arrow. Two copies of one// bar is two places a mic has to be added and two ways for them to drift, so// the bar is one view over the host's DRAFT: the host keeps the words and the// send (they are the host's model's — the thread persists its draft as a synced// column, the session does not), and the bar owns the microphone.//// ## Tap to talk, and the words arrive as you speak//// The mic is a TAP, never a hold — the dictation spec's rule, which every mic// in this app keeps. Tap it, speak, watch the transcript stream into the field;// tap ✓ to keep the words or ✕ to discard them. It is the Claude app's own// composer, as asked for: a mic beside the send arrow, live text, on-device// recognition (`SystemSpeechRecogniser`).//// **The mic and the arrow are BOTH drawn** — the rule this bar had first and// the capture dock has now taken too (`ComposerCard.swift`). A chat message is// often typed halfway and then spoken — a name you cannot spell, a sentence too// long for thumbs — so the mic stays reachable with words in the field, and the// dictation SPLICES onto them (`BodyDictation`, the note editor's engine, with// no cursor: the words go on the end). The arrow is dimmed rather than hidden// while there is nothing to send.//// ## The box is the app's, not this bar's//// The card, its one control row, how far the words grow and the transcript that// follows the newest of them are all `ComposerCard.swift`, shared with the// capture dock. This file keeps what is a CHAT's: the host's draft, the send it// was given, and the dictation engine it builds on arrival.//// ## What the engine gives this bar for free//// `BodyDictation` over `DictationModel` — the seven named endings, the restart// ladder, the interruption rule (a phone call ENDS the dictation with its// words, never empties the field), the words landing VERBATIM, and the// sentence every wordless ending owes. This file adds no rule of its own about speech; it// draws what those two decide and reads the voice dictionary once// (`VoiceBias`) so the person's own proper nouns bias the recogniser here as// they do on the keyboard.//// The engine is built on ARRIVAL, not in `init`, for the note editor's reason:// `DictationModel.live()` opens an audio engine the moment it exists, and a// view's `init` runs wherever a `View` value is described. And it is RELEASED// when the bar goes away, so a conversation left mid-sentence gives the// microphone back rather than keeping the orange dot lit over the next screen.importCottonKitimportSwiftUIpublicstructChatComposerBar: View { @Binding privatevar draft: Stringprivatelet placeholder: String/// The host's own gate on send — its length rule, its in-flight flag. The/// bar ANDs it with "the field is not dictation's".privatelet canSend: Bool/// The accessibility-identifier stem: `thread` → `thread-composer-input`,/// `thread-send`, `thread-mic`, and so on.privatelet identifier: Stringprivatelet onSend: () -> Void @State privatevar engine: DictationModel? @State privatevar speech: BodyDictation?/// The open database, from the environment rather than from the host:/// the bar is a View, and a View is not handed a database. The dictation/// engine is built from it once the bar is on screen; a bar mounted with/// none — a preview, a test — draws its field and no microphone. @Environment(\.cottonCore) privatevar database @Environment(\.cottonHaptics) privatevar haptics @Environment(ToastCenter.self) privatevar toasts/// The column this bar stands in, measured by the host — see/// `ComposerCard.swift`'s header on why the cap cannot be a constant. @Environment(\.cottonComposerColumn) privatevar columnpublicinit( draft: Binding<String>, placeholder: String, canSend: Bool, identifier: String, onSend: @escaping () -> Void ) { _draft = draftself.placeholder = placeholderself.canSend = canSendself.identifier = identifierself.onSend = onSend }privatevar isLive: Bool { speech?.isActive ?? false }privatevar isFinishing: Bool { speech?.isFinishing ?? false }privatevar controls: ComposerControls {ComposerControls.decide(isLive: isLive, isFinishing: isFinishing, canSend: canSend) }publicvar body: someView {ComposerCard( controls: controls, identifier: identifier, waveform: engine?.waveform ?? [], isListening: engine?.isListening ?? false, clock: engine?.clock ?? "", canDictate: speech != nil, onDictate: { haptics.press()Task {guardlet database else { return }await speech?.start(core: database) } }, onSend: onSend, onCancelDictation: { haptics.press() speech?.cancel() }, onFinishDictation: { haptics.press()Task { await speech?.finish() } } ) { field } .padding(.horizontal, 12) .padding(.vertical, 8)// A tap on Send, on the microphone or on the field's own edge is not// a tap OUTSIDE the field: the frame's dismissal skips this bar. .keyboardStaysOnTap() .task {// Once, on arrival. `@State` survives every redraw, so a second// recogniser is never built for the same bar.guardlet database else { return }if speech == nil { attachDictation(database) } }// The microphone goes back with the screen, whatever the session was// doing — the dismissal nobody tapped. .onDisappear { engine?.release() } }// ── The words ─────────────────────────────────────────────────────────/// What the field shows: the live splice while dictating, the draft/// otherwise. Typing while the words are dictation's is refused rather/// than raced — the editor is not even mounted then, and this guard is the/// backstop for a keyboard that was already up.privatevar fieldBinding: Binding<String> {Binding( get: { speech?.displayBody ?? draft }, set: { next inguard !isLive else { return } draft = next } ) }privatevar field: someView {ComposerWords( shown: speech?.displayBody ?? draft, placeholder: placeholder, isDictating: isLive, cap: ComposerGrowth.fieldCap(column: column), identifier: identifier ) {TextEditor(text: fieldBinding) .scrollContentBackground(.hidden) .paperBody(composerTypeSize) .accessibilityLabel(placeholder) .accessibilityIdentifier("\(identifier)-composer-input") } }// ── The engine ────────────────────────────────────────────────────────/// The dictation host, wired to the draft. Every ending becomes a sentence/// through `VoiceTake` — nil for the two that need none, a dictation that/// landed its words and a discard the person asked for.privatefunc attachDictation(_ database: any CottonCalling) {let engine = DictationModel.live()let words = $draftlet toasts = toasts speech = BodyDictation( dictation: engine, haptics: haptics, body: { words.wrappedValue }, setBody: { words.wrappedValue = $0 }, onEnded: { outcome iniflet sentence = VoiceTake.dictationMessage(outcome) { toasts.show(sentence, tone: .error) } } )self.engine = engine }}