Cotton code
249 lines · 10.3 KB · SwiftRaw
// The TRACK face (card kind `track`, 66a451ec1; the Codex album-2 card, 23// Oct 2026): one song kept as a file, on the cream card, over its own art// faded into the stock — or, while it has none, its sleeve printed with its// title and artist, stood a little turned at the head where the art would// be. The title typed heavy at the head, its place on the album by hand// under it (`number`, "Track 3"), the artist and the album typed in pencil// and its length typed; at the foot the scrubber — the playhead// running while this track is the one the player holds, a still line from// nought to its length while it is not — and the dark disc that plays it.// The track's place in its album is the deck's to say, never the card's.// Every word is the core's (`DeckCard`); the file is the `play` place's. The// player is the app's: it says where it stands with this track (`TrackPlay`)// and plays, pauses and seeks it (the player the app installs, `faceTrackPlayer`: CottonUI's `DeckTrackPlaying`); the art is the// picture it hands in.import CottonPaperimport CottonValuesimport SwiftUIenum TrackFaceKnobs {    static let inset = EdgeInsets(top: 28, leading: 26, bottom: 22, trailing: 24)    /// The art across the card: where its fade is centred, as shares of the    /// card, and how far out it has faded to nothing, as a share of its    /// longer side.    static let artCentre = UnitPoint(x: 0.66, y: 0.58)    static let artReach: CGFloat = 0.78    static let artSaturation: CGFloat = 0.85    /// The sleeve kept without art, at the head of the card where the art    /// would be: its side, of the card's width; its turn; and the room    /// between it and the title.    static let sleeveShare: CGFloat = 0.36    static let sleeveTurn: Double = 3    static let sleeveGap: CGFloat = 18    /// The head's share of the card's width: the art shows past it.    static let headWidth: CGFloat = 0.8    static let titleSize: CGFloat = 27    static let handSize: CGFloat = 25    static let subtitleSize: CGFloat = 13.5    static let lengthSize: CGFloat = 15    static let disc: CGFloat = 46    static let noticeHand: CGFloat = 16    static let stillKnob: CGFloat = 10}public struct TrackFace<Art: View>: View {    let card: DeckCard    /// The track's art filling its frame, when the card names one.    var art: Art?    /// Where the player stands with this track.    var play: TrackPlay = .still    /// Plays or pauses the track; nil for a card with no file to play.    var toggle: (() -> Void)?    /// Moves the playhead of the track the player holds.    var seek: (TimeInterval) -> Void = { _ in }    public init(        card: DeckCard, art: Art?, play: TrackPlay = .still, toggle: (() -> Void)?,        seek: @escaping (TimeInterval) -> Void = { _ in }    ) {        self.card = card        self.art = art        self.play = play        self.toggle = toggle        self.seek = seek    }    /// This card's track is the one the player holds.    private var held: Bool { play.held }    private var playing: Bool { held && play.playing }    public var body: some View {        GeometryReader { sheet in            ZStack(alignment: .topLeading) {                if let art {                    FadedArt(art: art)                        .frame(width: sheet.size.width, height: sheet.size.height)                }                VStack(alignment: .leading, spacing: 0) {                    // Without art, its sleeve stands at the head where the                    // art would be, and the words go under it.                    if card.text("photo") == nil {                        let side = sheet.size.width * TrackFaceKnobs.sleeveShare                        TypeSleeve(title: card.text("title"), artist: card.text("subtitle"))                            .frame(width: side, height: side)                            .rotationEffect(.degrees(TrackFaceKnobs.sleeveTurn))                            .frame(maxWidth: .infinity)                            .padding(.bottom, TrackFaceKnobs.sleeveGap)                    }                    head                        .frame(width: sheet.size.width * TrackFaceKnobs.headWidth, alignment: .leading)                    Spacer(minLength: 8)                    foot                }                .padding(TrackFaceKnobs.inset)            }            .frame(width: sheet.size.width, height: sheet.size.height, alignment: .topLeading)            .clipped()        }        .onCardPaper(TrackFaceCatalogue.stock.laid(card.pace))    }    /// The title, its place by hand, the artist and album, the length.    private var head: some View {        VStack(alignment: .leading, spacing: 0) {            if let title = card.text("title") {                HeavyType(words: title, size: TrackFaceKnobs.titleSize)                    .lineLimit(2)                    .minimumScaleFactor(0.7)                    .accessibilityIdentifier("deck-title")                    .facePart(.title)            }            if let number = card.text("number") {                Text(number)                    .font(CottonFonts.hand(TrackFaceKnobs.handSize))                    .lineLimit(1)                    .minimumScaleFactor(0.7)                    .faceInk(.ink)                    .padding(.top, 4)                    .accessibilityIdentifier("face-number")            }            if let subtitle = card.text("subtitle") {                Text(subtitle)                    .font(CottonFonts.type(TrackFaceKnobs.subtitleSize))                    .lineLimit(2)                    .minimumScaleFactor(0.8)                    .faceInk(.pencil)                    .padding(.top, 6)                    .accessibilityIdentifier("face-byline")                    .facePart(.subtitle)            }            if let length = card.text("length") {                Text(length)                    .font(CottonFonts.type(TrackFaceKnobs.lengthSize))                    .monospacedDigit()                    .faceInk(.ink)                    .padding(.top, 8)            }        }    }    /// The scrubber, or the still line, and the disc; what went wrong, by    /// hand, over them.    private var foot: some View {        VStack(alignment: .leading, spacing: 6) {            if held, let fault = play.fault {                Text(HandRun.tail(ShellLines.line("faces", fault == .unavailable ? "media.could_not_fetch" : "recording.none")))                    .font(CottonFonts.hand(TrackFaceKnobs.noticeHand))                    .lineLimit(2)                    .faceInk(.pencil)            }            HStack(alignment: .center, spacing: 14) {                if held, play.duration > 0 {                    TrackScrubber(elapsed: play.elapsed, duration: play.duration, title: card.text("title"), seek: seek)                } else {                    StillRule(length: card.text("length"))                }                if let toggle {                    Button(action: toggle) {                        TrackDisc(playing: playing, size: TrackFaceKnobs.disc)                            .opacity(held && play.loading ? 0.5 : 1)                    }                    .buttonStyle(MotionButtonStyle())                    .accessibilityLabel(ShellLines.line("faces", playing ? "media.pause" : "media.play"))                    .accessibilityValue(card.text("title") ?? "")                    .accessibilityIdentifier("track-play")                    .facePart(.play)                }            }        }    }}/// The track's art across the whole card, printed into the stock and faded/// out toward its edges and the head.private struct FadedArt<Art: View>: View {    let art: Art    @Environment(\.theme) private var theme    var body: some View {        GeometryReader { box in            Color.clear                .overlay(art)                .clipped()                .saturation(TrackFaceKnobs.artSaturation)                .mask {                    RadialGradient(                        colors: [.black, .black.opacity(0.85), .clear],                        center: TrackFaceKnobs.artCentre, startRadius: 0,                        endRadius: max(box.size.width, box.size.height) * TrackFaceKnobs.artReach)                }                .blendMode(theme.printBlend)        }        .accessibilityHidden(true)    }}/// The scrubber's line while the track is not the one playing: nothing/// played, the knob at its start, nought and the length under it.private struct StillRule: View {    let length: String?    @Environment(\.theme) private var theme    var body: some View {        VStack(spacing: 4) {            ZStack(alignment: .leading) {                Capsule().fill(theme.print(.pencil).opacity(0.35)).frame(height: MusicPartsKnobs.line)                Circle().fill(theme.print(.ink)).frame(width: TrackFaceKnobs.stillKnob, height: TrackFaceKnobs.stillKnob)            }            .frame(height: MusicPartsKnobs.height)            HStack {                Text(TrackClock.said(0))                Spacer(minLength: 8)                if let length { Text(length) }            }            .font(CottonFonts.type(MusicPartsKnobs.clockSize))            .monospacedDigit()            .faceInk(.pencil)        }        .accessibilityHidden(true)    }}/// The track face, by the kind's name, and the cards it is reviewed in:/// under the player the app installs (`faceTrackPlayer`), else still.public enum TrackFaceCatalogue {    public static let stock: FaceStock = .cream    public static let ground: FaceGround = .paper    public static let face = Face(id: "track", specimens: [Specimen("lantern", lantern), Specimen("tide", tide)]) { card, picture, _ in        TrackPlaying(card: card) { player in            TrackFace(card: card, art: picture, play: player.play, toggle: player.toggle, seek: player.seek)        }    }    public static let lantern = DeckCard(face: .track, fields: [        "title": .text("Lantern Weather"),        "number": .text("Track 1"),        "subtitle": .text("The Example Quartet \u{00B7} Harbour Lights"),        "length": .text("3:41"),        "play": .text("media/track-1"),    ])    public static let tide = DeckCard(face: .track, fields: [        "title": .text("Slow Tide"),        "number": .text("Track 2"),        "subtitle": .text("The Example Quartet \u{00B7} Harbour Lights"),        "length": .text("5:02"),        "play": .text("media/track-2"),    ])}