// The SONG PLAYING as a RECORD ON A TURNTABLE (the owner's go, 4 Nov 2026; the// dark room's pictures `listening-turntable`, `listening-turntable-paused`):// the second player face of Music's Listening deck (the first is the hi-fi,// `SongHifiFace`; the `player` face setting picks one), drawn for the playing// song's card, a bare object on the music's black tolex (`FaceGround.tolex`)// with no paper and no rule of the deck's. The black felt slipmat; on it the// RECORD, turning at 33⅓ while it plays, its LABEL turning with it — the// album's cover cropped round inside a cream paper ring where there is one,// else the blank cream label — typed with the song's name in a condensed heavy// sans above the spindle, a thin rule under it, the artist and the album in// small capitals under the rule, and the time played, the time left and the// song's length small at its foot; the brass SPINDLE; the brass TONEARM on its// pivot, its needle in the groove: on the outer groove at nought, on the// run-out groove at the song's end, so the arm is the clock; the red jewel// lit while it plays. The playing album's SLEEVE leans at the upper left// behind the record, its cover in it, else its typeset blank.//// PAINTED (`Media.xcassets`, `scripts/import-music-materials.py turntable`):// the slipmat, the record, the blank label, the spindle, the arm and its// shadow are pictures cut from wordless paintings, each laid where the// materials' `layout.json` places it in the turntable's 1420 x 1450 box,// scaled by the drawn width over it — save the label, laid larger than the// layout's (410) as the picture has it, over the record's middle and inside// its run-out groove, so its words read on a phone. The jewel is the hi-fi's// (`hifi-jewel-on`, `hifi-jewel-off`: the turntable's are the same files).// The sleeve's frame, blank and shadow are the album's.//// THE RECORD TURNS by the time played: 200 degrees a second, 33⅓ turns a// minute, gliding between the player's readings. Paused it coasts on to its// next whole turn and stops with its label upright, so a paused record reads;// played again it turns on from there; a seek moves the arm, not the record.// Its words are set once into a picture (`TurntableLabelPrint`) the record// turns; only the three times are typed live on it. Reduce Motion: the record// never turns, and the arm steps.//// THE ARM, rigid on its pivot, turns inward from the outer groove as the song// plays, the needle's distance from the record's centre running straight// from the outer groove to the run-out by the time played over the song's// length (`layout.json`'s `tonearmSweep`). Paused, or holding none of the// song, it is lifted: a few degrees back toward its rest, its shadow thrown// further off by the lamp.//// THE CONTROLS, tagged (`facePart`): the record is `play` — a tap plays or// pauses; the arm is `scrub` — dragged along its arc it seeks, the arm and// the times following the finger, the player moved there when the finger// lifts; beside the record, left of its centre `previous` and right of it// `next`, which the shell runs; the sleeve is `picture`.//// Every word is the card's (`title`, `artist`, `album`, its length in seconds// `duration`), the player's or a line.//// THE PARTY (`SongPartyFace.swift`, `song.party`) is this turntable in// another look (`TurntableLook.party`): the same record, arm, jewel, turn// and gestures on the party's dance floor, its black label typed red and// cream, the cover lying flat at the upper left, the one warm reflection// lying still over the turning record. What differs is read off the look.importCottonPaperimportCottonValuesimportSwiftUIenumSongTurntableFaceKnobs {// THE TURNTABLE'S BOX every placement is in, in the paintings' pixels// (`layout.json`): the face draws its whole drawing its room's width,// never wider than `widest` (the box's declared phone width), and every// place below is scaled by the drawn width over it.staticlet box = CGSize(width: 1420, height: 1450)staticlet widest: CGFloat = 355/// The whole drawing, the sleeve leaning above and left of the mat and/// its shadow, down to the box's foot: what the face fits to its room.staticlet whole = CGRect(x: -100, y: -190, width: 1420, height: 1640)staticlet slipmat = CGRect(x: 0, y: 150, width: 1200, height: 1200)staticlet record = CGRect(x: 30, y: 180, width: 1140, height: 1140)staticlet centre = CGPoint(x: 600, y: 750)staticlet spindle = CGRect(x: 580, y: 730, width: 40, height: 40)// THE LABEL over the record's middle: laid larger than the layout's 410,// as the picture has it, its round (radius 250) inside the run-out// groove (260); its painting's pixels (`painted`), every place on it// below being in them, about its centre (450, 450); the painted round's// and the spindle hole's radii (measured off `label-blank`); and, over a// cover, the cream paper ring the cover sits in.staticlet label = CGRect(x: 344, y: 494, width: 512, height: 512)staticlet painted: CGFloat = 900staticlet round: CGFloat = 439staticlet hole: CGFloat = 29staticlet ring: CGFloat = 34/// The veil over a cover, so the cream words read on any picture.staticlet veil: Double = 0.42// THE LABEL'S WORDS, in the label painting's pixels: the name above the// spindle hole, the rule, the artist and the album under it, the times// at the foot.staticlet titleBox = CGRect(x: 160, y: 150, width: 580, height: 262)staticlet titleSize: CGFloat = 128staticlet titleSteps: [CGFloat] = [1, 0.84, 0.7]staticlet titleTwoLines: CGFloat = 0.72staticlet rule = CGRect(x: 330, y: 500, width: 240, height: 5)staticlet artistBox = CGRect(x: 140, y: 520, width: 620, height: 66)staticlet albumBox = CGRect(x: 150, y: 590, width: 600, height: 60)staticlet artistSize: CGFloat = 54staticlet albumSize: CGFloat = 46staticlet capitalsTracking: CGFloat = 0.12staticlet timesBox = CGRect(x: 230, y: 672, width: 440, height: 64)staticlet timeSize: CGFloat = 44staticlet timesGap: CGFloat = 34/// The label's words are set into a picture at least this many pixels a/// point, so they are sharp under the record's turn.staticlet printScale: CGFloat = 2// THE TURN: 33⅓ turns a minute; the glide between the player's readings// (it reads some four times a second) and the coast to rest.staticlet degreesPerSecond: Double = 360 * (100.0 / 3) / 60staticlet glide: Double = 0.25staticlet coast: Double = 1.4/// A move of the time played that is not the record playing on: back,/// or further on than this in one reading — a seek.staticlet leap: TimeInterval = 1// THE ARM, rigid on its pivot (`tonearmPivot`; `tonearmSourcePivot` in// its painting), its needle on the outer groove as painted// (`stylusAtOuter`); the outer and run-out grooves' radii// (`grooveRadii`); its shadow, centred on it as laid; how far it lifts// back paused and how far the lamp throws its shadow, down and lifted.staticlet arm = CGRect(x: 805, y: 60, width: 480, height: 1150)staticlet armShadow = CGRect(x: 775, y: 30, width: 540, height: 1210)staticlet pivot = CGPoint(x: 1200, y: 150)staticlet stylus = CGPoint(x: 840, y: 1184)staticlet grooves = (outer: 495.9395124407814, inner: 260.0)staticlet lift: Double = 3staticlet shadowDown = CGSize(width: 6, height: 10)staticlet shadowLifted = CGSize(width: 34, height: 46)staticlet liftEase: Double = 0.45/// The arm's line in its painting, pivot to needle (measured off/// `tonearm` row by row), and the width a finger finds it within.staticlet armLine: [CGPoint] = [ CGPoint(x: 394, y: 60), CGPoint(x: 364, y: 200), CGPoint(x: 334, y: 350), CGPoint(x: 304, y: 550), CGPoint(x: 273, y: 700), CGPoint(x: 234, y: 800), CGPoint(x: 163, y: 900), CGPoint(x: 93, y: 1000), CGPoint(x: 37, y: 1126), ]staticlet armReach: CGFloat = 130/// A drag this long before the arm moves: a tap on it seeks nowhere.staticlet armSlop: CGFloat = 4// THE JEWEL (`jewelRect`), its stone the hi-fi's.staticlet jewelCentre = CGPoint(x: 1281, y: 1231)staticlet jewelStone: CGFloat = 42// THE SLEEVE leaning at the upper left behind the record, as the picture// has it: its painted box; its aperture, shadow and blank's words are the// hi-fi's sleeve's, in the sleeve's painted pixels.staticlet sleeve = CGRect(x: -40, y: -130, width: 760, height: 760)staticlet lean: Double = -4/// Beside the record: `previous` left of its centre, `next` right of it,/// over the mat's rows, under the record, the arm and the sleeve, out to/// the edges of the look's whole drawing.staticfunc previous(_ whole: CGRect) -> CGRect { CGRect(x: whole.minX, y: slipmat.minY, width: centre.x - whole.minX, height: slipmat.height) }staticfunc next(_ whole: CGRect) -> CGRect { CGRect(x: centre.x, y: slipmat.minY, width: whole.maxX - centre.x, height: slipmat.height) }}/// The look a turntable is drawn in: the dark room's (`song.turntable`), on/// the black tolex with its cream label and its sleeve leaning upper left; or/// the party's (`song.party`), on the dance floor with its black label, the/// cover lying flat upper left and the reflection over the record.publicenumTurntableLook: Sendable, Hashable {case darkRoomcase party/// The whole drawing the face fits to its room.var whole: CGRect {switchself {case .darkRoom: SongTurntableFaceKnobs.wholecase .party: SongPartyFaceKnobs.whole } }/// The label over the record's middle.var label: TurntableLabelKnobs {switchself {case .darkRoom: .darkRoomcase .party: .party } }var identifier: String {switchself {case .darkRoom: "song-turntable"case .party: "song-party" } }}/// A look's label: its painting, laid at `rect` of the turntable's box; its/// painting's side (`painted`) and, in its pixels, the round a cover is cut/// to and the spindle hole; the veil over a cover; and its three times, the box they are typed in,/// their size and the gap between them.structTurntableLabelKnobs {let painting: Stringlet rect: CGRectlet painted: CGFloatlet coverRadius: CGFloatlet hole: CGFloatlet veil: Doublelet timesBox: CGRectlet timeSize: CGFloatlet timesGap: CGFloatstaticlet darkRoom: TurntableLabelKnobs = {let knobs = SongTurntableFaceKnobs.selfreturnTurntableLabelKnobs( painting: "label-blank", rect: knobs.label, painted: knobs.painted, coverRadius: knobs.round - knobs.ring, hole: knobs.hole, veil: knobs.veil, timesBox: knobs.timesBox, timeSize: knobs.timeSize, timesGap: knobs.timesGap) }()staticlet party: TurntableLabelKnobs = {let knobs = SongPartyFaceKnobs.selfreturnTurntableLabelKnobs( painting: "party-label-blank", rect: knobs.label, painted: knobs.painted, coverRadius: knobs.coverRound, hole: knobs.hole, veil: knobs.veil, timesBox: knobs.timesBox, timeSize: knobs.timeSize, timesGap: knobs.timesGap) }()}/// What the turntable reads off its card and its player.publicenumTurntableDeck {/// How the turntable stands: playing, or paused (holding none of the/// song, too) — the hi-fi's word.publicstaticfunc state(_ play: TrackPlay) -> HifiDeck.State { HifiDeck.state(play) }/// The arm's angle at `elapsed` of `length`, in degrees clockwise from/// the outer groove: the needle's distance from the record's centre runs/// straight from the outer groove at nought to the run-out at the end.publicstaticfunc arm(_ elapsed: TimeInterval, of length: TimeInterval) -> Double {let share = length > 0 && elapsed.isFinite ? min(1, max(0, elapsed / length)) : 0let grooves = SongTurntableFaceKnobs.groovesreturn opening(grooves.outer) - opening(grooves.outer + (grooves.inner - grooves.outer) * share) }/// The arm's whole sweep, outer groove to run-out, in degrees.publicstaticvar sweep: Double { arm(1, of: 1) }/// The arm's angle as it stands: lifted a few degrees back toward its/// rest while paused or holding none of the song.publicstaticfunc arm(_ elapsed: TimeInterval, of length: TimeInterval, lifted: Bool) -> Double { arm(elapsed, of: length) - (lifted ? SongTurntableFaceKnobs.lift : 0) }/// Where the needle lies with the arm turned `degrees` clockwise from the/// outer groove, round its pivot.publicstaticfunc needle(_ degrees: Double) -> CGPoint {let knobs = SongTurntableFaceKnobs.selflet x = Double(knobs.stylus.x - knobs.pivot.x), y = Double(knobs.stylus.y - knobs.pivot.y)let r = degrees * .pi / 180return 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: its needle's/// groove, outer nought, run-out one.publicstaticfunc share(atArm degrees: Double) -> Double {let knobs = SongTurntableFaceKnobs.selflet at = needle(degrees)let radius = hypot(Double(at.x - knobs.centre.x), Double(at.y - knobs.centre.y))return min(1, max(0, (knobs.grooves.outer - radius) / (knobs.grooves.outer - knobs.grooves.inner))) }/// Where a turn of the arm by `degrees` from `elapsed` seeks to: its/// sweep is the whole song, held to it.publicstaticfunc turned(_ elapsed: TimeInterval, by degrees: Double, of length: TimeInterval) -> TimeInterval {guard length > 0else { return0 }let to = min(sweep, max(0, arm(elapsed, of: length) + degrees))return share(atArm: to) * 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.staticfunc opening(_ radius: Double) -> Double {let knobs = SongTurntableFaceKnobs.selflet 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 record's turn, in degrees clockwise from its label upright:/// playing, 33⅓ turns a minute of the time played, on from `phase`;/// paused, the next whole turn on, where it coasts to rest with its label/// upright; under Reduce Motion, upright always.publicstaticfunc turn(_ elapsed: TimeInterval, playing: Bool, phase: Double, reduceMotion: Bool = false) -> Double {guard !reduceMotion else { return0 }let turning = elapsed * SongTurntableFaceKnobs.degreesPerSecond + phasereturn playing ? turning : (turning / 360).rounded(.up) * 360 }/// The phase the record plays on from when it starts again at `elapsed`/// from rest: where it came to rest, so it turns on from there.publicstaticfunc resumed(_ elapsed: TimeInterval, phase: Double) -> Double { turn(elapsed, playing: false, phase: phase) - elapsed * SongTurntableFaceKnobs.degreesPerSecond }/// The phase after the time played moves from `from` to `to`: kept while/// the record plays on, moved against a seek (back, or a leap on) so the/// record does not spin to it.publicstaticfunc phase(_ phase: Double, from: TimeInterval, to: TimeInterval) -> Double {let by = to - fromguard by < 0 || by > SongTurntableFaceKnobs.leap else { return phase }return phase - by * SongTurntableFaceKnobs.degreesPerSecond }/// The label's words, in their order: the song's name, its artist and/// its album in capitals.publicstaticfunc label(_ card: DeckCard) -> [String] { [card.text("title"), card.text("artist")?.uppercased(), card.text("album")?.uppercased()].compactMap { $0 } }/// The three times at the label's foot: played, left (running down) and/// the song's length; only the time played while the length is unknown.publicstaticfunc times(_ elapsed: TimeInterval, of length: TimeInterval) -> [String] {guard length > 0else { return [TrackClock.said(elapsed)] }// Left from the whole seconds played, so the clocks add up.return [TrackClock.said(elapsed), TrackClock.left(elapsed.rounded(.down), of: length), TrackClock.said(length)] }}publicstructSongTurntableFace: View {let card: DeckCard/// The playing album's cover; nil lays the blank label and the sleeve's/// typeset blank.var cover: FacePicture?var play: TrackPlay = .stillvar toggle: (() -> Void)?var seek: (TimeInterval) -> Void = { _ in }let look: TurntableLook/// Where a drag of the arm has the song, until the finger lifts. @State privatevar scrubbing: TimeInterval?/// Where the record turns on from (`TurntableDeck.turn`). @State privatevar phase: Double = 0 @Environment(\.accessibilityReduceMotion) privatevar reduceMotion @Environment(\.displayScale) privatevar displayScalepublicinit( card: DeckCard, cover: FacePicture?, play: TrackPlay = .still, toggle: (() -> Void)? = nil, seek: @escaping (TimeInterval) -> Void = { _ in }, look: TurntableLook = .darkRoom ) {self.card = cardself.cover = coverself.play = playself.toggle = toggleself.seek = seekself.look = look }publicvar body: someView {let knobs = SongTurntableFaceKnobs.selflet state = TurntableDeck.state(play)let length = HifiDeck.length(play, card)let elapsed = play.held ? (scrubbing ?? play.elapsed) : 0let whole = look.wholeGeometryReader { room inlet width = min(room.size.width, knobs.widest, room.size.height * whole.width / whole.height)let s = width / whole.width ZStack(alignment: .topLeading) { beside(whole, s)if look == .darkRoom { leaningSleeve(s) }TurntablePainted(name: "slipmat").placed(knobs.slipmat, s) turning(state: state, elapsed: elapsed, length: length, s: s)TurntablePainted(name: "spindle").placed(knobs.spindle, s) arm(state: state, elapsed: elapsed, length: length, s: s) jewel(state: state, s: s)if look == .party {TurntablePainted(name: "party-glow").placed(SongPartyFaceKnobs.glow, s) lyingCover(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) .frame(width: room.size.width, height: room.size.height) .accessibilityElement(children: .contain) .accessibilityIdentifier(look.identifier) } .onChange(of: state) { was, now inif 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, turned together; the label's words set into/// a picture once, the three times typed on it live.privatefunc turning(state: HifiDeck.State, elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> someView {let knobs = SongTurntableFaceKnobs.selflet playing = state == .playinglet turn = TurntableDeck.turn(play.held ? play.elapsed : 0, playing: playing, phase: phase, reduceMotion: reduceMotion)let side = knobs.record.width * slet face = look.labellet k = face.rect.width / face.paintedlet inRecord = { (rect: CGRect) in CGRect(x: rect.minX - knobs.record.minX, y: rect.minY - knobs.record.minY, width: rect.width, height: rect.height) }let labelRect = inRecord(face.rect)let inLabel = { (rect: CGRect) in CGRect(x: labelRect.minX + rect.minX * k, y: labelRect.minY + rect.minY * k, width: rect.width * k, height: rect.height * k) }return ZStack(alignment: .topLeading) {TurntablePainted(name: "record").frame(width: side, height: side) label(s).at(labelRect, s)TurntableLabelPrint( card: card, covered: cover != nil, side: face.rect.width * s, scale: max(displayScale, knobs.printScale), look: look) .at(labelRect, s) times(elapsed: elapsed, length: length, s: s * k) .frame(width: face.timesBox.width * k * s, height: face.timesBox.height * k * s) .at(inLabel(face.timesBox), s) } .frame(width: side, height: side, alignment: .topLeading) .rotationEffect(.degrees(turn)) .animation(reduceMotion ? nil : playing ? .linear(duration: knobs.glide) : .easeOut(duration: knobs.coast), value: turn) .allowsHitTesting(false) .overlay {Circle() .fill(Color.clear) .contentShape(Circle()) .onTapGesture { toggle?() } .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("faces", playing ? "media.pause" : "media.play")) .accessibilityValue(MusicWhole.joined(TurntableDeck.label(card).prefix(2).map { $0 }) ?? "") .accessibilityAddTraits(.isButton) .accessibilityAction { toggle?() } .accessibilityIdentifier("turntable-record") .facePart(.play) } .overlay { labelWords } .at(knobs.record, s) }/// The label under its words: the cover cropped round inside the cream/// paper ring (the party's red and cream rings), veiled, the spindle hole/// through it; else the blank label.privatefunc label(_ s: CGFloat) -> someView {let face = look.labellet side = face.rect.width * slet k = side / face.paintedlet inner = face.coverRadius * 2 * kreturn ZStack {TurntablePainted(name: face.painting)iflet cover { cover .frame(width: inner, height: inner) .clipShape(Circle())Circle() .fill(Color.black.opacity(face.veil)) .frame(width: inner, height: inner)Circle() .fill(MusicMaterial.window.colour) .frame(width: face.hole * 2 * k, height: face.hole * 2 * k) } } .frame(width: side, height: side) }/// The label's words as VoiceOver reads them: the song's name, a header,/// and its artist and album — drawn in the printed picture, read here.privatevar labelWords: someView { VStack {iflet title = card.text("title") {Color.clear .accessibilityElement() .accessibilityLabel(title) .accessibilityAddTraits(.isHeader) .accessibilityIdentifier("deck-title") }iflet byline = HifiDeck.byline(card) {Color.clear .accessibilityElement() .accessibilityLabel(byline) .accessibilityIdentifier("face-byline") } } .allowsHitTesting(false) }/// The time played, the time left and the song's length, small in a row./// The dark room's in its serif, black on the cream label and cream on a/// cover; the party's typed in the cream on its black label.privatefunc times(elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> someView {let face = look.labellet party = look == .partyreturn HStack(spacing: face.timesGap * s) {ForEach(Array(TurntableDeck.times(elapsed, of: length).enumerated()), id: \.offset) { _, time inText(time) } } .font(party ? CottonFonts.type(face.timeSize * s) : CottonFonts.serif(face.timeSize * s)) .monospacedDigit() .lineLimit(1) .fixedSize() .foregroundStyle( party ? MusicMaterial.partyCream.colour : cover == nil ? MusicMaterial.labelInk.colour : MusicMaterial.engraving.colour) .accessibilityHidden(true) }// MARK: The arm and the jewel/// The arm on its pivot at the share played, lifted while paused, its/// shadow under it; dragged along its arc it seeks.privatefunc arm(state: HifiDeck.State, elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> someView {let knobs = SongTurntableFaceKnobs.selflet lifted = state == .pausedlet angle = TurntableDeck.arm(elapsed, of: length, lifted: lifted)let throwBy = lifted ? knobs.shadowLifted : knobs.shadowDownlet scrubs = play.held && length > 0let pivot = CGPoint(x: knobs.pivot.x * s, y: knobs.pivot.y * s)let anchor = { (rect: CGRect) inUnitPoint(x: (knobs.pivot.x - rect.minX) / rect.width, y: (knobs.pivot.y - rect.minY) / rect.height) }let motion: Animation? = reduceMotion ? nil : .easeInOut(duration: knobs.liftEase)return ZStack(alignment: .topLeading) {TurntablePainted(name: "tonearm-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)TurntableArm( angle: angle, anchor: anchor(knobs.arm), size: CGSize(width: knobs.arm.width * s, height: knobs.arm.height * s), pivot: pivot, s: s, turn: { degrees inguard scrubs else { return } scrubbing = TurntableDeck.turned(scrubbing ?? play.elapsed, by: degrees, of: length) }, lifted: {iflet scrubbing { seek(scrubbing) } scrubbing = nil }) .animation(motion, value: lifted) .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("faces", "turntable.position")) .accessibilityValue(scrubs ? TrackClock.said(elapsed) : "") .accessibilityAdjustableAction { direction inguard scrubs else { return }let by = TurntableDeck.sweep / 20 seek(TurntableDeck.turned(play.elapsed, by: direction == .increment ? by : -by, of: length)) } .accessibilityIdentifier("turntable-arm") .facePart(.scrub) .at(knobs.arm, s) } .coordinateSpace(.named(TurntableArm.space)) }/// The red jewel, lit while it plays.privatefunc jewel(state: HifiDeck.State, s: CGFloat) -> someView {let knobs = SongTurntableFaceKnobs.selflet lit = state == .playinglet painting = lit ? SongHifiFaceKnobs.jewelOn : SongHifiFaceKnobs.jewelOfflet scale = knobs.jewelStone / painting.stone.widthlet 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)returnTurntablePainted(name: lit ? "hifi-jewel-on" : "hifi-jewel-off").placed(drawn, s) }// MARK: Beside the record, and the sleeve/// Beside the record: a tap left of its centre the previous song, right/// of it the next — the shell's acts.privatefunc beside(_ whole: CGRect, _ s: CGFloat) -> someView {let previous = SongTurntableFaceKnobs.previous(whole)let next = SongTurntableFaceKnobs.next(whole)return ZStack(alignment: .topLeading) {Color.clear .contentShape(Rectangle()) .frame(width: previous.width * s, height: previous.height * s) .accessibilityElement() .accessibilityLabel(ShellLines.line("faces", "turntable.previous")) .accessibilityAddTraits(.isButton) .accessibilityIdentifier("turntable-previous") .facePart(.previous) .at(previous, s)Color.clear .contentShape(Rectangle()) .frame(width: next.width * s, height: next.height * s) .accessibilityElement() .accessibilityLabel(ShellLines.line("faces", "turntable.next")) .accessibilityAddTraits(.isButton) .accessibilityIdentifier("turntable-next") .facePart(.next) .at(next, s) } }/// The playing album's sleeve, leaning behind the record: its shadow, its/// cover (else the typeset blank) and its frame.privatefunc leaningSleeve(_ s: CGFloat) -> someView {let knobs = SongTurntableFaceKnobs.selflet hifi = SongHifiFaceKnobs.selflet k = knobs.sleeve.width / hifi.sleevePaintedlet inSleeve = { (rect: CGRect) in CGRect(x: rect.minX * k, y: rect.minY * k, width: rect.width * k, height: rect.height * k) }return ZStack(alignment: .topLeading) {TurntablePainted(name: "sleeve-shadow").placed(inSleeve(hifi.sleeveShadow), s)Group {iflet cover { cover.frame(width: hifi.cover.width * k * s, height: hifi.cover.height * k * s).clipped() } else { blank(k: k, s: s) } } .frame(width: hifi.cover.width * k * s, height: hifi.cover.height * k * s) .at(inSleeve(hifi.cover), s)TurntablePainted(name: "sleeve-front").placed(CGRect(x: 0, y: 0, width: knobs.sleeve.width, height: knobs.sleeve.height), s) } .frame(width: knobs.sleeve.width * s, height: knobs.sleeve.height * s, alignment: .topLeading) .rotationEffect(.degrees(knobs.lean), anchor: .bottom) .contentShape(Rectangle()) .accessibilityElement() .accessibilityLabel(MusicWhole.joined([card.text("album"), card.text("artist")]) ?? "") .accessibilityIdentifier("turntable-sleeve") .facePart(.picture) .at(knobs.sleeve, s) }/// The sleeve's blank stock, the album and its artist typeset on it.privatefunc blank(k: CGFloat, s: CGFloat) -> someView {let hifi = SongHifiFaceKnobs.selflet words = hifi.blankWordsreturn ZStack(alignment: .topLeading) {TurntablePainted(name: "sleeve-blank") VStack(spacing: 18 * k * s) {iflet album = card.text("album") {Text(album) .font(CottonFonts.serif(hifi.blankTitleSize * k * s)) .multilineTextAlignment(.center) .lineLimit(3) .minimumScaleFactor(0.5) }iflet artist = card.text("artist") {Text(artist) .font(CottonFonts.serif(hifi.blankArtistSize * k * s)) .italic() .lineLimit(1) .minimumScaleFactor(0.5) } } .foregroundStyle(MusicMaterial.engraving.colour) .frame(width: words.width * k * s, height: words.height * k * s) .offset(x: words.minX * k * s, y: words.minY * k * s) } }/// The party's: the playing album's cover lying flat on the floor at the/// upper left, where it has one; nothing where it has none, its label/// saying the album. @ViewBuilderprivatefunc lyingCover(_ s: CGFloat) -> someView {iflet cover {let rect = SongPartyFaceKnobs.cover cover .frame(width: rect.width * s, height: rect.height * s) .clipped() .contentShape(Rectangle()) .accessibilityElement() .accessibilityLabel(MusicWhole.joined([card.text("album"), card.text("artist")]) ?? "") .accessibilityIdentifier("party-cover") .facePart(.picture) .at(rect, s) } }}/// The label's words, set once into a picture the record turns: the song's/// name, the rule, its artist and its album. Set again only when the words,/// the label's size or the ink change.structTurntableLabelPrint: View {let card: DeckCard/// Laid over a cover: cream words, else the label's black.let covered: Bool/// The label's side, in points.let side: CGFloat/// The picture's pixels a point.let scale: CGFloat/// Whose label: the dark room's words, or the party's.let look: TurntableLookvar body: someView {let key = TurntableLabelPrintCache.Key(words: TurntableDeck.label(card), covered: covered, side: side, scale: scale, look: look)let image = TurntableLabelPrintCache.shared.image(for: key) {let renderer = ImageRenderer(content: TurntableLabelPrintWords(card: card, covered: covered, side: side, look: look)) renderer.scale = scalereturn renderer.cgImage }Group {iflet image {Image(decorative: image, scale: scale).resizable() } } .frame(width: side, height: side) .accessibilityHidden(true) }}/// A look's printed words: the dark room's, or the party's.structTurntableLabelPrintWords: View {let card: DeckCardlet covered: Boollet side: CGFloatlet look: TurntableLookvar body: someView {switch look {case .darkRoom: TurntableLabelWords(card: card, covered: covered, side: side)case .party: PartyLabelWords(card: card, side: side) } }}/// The label's printed words, laid on a square the label's side.structTurntableLabelWords: View {let card: DeckCardlet covered: Boollet side: CGFloatvar body: someView {let knobs = SongTurntableFaceKnobs.selflet k = side / knobs.paintedlet ink = covered ? MusicMaterial.engraving.colour : MusicMaterial.labelInk.colourlet place = { (rect: CGRect) in CGRect(x: rect.minX * k, y: rect.minY * k, width: rect.width * k, height: rect.height * k) } ZStack(alignment: .topLeading) {iflet title = card.text("title") {TurntableTitle(words: title, s: k) .frame(width: knobs.titleBox.width * k, height: knobs.titleBox.height * k, alignment: .bottom) .at(place(knobs.titleBox), 1) }Rectangle() .fill(ink) .frame(width: knobs.rule.width * k, height: max(0.5, knobs.rule.height * k)) .at(place(knobs.rule), 1)iflet artist = card.text("artist") { capitals(artist, size: knobs.artistSize * k) .frame(width: knobs.artistBox.width * k, height: knobs.artistBox.height * k) .at(place(knobs.artistBox), 1) }iflet album = card.text("album") { capitals(album, size: knobs.albumSize * k) .frame(width: knobs.albumBox.width * k, height: knobs.albumBox.height * k) .at(place(knobs.albumBox), 1) } } .foregroundStyle(ink) .frame(width: side, height: side, alignment: .topLeading) }/// Words in small capitals: spaced while they fit, else close set and/// shrinking.privatefunc capitals(_ words: String, size: CGFloat) -> someView {ViewThatFits(in: .horizontal) { line(words, size: size, tracking: SongTurntableFaceKnobs.capitalsTracking).fixedSize() line(words, size: size, tracking: 0).minimumScaleFactor(0.45) } }privatefunc line(_ words: String, size: CGFloat, tracking: CGFloat) -> someView {Text(words.uppercased()) .font(CottonFonts.serif(size)) .tracking(tracking * size) .lineLimit(1) }}/// The label's printed pictures set so far, a few: one per song the/// turntable has played lately, at each size it was drawn.@MainActorfinalclassTurntableLabelPrintCache {staticlet shared = TurntableLabelPrintCache()staticlet kept = 12structKey: Hashable {let words: [String]let covered: Boollet side: CGFloatlet scale: CGFloatlet look: TurntableLook }privatefinalclassBox: NSObject {let key: Keyinit(_ key: Key) { self.key = key }overridevar hash: Int { key.hashValue }overridefunc isEqual(_ object: Any?) -> Bool { (object as? Box)?.key == key } }privatelet images = NSCache<Box, CGImage>()init() { images.countLimit = Self.kept }func image(for key: Key, render: () -> CGImage?) -> CGImage? {guard key.side.isFinite, key.side > 0else { returnnil }let box = Box(key)iflet image = images.object(forKey: box) { return image }guardlet image = render() else { returnnil } images.setObject(image, forKey: box)return image }}/// The song's name in a condensed heavy sans: one line at its size, else a/// step or two smaller, else two lines smaller still, shrinking before it/// wraps.privatestructTurntableTitle: View {let words: Stringlet s: CGFloatvar body: someView {let knobs = SongTurntableFaceKnobs.selfViewThatFits(in: .horizontal) {ForEach(knobs.titleSteps, id: \.self) { step in line(knobs.titleSize * step).lineLimit(1).fixedSize() } line(knobs.titleSize * knobs.titleTwoLines) .lineLimit(2) .minimumScaleFactor(0.5) .multilineTextAlignment(.center) } }privatefunc line(_ size: CGFloat) -> Text {Text(words).font(.system(size: size * s, weight: .heavy).width(.condensed)) }}/// The brass arm turned to `angle` round its pivot; a finger dragging along/// its arc turns it by the angle swept round the pivot.privatestructTurntableArm: View {staticlet space = "turntable"let angle: Doublelet anchor: UnitPointlet size: CGSize/// The pivot in the turntable's space, in points.let pivot: CGPointlet s: CGFloatlet turn: (_ degrees: Double) -> Voidlet lifted: () -> Void @State privatevar last: Double?var body: someView {TurntablePainted(name: "tonearm") .frame(width: size.width, height: size.height) .contentShape(TurntableArmLine(scale: size.width / SongTurntableFaceKnobs.arm.width)) .rotationEffect(.degrees(angle), anchor: anchor) .gesture(DragGesture(minimumDistance: SongTurntableFaceKnobs.armSlop * s, coordinateSpace: .named(Self.space)) .onChanged { touch inlet now = HifiDeck.angle(of: touch.location, round: pivot)iflet last { turn(HifiDeck.turn(from: last, to: now)) } last = now } .onEnded { _ in last = nil lifted() }) }}/// Where a finger finds the arm: a band along its painted line.privatestructTurntableArmLine: Shape {let scale: CGFloatfunc path(in rect: CGRect) -> Path {var line = Path() line.addLines(SongTurntableFaceKnobs.armLine.map { CGPoint(x: $0.x * scale, y: $0.y * scale) })return line.strokedPath(StrokeStyle(lineWidth: SongTurntableFaceKnobs.armReach * scale, lineCap: .round, lineJoin: .round)) }}/// One of the turntable's paintings, stretched to its frame; nothing where/// the build carries no pictures. A painting takes no tap: the parts do.privatestructTurntablePainted: View {let name: Stringvar body: someView {iflet image = CottonMedia.image(name) { image.resizable().interpolation(.high).allowsHitTesting(false).accessibilityHidden(true) } }}privateextensionView {/// This view laid at `rect` of the turntable's box, in its pixels,/// scaled by `s`.func placed(_ rect: CGRect, _ s: CGFloat) -> someView { 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) -> someView { offset(x: rect.minX * s, y: rect.minY * s) }}/// The turntable's face, and the songs it is reviewed playing. Under the/// player the app installs (`faceTrackPlayer`), else paused; each specimen/// names the player's word it is reviewed under (`Specimen.play`).publicenumSongTurntableFaceCatalogue {publicstaticlet stock: FaceStock = .ownpublicstaticlet ground: FaceGround = .tolexpublicstaticlet face = Face(id: "song.turntable", specimens: [Specimen("playing", harbour, picture: "poster-ferry", play: TrackPlay(held: true, playing: true, elapsed: 134, duration: 242)),Specimen("paused", harbour, picture: "poster-ferry", play: TrackPlay(held: true, playing: false, elapsed: 134, duration: 242)),Specimen("no cover", train, play: TrackPlay(held: true, playing: true, elapsed: 61, duration: 197)),Specimen("largest", largest, play: TrackPlay(held: true, playing: false, elapsed: 312, duration: 669)), ]) { card, picture, _ inTrackPlaying(card: card) { player inSongTurntableFace(card: card, cover: picture, play: player.play, toggle: player.toggle, seek: player.seek) } }/// An invented song off an invented album, 4:02 long.publicstaticlet harbour = DeckCard(face: .songTurntable, fields: ["title": .text("Harbour Lights"),"artist": .text("The Low Tide"),"album": .text("Night Ferry"),"duration": .number(242), ])/// Another album's song, no cover, 3:17 long.publicstaticlet train = DeckCard(face: .songTurntable, fields: ["title": .text("Slow Train"),"artist": .text("Mara Vance"),"album": .text("Common Ground"),"duration": .number(197), ])/// The longest words, no cover, drawn at every text size Loom has, the/// largest among them.publicstaticlet largest = DeckCard(face: .songTurntable, fields: ["title": .text("The Slow Train Home Through Sixteen Lanterns"),"artist": .text("Mara Vance and The Low Tide"),"album": .text("Sixteen Lanterns and the Blue Hour Sessions"),"duration": .number(669), ])}