Cotton code
226 lines · 9.7 KB · SwiftRaw
// The ARTIFACT face (reference `artifacts.png` and its cases) — the face of// a tool that reads another tool's table (Artifacts reads documents). The// framed cream card (house references 4/31, 5/33, 90): the page in a// browser's window on a wash of blue in a cream mat — its picture, or the// empty window with a blank page ruled inside while there is none — then the// title typed heavy, "made with …" in type, and at the foot the day by hand// with the group on a buff tag beside it, or on a hairline when there is no// group; the day's label only when no "made with …" says it. Every word is// the core's (`DeckCard`).import CottonPaperimport CottonValuesimport SwiftUIenum ArtifactFaceKnobs {    static let inset = EdgeInsets(top: 26, leading: 26, bottom: 24, trailing: 24)    static let windowShare: CGFloat = 0.52    /// The cream mat round the wash, and the wash round the window.    static let mat: CGFloat = 8    static let wash: CGFloat = 12    static let washSaturation: Double = 1.6    static let titleSize: CGFloat = 23    static let madeSize: CGFloat = 15    static let dateHand: CGFloat = 29    static let bar: CGFloat = 16    static let dot: CGFloat = 6    static let dotOpacity: Double = 0.7    static let field: CGFloat = 7    static let line: CGFloat = 0.7    /// The blank page's ruled frame inside the empty window.    static let blankInset: CGFloat = 9}public struct ArtifactFace<Picture: View>: View {    let card: DeckCard    /// The page's picture filling its frame; nil draws the empty window.    var picture: Picture?    @Environment(\.theme) private var theme    public init(card: DeckCard, picture: Picture? = nil) {        self.card = card        self.picture = picture    }    public var body: some View {        GeometryReader { sheet in            VStack(alignment: .leading, spacing: 0) {                if let kind = card.text("subtitle") { FaceKind(words: kind).padding(.bottom, 12).facePart(.subtitle) }                BrowserWindow(picture: picture)                    .padding(ArtifactFaceKnobs.wash)                    .background(                        PaperSurface(dye: .blue, edge: .torn, seed: "artifact-wash")                            .saturation(ArtifactFaceKnobs.washSaturation)                    )                    .padding(ArtifactFaceKnobs.mat)                    .background(theme.faceStocks.printMount)                    .overlay(Rectangle().stroke(theme.print(.pencil).opacity(0.25), lineWidth: ArtifactFaceKnobs.line))                    .frame(height: sheet.size.height * ArtifactFaceKnobs.windowShare)                    .shadow(color: theme.colors.shadow.color.opacity(0.22), radius: 2.5, x: 0, y: 1.5)                    .accessibilityElement(children: .ignore)                    .accessibilityLabel(card.text("title") ?? "")                    .facePart(.picture)                // A page filed under its address reads by its name, the                // address's host under it (real-data walk, 23 Oct 2026).                let named = card.text("title").map(ArtifactTitle.init)                if let named {                    HeavyType(words: named.words, size: ArtifactFaceKnobs.titleSize)                        .lineLimit(2)                        .minimumScaleFactor(0.8)                        .padding(.top, 20)                        .accessibilityIdentifier("deck-title")                        .facePart(.title)                }                if let host = named?.host {                    Text(host)                        .font(CottonFonts.type(ArtifactFaceKnobs.madeSize))                        .lineLimit(1)                        .faceInk(.pencil)                        .padding(.top, 4)                }                let made = card.text("source")                if let made {                    Text(made)                        .font(CottonFonts.type(ArtifactFaceKnobs.madeSize))                        .lineLimit(1)                        .faceInk(.ink)                        .padding(.top, 6)                }                Spacer(minLength: 6)                let group = card.text("group")                // With no tag beside it, the day sits on a hairline.                if group == nil, card.text("date") != nil { FaceHairline().padding(.bottom, 8) }                HStack(alignment: .bottom) {                    if let date = card.text("date") {                        VStack(alignment: .leading, spacing: 2) {                            // "made with …" already says what the day is.                            if made == nil, let label = card.label("date") { FaceLabel(words: label) }                            Text(HandRun.tail(date))                                .font(CottonFonts.hand(ArtifactFaceKnobs.dateHand))                                .lineLimit(1)                                .minimumScaleFactor(0.7)                                .faceInk(.ink)                                .accessibilityIdentifier("face-date")                        }                        .facePart(.when)                    }                    Spacer(minLength: 8)                    if let group {                        PaperLabel(words: group)                            .facePart(.group)                    }                }            }            .padding(ArtifactFaceKnobs.inset)        }        .onCardPaper(ArtifactFaceCatalogue.stock.laid(card.pace))    }}/// A browser's window: a bar with its three dots and an address field, the/// page under it — or, while there is no picture, a blank page ruled inside.private struct BrowserWindow<Picture: View>: View {    let picture: Picture?    @Environment(\.theme) private var theme    var body: some View {        VStack(spacing: 0) {            HStack(spacing: ArtifactFaceKnobs.dot * 0.8) {                ForEach([PaperInk.red, .pencil, .green], id: \.self) { ink in                    Circle()                        .fill(theme.print(ink).opacity(ArtifactFaceKnobs.dotOpacity))                        .frame(width: ArtifactFaceKnobs.dot, height: ArtifactFaceKnobs.dot)                }                RoundedRectangle(cornerRadius: 2)                    .stroke(theme.print(.pencil).opacity(0.35), lineWidth: ArtifactFaceKnobs.line)                    .frame(height: ArtifactFaceKnobs.field)                    .padding(.leading, ArtifactFaceKnobs.dot)            }            .padding(.horizontal, 8)            .frame(height: ArtifactFaceKnobs.bar)            Rectangle().fill(theme.print(.pencil).opacity(0.25)).frame(height: ArtifactFaceKnobs.line)            ZStack {                Rectangle().fill(theme.faceStocks.printMount)                if let picture {                    Color.clear.overlay(picture, alignment: .top).clipped()                } else {                    RoundedRectangle(cornerRadius: 2)                        .stroke(theme.print(.pencil).opacity(0.22), lineWidth: ArtifactFaceKnobs.line)                        .padding(ArtifactFaceKnobs.blankInset)                }            }        }        .background(theme.faceStocks.printMount)        .overlay(Rectangle().stroke(theme.print(.pencil).opacity(0.3), lineWidth: ArtifactFaceKnobs.line))        .accessibilityHidden(true)    }}/// A word typed on a slip of buff paper, a little askew (a group's label).struct PaperLabel: View {    let words: String    @Environment(\.theme) private var theme    var body: some View {        Text(words)            .font(CottonFonts.type(FacePartsKnobs.tagSize))            .lineLimit(1)            .faceInk(.ink)            .padding(.horizontal, 14)            .padding(.vertical, 8)            .background(                PaperSurface(dye: .buff, edge: .torn, seed: "label-\(words)")                    .saturation(1.3)                    .shadow(color: theme.colors.shadow.color.opacity(0.2), radius: 1.5, x: 0, y: 1)            )            .rotationEffect(.degrees(-4))            .accessibilityIdentifier("face-group")    }}/// An artifact's title as the card says it: a page titled with its own/// address reads by the address's last part ("trip-planner.html" → "Trip/// planner"), its host kept apart; any other title as it is.struct ArtifactTitle: Equatable {    let words: String    let host: String?}extension ArtifactTitle {    init(_ title: String) {        let trimmed = title.trimmingCharacters(in: .whitespacesAndNewlines)        guard trimmed.contains("://"), let url = URL(string: trimmed), let host = url.host() else {            words = title            host = nil            return        }        let last = url.deletingPathExtension().lastPathComponent        let spoken = last.replacingOccurrences(of: "-", with: " ").replacingOccurrences(of: "_", with: " ")            .trimmingCharacters(in: .whitespaces)        if spoken.isEmpty || spoken == "/" {            words = host            self.host = nil        } else {            words = spoken.prefix(1).uppercased() + spoken.dropFirst()            self.host = host        }    }}/// The artifact face, by the kind's name, and the cards it is reviewed in.public enum ArtifactFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "artifact", specimens: [Specimen("planner", planner, picture: "verandah-wide")]) { card, picture, _ in        ArtifactFace(card: card, picture: picture)    }    public static let planner = DeckCard(face: .artifact, fields: [        "subtitle": .text("Artifact"),        "title": .text("Trip budget planner"),        "source": .text("made with Claude"),        "date": .text("14 Oct 2026"),        "group": .text("Travel"),        "photo": .text("verandah-wide"),    ])}