// 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. import CottonPaper import CottonValues import SwiftUI enum SongTurntableFaceKnobs { // 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. static let box = CGSize(width: 1420, height: 1450) static let 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. static let whole = CGRect(x: -100, y: -190, width: 1420, height: 1640) static let slipmat = CGRect(x: 0, y: 150, width: 1200, height: 1200) static let record = CGRect(x: 30, y: 180, width: 1140, height: 1140) static let centre = CGPoint(x: 600, y: 750) static let 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. static let label = CGRect(x: 344, y: 494, width: 512, height: 512) static let painted: CGFloat = 900 static let round: CGFloat = 439 static let hole: CGFloat = 29 static let ring: CGFloat = 34 /// The veil over a cover, so the cream words read on any picture. static let 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. static let titleBox = CGRect(x: 160, y: 150, width: 580, height: 262) static let titleSize: CGFloat = 128 static let titleSteps: [CGFloat] = [1, 0.84, 0.7] static let titleTwoLines: CGFloat = 0.72 static let rule = CGRect(x: 330, y: 500, width: 240, height: 5) static let artistBox = CGRect(x: 140, y: 520, width: 620, height: 66) static let albumBox = CGRect(x: 150, y: 590, width: 600, height: 60) static let artistSize: CGFloat = 54 static let albumSize: CGFloat = 46 static let capitalsTracking: CGFloat = 0.12 static let timesBox = CGRect(x: 230, y: 672, width: 440, height: 64) static let timeSize: CGFloat = 44 static let 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. static let 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. static let degreesPerSecond: Double = 360 * (100.0 / 3) / 60 static let glide: Double = 0.25 static let 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. static let 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. static let arm = CGRect(x: 805, y: 60, width: 480, height: 1150) static let armShadow = CGRect(x: 775, y: 30, width: 540, height: 1210) static let pivot = CGPoint(x: 1200, y: 150) static let stylus = CGPoint(x: 840, y: 1184) static let grooves = (outer: 495.9395124407814, inner: 260.0) static let lift: Double = 3 static let shadowDown = CGSize(width: 6, height: 10) static let shadowLifted = CGSize(width: 34, height: 46) static let 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. static let 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), ] static let armReach: CGFloat = 130 /// A drag this long before the arm moves: a tap on it seeks nowhere. static let armSlop: CGFloat = 4 // THE JEWEL (`jewelRect`), its stone the hi-fi's. static let jewelCentre = CGPoint(x: 1281, y: 1231) static let 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. static let sleeve = CGRect(x: -40, y: -130, width: 760, height: 760) static let 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. static func previous(_ whole: CGRect) -> CGRect { CGRect(x: whole.minX, y: slipmat.minY, width: centre.x - whole.minX, height: slipmat.height) } static func 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. public enum TurntableLook: Sendable, Hashable { case darkRoom case party /// The whole drawing the face fits to its room. var whole: CGRect { switch self { case .darkRoom: SongTurntableFaceKnobs.whole case .party: SongPartyFaceKnobs.whole } } /// The label over the record's middle. var label: TurntableLabelKnobs { switch self { case .darkRoom: .darkRoom case .party: .party } } var identifier: String { switch self { 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. struct TurntableLabelKnobs { let painting: String let rect: CGRect let painted: CGFloat let coverRadius: CGFloat let hole: CGFloat let veil: Double let timesBox: CGRect let timeSize: CGFloat let timesGap: CGFloat static let darkRoom: TurntableLabelKnobs = { let knobs = SongTurntableFaceKnobs.self return TurntableLabelKnobs( 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) }() static let party: TurntableLabelKnobs = { let knobs = SongPartyFaceKnobs.self return TurntableLabelKnobs( 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. public enum TurntableDeck { /// How the turntable stands: playing, or paused (holding none of the /// song, too) — the hi-fi's word. public static func 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. public static func arm(_ elapsed: TimeInterval, of length: TimeInterval) -> Double { let share = length > 0 && elapsed.isFinite ? min(1, max(0, elapsed / length)) : 0 let grooves = SongTurntableFaceKnobs.grooves return opening(grooves.outer) - opening(grooves.outer + (grooves.inner - grooves.outer) * share) } /// The arm's whole sweep, outer groove to run-out, in degrees. public static var 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. public static func 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. public static func needle(_ degrees: Double) -> CGPoint { let knobs = SongTurntableFaceKnobs.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: its needle's /// groove, outer nought, run-out one. public static func share(atArm degrees: Double) -> Double { let knobs = SongTurntableFaceKnobs.self let 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. public static func turned(_ elapsed: TimeInterval, by degrees: Double, of length: TimeInterval) -> TimeInterval { guard length > 0 else { return 0 } 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. static func opening(_ radius: Double) -> Double { let knobs = SongTurntableFaceKnobs.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 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. public static func turn(_ elapsed: TimeInterval, playing: Bool, phase: Double, reduceMotion: Bool = false) -> Double { guard !reduceMotion else { return 0 } let turning = elapsed * SongTurntableFaceKnobs.degreesPerSecond + phase return 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. public static func 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. public static func phase(_ phase: Double, from: TimeInterval, to: TimeInterval) -> Double { let by = to - from guard 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. public static func 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. public static func times(_ elapsed: TimeInterval, of length: TimeInterval) -> [String] { guard length > 0 else { 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)] } } public struct SongTurntableFace: 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 = .still var toggle: (() -> Void)? var seek: (TimeInterval) -> Void = { _ in } let look: TurntableLook /// 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 @Environment(\.displayScale) private var displayScale public init( card: DeckCard, cover: FacePicture?, play: TrackPlay = .still, toggle: (() -> Void)? = nil, seek: @escaping (TimeInterval) -> Void = { _ in }, look: TurntableLook = .darkRoom ) { self.card = card self.cover = cover self.play = play self.toggle = toggle self.seek = seek self.look = look } public var body: some View { let knobs = SongTurntableFaceKnobs.self let state = TurntableDeck.state(play) let length = HifiDeck.length(play, card) let elapsed = play.held ? (scrubbing ?? play.elapsed) : 0 let whole = look.whole GeometryReader { room in let 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 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, turned together; the label's words set into /// a picture once, the three times typed on it live. private func turning(state: HifiDeck.State, elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> some View { let knobs = SongTurntableFaceKnobs.self let playing = state == .playing let turn = TurntableDeck.turn(play.held ? play.elapsed : 0, playing: playing, phase: phase, reduceMotion: reduceMotion) let side = knobs.record.width * s let face = look.label let k = face.rect.width / face.painted let 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. private func label(_ s: CGFloat) -> some View { let face = look.label let side = face.rect.width * s let k = side / face.painted let inner = face.coverRadius * 2 * k return ZStack { TurntablePainted(name: face.painting) if let 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. private var labelWords: some View { VStack { if let title = card.text("title") { Color.clear .accessibilityElement() .accessibilityLabel(title) .accessibilityAddTraits(.isHeader) .accessibilityIdentifier("deck-title") } if let 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. private func times(elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> some View { let face = look.label let party = look == .party return HStack(spacing: face.timesGap * s) { ForEach(Array(TurntableDeck.times(elapsed, of: length).enumerated()), id: \.offset) { _, time in Text(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. private func arm(state: HifiDeck.State, elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> some View { let knobs = SongTurntableFaceKnobs.self let lifted = state == .paused let angle = TurntableDeck.arm(elapsed, of: length, lifted: lifted) let throwBy = lifted ? knobs.shadowLifted : knobs.shadowDown let scrubs = play.held && length > 0 let pivot = CGPoint(x: knobs.pivot.x * s, y: knobs.pivot.y * s) 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.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 in guard scrubs else { return } scrubbing = TurntableDeck.turned(scrubbing ?? play.elapsed, by: degrees, of: length) }, lifted: { if let 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 in guard 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. private func jewel(state: HifiDeck.State, s: CGFloat) -> some View { let knobs = SongTurntableFaceKnobs.self let lit = state == .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 TurntablePainted(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. private func beside(_ whole: CGRect, _ s: CGFloat) -> some View { 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. private func leaningSleeve(_ s: CGFloat) -> some View { let knobs = SongTurntableFaceKnobs.self let hifi = SongHifiFaceKnobs.self let k = knobs.sleeve.width / hifi.sleevePainted let 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 { if let 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. private func blank(k: CGFloat, s: CGFloat) -> some View { let hifi = SongHifiFaceKnobs.self let words = hifi.blankWords return ZStack(alignment: .topLeading) { TurntablePainted(name: "sleeve-blank") VStack(spacing: 18 * k * s) { if let album = card.text("album") { Text(album) .font(CottonFonts.serif(hifi.blankTitleSize * k * s)) .multilineTextAlignment(.center) .lineLimit(3) .minimumScaleFactor(0.5) } if let 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. @ViewBuilder private func lyingCover(_ s: CGFloat) -> some View { if let 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. struct TurntableLabelPrint: 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: TurntableLook var body: some View { 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 = scale return renderer.cgImage } Group { if let 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. struct TurntableLabelPrintWords: View { let card: DeckCard let covered: Bool let side: CGFloat let look: TurntableLook var body: some View { 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. struct TurntableLabelWords: View { let card: DeckCard let covered: Bool let side: CGFloat var body: some View { let knobs = SongTurntableFaceKnobs.self let k = side / knobs.painted let ink = covered ? MusicMaterial.engraving.colour : MusicMaterial.labelInk.colour let place = { (rect: CGRect) in CGRect(x: rect.minX * k, y: rect.minY * k, width: rect.width * k, height: rect.height * k) } ZStack(alignment: .topLeading) { if let 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) if let 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) } if let 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. private func capitals(_ words: String, size: CGFloat) -> some View { ViewThatFits(in: .horizontal) { line(words, size: size, tracking: SongTurntableFaceKnobs.capitalsTracking).fixedSize() line(words, size: size, tracking: 0).minimumScaleFactor(0.45) } } private func line(_ words: String, size: CGFloat, tracking: CGFloat) -> some View { 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. @MainActor final class TurntableLabelPrintCache { static let shared = TurntableLabelPrintCache() static let kept = 12 struct Key: Hashable { let words: [String] let covered: Bool let side: CGFloat let scale: CGFloat let look: TurntableLook } private final class Box: NSObject { let key: Key init(_ key: Key) { self.key = key } override var hash: Int { key.hashValue } override func isEqual(_ object: Any?) -> Bool { (object as? Box)?.key == key } } private let images = NSCache() init() { images.countLimit = Self.kept } func image(for key: Key, render: () -> CGImage?) -> CGImage? { guard key.side.isFinite, key.side > 0 else { return nil } let box = Box(key) if let image = images.object(forKey: box) { return image } guard let image = render() else { return nil } 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. private struct TurntableTitle: View { let words: String let s: CGFloat var body: some View { let knobs = SongTurntableFaceKnobs.self ViewThatFits(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) } } private func 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. private struct TurntableArm: View { static let space = "turntable" let angle: Double let anchor: UnitPoint let size: CGSize /// The pivot in the turntable's space, in points. let pivot: CGPoint let s: CGFloat let turn: (_ degrees: Double) -> Void let lifted: () -> Void @State private var last: Double? var body: some View { 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 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() }) } } /// Where a finger finds the arm: a band along its painted line. private struct TurntableArmLine: Shape { let scale: CGFloat func 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. private struct TurntablePainted: 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 turntable'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 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`). public enum SongTurntableFaceCatalogue { public static let stock: FaceStock = .own public static let ground: FaceGround = .tolex public static let 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, _ in TrackPlaying(card: card) { player in SongTurntableFace(card: card, cover: picture, play: player.play, toggle: player.toggle, seek: player.seek) } } /// An invented song off an invented album, 4:02 long. public static let 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. public static let 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. public static let 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), ]) }