Cotton code
201 lines · 8.6 KB · SwiftRaw
// The PLAYLIST on the album face (card kind `album` on a table whose card// opens through its entries into tracks; the Codex playlist-1 card, 23 Oct// 2026): a cassette's J-card in its clear case, taped to the card — the name// written large by hand, how many songs and how long typed under it, the day// it was made by hand under its label (`date`), and the first four of the// songs it holds pinned in pairs: each cover once, and a sleeve printed with// its title and artist for a song that keeps no cover. Every word is the// core's (`DeckCard`); the covers are the songs' own, read through the store// by the deck (`DeckCardFill`, `MusicWholeCovers`, CottonUI).import CottonPaperimport CottonValuesimport SwiftUIpublic struct PlaylistFace<Cover: View>: View {    let card: DeckCard    /// The songs it holds, each cover once, in its order.    let covers: [MusicTile]    /// A cover by its key, decoded at an edge in points.    let cover: (_ key: String, _ edge: CGFloat) -> Cover    public init(card: DeckCard, covers: [MusicTile], cover: @escaping (_ key: String, _ edge: CGFloat) -> Cover) {        self.card = card        self.covers = covers        self.cover = cover    }    public var body: some View {        ZStack(alignment: .top) {            JCase()            VStack(spacing: 0) {                if let name = card.text("title") {                    Text(name)                        .font(CottonFonts.hand(MusicWholeKnobs.nameHand))                        .multilineTextAlignment(.center)                        .lineLimit(2)                        .minimumScaleFactor(0.5)                        .faceInk(.ink)                        .accessibilityAddTraits(.isHeader)                        .accessibilityIdentifier("deck-title")                        .facePart(.title)                }                if let facts = MusicWhole.joined([card.text("count"), card.text("length")]) {                    Text(facts)                        .font(CottonFonts.type(MusicWholeKnobs.playlistFacts))                        .lineLimit(1)                        .minimumScaleFactor(0.8)                        .faceInk(.ink)                        .padding(.top, 4)                        .accessibilityIdentifier("face-facts")                }                if let made = card.dated {                    Text([card.label("date"), made].compactMap { $0 }.joined(separator: " "))                        .font(CottonFonts.hand(MusicWholeKnobs.madeHand))                        .lineLimit(1)                        .minimumScaleFactor(0.7)                        .faceInk(.ink)                        .padding(.top, 2)                        .accessibilityIdentifier("face-date")                        .facePart(.when)                }                PinnedCovers(tiles: Array(covers.prefix(MusicWholeKnobs.covers)), cover: cover)                    .padding(.top, 18)            }            .padding(MusicWholeKnobs.cardPadding)            .padding(MusicWholeKnobs.cardInset)            Tape(size: MusicWholeKnobs.tape)                .offset(y: -MusicWholeKnobs.tape.height / 2)        }        .padding(MusicWholeKnobs.inset)        .onCardPaper(PlaylistFaceCatalogue.stock.laid(card.pace))    }}/// The clear case and the J-card in it: two hairlines round a rounded/// corner, the card's paper a shade warmer than the stock inside them.private struct JCase: View {    @Environment(\.theme) private var theme    var body: some View {        let corner = MusicWholeKnobs.caseCorner        ZStack {            PaperSurface(dye: .straw, edge: .cut, seed: "j-card")                .saturation(0.45)                .clipShape(RoundedRectangle(cornerRadius: corner / 2))                .shadow(color: theme.colors.shadow.color.opacity(0.18), radius: 1.5, x: 0, y: 1)                .padding(MusicWholeKnobs.cardInset)            Group {                RoundedRectangle(cornerRadius: corner)                    .stroke(theme.print(.pencil).opacity(0.5), lineWidth: 1.1)                RoundedRectangle(cornerRadius: corner - MusicWholeKnobs.caseLine / 2)                    .inset(by: MusicWholeKnobs.caseLine)                    .stroke(theme.print(.pencil).opacity(0.25), lineWidth: 0.75)            }            .blendMode(theme.printBlend)        }        .accessibilityHidden(true)    }}/// A strip of clear tape: the paper showing through, its edges a hair/// darker.private struct Tape: View {    let size: CGSize    @Environment(\.theme) private var theme    var body: some View {        Rectangle()            .fill(theme.colors.paper.color.opacity(0.55))            .overlay(Rectangle().stroke(theme.print(.pencil).opacity(0.18), lineWidth: 0.6))            .frame(width: size.width, height: size.height)            .rotationEffect(.degrees(MusicWholeKnobs.tapeTurn))            .accessibilityHidden(true)    }}/// The songs pinned on the J-card, two across, each turned a little and/// taped at its head: a song's own cover in a white mount, or, for a song/// that keeps none, its sleeve a size smaller printed with its title and/// artist; a lone one pinned large.private struct PinnedCovers<Cover: View>: View {    let tiles: [MusicTile]    let cover: (_ key: String, _ edge: CGFloat) -> Cover    var body: some View {        GeometryReader { room in            let gutter = MusicWholeKnobs.coverGutter            let rows = tiles.count > 1 ? (tiles.count + 1) / MusicWholeKnobs.coverColumns : 1            let across = tiles.count > 1 ? MusicWholeKnobs.coverColumns : 1            let side = max(0, min(                (room.size.width - gutter * CGFloat(across - 1)) / CGFloat(across),                (room.size.height - gutter * CGFloat(rows - 1)) / CGFloat(rows)))            VStack(spacing: gutter) {                ForEach(0..<rows, id: \.self) { row in                    HStack(spacing: gutter) {                        ForEach(Array(tiles.enumerated()).filter { $0.offset / across == row }, id: \.offset) { at, tile in                            pinned(tile, at: at, side: side)                        }                    }                }            }            .frame(width: room.size.width, height: room.size.height)        }        .accessibilityHidden(true)    }    private func pinned(_ tile: MusicTile, at: Int, side: CGFloat) -> some View {        Group {            switch tile {            case .art(let key):                MountedPrint(photo: cover(key, MusicWholeKnobs.coverEdge))                    .padding(MusicWholeKnobs.coverMount)                    .background(MountPaper())                    .frame(width: side, height: side)            case .sleeve(let title, let artist):                TypeSleeve(title: title, artist: artist)                    .frame(width: side * MusicWholeKnobs.sleeveShare, height: side * MusicWholeKnobs.sleeveShare)            }        }        .rotationEffect(.degrees(at.isMultiple(of: 2) ? -MusicWholeKnobs.coverTurn : MusicWholeKnobs.coverTurn))        .overlay(alignment: .top) {            Tape(size: MusicWholeKnobs.smallTape).offset(y: -MusicWholeKnobs.smallTape.height / 2)        }        .frame(width: side, height: side)    }}/// The white mount under a pinned cover, a soft shadow where it lifts.private struct MountPaper: View {    @Environment(\.theme) private var theme    var body: some View {        Rectangle()            .fill(theme.faceStocks.printMount)            .shadow(color: theme.colors.shadow.color.opacity(0.2), radius: 1.5, x: 0, y: 1)    }}/// The playlist face, and the cards it is reviewed in. No card kind names it/// yet (`music.playlist`): the deck draws it for an album whose whole deals/// tracks through its entries (the album face, `whole`), its songs' covers on/// its card (`covers`); here it is drawn from its card alone, pinning the/// picture the card names, as the deck does before the covers are read.public enum PlaylistFaceCatalogue {    public static let stock: FaceStock = .cream    public static let ground: FaceGround = .paper    public static let face = Face(id: "music.playlist", specimens: [Specimen("sunday", sunday)]) { card, _, _ in        PlaylistFace(card: card, covers: [card.text("photo")].compactMap { $0 }.map(MusicTile.art)) { key, edge in            FacePicture(key: key, edge: edge)        }    }    public static let sunday = DeckCard(face: .musicPlaylist, fields: [        "title": .text("Sunday Morning"),        "count": .text("14 songs"),        "length": .text("58:03"),        "date": .text("Sat 3 Oct"),        // The date's label, brought by the card (`labels`).        "labels": .object(["date": .text("Made")]),    ])}