Cotton code
128 lines · 6.2 KB · SwiftRaw
// The reply sheet: a slip's question, and a field for the owner's answer in// their own words (`reply`). Drawn once for every table whose slip names a// reply — the questions assistants asked were the first.//// Cancel at the left, the round ink Send at the right; under them a torn paper// in the slip's tint with the slip's foot line (what the question is about),// the question itself, and a ruled multi-line field the keyboard focuses at// once. The keyboard's dictation is the voice path: a spoken answer lands in// the same field. Send is inert while the field is blank// (`TableDefinition.replyPatch` is the one rule), and the sheet stays up with// its words when the write is refused.import CottonValuesimport SwiftUIstruct ReplySheet: View {    @Environment(\.theme) private var theme    let definition: TableDefinition    let record: TableRecord    let reply: SlipReply    /// Sends the words; `true` once the answer landed.    let onSend: (String) async -> Bool    let onClose: () -> Void    @Environment(\.linkTitles) private var links    @State private var draft = ""    @State private var sending = false    @FocusState private var focused: Bool    private var canSend: Bool {        !sending && definition.replyPatch(draft) != nil    }    /// The slip's foot line — what the question is about — over the question.    private var about: String? {        FaceText.slipLine(record[definition.slip.trailing], words: record.columnWords(definition.slip.trailing), column: definition.column(definition.slip.trailing), links: links.byID, currency: record[definition.column(definition.slip.trailing)?.currency].text, readsPlaceholder: false)    }    var body: some View {        VStack(alignment: .leading, spacing: 0) {            HStack(alignment: .center) {                Button(action: onClose) {                    Text(CottonStrings.cancel)                        .paperHand(theme.scale.handSize + 4)                        .frame(minHeight: ReplySheetKnobs.faceReplySheetControlMinHeight)                        .contentShape(Rectangle())                }                .buttonStyle(MotionButtonStyle())                .accessibilityIdentifier("reply-\(definition.id)-cancel")                Spacer()                Button(action: send) {                    Image(systemName: sending ? "ellipsis" : "arrow.up")                        .font(theme.type.interface(ReplySheetKnobs.faceReplySheetSendSymbolSize, weight: .semibold))                        .foregroundStyle(theme.sheet(.cream))                        .frame(width: ReplySheetKnobs.faceReplySheetSendSide, height: ReplySheetKnobs.faceReplySheetSendSide)                        .background(Circle().fill(theme.print(.ink)))                }                .buttonStyle(MotionButtonStyle())                .disabled(!canSend)                .opacity(canSend ? 1 : DisabledOpacity.control)                .accessibilityLabel(sending ? ShellLines.line("faces", "reply.sending") : ShellLines.line("faces", "reply.send", ["verb": reply.verb.lowercased()]))                .accessibilityIdentifier("reply-\(definition.id)-send")            }            ScrollView {                Paper(dye: FaceTint.dye(for: record.id), edge: .torn, seed: "reply-\(record.id)", tilt: false, inset: ReplySheetKnobs.faceReplySheetPaperInset) {                    VStack(alignment: .leading, spacing: 0) {                        if let about {                            Text(about).paperMeta(theme.scale.metaSize - 0.5)                        }                        Text(record.said?.slip.title ?? "")                            .paperBody(theme.scale.bodySize + 2)                            .fixedSize(horizontal: false, vertical: true)                            .padding(.top, ReplySheetKnobs.faceReplySheetQuestionTopPadding)                            .accessibilityAddTraits(.isHeader)                        TextField(reply.placeholder ?? "", text: $draft, axis: .vertical)                            .textFieldStyle(.plain)                            .font(theme.type.body(theme.scale.bodySize + 1))                            .foregroundStyle(theme.print(.ink))                            .lineSpacing(ReplySheetKnobs.paperRuledTypedSpacing)                            .lineLimit(ReplySheetKnobs.faceReplySheetFieldMinLines...)                            .padding(.top, ReplySheetKnobs.faceReplySheetBodyTopPadding)                            .background(alignment: .top) {                                RuledLines().padding(.top, ReplySheetKnobs.faceReplySheetRuleTopPadding)                            }                            .focused($focused)                            .accessibilityIdentifier("reply-\(definition.id)-field")                    }                    .frame(maxWidth: .infinity, alignment: .leading)                }                .padding(.top, ReplySheetKnobs.faceReplySheetPaperTopPadding)            }            .scrollBounceBehavior(.basedOnSize)            Spacer(minLength: 0)        }        .padding(ReplySheetKnobs.faceReplySheetPadding)        .frame(maxWidth: .infinity, alignment: .leading)        .accessibilityIdentifier("reply-\(definition.id)-sheet")        .onAppear { focused = true }    }    private func send() {        guard canSend else { return }        sending = true        let words = draft        Task {            let landed = await onSend(words)            sending = false            if landed { onClose() }        }    }}/// The sizes, counts and timings ReplySheet draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum ReplySheetKnobs {    static let faceReplySheetBodyTopPadding: CGFloat = 14    static let faceReplySheetControlMinHeight: CGFloat = 44    static let faceReplySheetFieldMinLines = 3    static let faceReplySheetPadding: CGFloat = 20    static let faceReplySheetPaperInset: CGFloat = 20    static let faceReplySheetPaperTopPadding: CGFloat = 8    static let faceReplySheetQuestionTopPadding: CGFloat = 4    static let faceReplySheetRuleTopPadding: CGFloat = 8    static let faceReplySheetSendSide: CGFloat = 48    static let faceReplySheetSendSymbolSize: CGFloat = 18    static let paperRuledTypedSpacing: CGFloat = 8}