// The ALBUM as ONE RECORD RESTING ON A TRAY (the owner's go, 7 Nov 2026, after // the hi-fi albums picture he chose, `music9-worlds/hifi-albums`; one tray // only, never the stacked three: the deck turns cards, it does not stack // them): a clear acrylic tray round an inset satin base, lying on the // music's black tolex (`FaceGround.tolex`) which shows through its lip, its // record in the faint round well, its own short square-cut ARM on a square // pivot block at the upper right, the hi-fi's red // jewel at the lower right, and along its foot a STRIP: the album's cover // cropped to it, a cream panel at its left typed with the title and the // artist, the tray's rail over its lower edge. // // The cream LABEL is typed with the album's title in the serif and its // artist in italic under it; while the album plays, the time played and the // time left of the song it holds either side of the spindle. At rest the arm // lies off the record; while the album plays it sits on the outer groove and // sweeps inward with the album's place — the songs before the one held and // how far into it, over the whole album, the needle's distance from the // record's centre running straight from the outer groove to the run-out — as // the turntable's arm does over a song. Paused it lifts a few degrees back. // // THE TILT: composed square-on, the whole tray is then tilted away as one // piece about its lower edge (`AlbumTrayFaceKnobs.tilt`), seen from above // as the picture has it. // // THE SAME TRAY is the song's face and the player's (`SongTrayFace.swift`, // `song.tray`, `TrayKind.song`): Round 11's 7-inch in the same well, the // label typing the song and its artist, the strip its album's cover; on // Listening the held song's, its record turning while it plays. // // PAINTED (`Media.xcassets`, `scripts/import-music-materials.py tray`, Round // 23's `music23-acrylic-tray` over Round 21's placements): the tray, its // shadow, the clear rail, the arm and its shadow, and Round 21's blank strip // card, each laid where its `layout.json` places it // in the tray's 1200 x 1600 box, scaled by the drawn width over it; the // record, the label, the 7-inch and its label, the spindle and the jewel are // Round 11's. The label is laid larger than the layout's (350 for the // 12-inch), as the turntable's is, filling the record inside its run-out // groove so its words read on a phone; its words keep the layout's places // and sizes, scaled with it. The cream panel is live, not painted. // // THE PARTS, tagged (`facePart`): the record is `play` — on the album a tap // plays the album from its first song (the player's `start`), and while the // album is held plays or pauses it; the strip, carrying the title, is // `title`. Nothing else: the album's card keeps to play and its title. // // Every word is the card's: `title`, `subtitle` (the artist), the songs' // lengths (`songs`, as the sleeve's back reads them) and its picture the // album's cover (`photo`). import CottonPaper import CottonValues import SwiftUI enum AlbumTrayFaceKnobs { // THE TRAY'S BOX every placement is in, in the paintings' pixels // (`layout.json`): the face draws the tray as large as its room lets, // `fill` of its width, or of its height as the tilted tray stands, as // the sleeve fills its card (no cap: the tray fills the card, as the // picture has it), and every place below is scaled by the drawn width // over it. static let box = CGSize(width: 1200, height: 1600) static let fill: CGFloat = 0.92 /// THE TILT (owner, 7 Nov 2026: the tray seen as in his picture, from /// above, tilted away, its top edge further; the perspective Round 23's /// tilted proof's, its eye 4000 of the box's 1600 away): the tray is composed /// square-on and the whole composition then turned this many degrees /// about its lower edge, the strip near its full size and the record /// foreshortened, under this perspective; its record turns and its arm /// sweeps inside it, its words tilt with it. Reduce Motion changes /// nothing about it. static let tilt: Double = 30 static let perspective: CGFloat = 0.4 /// The share of its height the tilted tray stands, seen: its top edge /// turned back by the tilt and drawn nearer the vanishing point by the /// perspective (SwiftUI's: the eye a box's longest side over the /// perspective away), the box's height being its longest side. static var tilted: CGFloat { let r = tilt * .pi / 180 return CGFloat(cos(r) / (1 + Double(perspective) * sin(r))) } /// The whole drawing the face fits to its room: the tray (its shadow /// spills past it, as a shadow does). Nothing is drawn under the tray: /// its acrylic lip lets the ground show through. static let whole = CGRect(x: 0, y: 0, width: 1200, height: 1600) static let shadow = CGRect(x: -72, y: -72, width: 1344, height: 1744) static let centre = CGPoint(x: 600, y: 590) static let spindle = CGRect(x: 580, y: 570, width: 40, height: 40) // THE RECORDS in the well, both about the centre: the 12-inch at the // layout's `recordRect`, the 7-inch at its `song.tray.recordRect`. static let twelve = CGRect(x: 115, y: 105, width: 970, height: 970) static let seven = CGRect(x: 317, y: 307, width: 566, height: 566) // THE LABELS, laid larger than the layout's (350, 192), their painted // rounds inside each record's run-out groove; the painted round's share // of each painting's side (`label-blank`'s 439 of 900, the single's // whole 340). static let twelveLabel: CGFloat = 448 static let sevenLabel: CGFloat = 276 static let twelveRound: CGFloat = 439 / 900 static let sevenRound: CGFloat = 0.5 // THE LABEL'S WORDS, in the layout's label pixels (its 350 side) about // the label's centre, as `labelLiveText` places them: the title in the // serif above the spindle, the artist in italic under it, the times // either side of the spindle below it; each line's room inside the // label's text circle (`outerRadius` 146). static let labelLayout: CGFloat = 350 static let title = (y: CGFloat(-86), size: CGFloat(35), width: CGFloat(236)) static let artist = (y: CGFloat(-42), size: CGFloat(27), width: CGFloat(270)) static let times = (y: CGFloat(59), x: CGFloat(76), size: CGFloat(24), width: CGFloat(100)) /// The least a label's line is taken down to before it is cut. static let labelLeast: CGFloat = 0.5 // THE ARM, rigid on its pivot: its painting laid as painted, at the outer // groove (`tray-arm.png`'s rect), its pivot and its needle in that // painting (`sourcePivot`, `sourceStylus`); its shadow's rect, centred on // it, and where the lamp throws it, down and lifted. static let arm = CGRect(x: 893, y: 107, width: 263, height: 843) static let armShadow = CGRect(x: 869, y: 83, width: 311, height: 891) static let sourcePivot = CGPoint(x: 187.26027397260276, y: 73.21917808219177) static let sourceStylus = CGPoint(x: 35.71232876712329, y: 813.2191780821918) static var pivot: CGPoint { CGPoint(x: arm.minX + sourcePivot.x, y: arm.minY + sourcePivot.y) } static var stylus: CGPoint { CGPoint(x: arm.minX + sourceStylus.x, y: arm.minY + sourceStylus.y) } static let shadowDown = CGSize(width: 5, height: 8) static let shadowLifted = CGSize(width: 22, height: 30) /// The run-out grooves: the 12-inch's and the 7-inch's (its outer /// groove, the arm's sweep's start, is `sevenOuter`; the 12-inch's is /// where the arm is painted). static let twelveInner: Double = 220 static let sevenOuter: Double = 270 static let sevenInner: Double = 140 /// The arm at rest, in degrees clockwise from the outer groove: back off /// the record, upright beside it; lifted, a few degrees back from where /// it plays; the ease of a move between them. static let rest: Double = -10 static let lift: Double = 3 static let ease: Double = 0.45 /// Where a finger finds the arm: a band this wide along its shaft. static let armReach: CGFloat = 110 static let armSlop: CGFloat = 4 // THE JEWEL (`jewelRect`), its stone the hi-fi's. static let jewelCentre = CGPoint(x: 1097, y: 1142) static let jewelStone: CGFloat = 34 // THE STRIP: its blank card, the art's rect inside its cream rule, the // cream panel at its left, the words' inset in the panel and their // sizes (`stripFonts`); the rail laid over its lower edge. static let strip = CGRect(x: 52, y: 1227, width: 1096, height: 256) static let art = CGRect(x: 66, y: 1241, width: 1068, height: 228) static let panel = CGRect(x: 66, y: 1241, width: 390, height: 228) static let panelInset: CGFloat = 27 static let stripTitle: CGFloat = 42 static let stripArtist: CGFloat = 28 static let stripGap: CGFloat = 8 static let ledge = CGRect(x: 88, y: 1456, width: 1024, height: 26) /// Beside the record on the player: `previous` left of its centre, /// `next` right of it, over the tray above the strip, under the record /// and the arm. static let previous = CGRect(x: 0, y: 0, width: 600, height: 1227) static let next = CGRect(x: 600, y: 0, width: 600, height: 1227) } /// Which record the tray carries: the album's 12-inch, or the song's 7-inch /// (on the Songs deck and on Listening's player). public enum TrayKind: Sendable, Hashable { case album case song var record: CGRect { self == .album ? AlbumTrayFaceKnobs.twelve : AlbumTrayFaceKnobs.seven } var recordPainting: String { self == .album ? "record" : "single-record" } var labelPainting: String { self == .album ? "label-blank" : "single-label-blank" } var labelSide: CGFloat { self == .album ? AlbumTrayFaceKnobs.twelveLabel : AlbumTrayFaceKnobs.sevenLabel } var labelRound: CGFloat { self == .album ? AlbumTrayFaceKnobs.twelveRound : AlbumTrayFaceKnobs.sevenRound } var identifier: String { self == .album ? "album-tray" : "song-tray" } /// The record's outer groove and run-out, from its centre. var grooves: (outer: Double, inner: Double) { switch self { case .album: (TrayDeck.painted, AlbumTrayFaceKnobs.twelveInner) case .song: (AlbumTrayFaceKnobs.sevenOuter, AlbumTrayFaceKnobs.sevenInner) } } } /// What the tray reads off its card and its player. public enum TrayDeck { /// The needle's distance from the centre as the arm is painted: the /// 12-inch's outer groove. static var painted: Double { let knobs = AlbumTrayFaceKnobs.self return Double(hypot(knobs.stylus.x - knobs.centre.x, knobs.stylus.y - knobs.centre.y)) } /// The share of the whole the arm stands at: on an album, the songs /// before the one held and how far into it over the album's length (its /// songs' lengths, `songs`); on a song, or an album whose songs' lengths /// are not known, how far into the song over its length. Nought while /// it holds none. public static func share(_ play: TrackPlay, songs: [TimeInterval] = []) -> Double { guard play.held else { return 0 } let whole = songs.reduce(0, +) if let at = play.song, songs.indices.contains(at), whole > 0 { let into = min(max(0, play.elapsed.isFinite ? play.elapsed : 0), songs[at]) return min(1, max(0, (songs[.. 0, play.elapsed.isFinite else { return 0 } return min(1, max(0, play.elapsed / play.duration)) } /// The arm's angle at `share` of the record, in degrees clockwise from /// where it is painted: the needle's distance from the centre running /// straight from the record's outer groove at nought to its run-out at /// one, the rigid arm turned round its pivot to meet it. public static func arm(_ share: Double, on kind: TrayKind) -> Double { let grooves = kind.grooves let held = min(1, max(0, share)) return opening(painted) - opening(grooves.outer + (grooves.inner - grooves.outer) * held) } /// The arm as it stands: at rest off the record while it holds none of /// the card; lifted a few degrees back while paused; else at its share. public static func arm(_ play: TrackPlay, songs: [TimeInterval] = [], on kind: TrayKind) -> Double { guard play.held else { return AlbumTrayFaceKnobs.rest } let playing = HifiDeck.state(play) == .playing return arm(share(play, songs: songs), on: kind) - (playing ? 0 : AlbumTrayFaceKnobs.lift) } /// Where the needle lies with the arm turned `degrees` clockwise from /// where it is painted. public static func needle(_ degrees: Double) -> CGPoint { let knobs = AlbumTrayFaceKnobs.self let x = Double(knobs.stylus.x - knobs.pivot.x), y = Double(knobs.stylus.y - knobs.pivot.y) let r = degrees * .pi / 180 return CGPoint(x: Double(knobs.pivot.x) + x * cos(r) - y * sin(r), y: Double(knobs.pivot.y) + x * sin(r) + y * cos(r)) } /// The share of the song the arm at `degrees` stands at, on `kind`'s /// record: its needle's groove, outer nought, run-out one. public static func share(atArm degrees: Double, on kind: TrayKind) -> Double { let knobs = AlbumTrayFaceKnobs.self let at = needle(degrees) let radius = hypot(Double(at.x - knobs.centre.x), Double(at.y - knobs.centre.y)) let grooves = kind.grooves return min(1, max(0, (grooves.outer - radius) / (grooves.outer - grooves.inner))) } /// Where a turn of the arm by `degrees` from `elapsed` of a song /// `length` long seeks to: its sweep is the whole song, held to it. public static func turned(_ elapsed: TimeInterval, by degrees: Double, of length: TimeInterval, on kind: TrayKind) -> TimeInterval { guard length > 0 else { return 0 } let from = arm(min(1, max(0, elapsed / length)), on: kind) let to = min(arm(1, on: kind), max(arm(0, on: kind), from + degrees)) return share(atArm: to, on: kind) * length } /// The angle at the pivot between the needle and the record's centre, /// the needle at `radius` from the centre: the law of cosines over the /// arm's length and the pivot's distance from the centre. static func opening(_ radius: Double) -> Double { let knobs = AlbumTrayFaceKnobs.self let arm = hypot(Double(knobs.stylus.x - knobs.pivot.x), Double(knobs.stylus.y - knobs.pivot.y)) let apart = hypot(Double(knobs.centre.x - knobs.pivot.x), Double(knobs.centre.y - knobs.pivot.y)) let cosine = (arm * arm + apart * apart - radius * radius) / (2 * arm * apart) return acos(min(1, max(-1, cosine))) * 180 / .pi } /// The label's two lines: the title, and under it the artist (a player /// card's `artist`, else the card's `subtitle`). public static func label(_ card: DeckCard) -> [String] { [card.text("title"), card.text("artist") ?? card.text("subtitle")].compactMap { $0 } } /// The strip's two lines: the album's title and its artist — on the /// player, the held song's album (`album`) and its artist; else the /// card's own title and artist. public static func strip(_ card: DeckCard, player: Bool) -> [String] { let artist = card.text("artist") ?? card.text("subtitle") let title = player ? card.text("album") ?? card.text("title") : card.text("title") return [title, artist].compactMap { $0 } } /// The times either side of the spindle while it holds the card: the /// time played and the time left of the song held; none at rest, or /// while the song's length is unknown. public static func times(_ play: TrackPlay) -> [String] { guard play.held, play.duration > 0 else { return [] } // Left from the whole seconds played, so the clocks add up. return [TrackClock.said(play.elapsed), TrackClock.left(play.elapsed.rounded(.down), of: play.duration)] } /// An album's songs' lengths, in its order (`songs`). public static func songs(_ card: DeckCard) -> [TimeInterval] { card.entries("songs").map { max(0, $0["duration"]?.number ?? 0) } } } /// The tray: the album's 12-inch, or the song's 7-inch and, on Listening, /// the player's. public struct TrayFace: View { let card: DeckCard /// The album's cover, laid along the strip; nil lays the strip's blank. var cover: FacePicture? var play: TrackPlay = .still var toggle: (() -> Void)? var seek: (TimeInterval) -> Void = { _ in } /// Plays the album from its song at this place (the album's tap). var start: ((Int) -> Void)? let kind: TrayKind /// Where a drag of the arm has the song, until the finger lifts. @State private var scrubbing: TimeInterval? /// Where the record turns on from (`TurntableDeck.turn`). @State private var phase: Double = 0 @Environment(\.accessibilityReduceMotion) private var reduceMotion public init( card: DeckCard, cover: FacePicture?, play: TrackPlay = .still, toggle: (() -> Void)? = nil, seek: @escaping (TimeInterval) -> Void = { _ in }, start: ((Int) -> Void)? = nil, kind: TrayKind ) { self.card = card self.cover = cover self.play = play self.toggle = toggle self.seek = seek self.start = start self.kind = kind } /// The song's tray holding its song is the player: its record turns, its /// arm scrubs and beside it are previous and next. private var player: Bool { kind == .song && play.held } /// Where it stands, a drag of the arm put in for the time played. private var standing: TrackPlay { var standing = play if let scrubbing { standing.elapsed = scrubbing } return standing } public var body: some View { let knobs = AlbumTrayFaceKnobs.self let whole = knobs.whole GeometryReader { room in let width = knobs.fill * min(room.size.width, room.size.height * whole.width / (whole.height * knobs.tilted)) let s = width / whole.width ZStack(alignment: .topLeading) { if player { beside(s) } TrayPainted(name: "tray-shadow").placed(knobs.shadow, s) TrayPainted(name: "tray").placed(CGRect(origin: .zero, size: knobs.box), s) record(s) TrayPainted(name: "spindle").placed(knobs.spindle, s) arm(s) jewel(s) strip(s) TrayPainted(name: "tray-ledge").placed(knobs.ledge, s) } .frame(width: knobs.box.width * s, height: knobs.box.height * s, alignment: .topLeading) .offset(x: -whole.minX * s, y: -whole.minY * s) .frame(width: width, height: whole.height * s, alignment: .topLeading) .rotation3DEffect(.degrees(knobs.tilt), axis: (x: 1, y: 0, z: 0), anchor: .bottom, perspective: knobs.perspective) // Its foot where the tilted tray, seen, stands in the middle. .offset(y: -whole.height * s * (1 - knobs.tilted) / 2) .frame(width: room.size.width, height: room.size.height) .accessibilityElement(children: .contain) .accessibilityIdentifier(kind.identifier) } .onChange(of: HifiDeck.state(play)) { was, now in if was == .paused, now == .playing { phase = TurntableDeck.resumed(play.elapsed, phase: phase) } } .onChange(of: play.elapsed) { was, now in phase = TurntableDeck.phase(phase, from: was, to: now) } } // MARK: The record /// The record and its label, typed with the card's words and the times; /// on the player turning while it plays. A tap on it plays. private func record(_ s: CGFloat) -> some View { let rect = kind.record let playing = HifiDeck.state(play) == .playing let turn = player ? TurntableDeck.turn(play.elapsed, playing: playing, phase: phase, reduceMotion: reduceMotion) : 0 let side = rect.width * s let label = kind.labelSide * s return ZStack { TrayPainted(name: kind.recordPainting).frame(width: side, height: side) TrayPainted(name: kind.labelPainting).frame(width: label, height: label) words(label: label) } .frame(width: side, height: side) .rotationEffect(.degrees(turn)) .animation(player && !reduceMotion ? (playing ? .linear(duration: SongTurntableFaceKnobs.glide) : .easeOut(duration: SongTurntableFaceKnobs.coast)) : nil, value: turn) .allowsHitTesting(false) .overlay { Circle() .fill(Color.clear) .contentShape(Circle()) .modifier(TrayTap(action: tapped)) .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("faces", playing ? "media.pause" : "media.play")) .accessibilityValue(MusicWhole.joined(TrayDeck.label(card)) ?? "") .accessibilityAddTraits(.isButton) .accessibilityIdentifier("tray-record") .facePart(.play) } .at(rect, s) } /// What a tap on the record does that the face does itself: held, it /// plays or pauses; an album not held plays from its first song. A song /// not held is the deck's to play (its `play` part); nil leaves the tap /// to it. private var tapped: (() -> Void)? { if play.held, let toggle { return toggle } if kind == .album, let start { return { start(0) } } return nil } /// The label's words in the layout's places, scaled with the label: the /// title above the spindle, the artist under it, the times either side /// below it. private func words(label: CGFloat) -> some View { let knobs = AlbumTrayFaceKnobs.self let k = label / knobs.labelLayout let centre = label / 2 let lines = TrayDeck.label(card) let times = TrayDeck.times(standing) return ZStack { if let title = lines.first { Text(title) .font(CottonFonts.serif(knobs.title.size * k)) .lineLimit(1) .minimumScaleFactor(knobs.labelLeast) .frame(width: knobs.title.width * k) .position(x: centre, y: centre + knobs.title.y * k) } if lines.count > 1 { Text(lines[1]) .font(CottonFonts.cap(knobs.artist.size * k)) .lineLimit(1) .minimumScaleFactor(knobs.labelLeast) .frame(width: knobs.artist.width * k) .position(x: centre, y: centre + knobs.artist.y * k) } ForEach(Array(times.enumerated()), id: \.offset) { at, time in Text(time) .font(CottonFonts.type(knobs.times.size * k)) .monospacedDigit() .lineLimit(1) .fixedSize() .frame(width: knobs.times.width * k) .position(x: centre + (at == 0 ? -knobs.times.x : knobs.times.x) * k, y: centre + knobs.times.y * k) } } .foregroundStyle(MusicMaterial.labelInk.colour) .frame(width: label, height: label) .accessibilityHidden(true) } // MARK: The arm and the jewel /// The arm on its pivot: at rest off the record, on its groove while it /// holds the card, lifted while paused, its shadow under it; on the /// player dragged along its arc it seeks. private func arm(_ s: CGFloat) -> some View { let knobs = AlbumTrayFaceKnobs.self let songs = kind == .album ? TrayDeck.songs(card) : [] let angle = TrayDeck.arm(standing, songs: songs, on: kind) let lifted = !play.held || HifiDeck.state(play) == .paused let throwBy = lifted ? knobs.shadowLifted : knobs.shadowDown let length = HifiDeck.length(play, card) let scrubs = player && length > 0 let anchor = { (rect: CGRect) in UnitPoint(x: (knobs.pivot.x - rect.minX) / rect.width, y: (knobs.pivot.y - rect.minY) / rect.height) } let motion: Animation? = reduceMotion ? nil : .easeInOut(duration: knobs.ease) return ZStack(alignment: .topLeading) { TrayPainted(name: "tray-arm-shadow") .frame(width: knobs.armShadow.width * s, height: knobs.armShadow.height * s) .rotationEffect(.degrees(angle), anchor: anchor(knobs.armShadow)) .offset(x: throwBy.width * s, y: throwBy.height * s) .animation(motion, value: lifted) .at(knobs.armShadow, s) TrayArm( angle: angle, anchor: anchor(knobs.arm), size: CGSize(width: knobs.arm.width * s, height: knobs.arm.height * s), pivot: CGPoint(x: knobs.pivot.x * s, y: knobs.pivot.y * s), s: s, scrubs: scrubs, turn: { degrees in scrubbing = TrayDeck.turned(scrubbing ?? play.elapsed, by: degrees, of: length, on: kind) }, lifted: { if let scrubbing { seek(scrubbing) } scrubbing = nil }) .animation(motion, value: lifted) .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("faces", "tray.position")) .accessibilityValue(scrubs ? TrackClock.said(standing.elapsed) : "") .accessibilityAdjustableAction { direction in guard scrubs else { return } let by = TrayDeck.arm(1, on: kind) / 20 seek(TrayDeck.turned(play.elapsed, by: direction == .increment ? by : -by, of: length, on: kind)) } .accessibilityIdentifier("tray-arm") .modifier(TrayScrub(scrubs: scrubs)) .at(knobs.arm, s) } .coordinateSpace(.named(TrayArm.space)) } /// The hi-fi's red jewel, lit while it plays. private func jewel(_ s: CGFloat) -> some View { let knobs = AlbumTrayFaceKnobs.self let lit = HifiDeck.state(play) == .playing let painting = lit ? SongHifiFaceKnobs.jewelOn : SongHifiFaceKnobs.jewelOff let scale = knobs.jewelStone / painting.stone.width let drawn = CGRect( x: knobs.jewelCentre.x - painting.stone.midX * scale, y: knobs.jewelCentre.y - painting.stone.midY * scale, width: painting.size.width * scale, height: painting.size.height * scale) return TrayPainted(name: lit ? "hifi-jewel-on" : "hifi-jewel-off").placed(drawn, s) } // MARK: Beside the record, and the strip /// Beside the record on the player: a tap left of its centre the /// previous song, right of it the next — the shell's acts. private func beside(_ s: CGFloat) -> some View { let knobs = AlbumTrayFaceKnobs.self return ZStack(alignment: .topLeading) { Color.clear .contentShape(Rectangle()) .frame(width: knobs.previous.width * s, height: knobs.previous.height * s) .accessibilityElement() .accessibilityLabel(ShellLines.line("faces", "tray.previous")) .accessibilityAddTraits(.isButton) .accessibilityIdentifier("tray-previous") .facePart(.previous) .at(knobs.previous, s) Color.clear .contentShape(Rectangle()) .frame(width: knobs.next.width * s, height: knobs.next.height * s) .accessibilityElement() .accessibilityLabel(ShellLines.line("faces", "tray.next")) .accessibilityAddTraits(.isButton) .accessibilityIdentifier("tray-next") .facePart(.next) .at(knobs.next, s) } } /// The strip along the tray's foot: its blank card, the cover cropped to /// it where there is one, the cream panel at its left typed with the /// title and the artist; the title's part. private func strip(_ s: CGFloat) -> some View { let knobs = AlbumTrayFaceKnobs.self let lines = TrayDeck.strip(card, player: player) return ZStack(alignment: .topLeading) { TrayPainted(name: "strip-blank").placed(knobs.strip, s) if let cover { cover .frame(width: knobs.art.width * s, height: knobs.art.height * s) .clipped() .allowsHitTesting(false) .accessibilityHidden(true) .at(knobs.art, s) } Rectangle() .fill(MusicMaterial.panel.colour) .allowsHitTesting(false) .placed(knobs.panel, s) VStack(alignment: .leading, spacing: knobs.stripGap * s) { if let title = lines.first { Text(title) .font(CottonFonts.serif(knobs.stripTitle * s)) .lineLimit(2) .minimumScaleFactor(0.6) .accessibilityAddTraits(.isHeader) .accessibilityIdentifier("deck-title") } if lines.count > 1 { Text(lines[1]) .font(CottonFonts.cap(knobs.stripArtist * s)) .lineLimit(1) .minimumScaleFactor(0.6) .accessibilityIdentifier("face-byline") } } .foregroundStyle(MusicMaterial.labelInk.colour) .padding(.horizontal, knobs.panelInset * s) .frame(width: knobs.panel.width * s, height: knobs.panel.height * s, alignment: .leading) .contentShape(Rectangle()) .accessibilityElement(children: .contain) .accessibilityIdentifier("tray-strip") .facePart(.title) .at(knobs.panel, s) } } } /// The record's tap the face keeps on itself, where it has one; none leaves /// the tap to the deck's part. private struct TrayTap: ViewModifier { let action: (() -> Void)? func body(content: Content) -> some View { if let action { content.onTapGesture(perform: action).accessibilityAction { action() } } else { content } } } /// The arm is the player's scrub part; elsewhere it is no part. private struct TrayScrub: ViewModifier { let scrubs: Bool func body(content: Content) -> some View { if scrubs { content.facePart(.scrub) } else { content } } } /// The arm turned to `angle` round its pivot; where it scrubs, a finger /// dragging along its arc turns it by the angle swept round the pivot. private struct TrayArm: View { static let space = "tray" let angle: Double let anchor: UnitPoint let size: CGSize /// The pivot in the tray's space, in points. let pivot: CGPoint let s: CGFloat let scrubs: Bool let turn: (_ degrees: Double) -> Void let lifted: () -> Void @State private var last: Double? var body: some View { if scrubs { TrayPainted(name: "tray-arm") .frame(width: size.width, height: size.height) .contentShape(TrayArmLine(scale: size.width / AlbumTrayFaceKnobs.arm.width)) .rotationEffect(.degrees(angle), anchor: anchor) .gesture( DragGesture(minimumDistance: AlbumTrayFaceKnobs.armSlop * s, coordinateSpace: .named(Self.space)) .onChanged { touch in let now = HifiDeck.angle(of: touch.location, round: pivot) if let last { turn(HifiDeck.turn(from: last, to: now)) } last = now } .onEnded { _ in last = nil lifted() }) } else { TrayPainted(name: "tray-arm") .frame(width: size.width, height: size.height) .rotationEffect(.degrees(angle), anchor: anchor) .allowsHitTesting(false) } } } /// Where a finger finds the arm: a band along its rod, pivot to needle. private struct TrayArmLine: Shape { let scale: CGFloat func path(in rect: CGRect) -> Path { let knobs = AlbumTrayFaceKnobs.self var line = Path() line.addLines([knobs.sourcePivot, knobs.sourceStylus].map { CGPoint(x: $0.x * scale, y: $0.y * scale) }) return line.strokedPath(StrokeStyle(lineWidth: knobs.armReach * scale, lineCap: .round)) } } /// One of the tray's paintings, stretched to its frame; nothing where the /// build carries no pictures. A painting takes no tap: the parts do. private struct TrayPainted: View { let name: String var body: some View { if let image = CottonMedia.image(name) { image.resizable().interpolation(.high).allowsHitTesting(false).accessibilityHidden(true) } } } private extension View { /// This view laid at `rect` of the tray's box, in its pixels, scaled by `s`. func placed(_ rect: CGRect, _ s: CGFloat) -> some View { frame(width: rect.width * s, height: rect.height * s).offset(x: rect.minX * s, y: rect.minY * s) } /// This view moved to `rect`'s corner, scaled by `s`: last, after a part /// is tagged (`facePart`), so the region the deck reads for it is where /// it is drawn. func at(_ rect: CGRect, _ s: CGFloat) -> some View { offset(x: rect.minX * s, y: rect.minY * s) } } /// The album's tray, beside its sleeve, and the albums it is reviewed in: /// the sleeve's invented albums. Under the player the app installs /// (`faceTrackPlayer`), else at rest; the playing specimen names the /// player's word it is reviewed under. public enum AlbumTrayFaceCatalogue { public static let stock: FaceStock = .own public static let ground: FaceGround = .tolex public static let face = Face(id: "album.tray", specimens: [ Specimen("at rest", ferry, picture: "cover-night-ferry"), Specimen("playing", ferry, picture: "cover-night-ferry", play: TrackPlay(held: true, playing: true, elapsed: 134, duration: 242, song: 0)), Specimen("no cover", common), Specimen("largest", largest), ]) { card, picture, _ in TrackPlaying(card: card) { player in TrayFace(card: card, cover: picture, play: player.play, toggle: player.toggle, seek: player.seek, start: player.start, kind: .album) } } /// The sleeve's Night Ferry, its cover kept, worn on the tray. public static let ferry = worn(AlbumSleeveFaceCatalogue.ferry) /// No cover kept: the strip's blank. public static let common = worn(AlbumSleeveFaceCatalogue.common) /// The longest words, drawn at every text size Loom has, the largest /// among them: a print on an object scales with the object. public static let largest = worn(AlbumSleeveFaceCatalogue.largest) static func worn(_ album: DeckCard) -> DeckCard { var card = album card.face = "album.tray" return card } }