import SwiftUI /// Per-frame clock shared by the animated views (Android Ticker). Returns dt in seconds, clamped. final class FrameClock { private var last: Date? func step(_ now: Date) -> Float { defer { last = now } guard let last else { return 1 / 60 } return Float(min(0.1, max(0, now.timeIntervalSince(last)))) } } var animFPS: Double { Prefs.shared.lowPower ? 30 : 60 } // MARK: Cookie cover with spectrum bars (CookieView.java) final class CookieModel { var spin: Float = 0, rot: Float = 0, appear: Float = 1, pulse: Float = 0, bass: Float = 0 var lv = [Float](repeating: 0, count: 48) var target = [Float](repeating: 0, count: 48) var coverID: ObjectIdentifier? let clock = FrameClock() func frame(_ now: Date, playing: Bool, bars: Bool, spectrum: Spectrum?) { let f = clock.step(now) spin += ((playing && !Prefs.shared.lowPower ? 1 : 0) - spin) * min(1, 2.5 * f) rot += spin * f * 0.22 if appear < 1 { appear = min(1, appear + 2.8 * f) } let have = bars && playing && (spectrum?.levels(&target) ?? false) var b: Float = 0 for i in lv.indices { let t = have ? target[i] : 0 let k: Float = t > lv[i] ? 0.55 : 0.16 lv[i] += (t - lv[i]) * min(1, k * f * 60) if i < 4 { b += lv[i] } } bass = b / 4 pulse += ((have ? 1.8 * max(0, bass - 0.45) : 0) - pulse) * min(1, 14 * f) } var busy: Bool { spin > 0.01 || appear < 1 || pulse > 0.01 } } struct CookieView: View { var image: UIImage? var playing: Bool var small = false @EnvironmentObject var theme: Theme @EnvironmentObject var prefs: Prefs @State private var model = CookieModel() var body: some View { let bars = prefs.visualizer && !prefs.lowPower TimelineView(.animation(minimumInterval: 1 / animFPS, paused: !(playing || model.busy))) { tl in Canvas { ctx, size in model.frame(tl.date, playing: playing, bars: bars, spectrum: Player.shared.spectrum) draw(&ctx, size, bars: bars) } } .aspectRatio(1, contentMode: .fit) .onChange(of: image.map(ObjectIdentifier.init)) { _ in model.appear = 0 } } private func draw(_ ctx: inout GraphicsContext, _ size: CGSize, bars: Bool) { let p = theme.palette let m = min(size.width, size.height) let c = CGPoint(x: size.width / 2, y: size.height / 2) let full = m / 2 - 2 let base = (bars ? 0.72 : 0.97) * full let depth: CGFloat = bars ? 0.06 : 0.045 let a = CGFloat(model.appear) let r = (0.9 + 0.1 * (1 - (1 - a) * (1 - a))) * (1 + 0.045 * CGFloat(model.pulse)) * base if bars { Shapes.drawBars(&ctx, center: c, radius: r, depth: depth, rotation: Double(model.rot), length: full - base * 1.06 - full * 0.04, levels: model.lv, count: small ? 44 : 72, color: p.primary, width: small ? max(1.6, m / 50) : max(2.6, m / 85)) } let path = Shapes.cookie(center: c, radius: r, depth: depth, rotation: Double(model.rot)) if let image { var clipped = ctx clipped.clip(to: path) let iw = image.size.width, ih = image.size.height let s = 2 * r / min(iw, ih) clipped.draw(Image(uiImage: image), in: CGRect(x: c.x - iw * s / 2, y: c.y - ih * s / 2, width: iw * s, height: ih * s)) } else { ctx.fill(path, with: .color(p.cPrimaryC)) let icon = ctx.resolve(Image(systemName: "opticaldisc.fill")) ctx.draw(icon.shading(.color(p.cOnPrimaryC)) as GraphicsContext.ResolvedImage, in: CGRect(x: c.x - r * 0.4, y: c.y - r * 0.4, width: r * 0.8, height: r * 0.8)) } } } extension GraphicsContext.ResolvedImage { func shading(_ s: GraphicsContext.Shading) -> GraphicsContext.ResolvedImage { var copy = self copy.shading = s return copy } } // MARK: Wavy seek bar (WavySeekBar.java) final class WavyModel { var amp: Float = 0, phase: Float = 0 var value: Double = 0 var target: Double = -1 let clock = FrameClock() } struct WavySeekBar: View { var position: Double var duration: Double var playing: Bool var height: CGFloat = 40 var onDrag: (Double?) -> Void = { _ in } var onSeek: (Double) -> Void @EnvironmentObject var theme: Theme @State private var model = WavyModel() @State private var drag: Double? var body: some View { GeometryReader { geo in TimelineView(.animation(minimumInterval: 1 / animFPS, paused: !(playing || model.amp > 0.01))) { tl in Canvas { ctx, size in step(tl.date) let p = theme.palette let pad: CGFloat = 10 Shapes.drawWavy(&ctx, x0: pad, x1: size.width - pad, y: size.height / 2, frac: CGFloat(drag ?? model.value), amp: CGFloat(model.amp), phase: CGFloat(model.phase), stroke: 4.5, wavelength: 36, height: 3.6, active: p.cPrimary, inactive: p.cSecondaryC, thumb: true, thumbHeight: min(size.height - 2, drag != nil ? 26 : 20)) } } .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0) .onChanged { g in let f = frac(g.location.x, geo.size.width) drag = f onDrag(f * duration) } .onEnded { g in let f = frac(g.location.x, geo.size.width) drag = nil model.value = f model.target = -1 onDrag(nil) onSeek(f * duration) }) } .frame(height: height) .onChange(of: position) { _ in sync() } .onAppear { model.value = duration > 0 ? position / duration : 0 } } private func frac(_ x: CGFloat, _ w: CGFloat) -> Double { Double(max(0, min(1, (x - 10) / max(1, w - 20)))) } private func sync() { guard drag == nil else { return } let f = duration > 0 ? max(0, min(1, position / duration)) : 0 if playing && abs(f - model.value) * max(1, duration) <= 0.9 { model.target = f } else { model.value = f; model.target = -1 } } private func step(_ now: Date) { let f = model.clock.step(now) model.amp += ((playing ? 1 : 0) - model.amp) * min(1, 7 * f) model.phase = (model.phase + 6.6 * f).truncatingRemainder(dividingBy: 2 * .pi) if playing && duration > 0 && drag == nil { model.value = min(1, model.value + Double(f) / duration) if model.target >= 0 { model.value += Double(min(1, 3 * f)) * (model.target - model.value) if abs(model.target - model.value) < 1e-5 { model.target = -1 } } } } } // MARK: Morphing play button (PlayButton.java) struct PlayButton: View { var playing: Bool var tonal = false var action: () -> Void @EnvironmentObject var theme: Theme @State private var pressed = false var body: some View { let p = theme.palette GeometryReader { g in let w = g.size.width, h = g.size.height let press: CGFloat = pressed ? 1 : 0 let morph: CGFloat = playing ? 1 : 0 let sc = 1 - 0.08 * press let bw = w * sc * (1 - 0.12 * morph), bh = h * sc let radius = bh / 2 + (bh * 0.3 - bh / 2) * morph ZStack { RoundedRectangle(cornerRadius: radius, style: .continuous) .fill(tonal ? p.cSecondaryC : p.cPrimary) .frame(width: bw, height: bh) Image(systemName: "play.fill") .resizable().scaledToFit() .frame(width: bh * 0.4, height: bh * 0.4) .scaleEffect(0.7 + 0.3 * (1 - morph)) .opacity(1 - morph) Image(systemName: "pause.fill") .resizable().scaledToFit() .frame(width: bh * 0.4, height: bh * 0.4) .scaleEffect(0.7 + 0.3 * morph) .opacity(morph) } .foregroundColor(tonal ? p.cOnSecondaryC : p.cOnPrimary) .frame(width: w, height: h) .animation(.spring(response: 0.32, dampingFraction: 0.72), value: playing) .animation(.easeOut(duration: 0.12), value: pressed) } .contentShape(Rectangle()) .simultaneousGesture(DragGesture(minimumDistance: 0).onChanged { _ in pressed = true }.onEnded { _ in pressed = false }) .onTapGesture { UIImpactFeedbackGenerator(style: .light).impactOccurred(); action() } .accessibilityLabel(playing ? "Pause" : "Play") .accessibilityAddTraits(.isButton) } } // MARK: Floating bass shape (BassShapeView.java) struct BassShapeView: View { var playing: Bool @EnvironmentObject var theme: Theme @State private var anim = Shapes.Animator() @State private var clock = FrameClock() var body: some View { TimelineView(.animation(minimumInterval: 1 / animFPS, paused: !(playing || !anim.settled))) { tl in Canvas { ctx, size in anim.playing = playing let dt = clock.step(tl.date) anim.step(dt, bass: playing ? Player.shared.spectrum.bass() : -1) let p = theme.palette let c = CGPoint(x: size.width / 2, y: size.height / 2) anim.draw(&ctx, center: c, radius: min(c.x, c.y) * 0.86, fill: Color(rgb: p.primaryContainer, alpha: 0.94), innerFill: p.cPrimary, stroke: p.primary) } } } }