Cotton code
126 lines · 5.3 KB · SwiftRaw
// The receipt, the seventh face (shared format, added 30 Sep on the owner's// word): the cream paper card with the perforated bottom edge that the Import// chat screen draws — a small-capitals label line typed across its head, a// pencil rule, then a handwritten title and a plain explanation. It is the face// for a step in progress or just finished: imports, pairing, wait and gate// screens, and the change receipt (what a command did, with its Undo).//// Drawn once, here. A screen that shows such a card passes its label and puts// whatever that step has to say inside; a table whose tile is `receipt` draws// its records with the definition's slip columns. Never delete this card when// converting a screen (`ReceiptFaceTests` holds the screens that wear it).import CottonValuesimport SwiftUIpublic struct ReceiptFace<Content: View>: View {    @Environment(\.theme) private var theme    private let label: String    private let seed: String    private let dye: PaperDye    private let labelIdentifier: String?    private let content: Content    /// - Parameters:    ///   - label: the line typed across the head ("WHATSAPP EXPORT · READING").    ///   - seed: keeps the perforation and the rule the same on every draw.    ///   - labelIdentifier: the label line's accessibility identifier, for UI tests.    public init(        label: String,        seed: String,        dye: PaperDye = .cream,        labelIdentifier: String? = nil,        @ViewBuilder content: () -> Content    ) {        self.label = label        self.seed = seed        self.dye = dye        self.labelIdentifier = labelIdentifier        self.content = content()    }    public var body: some View {        Paper(dye: dye, edge: .receipt, seed: seed, tilt: false, inset: ReceiptFaceKnobs.receiptInset) {            VStack(alignment: .leading, spacing: 0) {                Text(label)                    .paperMeta(theme.scale.metaSize - 0.5)                    .accessibilityIdentifier(labelIdentifier ?? "receipt-label")                PencilRule(seed: "\(seed)-head", theme: theme)                    .stroke(theme.print(.pencil), lineWidth: ReceiptFaceKnobs.receiptRuleLineWidth)                    .frame(height: ReceiptFaceKnobs.receiptRuleHeight)                    .padding(.top, ReceiptFaceKnobs.receiptRuleTopPadding)                    .padding(.bottom, ReceiptFaceKnobs.receiptRuleBottomPadding)                content            }            .frame(maxWidth: .infinity, alignment: .leading)        }        .fixedSize(horizontal: false, vertical: true)    }}/// The receipt's usual inside: a handwritten title over a plain explanation.struct ReceiptBody: View {    @Environment(\.theme) private var theme    private let title: String    private let explanation: String?    init(title: String, explanation: String?) {        self.title = title        self.explanation = explanation    }    var body: some View {        VStack(alignment: .leading, spacing: ReceiptFaceKnobs.receiptContentSpacing) {            Text(title)                .paperHand(theme.scale.handSize + 4, ink: .ink)                .lineLimit(ReceiptFaceKnobs.receiptTitleLineLimit)                .fixedSize(horizontal: false, vertical: true)            if let explanation {                Text(explanation)                    .paperBody(theme.scale.bodySize - 1, ink: .pencil)                    .lineSpacing(ReceiptFaceKnobs.receiptBodyLineSpacing)                    .fixedSize(horizontal: false, vertical: true)            }        }        .frame(maxWidth: .infinity, alignment: .leading)    }}extension ReceiptFace where Content == ReceiptBody {    /// A receipt that says one thing: its title and an explanation.    init(label: String, title: String, explanation: String?, seed: String) {        self.init(label: label, seed: seed) { ReceiptBody(title: title, explanation: explanation) }    }    /// One record as a receipt, from its definition: the table's name and the    /// slip's trailing value across the head, the slip's title handwritten,    /// its subtitle as the explanation.    init(definition: TableDefinition, record: TableRecord) {        let slip = definition.slip        func words(_ key: String?) -> String? {            key.flatMap { FaceText.line(record[$0], words: record.columnWords($0), column: definition.column($0), currency: record[definition.column($0)?.currency].text) }        }        let head = [definition.name.uppercased(), words(slip.trailing)?.uppercased()]            .compactMap { $0 }            .joined(separator: " · ")        self.init(            label: head,            title: words(slip.title) ?? definition.name,            explanation: words(slip.subtitle),            seed: "receipt-\(record.id)")    }}/// The sizes, counts and timings ReceiptFace draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum ReceiptFaceKnobs {    static let receiptBodyLineSpacing: CGFloat = 3    static let receiptContentSpacing: CGFloat = 12    static let receiptInset: CGFloat = 22    static let receiptRuleBottomPadding: CGFloat = 16    static let receiptRuleHeight: CGFloat = 3    static let receiptRuleLineWidth: CGFloat = 0.8    static let receiptRuleTopPadding: CGFloat = 10    static let receiptTitleLineLimit = 2}