// 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 … } import SwiftUI /// Every word the composer says, handed in by the app (its core's lines). public struct ComposerLines: Equatable, Sendable { /// The text area's placeholder. public var hint: String /// The send button's spoken name. public var send: String /// The microphone, idle. public var mic: String /// The stop button while dictating: the words stay, typing resumes. public var stop: String /// The ✕ while dictating: what this dictation added goes. public var cancel: String /// Said while the microphone listens (VoiceOver's value for the waveform). public var listening: String public init(hint: String, send: String, mic: String, stop: String, cancel: String, listening: String) { self.hint = hint self.send = send self.mic = mic self.stop = stop self.cancel = cancel self.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. public struct ComposerPick: Equatable, Sendable { public struct Option: Equatable, Identifiable, Sendable { public let id: String /// The full name the row and the chip show. public let name: String /// The dot beside the name; nil draws none. public let tint: Color? public init(id: String, name: String, tint: Color? = nil) { self.id = id self.name = name self.tint = tint } } public struct Section: Equatable, Identifiable, Sendable { public let id: String /// An optional heading over the section's rows. public let title: String? public let options: [Option] public init(id: String, title: String? = nil, options: [Option]) { self.id = id self.title = title self.options = options } } /// The option the chip shows and the menu ticks. public var chosen: String public var sections: [Section] /// The last row's words ("New character…"); nil draws no such row. public var new: String? /// The chip's spoken name before its value ("Next line"). public var label: String public init(chosen: String, sections: [Section], new: String? = nil, label: String) { self.chosen = chosen self.sections = sections self.new = new self.label = label } /// `chosen` while the "new…" row is picked: the chip shows that row's words. public static let newID = "composer-pick-new" /// The chosen option, as the chip shows it. public var current: Option? { if chosen == Self.newID, let new { return Option(id: Self.newID, name: new) } for section in sections { if let found = section.options.first(where: { $0.id == chosen }) { return found } } return nil } } /// What a tap in the pick menu chose. public enum ComposerPickChoice: Equatable, Sendable { case option(String) case new } /// The composer's colours and its face, from the app's theme. public struct ComposerStyle: Sendable { /// The rounded panel's fill. public var panel: Color /// The panel's hairline rim. public var rim: Color /// The words. public var ink: Color /// The placeholder, tentative words and resting controls. public var faint: Color /// The chip's fill. public var chip: Color /// The microphone and send's fill, and the waveform. public var accent: Color /// What is drawn on the accent. public var onAccent: Color /// A held voice message slid away to be thrown out. public var danger: Color /// The face the words are written in. public var face: ComposerFace /// The face of the chip's name. public var chipFace: ComposerFace public var cornerRadius: CGFloat public init( 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 = panel self.rim = rim self.ink = ink self.faint = faint self.chip = chip self.accent = accent self.onAccent = onAccent self.danger = danger self.face = face self.chipFace = chipFace self.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. public struct ComposerFace: Equatable, Sendable { public var name: String? public var size: CGFloat public var textStyle: Font.TextStyle public init(name: String? = nil, size: CGFloat = 17, textStyle: Font.TextStyle = .body) { self.name = name self.size = size self.textStyle = textStyle } public var font: Font { if let name { return .custom(name, size: size, relativeTo: textStyle) } return .system(size: size).weight(.regular) } } /// Where a dictation stands, as the composer draws it. public enum ComposerDictationPhase: Equatable, Sendable { case idle /// Asked for; the engine is loading. case preparing case 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). public var isDictating: Bool { switch self { case .preparing, .listening, .settling: true case .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. @MainActor public protocol ComposerDictation: 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. @MainActor public protocol ComposerRecorder: 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. public enum ComposerRules { /// About seven lines, then the text scrolls inside itself. public static let maxLines = 7 /// A press on the microphone held this long records; shorter, it is a tap and dictates. public static let holdToRecord: Duration = .milliseconds(300) /// A held voice message slid this far from where the finger came down is thrown away (Android's 96 dp). public static let slideAway: CGFloat = 96 /// Whether a held voice message has been slid away, in any direction, to be thrown out. public static func 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. public static func canSend(text: String, rule: Bool?) -> Bool { if let 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. public static func 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. public static func 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. public static func returnSends(appSaysReturnSends: Bool, shift: Bool, command: Bool) -> Bool { if command { return true } if shift { return false } return appSaysReturnSends } /// The waveform's history after one more level: the newest at the end, /// at most `count` bars, each clamped to 0…1. public static func 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 } }