Cotton code
142 lines · 5.9 KB · SwiftRaw
// Nancy's Naga Kitchen's CHEF (Codex round 2, nan-chef): her own portrait// from the database, tall beside her name (her letters on a round mount with// none — never a stock face), where she is from, her rating, her reviews and// how long she has cooked here, each under its label by its symbol, and her// words under them. Every word is the core's.import CottonPaperimport CottonValuesimport SwiftUI/// The chef: her portrait tall beside her name and facts, her words under.public struct KitchenChefFace<Portrait: View>: View {    let card: DeckCard    var portrait: Portrait?    var rating: Double?    var labels = CardLabels()    @Environment(\.theme) private var theme    public init(card: DeckCard, portrait: Portrait? = nil, rating: Double? = nil, labels: CardLabels = CardLabels()) {        self.card = card        self.portrait = portrait        self.rating = rating        self.labels = labels    }    public var body: some View {        GeometryReader { room in            VStack(alignment: .leading, spacing: 14) {                HStack(alignment: .top, spacing: 14) {                    KitchenChefHead(card: card, labels: labels)                    KitchenPortraitFrame(card: card, picture: portrait)                        .frame(width: (room.size.width - 2 * KitchenKnobs.side) * 0.5, height: room.size.height * 0.42)                        .facePart(.picture)                }                KitchenChefFigures(card: card, rating: rating, labels: labels)                if let words = card.text("body") {                    Text(words)                        .font(theme.type.body(KitchenKnobs.body + 1))                        .foregroundStyle(theme.colors.inkBody.color)                        .minimumScaleFactor(0.7)                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)                        .facePart(.body)                }            }            .padding(KitchenKnobs.side)        }        .onCardPaper(KitchenChefFaceCatalogue.stock.laid(card.pace))    }}private struct KitchenChefHead: View {    let card: DeckCard    let labels: CardLabels    var body: some View {        VStack(alignment: .leading, spacing: 12) {            if let name = card.text("title") {                KitchenHeading(words: name, size: 52).lineLimit(2).minimumScaleFactor(0.5).accessibilityIdentifier("deck-title")                    .facePart(.title)            }            if let from = card.text("subtitle") {                KitchenFact(label: labels[slot: "subtitle"], said: from, symbol: "mappin.and.ellipse", lines: 3)                    .facePart(.subtitle)            }            Spacer(minLength: 0)        }        .frame(maxWidth: .infinity, alignment: .leading)    }}/// Her rating, her reviews and how long she has cooked here: each figure/// under its label, a rule above them.private struct KitchenChefFigures: View {    let card: DeckCard    var rating: Double?    let labels: CardLabels    @Environment(\.theme) private var theme    var body: some View {        let ratingLabel = labels[column: KitchenColumns.rating]        let sinceLabel = labels[slot: "date"]        let facts = card.kitchenFacts(except: Set([ratingLabel, sinceLabel].compactMap { $0 }))        VStack(spacing: 10) {            Rectangle().fill(theme.colors.hairline.color).frame(height: 1)            HStack(alignment: .bottom, spacing: 12) {                if let rating {                    KitchenFigure(label: ratingLabel, words: rating.formatted(.number.precision(.fractionLength(1))),                                  size: 30, ink: theme.colors.accent.color)                        .accessibilityIdentifier("face-rating")                }                ForEach(Array(facts.prefix(1).enumerated()), id: \.offset) { _, fact in                    Spacer(minLength: 0)                    KitchenFigure(label: fact.label, words: fact.said, size: 30)                }                if let date = card.text("date") {                    Spacer(minLength: 0)                    KitchenFigure(label: sinceLabel, words: date, size: 30, trailing: true)                        .facePart(.when)                }            }        }    }}/// A portrait in a terracotta frame; with none, the initials on their/// round mount.struct KitchenPortraitFrame<Picture: View>: View {    let card: DeckCard    let picture: Picture?    @Environment(\.theme) private var theme    var body: some View {        let shape = RoundedRectangle(cornerRadius: KitchenKnobs.radius, style: .continuous)        if let picture {            ZStack {                shape.fill(theme.colors.groundMuted.color)                picture.frame(maxWidth: .infinity, maxHeight: .infinity).clipShape(shape)            }            .overlay(shape.stroke(theme.colors.accent.color, lineWidth: 3))            .shadow(color: .black.opacity(KitchenKnobs.shadow), radius: KitchenKnobs.shadowRadius, y: KitchenKnobs.shadowDrop)            .accessibilityIdentifier("face-portrait")        } else if case .monogram(let letters)? = KitchenPortrait.of(card) {            KitchenInitials(letters: letters).frame(maxHeight: .infinity, alignment: .top)        }    }}/// The kitchen's chef face, and the cards it is reviewed in. The kitchen's/// kit names it for its chef (`nancy.chef`), with her rating and her table's/// labels.public enum KitchenChefFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "nancy.chef", specimens: [Specimen("chef", chef)]) { card, picture, _ in        KitchenChefFace(card: card, portrait: picture, rating: card.stars, labels: card.labels)    }    public static let chef = DeckCard(face: .plain, fields: [        "title": .text("Invented Chef"), "subtitle": .text("Invented town"), "body": .text("Invented words."),        "date": .text("3 months"), "facts": .list([.object(["label": .text("Reviews"), "said": .text("21")])]),    ])}