Cotton code
115 lines · 5.5 KB · SwiftRaw
// A record's sleeve while it keeps no art of its own (`TypeSleeve`), and the// sizes the music faces share (`MusicPartsKnobs`): a track's art, an album's// and a playlist's, the record, the play disc and the scrubber (CottonUI's// `MusicParts`) all read them.import CottonPaperimport SwiftUIpublic enum MusicPartsKnobs {    /// The record's grooves, as rings between its edge and its label.    public static let grooves = 7    public static let groove: CGFloat = 0.6    public static let label: CGFloat = 0.36    public static let spindle: CGFloat = 0.035    /// The scrubber's line, its filled part and its knob.    public static let line: CGFloat = 2    public static let knob: CGFloat = 14    public static let knobDragging: CGFloat = 20    /// The scrubber's height: the knob's room, so a finger finds it.    public static let height: CGFloat = 28    /// One VoiceOver swipe on the scrubber moves the playhead this far.    public static let step: TimeInterval = 10    public static let clockSize: CGFloat = 12    /// A sleeve kept without art, of the slot its art would fill: smaller,    /// so it never reads as an empty frame.    public static let sleeveShare: CGFloat = 0.84    /// The sleeve's words, of its side: the room round them, the title's    /// size and the artist's. Below `sleeveFull` points it sets the title    /// alone, larger (`sleeveSmallTitle`); below `sleeveWords`, nothing.    public static let sleevePadding: CGFloat = 0.09    public static let sleeveTitle: CGFloat = 0.13    public static let sleeveArtist: CGFloat = 0.12    public static let sleeveFull: CGFloat = 110    public static let sleeveSmallTitle: CGFloat = 0.2    public static let sleeveWords: CGFloat = 40    /// The grooves rippling out of its lower corner: how many, how far the    /// widest reaches (of its side), their line and how faint.    public static let sleeveGrooves = 6    public static let sleeveReach: CGFloat = 0.78    public static let sleeveCorner = UnitPoint(x: 0.92, y: 0.94)    public static let sleeveGroove: CGFloat = 0.8    public static let sleeveGrooveOpacity: Double = 0.32    public static let sleeveStamp: Double = 0.94    public static let sleeveShadow: Double = 0.28}/// A record's sleeve while it keeps no art of its own: printed in the ink/// its title chooses on the house paper, grooves rippling faintly out of its/// lower corner, and the title set in the display face at its head with the/// artist by hand under it, in the paper's cream — never a picture that is/// not the record's. Small, it sets the title alone; too small to read, it/// is the sleeve and its grooves.public struct TypeSleeve: View {    let title: String?    var artist: String? = nil    @Environment(\.theme) private var theme    public init(title: String?, artist: String? = nil) {        self.title = title        self.artist = artist    }    public var body: some View {        GeometryReader { box in            let side = min(box.size.width, box.size.height)            let cream = theme.faceStocks.cream            ZStack(alignment: .topLeading) {                InkedPaper(ink: CoverInk.of(title, theme: theme), seed: "sleeve-\(title ?? "")")                Grooves()                    .stroke(cream.opacity(MusicPartsKnobs.sleeveGrooveOpacity), lineWidth: MusicPartsKnobs.sleeveGroove)                if side >= MusicPartsKnobs.sleeveWords {                    let full = side >= MusicPartsKnobs.sleeveFull                    VStack(alignment: .leading, spacing: side * MusicPartsKnobs.sleevePadding / 3) {                        if let title {                            Text(title)                                .font(CottonFonts.display(side * (full ? MusicPartsKnobs.sleeveTitle : MusicPartsKnobs.sleeveSmallTitle)))                                .lineLimit(3)                                .minimumScaleFactor(0.6)                        }                        if full, let artist {                            Text(artist)                                .font(CottonFonts.hand(side * MusicPartsKnobs.sleeveArtist))                                .lineLimit(2)                                .minimumScaleFactor(0.6)                        }                    }                    .multilineTextAlignment(.leading)                    .foregroundStyle(cream.opacity(MusicPartsKnobs.sleeveStamp))                    .padding(side * MusicPartsKnobs.sleevePadding)                }            }            .frame(width: box.size.width, height: box.size.height, alignment: .topLeading)            .clipped()        }        .compositingGroup()        .shadow(color: theme.colors.shadow.color.opacity(MusicPartsKnobs.sleeveShadow), radius: 2.5, x: 1, y: 2)        .accessibilityHidden(true)    }    /// Rings round the sleeve's lower corner, the widest reaching most of    /// the way across.    private struct Grooves: Shape {        func path(in rect: CGRect) -> Path {            let side = min(rect.width, rect.height)            let centre = CGPoint(x: rect.minX + rect.width * MusicPartsKnobs.sleeveCorner.x,                                 y: rect.minY + rect.height * MusicPartsKnobs.sleeveCorner.y)            var path = Path()            for ring in 1...MusicPartsKnobs.sleeveGrooves {                let radius = side * MusicPartsKnobs.sleeveReach * CGFloat(ring) / CGFloat(MusicPartsKnobs.sleeveGrooves)                path.addEllipse(in: CGRect(x: centre.x - radius, y: centre.y - radius, width: radius * 2, height: radius * 2))            }            return path        }    }}