Cotton code
156 lines · 6.6 KB · SwiftRaw
// What a line said to the one input came to, beside its change receipt// (`OneInputNote`). The words themselves go in through the dial on the phone// (owner's rule 8, 16 Oct 2026); the Mac's docked input went with the Mac// client (architect, 20 Oct 2026), and what the words do is still// `OneInputModel`'s.import CottonKitimport SwiftUI/// What the input says beside the change receipt: that the words did/// nothing, and why, or a note beside what they did. Nil when the change/// receipt says it all.struct OneInputNote: Equatable {    let label: String    /// The words, in hand.    let title: String    let explanation: String    static func of(_ result: OneInputResult) -> OneInputNote? {        var lines: [String] = []        guard result.said != nil else { return nil }        let label = switch result.saidKind {        case .notDone?: ShellLines.line("input", "note.not_done")        case .aside?, nil: ShellLines.line("input", "note.note")        }        if let said = result.said { lines.append(said) }        return OneInputNote(label: label, title: result.words, explanation: lines.joined(separator: " "))    }}/// The input's note, on the receipt face, with Done to let it go.struct OneInputNoteCard: View {    let note: OneInputNote    let onDismiss: () -> Void    var body: some View {        ReceiptFace(label: note.label, seed: "one-input-\(note.title)", labelIdentifier: "one-input-note-head") {            VStack(alignment: .leading, spacing: 6) {                ReceiptBody(title: note.title, explanation: note.explanation)                    .accessibilityElement(children: .combine)                HStack {                    Spacer(minLength: 8)                    PaperTypedButton("Done", size: 14, action: onDismiss)                        .accessibilityIdentifier("one-input-note-dismiss")                }            }        }        // A container, never one combined element: Done is a button a        // reader reaches on its own, beside the words it lets go.        .accessibilityElement(children: .contain)        .accessibilityIdentifier("one-input-note")    }}/// The store's question before it makes the bar's record ("Which one?"),/// on the receipt face the input's note wears: the words in hand,/// the question under them, one row per answer, and Cancel to let it go with/// nothing made. An answer waits while it is on its way.struct CreateQuestionCard: View {    let question: CreateQuestion    let identifier: String    let onAnswer: (CreateQuestion.Option) async -> Void    let onDismiss: () -> Void    @State private var answering: String?    var body: some View {        ReceiptFace(label: ShellLines.line("input", "note.question"), seed: "one-input-question-\(question.words)", labelIdentifier: "one-input-question-head") {            VStack(alignment: .leading, spacing: 6) {                // The words in hand and the question, read as the card's                // header; each answer is a button of its own under it.                ReceiptBody(title: question.words, explanation: question.question)                    .accessibilityElement(children: .combine)                    .accessibilityAddTraits(.isHeader)                    .accessibilityIdentifier("\(identifier)-question-words")                // The card stands directly above the bar, inside the safe                // area, like a receipt, and a bottom inset is drawn at the                // height it asks for: a long run of ROWS pushed the head                // under the clock (walk 3), and rows scrolled inside a                // bounded height left the later ones never on screen (the                // 8th account, walk 5 Oct). Past a few, the options are                // short tags wrapped across the card, so every one is drawn                // and can be tapped; only a very long run scrolls.                switch Self.drawing(question.options.count) {                case .scrolledTags:                    ScrollView { wrapped }                        .frame(height: Self.scrolledHeight)                        .scrollBounceBehavior(.basedOnSize)                case .tags:                    wrapped                case .rows:                    options                }                HStack {                    Spacer(minLength: 8)                    PaperTypedButton("Cancel", size: 14, action: onDismiss)                        .accessibilityIdentifier("\(identifier)-question-dismiss")                }            }        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("\(identifier)-question")    }    /// Options drawn as rows; more are wrapped tags, and past    /// `wrappedOptions` the tags scroll in `scrolledHeight`.    static let plainOptions = 3    static let wrappedOptions = 14    /// How a question's answers are drawn, by how many it offers.    enum Drawing: Equatable { case rows, tags, scrolledTags }    /// Rows up to `plainOptions`, wrapped tags (every one on screen) up to    /// `wrappedOptions`, scrolled tags past that.    static func drawing(_ count: Int) -> Drawing {        if count > wrappedOptions { return .scrolledTags }        return count > plainOptions ? .tags : .rows    }    /// Three option rows (about 55 points each) and half of the next.    static let scrolledHeight: CGFloat = 190    private func answer(_ option: CreateQuestion.Option) {        guard answering == nil else { return }        answering = option.id        Task {            await onAnswer(option)            answering = nil        }    }    private var options: some View {        VStack(alignment: .leading, spacing: 6) {            ForEach(question.options) { option in                TypedRow(label: option.label, chevron: false) { answer(option) }                    .disabled(answering != nil)                    .accessibilityLabel(option.label)                    .accessibilityAddTraits(.isButton)                    .accessibilityIdentifier("\(identifier)-question-\(option.id)")            }        }    }    /// The same answers as tags wrapped across the card.    private var wrapped: some View {        PaperWrap(spacing: 10) {            ForEach(question.options) { option in                Button { answer(option) } label: { PaperTag(title: option.label, seed: option.id) }                    .buttonStyle(MotionButtonStyle())                    .disabled(answering != nil)                    .accessibilityLabel(option.label)                    .accessibilityIdentifier("\(identifier)-question-\(option.id)")            }        }        .frame(maxWidth: .infinity, alignment: .leading)    }}