// The SONG PLAYING as a BLACK HI-FI DECK (the owner's go, 4 Nov 2026; the dark // room's pictures `listening-hifi`, `listening-hifi-paused`): the player face // of Music's Listening deck, drawn for the playing song's card, a bare object // standing on the music's black tolex (`FaceGround.tolex`) with no paper and // no rule of the deck's. Two cream level meters behind the plate's windows, // their needles live; the song's name in spaced capitals and "artist · // album" under it in cream; the red jewel lit while it plays; the time played // above the brass knob, the knob turned by it from nought to the song's end // round its tick ring, "0:00" and the song's length at the ring's ends and // the time left below. The playing album's sleeve leans against the wall // above the deck, upper left, its cover in it, else its typeset blank. // // PAINTED (`Media.xcassets`, `scripts/import-music-materials.py`): the plate, // its shadow, the meter card (laid twice), the needle (laid twice), the knob, // its tick ring, the jewel lit and dark, and the sleeve's frame, blank and // shadow are pictures cut from wordless paintings, each laid where the // materials' `layout.json` places it in the deck's 1135 x 1015 box, scaled by // the drawn width over 1135 — save the knob and its ring, drawn smaller than // the layout's about the same centre, as the picture has them: at the // layout's size the ring's top runs through the song's words and the jewel. // The face draws live only what the data decides: every word and figure, the // needles' and the knob's angles, and which jewel is laid. // // THE PLAYER (`faceTrackPlayer`) says how far into the song it is and how // loud it sounds (`TrackPlay.level`, each channel nought to one): PLAYING the // jewel is lit and each needle stands at its channel's level, nought to one // read as −40 to +3 dB on the painted scale (`SongHifiFaceKnobs.marks`), // eased between the player's readings; PAUSED, or holding none of the song, // the jewel is dark and the needles rest at −40. Reduce Motion steps them. // Nothing here keeps time. // // THE CONTROLS, tagged (`facePart`): the knob is `scrub` — turned round its // centre it seeks, the knob and the times following the turn, the player // moved there when the finger lifts; the jewel is `play`, a tap plays or // pauses; the meters' plate is `previous` left of its centre and `next` // right of it, which the shell runs; the song's name is `title`, the sleeve // `picture`. // // Every word is the card's (`title`, `artist`, `album`, its length in seconds // `duration`), the player's or a line. import CottonPaper import CottonValues import SwiftUI enum SongHifiFaceKnobs { // THE DECK BOX every placement is in, in the paintings' pixels // (`layout.json`): the face draws it its own width, never wider than // `widest`, the sleeve leaning above it, and every place below is scaled // by the drawn width over it. static let box = CGSize(width: 1135, height: 1015) static let widest: CGFloat = 340 /// The whole drawing, the sleeve above the deck and the shadow under /// it: what the face fits to its room. static let whole = CGRect(x: 0, y: -380, width: 1135, height: 1475) static let shadow = CGRect(x: -80, y: -80, width: 1295, height: 1175) static let meterInserts = [CGRect(x: 71, y: 75, width: 471, height: 202), CGRect(x: 590, y: 75, width: 471, height: 202)] static let meterWindows = [CGRect(x: 75, y: 80, width: 463, height: 190), CGRect(x: 594, y: 80, width: 463, height: 190)] // THE METERS: each needle's pivot, its painted pivot and the length // from it to its tip; and the painted scale's marks, measured off // `hifi-meter` as the layout lays it — each figure's decibels, the // angle (clockwise from upright, round the pivot) of its long tick and // how far from the pivot that tick ends. Between marks the needle's // angle runs straight. static let pivots = [CGPoint(x: 307, y: 326), CGPoint(x: 826, y: 326)] static let needleSize = CGSize(width: 70, height: 375) static let needlePivot = CGPoint(x: 35, y: 343) static let needleTip: CGFloat = 6 static let needleLength: CGFloat = 231 static let marks: [(decibels: Double, angle: Double, reach: CGFloat)] = [ (-40, -55.5, 249), (-30, -34.6, 231), (-20, -21.9, 230), (-10, 0, 224), (0, 21.3, 229), (3, 48.6, 252), ] /// The level nought reads as the lowest mark, one as the highest. static var quietest: Double { marks[0].decibels } static var loudest: Double { marks[marks.count - 1].decibels } /// A figure stands this far past its tick's end, never higher than /// `figureCeiling` under its window's top. static let figureLift: CGFloat = 4 static let figureCeiling: CGFloat = 1 static let figureSize: CGFloat = 19 /// The width a figure, and a letter, is centred in. static let figureBox: CGFloat = 80 static let letterBox: CGFloat = 120 /// "dB" and the channel's letter, centred under the pivot's line. static let decibelsCentre: CGFloat = 186 static let channelCentre: CGFloat = 234 static let decibelsSize: CGFloat = 22 static let channelSize: CGFloat = 30 /// The needles ease between the player's readings, which come some /// twenty times a second. static let ease: Double = 1.0 / 20 // THE PLATE'S WORDS: the song's name over "artist · album", stood on // the block's foot; the name one line at its size, else a size or two // smaller, else two lines smaller still. static let words = CGRect(x: 160, y: 280, width: 815, height: 122) static let titleSize: CGFloat = 50 static let titleSteps: [CGFloat] = [1, 0.86, 0.74] static let titleTwoLines: CGFloat = 0.6 static let titleTracking: CGFloat = 0.16 static let bylineSize: CGFloat = 30 static let bylineTracking: CGFloat = 0.08 static let wordsGap: CGFloat = 6 // THE JEWEL: its centre and the lit stone's width; each painting's // stone (its opaque core, in its own pixels), so the stone is the same // size lit and dark and the lit halo spills round it. static let jewelCentre = CGPoint(x: 567, y: 422) static let jewelStone: CGFloat = 42 static let jewelOn = (size: CGSize(width: 193, height: 194), stone: CGRect(x: 25, y: 30, width: 143, height: 141)) static let jewelOff = (size: CGSize(width: 161, height: 167), stone: CGRect(x: 13, y: 10, width: 138, height: 147)) /// The jewel's touch reaches this far round its centre. static let jewelTouch: CGFloat = 70 // THE KNOB AND ITS RING, about one centre: the knob's sweep, nought to // the song's end, in degrees from upright; the ring's ends. static let knobCentre = CGPoint(x: 567, y: 730) static let knob: CGFloat = 376 static let ticks: CGFloat = 518 /// The ring's painting's pixels, and how far from its centre its ends' /// ticks reach in them. static let ticksPainted: CGFloat = 700 static let ringEndReach: CGFloat = 326 static let sweep = (start: -135.0, end: 135.0) /// The times: played above the ring's top, left under the knob, and /// nought and the length beside the ring's ends, `endGap` clear of them. static let elapsed = CGRect(x: 467, y: 440, width: 200, height: 40) static let remaining = CGRect(x: 467, y: 940, width: 200, height: 44) static let endGap: CGFloat = 14 static let endDrop: CGFloat = 6 static let ends = CGSize(width: 180, height: 44) static let timeSize: CGFloat = 32 // THE SLEEVE leaning above the deck, upper left (`coverAboveRect`): its // painted box, its cover's aperture and its shadow in that box, how far // it leans and the blank's typeset words. static let sleeve = CGRect(x: 140, y: -340, width: 360, height: 360) static let sleevePainted: CGFloat = 1130 static let cover = CGRect(x: 82, y: 84, width: 964, height: 964) static let sleeveShadow = CGRect(x: -80, y: -80, width: 1290, height: 1290) static let lean: Double = -4 static let blankWords = CGRect(x: 170, y: 250, width: 790, height: 520) static let blankTitleSize: CGFloat = 104 static let blankArtistSize: CGFloat = 64 /// The meters' plate, the band above the song's name, `previous` left /// of the deck's centre and `next` right of it. static let metersPlate = CGRect(x: 0, y: 0, width: 1135, height: 290) } /// What the hi-fi reads off its card and its player. public enum HifiDeck { /// How the deck stands. public enum State: Equatable, Sendable { /// It plays the song: the jewel lit, the needles at the levels. case playing /// It holds the song paused, or holds none of it: the jewel dark, /// the needles at rest. case paused } public static func state(_ play: TrackPlay) -> State { play.held && (play.playing || play.loading) ? .playing : .paused } /// The song's length: the player's where it knows one, else the card's. public static func length(_ play: TrackPlay, _ card: DeckCard) -> TimeInterval { play.held && play.duration > 0 ? play.duration : max(0, card.fields["duration"]?.number ?? 0) } /// The knob's angle, in degrees clockwise from upright: from its start /// at nought to its end at the song's length. public static func knob(_ elapsed: TimeInterval, of length: TimeInterval) -> Double { let sweep = SongHifiFaceKnobs.sweep let share = length > 0 && elapsed.isFinite ? min(1, max(0, elapsed / length)) : 0 return sweep.start + (sweep.end - sweep.start) * share } /// The decibels a level of nought to one reads on the painted scale. public static func decibels(_ level: Double) -> Double { let knobs = SongHifiFaceKnobs.self return knobs.quietest + (knobs.loudest - knobs.quietest) * min(1, max(0, level.isFinite ? level : 0)) } /// The needle's angle for `decibels`, in degrees clockwise from /// upright: straight between the painted marks either side. public static func needle(_ decibels: Double) -> Double { let marks = SongHifiFaceKnobs.marks let reading = min(marks[marks.count - 1].decibels, max(marks[0].decibels, decibels)) for (low, high) in zip(marks, marks.dropFirst()) where reading <= high.decibels { let share = (reading - low.decibels) / (high.decibels - low.decibels) return low.angle + (high.angle - low.angle) * share } return marks[marks.count - 1].angle } /// Each needle's angle, left then right: at the player's levels while /// it plays, at rest (the lowest mark) otherwise. public static func needles(_ play: TrackPlay) -> [Double] { let rest = needle(SongHifiFaceKnobs.quietest) guard state(play) == .playing, let level = play.level else { return [rest, rest] } return [needle(decibels(level.left)), needle(decibels(level.right))] } /// The figure printed over a mark: "−40", "0", "+3". public static func figure(_ decibels: Double) -> String { let n = String(Int(abs(decibels).rounded())) if decibels < 0 { return ShellLines.line("faces", "hifi.below", ["n": n]) } if decibels > 0 { return ShellLines.line("faces", "hifi.above", ["n": n]) } return n } /// The line under the song's name: its artist and its album, joined as /// the cards join words. public static func byline(_ card: DeckCard) -> String? { MusicWhole.joined([card.text("artist"), card.text("album")]) } /// The angle of `point` round `pivot`, in degrees clockwise from upright. static func angle(of point: CGPoint, round pivot: CGPoint) -> Double { atan2(Double(point.x - pivot.x), Double(pivot.y - point.y)) * 180 / .pi } /// The turn from one angle to the next, the short way round. static func turn(from: Double, to: Double) -> Double { var by = (to - from).truncatingRemainder(dividingBy: 360) if by > 180 { by -= 360 } if by <= -180 { by += 360 } return by } /// Where a turn of `degrees` from `elapsed` seeks to: the knob's sweep /// is the whole song, held to it. public static func turned(_ elapsed: TimeInterval, by degrees: Double, of length: TimeInterval) -> TimeInterval { let sweep = SongHifiFaceKnobs.sweep return min(length, max(0, elapsed + degrees / (sweep.end - sweep.start) * length)) } } public struct SongHifiFace: View { let card: DeckCard /// The playing album's cover; nil lays the sleeve's typeset blank. var cover: Cover? var play: TrackPlay = .still var toggle: (() -> Void)? var seek: (TimeInterval) -> Void = { _ in } /// Where a turn of the knob has the song, until the finger lifts. @State private var scrubbing: TimeInterval? @Environment(\.accessibilityReduceMotion) private var reduceMotion public init( card: DeckCard, cover: Cover?, play: TrackPlay = .still, toggle: (() -> Void)? = nil, seek: @escaping (TimeInterval) -> Void = { _ in } ) { self.card = card self.cover = cover self.play = play self.toggle = toggle self.seek = seek } public var body: some View { let knobs = SongHifiFaceKnobs.self let state = HifiDeck.state(play) let length = HifiDeck.length(play, card) let elapsed = play.held ? (scrubbing ?? play.elapsed) : 0 GeometryReader { room in let width = min(room.size.width, knobs.widest, room.size.height * knobs.whole.width / knobs.whole.height) let s = width / knobs.whole.width ZStack(alignment: .topLeading) { HifiPainted(name: "hifi-shadow").placed(knobs.shadow, s) ForEach(0..<2, id: \.self) { channel in HifiPainted(name: "hifi-meter").placed(knobs.meterInserts[channel], s) meterPrint(channel, s: s) } needles(state: state, s: s) HifiPainted(name: "hifi-plate").placed(CGRect(origin: .zero, size: knobs.box), s) metersPlate(s) words(s) HifiPainted(name: "hifi-ticks").placed(square(knobs.knobCentre, knobs.ticks), s) knob(elapsed: elapsed, length: length, s: s) times(elapsed: elapsed, length: length, s: s) jewel(state: state, s: s) leaningSleeve(s) } .frame(width: knobs.box.width * s, height: knobs.box.height * s, alignment: .topLeading) .offset(y: -knobs.whole.minY * s) .frame(width: width, height: knobs.whole.height * s, alignment: .topLeading) .frame(width: room.size.width, height: room.size.height) .accessibilityElement(children: .contain) .accessibilityIdentifier("song-hifi") } } private func square(_ centre: CGPoint, _ side: CGFloat) -> CGRect { CGRect(x: centre.x - side / 2, y: centre.y - side / 2, width: side, height: side) } // MARK: The meters /// What is printed on a meter's card: each mark's figure over its tick, /// "dB", and the channel's letter. private func meterPrint(_ channel: Int, s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let pivot = knobs.pivots[channel] let window = knobs.meterWindows[channel] let ink = MusicMaterial.print.colour return ZStack(alignment: .topLeading) { ForEach(knobs.marks.indices, id: \.self) { at in let mark = knobs.marks[at] let radians = mark.angle * .pi / 180 let reach = mark.reach + knobs.figureLift + knobs.figureSize / 2 let x = pivot.x + reach * CGFloat(sin(radians)) let y = max(window.minY + knobs.figureCeiling + knobs.figureSize / 2, pivot.y - reach * CGFloat(cos(radians))) Text(HifiDeck.figure(mark.decibels)) .font(CottonFonts.serif(knobs.figureSize * s)) .monospacedDigit() .lineLimit(1) .fixedSize() .foregroundStyle(ink) .frame(width: knobs.figureBox * s, height: knobs.figureSize * 1.4 * s) .offset(x: (x - knobs.figureBox / 2) * s, y: (y - knobs.figureSize * 0.7) * s) } Text(ShellLines.line("faces", "hifi.decibels")) .font(CottonFonts.serif(knobs.decibelsSize * s)) .lineLimit(1) .fixedSize() .foregroundStyle(ink) .frame(width: knobs.letterBox * s, height: knobs.decibelsSize * 1.4 * s) .offset(x: (pivot.x - knobs.letterBox / 2) * s, y: (knobs.decibelsCentre - knobs.decibelsSize * 0.7) * s) Text(ShellLines.line("faces", channel == 0 ? "hifi.left" : "hifi.right")) .font(CottonFonts.serif(knobs.channelSize * s)) .lineLimit(1) .fixedSize() .foregroundStyle(ink) .frame(width: knobs.letterBox * s, height: knobs.channelSize * 1.4 * s) .offset(x: (pivot.x - knobs.letterBox / 2) * s, y: (knobs.channelCentre - knobs.channelSize * 0.7) * s) } .accessibilityHidden(true) } /// The two needles at the player's levels, under the plate, whose /// windows show only their upper runs. private func needles(state: HifiDeck.State, s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let angles = HifiDeck.needles(play) let scale = knobs.needleLength / (knobs.needlePivot.y - knobs.needleTip) let size = CGSize(width: knobs.needleSize.width * scale, height: knobs.needleSize.height * scale) let anchor = UnitPoint(x: knobs.needlePivot.x / knobs.needleSize.width, y: knobs.needlePivot.y / knobs.needleSize.height) return ZStack(alignment: .topLeading) { ForEach(0..<2, id: \.self) { channel in let pivot = knobs.pivots[channel] HifiPainted(name: "hifi-needle") .frame(width: size.width * s, height: size.height * s) .rotationEffect(.degrees(angles[channel]), anchor: anchor) .animation(reduceMotion ? nil : .easeOut(duration: knobs.ease), value: angles[channel]) .offset(x: (pivot.x - knobs.needlePivot.x * scale) * s, y: (pivot.y - knobs.needlePivot.y * scale) * s) } } .accessibilityHidden(true) } /// The meters' plate: a tap left of the deck's centre the previous /// song, right of it the next — the shell's acts. private func metersPlate(_ s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let half = CGRect(x: knobs.metersPlate.minX, y: knobs.metersPlate.minY, width: knobs.metersPlate.width / 2, height: knobs.metersPlate.height) return ZStack(alignment: .topLeading) { Color.clear .contentShape(Rectangle()) .frame(width: half.width * s, height: half.height * s) .accessibilityElement() .accessibilityLabel(ShellLines.line("faces", "hifi.previous")) .accessibilityAddTraits(.isButton) .accessibilityIdentifier("hifi-previous") .facePart(.previous) .at(half, s) Color.clear .contentShape(Rectangle()) .frame(width: half.width * s, height: half.height * s) .accessibilityElement() .accessibilityLabel(ShellLines.line("faces", "hifi.next")) .accessibilityAddTraits(.isButton) .accessibilityIdentifier("hifi-next") .facePart(.next) .at(half.offsetBy(dx: half.width, dy: 0), s) } } // MARK: The plate's words /// The song's name in spaced capitals over "artist · album", in cream. private func words(_ s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let ink = MusicMaterial.engraving.colour return VStack(spacing: knobs.wordsGap * s) { if let title = card.text("title") { HifiTitle(words: title.uppercased(), s: s) .foregroundStyle(ink) .accessibilityElement() .accessibilityLabel(title) .accessibilityAddTraits(.isHeader) .accessibilityIdentifier("deck-title") .facePart(.title) } if let byline = HifiDeck.byline(card) { Text(byline) .font(CottonFonts.serif(knobs.bylineSize * s)) .tracking(knobs.bylineTracking * knobs.bylineSize * s) .lineLimit(1) .minimumScaleFactor(0.6) .foregroundStyle(ink) .accessibilityIdentifier("face-byline") .facePart(.subtitle) } } .frame(width: knobs.words.width * s, height: knobs.words.height * s, alignment: .bottom) .at(knobs.words, s) } // MARK: The knob, its times and the jewel /// The brass knob at the share played, turned round its centre to seek: /// the knob and the times follow the turn, the player moves when the /// finger lifts. private func knob(elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let frame = square(knobs.knobCentre, knobs.knob) let turns = play.held && length > 0 return HifiKnob( angle: HifiDeck.knob(elapsed, of: length), side: knobs.knob * s, turn: { degrees in guard turns else { return } scrubbing = HifiDeck.turned(scrubbing ?? play.elapsed, by: degrees, of: length) }, lifted: { if let scrubbing { seek(scrubbing) } scrubbing = nil }) .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("faces", "hifi.position")) .accessibilityValue(turns ? TrackClock.said(elapsed) : "") .accessibilityAdjustableAction { direction in guard turns else { return } let by = (knobs.sweep.end - knobs.sweep.start) / 20 seek(HifiDeck.turned(play.elapsed, by: direction == .increment ? by : -by, of: length)) } .accessibilityIdentifier("hifi-knob") .facePart(.scrub) .at(frame, s) } /// The time played above the ring, the time left under the knob, and /// nought and the song's length beside the ring's ends. private func times(elapsed: TimeInterval, length: TimeInterval, s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let reach = knobs.ringEndReach * knobs.ticks / knobs.ticksPainted let end = knobs.knobCentre.y + reach * CGFloat(cos(Double.pi / 4)) + knobs.endDrop let startX = knobs.knobCentre.x - reach * CGFloat(sin(Double.pi / 4)) - knobs.endGap let endX = knobs.knobCentre.x + reach * CGFloat(sin(Double.pi / 4)) + knobs.endGap return ZStack(alignment: .topLeading) { Text(TrackClock.said(elapsed)) .accessibilityIdentifier("hifi-elapsed") .placed(knobs.elapsed, s) if length > 0 { Text(TrackClock.said(0)) .placed(CGRect(x: startX - knobs.ends.width, y: end - knobs.ends.height / 2, width: knobs.ends.width, height: knobs.ends.height), s, alignment: .trailing) Text(TrackClock.said(length)) .accessibilityIdentifier("hifi-length") .placed(CGRect(x: endX, y: end - knobs.ends.height / 2, width: knobs.ends.width, height: knobs.ends.height), s, alignment: .leading) // Left from the whole seconds played, so the two clocks // always add up to the song's length. Text(TrackClock.left(elapsed.rounded(.down), of: length)) .accessibilityIdentifier("hifi-remaining") .placed(knobs.remaining, s) } } .font(CottonFonts.serif(knobs.timeSize * s)) .monospacedDigit() .lineLimit(1) .foregroundStyle(MusicMaterial.engraving.colour) .accessibilityHidden(true) } /// The red jewel: lit while it plays, dark otherwise; a tap plays or /// pauses. private func jewel(state: HifiDeck.State, s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let lit = state == .playing let painting = lit ? knobs.jewelOn : knobs.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) let touch = square(knobs.jewelCentre, knobs.jewelTouch * 2) return ZStack(alignment: .topLeading) { HifiPainted(name: lit ? "hifi-jewel-on" : "hifi-jewel-off").placed(drawn, s) Color.clear .contentShape(Circle()) .frame(width: touch.width * s, height: touch.height * s) .onTapGesture { toggle?() } .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("faces", lit ? "media.pause" : "media.play")) .accessibilityValue(card.text("title") ?? "") .accessibilityAddTraits(.isButton) .accessibilityAction { toggle?() } .accessibilityIdentifier("hifi-jewel") .facePart(.play) .at(touch, s) } } // MARK: The sleeve /// The playing album's sleeve, leaning against the wall above the deck: /// its shadow, its cover (else the typeset blank) and its frame. private func leaningSleeve(_ s: CGFloat) -> some View { let knobs = SongHifiFaceKnobs.self let k = knobs.sleeve.width / knobs.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) { HifiPainted(name: "sleeve-shadow").placed(inSleeve(knobs.sleeveShadow), s) Group { if let cover { cover.frame(width: knobs.cover.width * k * s, height: knobs.cover.height * k * s).clipped() } else { blank(k: k, s: s) } } .frame(width: knobs.cover.width * k * s, height: knobs.cover.height * k * s) .at(inSleeve(knobs.cover), s) HifiPainted(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) .accessibilityElement() .accessibilityLabel(MusicWhole.joined([card.text("album"), card.text("artist")]) ?? "") .accessibilityIdentifier("hifi-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 knobs = SongHifiFaceKnobs.self let words = knobs.blankWords return ZStack(alignment: .topLeading) { HifiPainted(name: "sleeve-blank") VStack(spacing: 18 * k * s) { if let album = card.text("album") { Text(album) .font(CottonFonts.serif(knobs.blankTitleSize * k * s)) .multilineTextAlignment(.center) .lineLimit(3) .minimumScaleFactor(0.5) } if let artist = card.text("artist") { Text(artist) .font(CottonFonts.serif(knobs.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 song's name in spaced capitals: one line at its size, else a step /// smaller, else two lines smaller still, shrinking before it wraps. private struct HifiTitle: View { let words: String let s: CGFloat var body: some View { let knobs = SongHifiFaceKnobs.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.7) .multilineTextAlignment(.center) } } private func line(_ size: CGFloat) -> Text { Text(words) .font(CottonFonts.brand(CottonFonts.brandHeadingFace, size * s, fallback: .semibold)) .tracking(SongHifiFaceKnobs.titleTracking * size * s) } } /// The brass knob turned to `angle` round its centre; a finger turning /// round the centre turns it by the angle swept. private struct HifiKnob: View { let angle: Double let side: CGFloat let turn: (_ degrees: Double) -> Void let lifted: () -> Void @State private var last: Double? var body: some View { let centre = CGPoint(x: side / 2, y: side / 2) HifiPainted(name: "hifi-knob") .frame(width: side, height: side) .rotationEffect(.degrees(angle)) .contentShape(Circle()) .gesture( DragGesture(minimumDistance: 0) .onChanged { touch in let now = HifiDeck.angle(of: touch.location, round: centre) if let last { turn(HifiDeck.turn(from: last, to: now)) } last = now } .onEnded { _ in last = nil lifted() }) } } /// One of the deck's paintings, stretched to its frame; nothing where the /// build carries no pictures. private struct HifiPainted: View { let name: String var body: some View { if let image = CottonMedia.image(name) { image.resizable().interpolation(.high).accessibilityHidden(true) } } } private extension View { /// This view laid at `rect` of the deck box, in its pixels, scaled by `s`. func placed(_ rect: CGRect, _ s: CGFloat, alignment: Alignment = .center) -> some View { frame(width: rect.width * s, height: rect.height * s, alignment: alignment) .offset(x: rect.minX * s, y: rect.minY * s) } /// This view moved to `rect`'s corner of the deck box: 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 hi-fi 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 SongHifiFaceCatalogue { public static let stock: FaceStock = .own public static let ground: FaceGround = .tolex public static let face = Face(id: "song.hifi", 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", harbour, play: TrackPlay(held: true, playing: true, elapsed: 61, duration: 242)), Specimen("long title", lanterns, play: TrackPlay(held: true, playing: true, elapsed: 312, duration: 403)), ]) { card, picture, _ in TrackPlaying(card: card) { player in SongHifiFace(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: .songHifi, fields: [ "title": .text("Harbour Lights"), "artist": .text("The Low Tide"), "album": .text("Night Ferry"), "duration": .number(242), ]) /// A long song name and a long album. public static let lanterns = DeckCard(face: .songHifi, fields: [ "title": .text("The Slow Train Home Through Sixteen Lanterns"), "artist": .text("Mara Vance"), "album": .text("Sixteen Lanterns and the Blue Hour Sessions"), "duration": .number(403), ]) }