Cotton code
291 lines · 11.7 KB · SwiftRaw
// The one slip — the full-width object every list of records draws// (recordface row 1, 21 Oct 2026: one slip face on every list).//// `PaperSlip` in `Paper.swift`'s opening note is a SIZE, not a type: a `Paper`// laid at a slip's proportion. This file is the FURNITURE that goes on one, so// that a todo, a note, a message, a meeting, an alarm and a place are the same// object with different things written in its slots rather than papers that// drift apart. `SlipFace` fills the slots from a table's slip layout; nothing// here names a kind.//// THE SLOTS, and a slip takes the ones it has:////   • LEADING — the todo's check, a person's print, an object, a stub, a//     number;//   • a HEADLINE drawn in place of the title (an alarm's time as figures);//   • a TITLE line in the serif, with a NOTE by hand at its end (a time);//   • the PROSE in the typewriter, clamped to `clamp` lines;//   • a META line along the foot with room at its end for a stamp, a badge,//     a figure, a byline;//   • SIDE — what stands at the slip's trailing edge: a switch, a picture;//   • BELOW — what spans the slip under its words: a player, a pair of//     pictures.//// The edge and the lean are the theme's (`slip_deckle`, `slip_tilt`); only a// notice says its own edge (the perforated receipt).//// WHAT FOLDS AND WHAT DOES NOT is `SlipFold`, below — pure, so the rule a// person taps against is settled by a test rather than by a layout pass.import CottonKitimport SwiftUI// MARK: - The fold/// The clamp rule a slip draws under, and whether a tap on one does anything.////// Its numbers are the worn theme's: `paper_slip_prose_lines` (the clamp a/// prose slip takes when it is not told otherwise — three typed lines, the/// memory slip on a project page), `paper_slip_summary_lines` (the project/// summary's four: a description is a paragraph somebody wrote to be read) and/// `paper_slip_line_width_characters` (how many characters of the monospaced/// typewriter one drawn line carries — a phone's 358-point column at 13pt sets/// about 45; being a few out costs a tap that folds nothing, never a sentence/// that cannot be opened).enum SlipFold {    /// The line limit the prose draws at: the clamp while folded, and none at    /// all once it is open.    static func lineLimit(clamp: Int, expanded: Bool) -> Int? {        expanded ? nil : max(1, clamp)    }    /// Whether this prose can be cut by the clamp at all.    ///    /// A paragraph short enough to fit never offers a tap, so nothing on the    /// page responds to a press by doing nothing visible. Empty prose folds    /// nothing; a clamp of zero or less is not a clamp and folds nothing    /// either.    static func folds(        _ text: String,        clamp: Int,        lineWidth: Int    ) -> Bool {        guard clamp > 0, !text.isEmpty else { return false }        let width = max(1, lineWidth)        var drawn = 0        for line in text.split(separator: "\n", omittingEmptySubsequences: false) {            // A written line takes at least one drawn line, and a long one            // wraps into as many as it needs.            drawn += max(1, (line.count + width - 1) / width)            if drawn > clamp { return true }        }        return drawn > clamp    }}// MARK: - The slip/// One record on a full-width slip.////// `leading` and `trailing` default to nothing, so the common call is the/// memory slip's — a dye, a seed and some prose.struct ObjectSlip<Leading: View, Trailing: View>: View {    @Environment(\.theme) private var theme    let seed: String    let dye: PaperDye    /// Nil is the theme's (`slip_deckle`); a notice names its receipt edge.    var edge: PaperEdge? = nil    /// The object's own name, in the serif. Absent for a kind whose prose IS    /// the record (memory, a thought).    var title: String?    var titleInk: PaperInk = .ink    var proseInk: PaperInk = .ink    var metaInk: PaperInk? = nil    /// How many lines the title may take; nil is the theme's.    var titleLines: Int? = nil    /// The title's size; nil is the theme's title size.    var titleSize: CGFloat? = nil    /// What the record says, in the typewriter.    var prose: String?    /// How many lines of prose the slip shows while it is folded; nil is the    /// theme's (`paper_slip_prose_lines`).    var clamp: Int? = nil    /// Folded or open. A slip whose caller keeps no state is always folded.    var expanded = false    /// The quiet typed line along the foot — a due date, a day, a count.    var meta: String?    /// The foot line reads in the app's red: a debt, a lateness.    var metaLate = false    /// The prose is struck through — a completed todo.    var struck = false    /// Words by hand at the title line's end ("6:14 pm").    var note: String? = nil    var noteInk: PaperInk = .pencil    /// Drawn in place of the title: an alarm's figures.    var headline: AnyView? = nil    /// At the slip's trailing edge, centred on its words: a switch, a picture.    var side: AnyView? = nil    /// Across the slip under its words: a player, a pair of pictures.    var below: AnyView? = nil    /// How strongly the words print: faint for a switch that is off.    var lettering: Double = 1    /// The check, at the slip's leading edge.    @ViewBuilder var leading: () -> Leading    /// A stamp or a verb at the end of the foot line.    @ViewBuilder var trailing: () -> Trailing    var body: some View {        Paper(            dye: dye, edge: edge ?? Self.edge(theme.scale), seed: seed,            tilt: theme.scale[.slipTilt] >= 0.5, inset: PaperSlipKnobs.paperSlipInset        ) {            VStack(alignment: .leading, spacing: PaperSlipKnobs.paperSlipStackGap) {                HStack(alignment: side == nil ? .top : .center, spacing: PaperSlipKnobs.paperSlipRowGap) {                    leading()                    VStack(alignment: .leading, spacing: PaperSlipKnobs.paperSlipStackGap) {                        if let headline { headline }                        titleLine                        if let prose, !prose.isEmpty {                            Text(prose)                                .paperBody(                                    theme.scale.bodySize - 1,                                    ink: proseInk == .red ? .red : struck ? .pencil : proseInk)                                .strikethrough(struck, color: theme.print(.pencil))                                .lineSpacing(PaperSlipKnobs.paperSlipBodyLineSpacing)                                .lineLimit(SlipFold.lineLimit(                                    clamp: clamp ?? SlipFaceKnobs.paperSlipProseLines, expanded: expanded))                                .multilineTextAlignment(.leading)                                .frame(maxWidth: .infinity, alignment: .leading)                        }                        foot                    }                    .opacity(lettering)                    .frame(maxWidth: .infinity, alignment: .leading)                    if let side { side }                }                if let below { below }            }            .frame(maxWidth: .infinity, alignment: .leading)            .padding(.vertical, PaperSlipKnobs.paperSlipVerticalInset)        }    }    /// The slip's edge where the caller names none: deckled when the theme's    /// `slip_deckle` says so, else cut.    static func edge(_ scale: ThemeScale) -> PaperEdge { scale[.slipDeckle] >= 0.5 ? .torn : .cut }    /// The title in the serif and the note by hand at its end; the note    /// alone when there is no title.    @ViewBuilder private var titleLine: some View {        let named = title.map { !$0.isEmpty } ?? false        let noted = note.map { !$0.isEmpty } ?? false        if named || noted {            HStack(alignment: .firstTextBaseline, spacing: PaperSlipKnobs.paperSlipFootGap) {                if let title, named {                    Text(title)                        .paperTitle(titleSize ?? theme.scale.titleSize, ink: titleInk)                        .lineLimit(titleLines ?? PaperSlipKnobs.paperSlipTitleLines)                        .multilineTextAlignment(.leading)                }                Spacer(minLength: PaperSlipKnobs.paperSlipFootMinGap)                if let note, noted {                    Text(note)                        .paperHand(theme.scale.handSize + 2, ink: noteInk)                        .lineLimit(1)                        .fixedSize(horizontal: true, vertical: false)                }            }        }    }    /// The foot line. Drawn only when there is something on it, so a slip with    /// neither a date nor a stamp has no empty band under its prose.    @ViewBuilder private var foot: some View {        if meta != nil || Trailing.self != EmptyView.self {            HStack(alignment: .firstTextBaseline, spacing: PaperSlipKnobs.paperSlipFootGap) {                if let meta, !meta.isEmpty {                    Text(meta)                        .paperBody(theme.scale.bodySize - 2, ink: metaInk ?? (metaLate && !struck ? .red : .pencil))                        .fixedSize(horizontal: false, vertical: true)                }                Spacer(minLength: PaperSlipKnobs.paperSlipFootMinGap)                trailing()            }            .padding(.top, PaperSlipKnobs.paperSlipFootTopInset)        }    }}extension ObjectSlip where Leading == EmptyView, Trailing == EmptyView {    /// A slip with nothing but what is written on it — the prose kinds.    init(        seed: String,        dye: PaperDye,        edge: PaperEdge? = nil,        title: String? = nil,        titleLines: Int? = nil,        titleSize: CGFloat? = nil,        prose: String? = nil,        clamp: Int? = nil,        expanded: Bool = false,        meta: String? = nil,        metaLate: Bool = false,        struck: Bool = false    ) {        self.init(            seed: seed,            dye: dye,            edge: edge,            title: title,            titleLines: titleLines,            titleSize: titleSize,            prose: prose,            clamp: clamp,            expanded: expanded,            meta: meta,            metaLate: metaLate,            struck: struck,            leading: { EmptyView() },            trailing: { EmptyView() }        )    }}extension ObjectSlip where Trailing == EmptyView {    /// A slip with a check at its leading edge and nothing at its foot's end.    init(        seed: String,        dye: PaperDye,        edge: PaperEdge? = nil,        title: String? = nil,        titleLines: Int? = nil,        prose: String? = nil,        clamp: Int? = nil,        expanded: Bool = false,        meta: String? = nil,        metaLate: Bool = false,        struck: Bool = false,        @ViewBuilder leading: @escaping () -> Leading    ) {        self.init(            seed: seed,            dye: dye,            edge: edge,            title: title,            titleLines: titleLines,            prose: prose,            clamp: clamp,            expanded: expanded,            meta: meta,            metaLate: metaLate,            struck: struck,            leading: leading,            trailing: { EmptyView() }        )    }}/// The sizes, counts and timings PaperSlip draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum PaperSlipKnobs {    static let paperSlipBodyLineSpacing: CGFloat = 3    static let paperSlipFootGap: CGFloat = 10    static let paperSlipFootMinGap: CGFloat = 4    static let paperSlipFootTopInset: CGFloat = 2    static let paperSlipInset: CGFloat = 14    static let paperSlipRowGap: CGFloat = 12    static let paperSlipStackGap: CGFloat = 6    static let paperSlipTitleLines = 2    static let paperSlipVerticalInset: CGFloat = 2}