// The shared composer's values (owner's direction, 19 Nov 2026): what an app// hands the one input bar every Cotton app writes with. The composer knows no// app: its words, the chip and its menu, the send rule and the colours all// come in through these, from the app's core answers and its theme.//// `composer.md` in the lane folder says how an app wires it; in short://// Composer(text: $text, words: words, pick: pick, style: style,// dictation: voice, returnSends: false) { send() }// .onPick { choice in … }importSwiftUI/// Every word the composer says, handed in by the app (its core's lines).publicstructComposerLines: Equatable, Sendable {/// The text area's placeholder.publicvar hint: String/// The send button's spoken name.publicvar send: String/// The microphone, idle.publicvar mic: String/// The stop button while dictating: the words stay, typing resumes.publicvar stop: String/// The ✕ while dictating: what this dictation added goes.publicvar cancel: String/// Said while the microphone listens (VoiceOver's value for the waveform).publicvar listening: Stringpublicinit(hint: String, send: String, mic: String, stop: String, cancel: String, listening: String) {self.hint = hintself.send = sendself.mic = micself.stop = stopself.cancel = cancelself.listening = listening }}/// The pick chip and the menu it opens: what the next words are (a speaker,/// a recipient, a kind). The app's core orders every row; the composer draws/// them in the order given and never sorts.publicstructComposerPick: Equatable, Sendable {publicstructOption: Equatable, Identifiable, Sendable {publiclet id: String/// The full name the row and the chip show.publiclet name: String/// The dot beside the name; nil draws none.publiclet tint: Color?publicinit(id: String, name: String, tint: Color? = nil) {self.id = idself.name = nameself.tint = tint } }publicstructSection: Equatable, Identifiable, Sendable {publiclet id: String/// An optional heading over the section's rows.publiclet title: String?publiclet options: [Option]publicinit(id: String, title: String? = nil, options: [Option]) {self.id = idself.title = titleself.options = options } }/// The option the chip shows and the menu ticks.publicvar chosen: Stringpublicvar sections: [Section]/// The last row's words ("New character…"); nil draws no such row.publicvar new: String?/// The chip's spoken name before its value ("Next line").publicvar label: Stringpublicinit(chosen: String, sections: [Section], new: String? = nil, label: String) {self.chosen = chosenself.sections = sectionsself.new = newself.label = label }/// `chosen` while the "new…" row is picked: the chip shows that row's words.publicstaticlet newID = "composer-pick-new"/// The chosen option, as the chip shows it.publicvar current: Option? {if chosen == Self.newID, let new { returnOption(id: Self.newID, name: new) }for section in sections {iflet found = section.options.first(where: { $0.id == chosen }) { return found } }returnnil }}/// What a tap in the pick menu chose.publicenumComposerPickChoice: Equatable, Sendable {case option(String)case new}/// The composer's colours and its face, from the app's theme.publicstructComposerStyle: Sendable {/// The rounded panel's fill.publicvar panel: Color/// The panel's hairline rim.publicvar rim: Color/// The words.publicvar ink: Color/// The placeholder, tentative words and resting controls.publicvar faint: Color/// The chip's fill.publicvar chip: Color/// The microphone and send's fill, and the waveform.publicvar accent: Color/// What is drawn on the accent.publicvar onAccent: Color/// A held voice message slid away to be thrown out.publicvar danger: Color/// The face the words are written in.publicvar face: ComposerFace/// The face of the chip's name.publicvar chipFace: ComposerFacepublicvar cornerRadius: CGFloatpublicinit( panel: Color, rim: Color, ink: Color, faint: Color, chip: Color, accent: Color, onAccent: Color, face: ComposerFace = ComposerFace(), chipFace: ComposerFace = ComposerFace(size: 15, textStyle: .subheadline), cornerRadius: CGFloat = 24, danger: Color = .red ) {self.panel = panelself.rim = rimself.ink = inkself.faint = faintself.chip = chipself.accent = accentself.onAccent = onAccentself.danger = dangerself.face = faceself.chipFace = chipFaceself.cornerRadius = cornerRadius }}/// A face by its PostScript name (nil: the system's), its size at the/// default text size and the text style it grows with.publicstructComposerFace: Equatable, Sendable {publicvar name: String?publicvar size: CGFloatpublicvar textStyle: Font.TextStylepublicinit(name: String? = nil, size: CGFloat = 17, textStyle: Font.TextStyle = .body) {self.name = nameself.size = sizeself.textStyle = textStyle }publicvar font: Font {iflet name { return .custom(name, size: size, relativeTo: textStyle) }return .system(size: size).weight(.regular) }}/// Where a dictation stands, as the composer draws it.publicenumComposerDictationPhase: Equatable, Sendable {case idle/// Asked for; the engine is loading.case preparingcase listening/// Stopped; the last words and the second pass are landing.case settling/// It could not start; the words say why.case failed(String)/// Whether the control row is the dictation's (✕, waveform, stop, send).publicvar isDictating: Bool {switchself {case .preparing, .listening, .settling: truecase .idle, .failed: false } }}/// A voice path the composer drives (Cotton's `voice.*` sessions behind it,/// in the app). The composer anchors it at the caret, writes what it hears/// into the text, and hands back the person's own edits so a second pass/// never overwrites them.@MainActorpublicprotocolComposerDictation: AnyObject {var phase: ComposerDictationPhase { get }/// The voice's loudness now, 0…1.var level: Double { get }/// The whole text as it should show while dictating; nil when not.var shown: String? { get }/// Where the tentative words sit in ``shown`` (UTF-16), drawn lighter.var tentativeRange: NSRange? { get }/// The caret just after the dictated words (UTF-16).var caret: Int { get }/// Begin at `selection` in `text` (UTF-16; nil: the end).func start(text: String, selection: NSRange?) async/// Everything settles; answers the whole text.func stop() async -> String/// What this dictation added goes; answers the text as it was.func cancel() -> String/// The composer wrote ``shown`` into its text.func didWrite(_ text: String)/// The text changed under the person's hand.func fieldChanged(_ text: String)/// The selection moved.func selectionChanged(_ range: NSRange)}/// A voice message the microphone records while it is held (Comms, 22 Nov/// 2026): a tap on the same microphone still dictates. The composer says when/// the hold begins and ends and draws the time and the loudness; the app/// records, and sends what it recorded.@MainActorpublicprotocolComposerRecorder: AnyObject {/// The microphone is open and recording.var recording: Bool { get }/// How long it has recorded, in seconds.var elapsed: TimeInterval { get }/// The voice's loudness now, 0…1.var level: Double { get }/// The hold's name for a screen reader, which cannot hold ("Hold to record").var holdLabel: String { get }/// The finger stayed down: record.func begin() async/// Let go: send what was recorded, or throw it away when `cancelled`/// (the finger slid away first). Before the recording began, nothing is kept.func end(cancelled: Bool)}/// The composer's rules, apart from its drawing, so they are tested alone.publicenumComposerRules {/// About seven lines, then the text scrolls inside itself.publicstaticlet maxLines = 7/// A press on the microphone held this long records; shorter, it is a tap and dictates.publicstaticlet holdToRecord: Duration = .milliseconds(300)/// A held voice message slid this far from where the finger came down is thrown away (Android's 96 dp).publicstaticlet slideAway: CGFloat = 96/// Whether a held voice message has been slid away, in any direction, to be thrown out.publicstaticfunc slidAway(_ translation: CGSize) -> Bool { (translation.width * translation.width + translation.height * translation.height).squareRoot() > slideAway }/// The send button's state: the app's word when it gives one, else/// whether there is anything but space to send.publicstaticfunc canSend(text: String, rule: Bool?) -> Bool {iflet rule { return rule }return !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }/// The text area's height for a measured content height: never under/// one line, never over `maxLines` lines (or `cap` points, whichever is/// less); over that it scrolls.publicstaticfunc height(content: CGFloat, line: CGFloat, inset: CGFloat, cap: CGFloat = .greatestFiniteMagnitude) -> CGFloat {let one = ceil(line + inset)let most = min(ceil(line * CGFloat(maxLines) + inset), max(one, cap))return min(max(content, one), most) }/// Whether the area scrolls at this content height.publicstaticfunc scrolls(content: CGFloat, line: CGFloat, inset: CGFloat, cap: CGFloat = .greatestFiniteMagnitude) -> Bool { content > height(content: content, line: line, inset: inset, cap: cap) + 0.5 }/// What a Return does: `true` sends, `false` writes a new line./// Shift-Return always writes a new line; Command-Return always sends.publicstaticfunc returnSends(appSaysReturnSends: Bool, shift: Bool, command: Bool) -> Bool {if command { returntrue }if shift { returnfalse }return appSaysReturnSends }/// The waveform's history after one more level: the newest at the end,/// at most `count` bars, each clamped to 0…1.publicstaticfunc waveform(_ history: [Double], adding level: Double, count: Int) -> [Double] {var next = history next.append(min(max(level, 0), 1))if next.count > count { next.removeFirst(next.count - count) }return next }}