Cotton code
388 lines · 17.8 KB · SwiftRaw
// The ALBUM as a SLIDE CAROUSEL TRAY (Cotton Photos, 12 Nov 2026; the// coordinator's brief for the photos-mac lane): a round projector tray of// charcoal plastic lying on the darkroom bench (`FaceGround.darkroomBench`),// its slides standing in its slots, a few of them taken out and laid on the// bench in front of it. No paper but the label: a tray is plastic, a slide's// mount is plastic, and only the round label pressed on its hub is paper// (the owner: paper only where the real thing is paper). Nothing is painted yet:// the tray, its slots and the mounts are drawn from `PhotosMaterial`'s// colours; a painted tray replaces them as the album board's paint did.// The face draws live what the album decides:////   a slide's top edge in a slot for each photo the album holds, from the//   tray's notch round, eighty slots at most: an album of twelve fills//   twelve, a big album fills the tray;//   up to three of its prints as slides laid on the bench across the tray's//   front, the cover first, largest, in the middle and on top, the other two//   under it to either side; fewer prints leave the bench bare, never a//   stand-in slide;//   the round paper label on the hub: the album's name (three lines at most,//   set smaller before it is cut) and under it how many photos it holds and//   its days.//// Every word is the core's (`DeckCard`): the name (`title`), the count// (`count`) and the days (`span`); the prints are the album's own rows the// deck reads for its card (`DeckCard.prints`, the cover named by `photo`// first). Stepped by a tap, it cuts (`FaceStep.cut`).import CottonPaperimport CottonValuesimport SwiftUIenum AlbumCarouselFaceKnobs {    /// The face's room, its width over its height: the tray above, the    /// slides laid on the bench across its front.    static let aspect: CGFloat = 0.8    /// The face in the card's room: the most of its width and height.    static let fill: CGFloat = 0.97    // THE TRAY, as shares of the face's width: its diameter and its middle.    static let tray: CGFloat = 0.94    static let trayMiddle = CGPoint(x: 0.5, y: 0.5)    /// The slot ring's inner and outer edge, the hub's edge and the label's,    /// as shares of the tray's radius.    static let slotsInner: CGFloat = 0.6    static let slotsOuter: CGFloat = 0.93    static let hub: CGFloat = 0.55    static let label: CGFloat = 0.45    /// A carousel holds eighty slides.    static let slots = 80    /// The slot the notch stands at, where the first slide goes: twelve    /// o'clock, the round then running clockwise.    static let notch: Double = -90    /// A slide's top edge in its slot, as a share of the slot's width.    static let slideEdge: CGFloat = 0.42    // A SLIDE'S MOUNT: square, its corners rounded, its window a 3:2    // landscape (a 35 mm frame in a 50 mm mount), as shares of its side.    static let mountCorner: CGFloat = 0.07    static let window = CGSize(width: 0.72, height: 0.48)    /// A slide's shadow on the bench, as a share of its side.    static let slideShadow: CGFloat = 0.05    /// The type on the label, as shares of the face's width: print on an    /// object scales with the object, never with Dynamic Type.    static let nameSize: CGFloat = 0.05    static let factsSize: CGFloat = 0.034    static let nameLines = 3    static let nameLeast: CGFloat = 0.7    static let lineGap: CGFloat = 0.008}/// One of the bench's three places for a slide taken out of the tray: its/// middle, as shares of the face's width and height, its side as a share of/// the face's width, and how far it is turned.public struct SlidePlace: Equatable, Sendable {    public var x: CGFloat    public var y: CGFloat    public var side: CGFloat    public var turn: Double    /// The three places, the cover's first, in the order the album's prints    /// fill them: a fourth would hang off the card's foot under the cover's    /// shadow (12 Nov 2026).    public static let all = [        SlidePlace(x: 0.5, y: 0.84, side: 0.36, turn: -3),        SlidePlace(x: 0.2, y: 0.86, side: 0.29, turn: -11),        SlidePlace(x: 0.8, y: 0.85, side: 0.29, turn: 8),    ]    /// The order the places are laid in, back to front: the cover last, on top.    public static let stacking = [1, 2, 0]}/// What the carousel shows of an album.public enum CarouselSlides {    /// The prints laid on the bench: the cover first, then the album's    /// others as it holds them, at most one for each place.    public static func shown(_ card: DeckCard) -> [String] {        Array(AlbumPrints.ordered(card.prints ?? [], cover: card.text("photo")).prefix(SlidePlace.all.count))    }    /// How many slots hold a slide: the photos the album holds, as the core    /// counts them ("1,204" reads 1204), the tray's eighty at most.    public static func filled(_ card: DeckCard) -> Int {        let number = card.text("count").flatMap { Int($0.filter(\.isNumber)) } ?? 0        return min(max(number, 0), AlbumCarouselFaceKnobs.slots)    }    /// The angle, in degrees, of slot `index`'s middle, clockwise from the notch.    public static func angle(of index: Int) -> Double {        AlbumCarouselFaceKnobs.notch + 360 * (Double(index) + 0.5) / Double(AlbumCarouselFaceKnobs.slots)    }}public struct AlbumCarouselFace: View {    let card: DeckCard    public init(card: DeckCard) {        self.card = card    }    public var body: some View {        let knobs = AlbumCarouselFaceKnobs.self        GeometryReader { box in            let width = min(box.size.width * knobs.fill, box.size.height * knobs.fill * knobs.aspect)            let size = CGSize(width: width, height: width / knobs.aspect)            CarouselOnBench(card: card, size: size)                .frame(width: size.width, height: size.height)                .frame(width: box.size.width, height: box.size.height)        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("album-carousel")    }}/// The tray with its slides and its label, and the slides laid in front of it.private struct CarouselOnBench: View {    let card: DeckCard    let size: CGSize    var body: some View {        let knobs = AlbumCarouselFaceKnobs.self        let w = size.width        let diameter = knobs.tray * w        let middle = CGPoint(x: knobs.trayMiddle.x * w, y: knobs.trayMiddle.y * w)        let prints = CarouselSlides.shown(card)        ZStack(alignment: .topLeading) {            Tray(filled: CarouselSlides.filled(card), diameter: diameter)                .frame(width: diameter, height: diameter)                .position(middle)                .accessibilityHidden(true)            HubLabel(card: card, diameter: knobs.label * diameter, face: w)                .position(middle)            ForEach(SlidePlace.stacking, id: \.self) { index in                if index < prints.count {                    let place = SlidePlace.all[index]                    Slide(key: prints[index], side: place.side * w)                        .rotationEffect(.degrees(place.turn))                        .position(x: place.x * w, y: place.y * size.height)                        .facePart(.picture)                }            }        }        .frame(width: w, height: size.height, alignment: .topLeading)    }}/// The tray seen from above: its shadow on the bench, its outer wall, the/// ring of eighty slots with a slide's top edge in each filled one, the/// notch where the round starts, and its hub.private struct Tray: View {    let filled: Int    let diameter: CGFloat    var body: some View {        let knobs = AlbumCarouselFaceKnobs.self        Canvas { context, size in            let r = min(size.width, size.height) / 2            let c = CGPoint(x: size.width / 2, y: size.height / 2)            func ring(_ radius: CGFloat) -> Path {                Path(ellipseIn: CGRect(x: c.x - radius, y: c.y - radius, width: 2 * radius, height: 2 * radius))            }            // Its shadow on the bench, under the safelight from the upper left.            var shadow = context            shadow.addFilter(.blur(radius: r * 0.05))            shadow.translateBy(x: r * 0.03, y: r * 0.05)            shadow.fill(ring(r), with: .color(.black.opacity(0.6)))            // The outer wall, moulded charcoal, lit at its upper left rim.            context.fill(ring(r), with: .radialGradient(                Gradient(colors: [PhotosMaterial.trayLit.colour, PhotosMaterial.tray.colour]),                center: CGPoint(x: c.x - r * 0.3, y: c.y - r * 0.35), startRadius: 0, endRadius: r * 1.4))            context.stroke(ring(r - 0.6), with: .color(PhotosMaterial.trayLit.colour.opacity(0.8)), lineWidth: 1.2)            // The slot ring's recess.            let outer = knobs.slotsOuter * r            let inner = knobs.slotsInner * r            var recess = ring(outer)            recess.addPath(ring(inner))            context.fill(recess, with: .color(PhotosMaterial.trayRecess.colour), style: FillStyle(eoFill: true))            // The dividers between the slots, and a slide's top in each filled one.            for index in 0..<knobs.slots {                let wall = (knobs.notch + 360 * Double(index) / Double(knobs.slots)) * .pi / 180                var divider = Path()                divider.move(to: CGPoint(x: c.x + inner * cos(wall), y: c.y + inner * sin(wall)))                divider.addLine(to: CGPoint(x: c.x + outer * cos(wall), y: c.y + outer * sin(wall)))                context.stroke(divider, with: .color(PhotosMaterial.tray.colour), lineWidth: max(0.6, r * 0.012))                guard index < filled else { continue }                let middle = CarouselSlides.angle(of: index) * .pi / 180                let pitch = 2 * .pi * (inner + outer) / 2 / CGFloat(knobs.slots)                var edge = Path()                let from = inner + (outer - inner) * 0.06                let to = outer - (outer - inner) * 0.06                edge.move(to: CGPoint(x: c.x + from * cos(middle), y: c.y + from * sin(middle)))                edge.addLine(to: CGPoint(x: c.x + to * cos(middle), y: c.y + to * sin(middle)))                context.stroke(edge, with: .color(PhotosMaterial.mount.colour.opacity(0.92)),                               style: StrokeStyle(lineWidth: max(0.5, pitch * knobs.slideEdge), lineCap: .round))            }            // The notch: a small bright tab on the wall at the first slot.            let notch = knobs.notch * .pi / 180            let tab = CGPoint(x: c.x + (outer + (r - outer) / 2) * cos(notch), y: c.y + (outer + (r - outer) / 2) * sin(notch))            context.fill(Path(ellipseIn: CGRect(x: tab.x - r * 0.025, y: tab.y - r * 0.025, width: r * 0.05, height: r * 0.05)),                         with: .color(PhotosMaterial.mount.colour.opacity(0.7)))            // The hub, a raised disc, and its rim.            let hub = knobs.hub * r            context.fill(ring(inner * 0.99), with: .color(PhotosMaterial.tray.colour))            context.fill(ring(hub), with: .radialGradient(                Gradient(colors: [PhotosMaterial.trayLit.colour, PhotosMaterial.trayRecess.colour]),                center: CGPoint(x: c.x - hub * 0.4, y: c.y - hub * 0.45), startRadius: 0, endRadius: hub * 1.6))            context.stroke(ring(hub), with: .color(.black.opacity(0.45)), lineWidth: max(0.8, r * 0.01))        }    }}/// One slide taken out of the tray: the picture in its mount's window, the/// moulded mount round it, its shadow on the bench.private struct Slide: View {    let key: String    /// The mount's side, in points.    let side: CGFloat    var body: some View {        let knobs = AlbumCarouselFaceKnobs.self        let window = CGSize(width: knobs.window.width * side, height: knobs.window.height * side)        let corner = knobs.mountCorner * side        ZStack {            RoundedRectangle(cornerRadius: corner)                .fill(.black.opacity(0.55))                .blur(radius: knobs.slideShadow * side * 0.6)                .offset(x: knobs.slideShadow * side * 0.5, y: knobs.slideShadow * side)                .accessibilityHidden(true)            FacePicture(key: key, edge: max(window.width, window.height))                .frame(width: window.width, height: window.height)                .clipped()            Rectangle()                .path(in: CGRect(x: 0, y: 0, width: side, height: side))                .subtracting(Rectangle().path(in: CGRect(                    x: (side - window.width) / 2, y: (side - window.height) / 2,                    width: window.width, height: window.height)))                .fill(LinearGradient(colors: [PhotosMaterial.mountLit.colour, PhotosMaterial.mount.colour],                                     startPoint: .topLeading, endPoint: .bottomTrailing))                .clipShape(RoundedRectangle(cornerRadius: corner))                .overlay {                    RoundedRectangle(cornerRadius: corner)                        .strokeBorder(.black.opacity(0.18), lineWidth: max(0.5, side * 0.006))                }                .overlay {                    // The window's moulded lip.                    Rectangle()                        .strokeBorder(.black.opacity(0.35), lineWidth: max(0.5, side * 0.008))                        .frame(width: window.width, height: window.height)                }                .accessibilityHidden(true)        }        .frame(width: side, height: side)    }}/// The round paper label pressed on the hub: the album's name, and under it/// its count and its days.private struct HubLabel: View {    let card: DeckCard    /// The label's diameter, and the face's width, in points.    let diameter: CGFloat    let face: CGFloat    var body: some View {        let knobs = AlbumCarouselFaceKnobs.self        let facts = BoardPrints.facts(card)        // The words keep to the square inside the round label.        let room = diameter * 0.74        ZStack {            Circle()                .fill(PhotosMaterial.label.colour)                .overlay(Circle().strokeBorder(.black.opacity(0.2), lineWidth: max(0.5, diameter * 0.008)))                .shadow(color: .black.opacity(0.35), radius: diameter * 0.01, y: diameter * 0.008)                .accessibilityHidden(true)            VStack(spacing: knobs.lineGap * face) {                if let title = card.text("title") {                    Text(title)                        .font(CottonFonts.type(knobs.nameSize * face))                        .lineLimit(knobs.nameLines)                        .minimumScaleFactor(knobs.nameLeast)                        .multilineTextAlignment(.center)                        .foregroundStyle(PhotosMaterial.type.colour)                        .accessibilityIdentifier("deck-title")                        .facePart(.title)                }                if let facts {                    CarouselFacts(card: card, size: knobs.factsSize * face)                        .accessibilityRepresentation { Text(facts) }                        .accessibilityIdentifier("album-carousel-facts")                }            }            .frame(width: room, height: room)        }        .frame(width: diameter, height: diameter)    }}/// The label's second line: the count (the corner's) and the days (when),/// each its own part, joined as the core joins facts.private struct CarouselFacts: View {    let card: DeckCard    let size: CGFloat    var body: some View {        let count = BoardPrints.count(card)        let span = card.text("span")        VStack(spacing: 0) {            if let count {                Text(count).facePart(.corner)            }            if let span {                Text(span).facePart(.when)            }        }        .font(CottonFonts.type(size))        .lineLimit(1)        .minimumScaleFactor(0.6)        .multilineTextAlignment(.center)        .foregroundStyle(PhotosMaterial.type.colour)    }}/// The slide carousel, by its id beside the album's board, and the albums it/// is reviewed in: invented pictures only.public enum AlbumCarouselFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .darkroomBench    public static let face = Face(id: "album.carousel", specimens: [        Specimen("weekend", weekend, picture: "kelmoor-lake"), Specimen("two", two, picture: "verandah"),        Specimen("long-name", longName, picture: "waterfall"), Specimen("empty", empty),    ], steps: .cut) { card, _, _ in        AlbumCarouselFace(card: card)    }    static func album(_ fields: [String: RecordValue], prints: [String]) -> DeckCard {        var card = DeckCard(face: "album.carousel", corner: nil, pressing: false, pace: nil, plate: nil, fields: fields)        card.prints = prints        return card    }    /// A weekend away: more prints than the bench takes, the lake its cover.    public static let weekend = album([        "title": .text("Weekend in Kelmoor"),        "count": .text("23"),        "span": .text("Sun 26 Oct"),        "photo": .text("kelmoor-lake"),    ], prints: ["kelmoor-jetty", "kelmoor-lake", "portrait", "verandah", "waterfall", "verandah-wide"])    /// An afternoon's two prints: two slides out, two in the tray.    public static let two = album([        "title": .text("Tea at the hill house"),        "count": .text("2"),        "span": .text("Sat 17 Oct"),        "photo": .text("verandah"),    ], prints: ["verandah", "verandah-wide"])    /// A long name, and more photos than the tray holds: every slot full.    public static let longName = album([        "title": .text("The long drive down the coast and the night at the lighthouse"),        "count": .text("1,204"),        "span": .text("Fri 2 Oct \u{2013} Sun 11 Oct"),        "photo": .text("waterfall"),    ], prints: ["waterfall", "kelmoor-jetty", "verandah-wide", "portrait", "kelmoor-lake"])    /// A new album with nothing in it yet: an empty tray, a bare bench.    public static let empty = album([        "title": .text("Diwali"),        "count": .text("0"),    ], prints: [])}