Cotton code
231 lines · 10.2 KB · SwiftRaw
// The CODING SESSION face (KINDS, 7f4a5f0fb; the house references 99 and// 100): the framed cream card with its title typed heavy and when it was// last active by hand under it; where it runs — the machine, the branch, the// model (`source`, cut where the core joined them) — typed one under another// on the left, with its state stamped at their right in the ink its stored// value calls for, green while it runs (a green dot beside the stamp), red// while it waits on the owner or has failed, pencil while it queues or// starts, and nothing stamped once it has ended; the last line it said typed// under them, in a speech bubble while it waits, since then the line is// what it asks the owner; what it has done (`body`) in pencil beside the// laptop at the foot. Its task list is the page after it (`card.pages`,// `FaceTurns`, its pages on its card). Every// word is the core's (`DeckCard`); the face lays them out.import CottonPaperimport CottonValuesimport SwiftUIenum SessionFaceKnobs {    static let inset = EdgeInsets(top: 28, leading: 26, bottom: 34, trailing: 24)    static let titleSize: CGFloat = 26    static let dateHand: CGFloat = 24    static let whereSize: CGFloat = 15    static let whereLeading: CGFloat = 4    static let stampSize: CGFloat = 16    static let stampTurn: Double = -7    static let dot: CGFloat = 9    /// The rings of the dot's glow, as multiples of the dot.    static let glow: [CGFloat] = [2.2, 3.6]    static let glowOpacity: Double = 0.16    static let lineSize: CGFloat = 15    static let lineLimit = 3    static let bubbleSize: CGFloat = 16    static let summarySize: CGFloat = 12.5    static let artShare: CGFloat = 0.3    /// The laptop with no summary beside it.    static let artAlone: CGFloat = 0.36    static let gap: CGFloat = 16}/// How a session's state is marked, by its stored value.public enum SessionStateMark {    /// The ink its stamp is laid in.    public static func ink(_ state: String?) -> PaperInk {        switch state {        case "running": .green        case "waiting", "failed": .red        default: .pencil        }    }    /// Waiting on the owner: its last line is what it asks.    public static func asks(_ state: String?) -> Bool { state == "waiting" }}public struct SessionFace: View {    let card: DeckCard    /// The ink its state is stamped in (`SessionStateMark.ink`).    var mark: PaperInk = .pencil    /// Its last line is a question to the owner (`SessionStateMark.asks`).    var asking = false    /// The pages its card turns to after it (`card.pages`), each a column's    /// words under its label (`FaceTurns`).    var pages: [CardPage] = []    /// The page shown first: 0, the face (a gallery's specimen of a page).    var opened = 0    public init(card: DeckCard, mark: PaperInk = .pencil, asking: Bool = false, pages: [CardPage] = [], opened: Int = 0) {        self.card = card        self.mark = mark        self.asking = asking        self.pages = pages        self.opened = opened    }    public var body: some View {        FaceTurns(pages: pages, opened: opened) { front }            .onCardPaper(SessionFaceCatalogue.stock.laid(card.pace))    }    /// The face itself, before the pages its card turns to.    @ViewBuilder private var front: some View {        GeometryReader { sheet in            VStack(alignment: .leading, spacing: 0) {                if let title = card.text("title") {                    HeavyType(words: title, size: SessionFaceKnobs.titleSize)                        .lineLimit(3)                        .minimumScaleFactor(0.75)                        .accessibilityIdentifier("deck-title")                        .facePart(.title)                }                if let active = card.text("date") {                    Text(HandRun.tail(active))                        .font(CottonFonts.hand(SessionFaceKnobs.dateHand))                        .lineLimit(1)                        .minimumScaleFactor(0.7)                        .faceInk(.ink)                        .padding(.top, 2)                        .accessibilityIdentifier("face-date")                        .facePart(.when)                }                place.padding(.top, SessionFaceKnobs.gap)                if let line = card.text("subtitle") {                    Group {                        if asking {                            SpeechBubble(words: line, size: SessionFaceKnobs.bubbleSize, lines: SessionFaceKnobs.lineLimit)                                .padding(.leading, ChatSlipShape.tail)                        } else {                            Text(line)                                .font(CottonFonts.type(SessionFaceKnobs.lineSize))                                .lineLimit(SessionFaceKnobs.lineLimit)                                .lineSpacing(3)                                .faceInk(.ink)                                .fixedSize(horizontal: false, vertical: true)                        }                    }                    .padding(.top, SessionFaceKnobs.gap)                    .accessibilityIdentifier("face-last-line")                    .facePart(.subtitle)                }                HStack(alignment: .bottom, spacing: SessionFaceKnobs.gap) {                    if let summary = card.text("body") {                        Text(summary)                            .font(CottonFonts.type(SessionFaceKnobs.summarySize))                            .lineSpacing(3)                            // As many lines as the room beside the laptop holds.                            .truncationMode(.tail)                            .faceInk(.pencil)                            .frame(maxWidth: .infinity, alignment: .topLeading)                            .frame(maxHeight: .infinity, alignment: .top)                            .accessibilityIdentifier("face-summary")                            .facePart(.body)                    } else {                        Spacer(minLength: 0)                    }                    FaceArt(name: "face-art-laptop")                        .frame(width: sheet.size.width * (card.text("body") == nil ? SessionFaceKnobs.artAlone : SessionFaceKnobs.artShare))                        .frame(maxHeight: .infinity, alignment: .bottom)                }                .padding(.top, SessionFaceKnobs.gap)            }            .padding(SessionFaceKnobs.inset)        }    }    /// Where it runs, typed one under another, its state stamped at their right.    @ViewBuilder private var place: some View {        let chips = DeckCard.chips(card.text("source"))        let state = card.text("state")        if !chips.isEmpty || state != nil {            HStack(alignment: .center, spacing: SessionFaceKnobs.gap) {                VStack(alignment: .leading, spacing: SessionFaceKnobs.whereLeading) {                    ForEach(Array(chips.enumerated()), id: \.offset) { _, chip in                        Text(chip)                            .font(CottonFonts.type(SessionFaceKnobs.whereSize))                            .lineLimit(1)                            .minimumScaleFactor(0.7)                            .faceInk(.ink)                    }                }                .accessibilityElement(children: .combine)                .accessibilityIdentifier("face-chips")                Spacer(minLength: 0)                if let state {                    HStack(spacing: SessionFaceKnobs.gap) {                        if mark == .green { SessionDot() }                        RubberStamp(words: state, size: SessionFaceKnobs.stampSize, turn: SessionFaceKnobs.stampTurn, ink: mark)                    }                    .fixedSize()                    .facePart(.state)                }            }        }    }}/// The green dot of a session at work, its glow in fading rings.private struct SessionDot: View {    @Environment(\.theme) private var theme    var body: some View {        ZStack {            ForEach(SessionFaceKnobs.glow, id: \.self) { ring in                Circle()                    .fill(theme.print(.green).opacity(SessionFaceKnobs.glowOpacity))                    .frame(width: SessionFaceKnobs.dot * ring, height: SessionFaceKnobs.dot * ring)            }            Circle()                .fill(theme.print(.green))                .frame(width: SessionFaceKnobs.dot, height: SessionFaceKnobs.dot)        }        .blendMode(theme.printBlend)        .accessibilityHidden(true)    }}/// The coding session face, by the kind's name, and the cards it is reviewed/// in: its state marked off the stored value (`stored`), its task list the/// page after it.public enum SessionFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "session", specimens: [        Specimen("running", runningSession.reading {            $0.stored = ["state": "running"]            $0.pages = [CardPage(label: "Task list", text: "Draw the shelf face\nRegister it on the deck\nPush to main")]        }),        Specimen("failed", failedSession.reading { $0.stored = ["state": "failed"] }),    ]) { card, _, _ in        SessionFace(card: card, mark: SessionStateMark.ink(card.stored["state"]), asking: SessionStateMark.asks(card.stored["state"]), pages: card.pages)    }    /// A session running, with a task list to turn to.    public static let runningSession = DeckCard(face: .session, fields: [        "state": .text("Running"),        "title": .text("Example lane: the invented shelf face"),        "subtitle": .text("Built the shelf, now running the face tests"),        "body": .text("Drew the shelf face on the framed cream card, registered it on the deck and in the gallery, and measured it on the phone's stack."),        "source": .text("example-mac \u{00B7} shelf-face \u{00B7} model-x"),        "date": .text("2 minutes ago"),    ])    /// A session that failed, its last line the error.    public static let failedSession = DeckCard(face: .session, fields: [        "state": .text("Failed"),        "title": .text("Invented importer"),        "subtitle": .text("error: the example file could not be read"),        "source": .text("example-mini \u{00B7} main"),        "date": .text("yesterday"),    ])}