Cotton code
270 lines · 10.7 KB · SwiftRaw
// The DISH face (Nancy's kitchen, 23 Oct 2026): a plain card (`PlainFace`)// with a picture and a sum is a dish on its plate — the picture round in the// middle, the title typed heavy, the kind and the spice as chips (`subtitle`// and its `also`, cut where the core joined them; the kind marked green for// vegetarian, red for meat, as an Indian menu marks them), the rating in// stars beside the number of reviews (`count`), the price large, and the// start of the description (`body`). The columns its card turns to// (`card.pages`: the description, then what buyers say) are the pages after// it, each under its column's label and cut to the room as a note's. Every// word is the core's (`DeckCard`); the face lays them out. The app reads the// rating, the kind and the pages off the dish's table (CottonUI's// `DeckCardFill`) and hands them in on its card. In Nancy's app the kitchen's// own front is drawn instead (`KitchenDishFront`), with the same pages.import CottonPaperimport CottonValuesimport SwiftUIenum DishFaceKnobs {    static let inset = EdgeInsets(top: 24, leading: 24, bottom: 20, trailing: 22)    static let titleSize: CGFloat = 22    static let sumSize: CGFloat = 30    static let bodySize: CGFloat = 13    static let bodyLines = 3    static let pageSize: CGFloat = 14    static let gap: CGFloat = 12    /// A plate's width, of the card's inside.    static let plateShare: CGFloat = 0.56    static let plateRim: CGFloat = 5    static let plateShadow = 0.12    static let plateShadowRadius: CGFloat = 4    static let plateShadowDrop: CGFloat = 2    static let chipSize: CGFloat = 12    static let chipPad = EdgeInsets(top: 4, leading: 9, bottom: 4, trailing: 10)    static let chipMark: CGFloat = 8    static let countSize: CGFloat = 13    static let dot: CGFloat = 5    /// A page not shown, its dot.    static let dotFaint = 0.4    /// How far a swipe travels before it turns a page.    static let turn: CGFloat = 40}extension DeckCard {    /// A plain card with a picture and a sum is a dish.    public var isDish: Bool { text("photo") != nil && text("amount") != nil }}/// The ink a dish's kind is marked in; any other kind unmarked.public enum DishKindMark {    public static func ink(_ kind: String?) -> PaperInk? {        switch kind {        case "veg": .green        case "non_veg": .red        default: nil        }    }}public struct DishFace<Picture: View>: View {    let card: DeckCard    var picture: Picture?    var pages: [CardPage] = []    var rating: Double?    var mark: PaperInk?    /// In Nancy's app the kitchen's own front (`KitchenDishFront`), its    /// facts under the package's labels; the pages after it are the same.    var kitchen: CardLabels?    @Environment(\.deckPassOn) private var passOn    @State private var page = 0    public init(        card: DeckCard, picture: Picture?, pages: [CardPage] = [], rating: Double? = nil, mark: PaperInk? = nil,        kitchen: CardLabels? = nil    ) {        self.card = card        self.picture = picture        self.pages = pages        self.rating = rating        self.mark = mark        self.kitchen = kitchen    }    public var body: some View {        let count = 1 + pages.count        VStack(alignment: .leading, spacing: 0) {            Group {                if page == 0 { front } else { turned(pages[page - 1], count: count) }            }            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)            if count > 1 { dots(count) }        }        .padding(kitchen == nil ? DishFaceKnobs.inset : KitchenKnobs.dishInset)        .contentShape(Rectangle())        // A tap turns to the next page, the last back to the first; a swipe        // turns either way, and past either end moves card.        .onTapGesture { if count > 1 { page = (page + 1) % count } }        .facePages(count > 1 ? turning(count: count) : nil, at: "\(page)/\(count)") { forward in            let next = page + (forward ? 1 : -1)            if (0..<count).contains(next) { page = next } else { passOn?(forward) }        }        .animation(.easeOut(duration: 0.18), value: page)        .onCardPaper((kitchen == nil ? DishFaceCatalogue.stock : KitchenDishFaceCatalogue.stock).laid(card.pace))    }    @ViewBuilder private var front: some View {        if let kitchen {            KitchenDishFront(card: card, picture: picture, rating: rating, mark: mark, labels: kitchen)        } else {            plate        }    }    private var plate: some View {        GeometryReader { room in            let edge = room.size.width * DishFaceKnobs.plateShare            VStack(alignment: .leading, spacing: DishFaceKnobs.gap) {                HStack {                    Spacer(minLength: 0)                    PlateMount(picture: picture)                        .frame(width: edge, height: edge)                        .facePart(.picture)                    Spacer(minLength: 0)                }                if let title = card.text("title") {                    HeavyType(words: title, size: DishFaceKnobs.titleSize)                        .lineLimit(3)                        .minimumScaleFactor(0.7)                        .accessibilityIdentifier("deck-title")                        .facePart(.title)                }                let chips = DeckCard.chips(card.text("subtitle"))                if !chips.isEmpty {                    HStack(spacing: 6) {                        ForEach(Array(chips.enumerated()), id: \.offset) { index, chip in                            FaceChip(words: chip, mark: index == 0 ? mark : nil)                        }                    }                    .accessibilityElement(children: .combine)                    .accessibilityIdentifier("face-chips")                    .facePart(.subtitle)                }                HStack(alignment: .lastTextBaseline, spacing: 8) {                    if let rating { StarRow(rating: rating) }                    if let reviews = card.text("count") {                        Text(reviews)                            .font(CottonFonts.type(DishFaceKnobs.countSize))                            .faceInk(.pencil)                            .accessibilityIdentifier("face-count")                    }                    Spacer(minLength: 0)                    if let sum = card.text("amount") {                        Text(sum)                            .font(CottonFonts.type(DishFaceKnobs.sumSize))                            .lineLimit(1)                            .minimumScaleFactor(0.6)                            .faceInk(.ink)                            .accessibilityIdentifier("face-sum")                            .facePart(.amount)                    }                }                if let body = card.text("body") {                    FaceFact(value: body, size: DishFaceKnobs.bodySize, lines: DishFaceKnobs.bodyLines)                }                Spacer(minLength: 0)            }        }    }    private func turned(_ shown: CardPage, count: Int) -> some View {        VStack(alignment: .leading, spacing: 10) {            FaceLabel(words: shown.label)            // The column's words cut to the room as a note's; past its last            // page the card turns on, past its first back.            FacePages(text: shown.text, size: DishFaceKnobs.pageSize)                .environment(\.deckPassOn, { forward in                    let next = page + (forward ? 1 : -1)                    if (0..<count).contains(next) { page = next } else { passOn?(forward) }                })        }        .accessibilityIdentifier("face-page-\(page)")    }    private func dots(_ count: Int) -> some View {        HStack(spacing: 6) {            Spacer(minLength: 0)            ForEach(0..<count, id: \.self) { index in                Circle()                    .frame(width: DishFaceKnobs.dot, height: DishFaceKnobs.dot)                    .faceInk(index == page ? .ink : .pencil)                    .opacity(index == page ? 1 : DishFaceKnobs.dotFaint)            }            Spacer(minLength: 0)        }        .padding(.top, 8)        .accessibilityHidden(true)    }    private func turning(count: Int) -> some Gesture {        DragGesture(minimumDistance: 16)            .onEnded { end in                let dx = end.translation.width                guard abs(dx) > DishFaceKnobs.turn, abs(dx) > abs(end.translation.height) else { return }                let next = page + (dx < 0 ? 1 : -1)                if (0..<count).contains(next) { page = next } else { passOn?(dx < 0) }            }    }}/// A dish's picture on its plate: round, a paper rim around it; with no/// picture, the empty plate.struct PlateMount<Picture: View>: View {    let picture: Picture?    @Environment(\.theme) private var theme    var body: some View {        ZStack {            Circle().fill(theme.sheet(.cream))            if let picture {                picture                    .clipShape(Circle())                    .padding(DishFaceKnobs.plateRim)            }            Circle().stroke(theme.colors.hairline.color, lineWidth: 1)        }        .shadow(color: .black.opacity(DishFaceKnobs.plateShadow), radius: DishFaceKnobs.plateShadowRadius,                y: DishFaceKnobs.plateShadowDrop)        .accessibilityIdentifier("face-plate")    }}/// One of a card's parts as a chip: its words in a hairline capsule, marked/// in an ink when it has one.struct FaceChip: View {    let words: String    var mark: PaperInk?    @Environment(\.theme) private var theme    var body: some View {        HStack(spacing: 5) {            if let mark {                Circle()                    .frame(width: DishFaceKnobs.chipMark, height: DishFaceKnobs.chipMark)                    .faceInk(mark)            }            Text(words)                .font(CottonFonts.type(DishFaceKnobs.chipSize))                .lineLimit(1)                .faceInk(.ink)        }        .padding(DishFaceKnobs.chipPad)        .overlay(Capsule().stroke(theme.colors.hairline.color, lineWidth: 1))    }}/// The dish face: a plain card with a picture and a sum, on its plate. No/// card kind names it; the plain face draws it for a dish (`isDish`)./// The fixtures hold no dish, so the specimen's plate is empty.public enum DishFaceCatalogue {    public static let stock: FaceStock = .framed    public static let ground: FaceGround = .paper    public static let face = Face(id: "plain.dish", specimens: [Specimen("dal", dal)]) { card, picture, _ in        DishFace(card: card, picture: picture)    }    public static let dal = DeckCard(face: .plain, fields: [        "title": .text("Invented dal"), "amount": .text("\u{20B9}180"),        "subtitle": .text("Veg \u{00B7} Mild"), "count": .text("23 reviews"), "body": .text("An invented dish."),    ])}