import SwiftUI import UIKit enum NpPanel { case Art, Mixer, Queue, Lyrics } /// The close drag's offset. Only DropOffset reads it, so dragging re-renders just the offset. @Observable final class DropState { var y: CGFloat = 0 } /// ProMotion only runs as fast as someone asks. A plain drag asks nothing, and the 30 fps meters and /// bars then set the pace, so the close drag crawled. This display link asks for 120 Hz while held. @MainActor final class FrameBoost: NSObject { static let shared = FrameBoost() private var link: CADisplayLink? private var stopGen = 0 func start() { stopGen += 1 guard link == nil else { return } let l = CADisplayLink(target: self, selector: #selector(tick)) l.preferredFrameRateRange = CAFrameRateRange(minimum: 80, maximum: 120, preferred: 120) l.add(to: .main, forMode: .common) link = l } /// Keeps the boost through the spring back or the cover's slide down. func stop(after: Double = 0.6) { stopGen += 1 let gen = stopGen DispatchQueue.main.asyncAfter(deadline: .now() + after) { [weak self] in guard let self, gen == self.stopGen else { return } self.link?.invalidate() self.link = nil } } @objc private func tick() {} } private struct DropOffset: ViewModifier { let state: DropState func body(content: Content) -> some View { content.offset(y: state.y) } } func deviceIcon(_ d: OutputDevice) -> (String, UInt32) { switch d.kind { case .Speaker: ("iphone", 0x8E8E93) case .Bluetooth: ("headphones", 0x007AFF) case .Wired: ("headphones", 0x34C759) case .Usb: ("cable.connector", 0xFF9500) case .Hdmi: ("airplayaudio", 0x5856D6) case .Car: ("car.fill", 0x34C759) case .Other: ("hifispeaker", 0x8E8E93) } } /// Roughly Palette's vibrant swatch: the average hue of the most saturated, mid-bright pixels. func vibrant(_ cg: CGImage) -> (CGFloat, CGFloat, CGFloat)? { let n = 24 var px = [UInt8](repeating: 0, count: n * n * 4) guard let ctx = CGContext(data: &px, width: n, height: n, bitsPerComponent: 8, bytesPerRow: n * 4, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil } ctx.draw(cg, in: CGRect(x: 0, y: 0, width: n, height: n)) var all: [(CGFloat, CGFloat, CGFloat, CGFloat)] = [] for i in 0..<(n * n) { let c = UIColor(red: CGFloat(px[i * 4]) / 255, green: CGFloat(px[i * 4 + 1]) / 255, blue: CGFloat(px[i * 4 + 2]) / 255, alpha: 1) var h: CGFloat = 0, s: CGFloat = 0, v: CGFloat = 0, a: CGFloat = 0 c.getHue(&h, saturation: &s, brightness: &v, alpha: &a) // Favour colorful pixels that aren't near black or white. let score = s * (1 - abs(v - 0.55) * 1.4) all.append((h, s, v, score)) } let top = all.sorted { $0.3 > $1.3 }.prefix(max(1, all.count / 6)) // Average hue on the circle so reds either side of 0 don't cancel out. var x: CGFloat = 0, y: CGFloat = 0, s: CGFloat = 0, v: CGFloat = 0 for p in top { x += cos(p.0 * 2 * .pi); y += sin(p.0 * 2 * .pi); s += p.1; v += p.2 } let k = CGFloat(top.count) var h = atan2(y, x) / (2 * .pi) if h < 0 { h += 1 } return (h, s / k, v / k) } // ---- now playing ---------------------------------------------------------------------------- /// Full screen, Now Playing owns the window's color scheme. Docked beside the library it only /// sets its own pane's, since preferredColorScheme would repaint the library side too. private struct NpScheme: ViewModifier { var dark: Bool var docked: Bool func body(content: Content) -> some View { if docked { content.environment(\.colorScheme, dark ? .dark : .light) } else { content.preferredColorScheme(dark ? .dark : .light) } } } struct NowPlayingScreen: View { /// Shown beside the library on an unfolded iPhone Duo rather than as a full-screen cover: /// no grabber, no drag to close, and its color scheme stays inside the pane. var docked = false @Environment(Ui.self) private var ui @Environment(\.palette) private var base @Environment(\.dismiss) private var dismiss @State private var panel = NpPanel.Art /// How far the screen is being dragged down to close it. Kept out of the view's own state so a /// drag only moves the screen (DropOffset) instead of rebuilding all of Now Playing each frame. @State private var drop = DropState() /// Docked beside the hinge the pane is narrow, so the controls pack tighter. Full screen /// (folded or any other iPhone) uses the roomy layout across the whole window. private var tight: Bool { docked } /// Full screen on the Duo (folded, or a Split View half), the controls run down into the home indicator strip. private var runsLow: Bool { !docked && (folded || ui.isDuo) } /// Size of the art panel in the stacked layout, so the controls can match the art's width. @State private var artBox: CGSize = .zero /// Height of the wide layout's controls column, so the Duo art can match it. @State private var ctlH: CGFloat = 0 /// The screen's height, for the strip at the top that a drag down closes from. @State private var screenH: CGFloat = 0 /// Where the art sits on screen: folded, a drag down that starts on it closes Now Playing. @State private var artFrame: CGRect = .zero @Environment(\.verticalSizeClass) private var vSize /// Folded iPhone Duo: the outer display keeps a status column down the right edge. @State private var folded = false private var artInset: CGFloat { tight ? 18 : folded ? 12 : 28 } private var hPad: CGFloat { tight ? 20 : 28 } private var g: AppGraph { .shared } var body: some View { let pm = g.playback let c = base let track = pm.current?.track // Wide when there's no room to stack art over controls: landscape, or iPhone Duo's // outer display. Tall layouts (including the unfolded Duo) cap the controls' width. GeometryReader { geo in // Docked beside the hinge, the pane is phone-shaped: art stays on top unless it's truly wide. let wide = geo.size.width > geo.size.height * (docked ? 1.2 : 0.9) if let track { if wide { // The column never drops below what the transport row needs, so the buttons // can't spill onto the art or under the Duo rail. let col = tight ? min(max(geo.size.width * 0.46, 300), 420) : min(max(geo.size.width * 0.42, 300), 440) HStack(spacing: 0) { // On the Duo the art matches the controls' height, so it lines up with the title // and the button row instead of floating in the middle of a tall pane. let match = ui.isDuo && !docked && panel == .Art && ctlH > 0 panelView(track, header: false) .frame(maxWidth: .infinity, maxHeight: match ? min(ctlH, geo.size.height) : .infinity) .frame(maxHeight: .infinity) .transition(.opacity.combined(with: .scale(scale: 0.96))) controls(track, wide: true, width: col) .frame(width: col) .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { ctlH = $0 } .frame(maxHeight: .infinity) } } else { let cap: CGFloat = tight ? 520 : 560 // Controls line up with the art's edges instead of running wider than it. let art = min(artBox.width - 2 * artInset, artBox.height - 16) // Every panel keeps the art screen's width, so switching to the stems doesn't widen it. let w = !docked ? geo.size.width : art > 0 ? min(cap, max(300, art + 2 * hPad)) : cap VStack(spacing: 0) { panelView(track, header: true) .frame(maxWidth: panel == .Art ? .infinity : min(w, geo.size.width), maxHeight: .infinity) .onGeometryChange(for: CGSize.self) { $0.size } action: { if panel == .Art { artBox = $0 } } .transition(.opacity.combined(with: .scale(scale: 0.96))) controls(track, wide: false, width: min(w, geo.size.width)).frame(maxWidth: w) } .frame(maxWidth: .infinity) } } else { Text("Nothing Playing").font(.title3.weight(.semibold)).foregroundStyle(c.label2) .frame(maxWidth: .infinity, maxHeight: .infinity) } } // Folded, the iPhone Duo keeps a status column down the right edge. Run the player under it // (it only overlaps the sliding preset chips) instead of leaving a quarter of the screen empty. // Upright iPhones have no side insets, so this only changes the Duo. // Folded, the bottom inset is empty space too: run the controls down into it, and the art grows into the room. // The room under the controls has to be inside this view: padding outside it would keep // the view off the bottom edge, and then it can't run into the home indicator strip. .padding(.bottom, runsLow ? 6 : 0) .ignoresSafeArea(.container, edges: !docked && vSize == .regular ? (runsLow ? [.trailing, .bottom] : .trailing) : []) // Measured outside the line above, so it still sees the status column. .onGeometryChange(for: Bool.self) { $0.safeAreaInsets.trailing > 20 } action: { folded = $0 && vSize == .regular } .padding(.top, docked ? 4 : tight ? 14 : 20) .padding(.bottom, runsLow ? 0 : tight ? 4 : 8) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(c.bg.ignoresSafeArea()) .overlay(alignment: .top) { // Stands in for the sheet's grabber: tap or drag down to close. if !docked { Capsule().fill(c.label3).frame(width: 36, height: 5) .frame(width: 120, height: 20).contentShape(Rectangle()) .onTapGesture { dismiss() } .gesture(screenDrag(header: true)) } } .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { screenH = $0 } .modifier(DropOffset(state: drop)) // Drag down from the top strip (or the song header) to close, so it never fights the // stem faders. Swipe up anywhere on the art for the queue. .simultaneousGesture(screenDrag(), including: panel == .Art ? .all : .subviews) .environment(\.palette, c) .modifier(NpScheme(dark: c.dark, docked: docked)) .tint(c.label) .presentationBackground(.clear) .onAppear { if !docked && g.playback.karaoke { panel = .Lyrics } } } @ViewBuilder private func panelView(_ track: Track, header: Bool) -> some View { switch panel { case .Art: ArtPanel(thumb: track.thumb, playing: g.playback.playWhenReady, inset: header ? artInset : 12, leading: folded && !docked && !ui.split && g.settings.foldedArtLeft, edge: hPad).contentShape(Rectangle()) .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { if drop.y == 0 { artFrame = $0 } } case .Mixer: VStack(spacing: 0) { if header { mini(track) }; MixerPanel(spread: folded) } case .Queue: VStack(spacing: 0) { if header { mini(track) }; QueuePanel(close: { show(.Queue) }) } case .Lyrics: VStack(spacing: 0) { if header { mini(track) }; LyricsPanel(track: track) } } } /// Folded on the Duo the status column sits over the header's right end, so the menu goes. private func mini(_ track: Track) -> some View { MiniHeader(track: track, menu: !folded, compact: folded).contentShape(Rectangle()).gesture(screenDrag(header: true)) } private func controls(_ track: Track, wide: Bool, width: CGFloat) -> some View { // Folded, the controls keep the tight stems-panel spacing on every panel, so the art gets the room. let snug = folded let pm = g.playback let c = base let pad: CGFloat = wide && tight ? 14 : hPad // Full-size transport needs 312 pt; below that the buttons shrink to fit. let narrow = width - 2 * pad < 312 return VStack(spacing: 0) { if wide || panel == .Art { HStack(alignment: .center) { VStack(alignment: .leading, spacing: 1) { Text(track.title).font(.title3.weight(.semibold)).foregroundStyle(c.label).lineLimit(1) Text(track.artist).font(.title3).foregroundStyle(c.label2).lineLimit(1) .onTapGesture { if let k = track.artistKey { ui.showInLibrary(.ArtistPage(Artist(ratingKey: k, title: track.artist, sortTitle: track.artist))) } } } Spacer(minLength: 8) EllipsisButton(items: ui.menu.track(track)) } .padding(.top, snug && !docked ? 20 : tight ? 8 : ui.isDuo && !wide ? 30 : 14) .transition(.opacity.combined(with: .move(edge: .top))) } ProgressVolumeBar(played: g.settings.albumTheme ? c.accent : c.label, unplayed: c.label3, centerLabel: badge(track)) .padding(.top, tight ? (wide ? 14 : 18) : snug ? 14 : wide ? 24 : panel == .Art ? 34 : 30) HStack(spacing: 0) { SideToggle(symbol: "shuffle", on: pm.shuffle, action: pm.toggleShuffle, width: narrow ? 36 : 40) Spacer(minLength: 2) TransportButton(symbol: "backward.fill", size: narrow ? 28 : 34, box: narrow ? 52 : 72, action: pm.previous) Spacer(minLength: 2) Button(action: pm.togglePlay) { ZStack { if pm.buffering && pm.playWhenReady { ProgressView().controlSize(.large).tint(c.label) } else { Image(systemName: pm.playWhenReady ? "pause.fill" : "play.fill").font(.system(size: narrow ? 42 : 48)).foregroundStyle(c.label).contentTransition(.symbolEffect(.replace)) } } .animation(.snappy(duration: 0.25), value: pm.playWhenReady) .frame(width: narrow ? 64 : 80, height: narrow ? 64 : 80) .contentShape(Rectangle()) } .buttonStyle(PressFade()) Spacer(minLength: 2) TransportButton(symbol: "forward.fill", size: narrow ? 28 : 34, box: narrow ? 52 : 72, action: pm.next, hold: pm.nextAlbum) Spacer(minLength: 2) SideToggle(symbol: pm.repeatMode == .One ? "repeat.1" : "repeat", on: pm.repeatMode != .Off, action: pm.cycleRepeat, width: narrow ? 36 : 40) } .frame(height: tight || snug ? 76 : 84) .padding(.top, tight || snug ? 4 : 14) HStack { let hidden = NowPlayingButton.hidden(g.settings.hiddenNowPlaying) if !hidden.contains(.Mixer) { PanelToggle(symbol: "slider.vertical.3", on: docked ? ui.showsDeck && ui.deckPage == .Stems : panel == .Mixer) { show(.Mixer) } } if !hidden.contains(.Speed) { SpeedMenu() } if !hidden.contains(.Route) { Button { if docked { withAnimation(.snappy) { ui.showDeck(.Eq) } } else { ui.showInSettings(.Eq) } } label: { routeLabel(c) }.buttonStyle(PressFade()).frame(maxWidth: .infinity) } else { Spacer() } if !hidden.contains(.AirPlay) { AirPlayButton(tint: UIColor(c.label2), active: UIColor(c.accent)).frame(width: 40, height: 40) } if !hidden.contains(.Karaoke) { PanelToggle(symbol: "music.mic", on: pm.karaoke) { karaoke() } } } .frame(height: 48) // Swipe up on the art (or here) for the queue. Docked, the queue is already beside it. if !docked { Button { show(.Queue) } label: { VStack(spacing: 0) { Image(systemName: panel == .Queue ? "chevron.compact.down" : "chevron.compact.up").font(.system(size: 22, weight: .medium)) if panel != .Queue && !tight && !snug { Text("Playing Next").font(.caption2.weight(.medium)) } } .foregroundStyle(c.label3).frame(maxWidth: .infinity, minHeight: tight || snug ? 24 : 30).contentShape(Rectangle()) } .buttonStyle(PressFade()) .gesture(swipe(up: panel != .Queue)) } } .padding(.horizontal, pad) } /// Opens a panel, or goes back to the art if it's already open. /// Karaoke: vocals down and lyrics up. If it's on but another panel is showing, brings the lyrics back first. private func karaoke() { let pm = g.playback // Docked, the lyrics get the deck's whole side of the hinge. if docked { withAnimation(.snappy(duration: 0.25)) { if pm.karaoke && !(ui.showsDeck && ui.deckPage == .Lyrics) { ui.showDeck(.Lyrics); return } pm.setKaraoke(!pm.karaoke) ui.showDeck(pm.karaoke ? .Lyrics : .Stems) } return } withAnimation(.spring(duration: 0.38, bounce: 0.12)) { if pm.karaoke && panel != .Lyrics { panel = .Lyrics; return } pm.setKaraoke(!pm.karaoke) panel = pm.karaoke ? .Lyrics : .Art } } private func show(_ to: NpPanel) { // Docked, the stems and the queue have the other side of the hinge to themselves. // The queue lives only in the deck there, never a second copy beside it. if docked { if to == .Mixer { withAnimation(.snappy) { ui.showDeck(.Stems) } }; return } withAnimation(.spring(duration: 0.38, bounce: 0.12)) { panel = panel == to ? .Art : to } } /// Swipe up for the queue, down to go back to the art. private func swipe(up: Bool) -> some Gesture { DragGesture(minimumDistance: 20).onEnded { v in let dy = v.translation.height guard up ? dy < -50 : dy > 50, abs(dy) > abs(v.translation.width) else { return } if up && !g.settings.swipeUpQueue { return } withAnimation(.spring(duration: 0.38, bounce: 0.12)) { panel = up ? .Queue : .Art } } } /// Drag down to close: from the grabber or song header, or anywhere in the top tenth of the screen. /// Folded, also from the art (only while it shows, so never over the stem dials). private func fromTop(_ v: DragGesture.Value, header: Bool) -> Bool { header || v.startLocation.y < max(screenH * 0.1, 56) || (folded && !docked && panel == .Art && artFrame.contains(v.startLocation)) } private func screenDrag(header: Bool = false) -> some Gesture { DragGesture(minimumDistance: 20, coordinateSpace: .global) .onChanged { v in let dy = v.translation.height let top = fromTop(v, header: header) let y = !docked && top && dy > 0 && dy > abs(v.translation.width) ? dy : 0 if y > 0 { FrameBoost.shared.start() } if y != drop.y { drop.y = y } } .onEnded { v in FrameBoost.shared.stop() let dy = v.translation.height let vertical = abs(dy) > abs(v.translation.width) let top = fromTop(v, header: header) if !docked && top && vertical && (dy > 120 || v.predictedEndTranslation.height > 300) { dismiss(); return } withAnimation(.spring(duration: 0.3, bounce: 0.1)) { drop.y = 0 } // Never from the stems: a slightly upward fader drag or scroll there kept opening the queue. if !docked && vertical && dy < -50 && panel != .Mixer && g.settings.swipeUpQueue { withAnimation(.spring(duration: 0.38, bounce: 0.12)) { panel = .Queue } } } } private func routeLabel(_ c: Palette) -> some View { let eq = g.eq let device = eq.device let eqLabel: String = { guard eq.state.enabled, let p = eq.active else { return "" } return p.name.caseInsensitiveCompare(device.label) == .orderedSame ? " · EQ" : " · \(p.name)" }() return HStack(spacing: 6) { Image(systemName: deviceIcon(device).0).font(.system(size: 14)) Text(device.label + eqLabel).font(.footnote.weight(.medium)).lineLimit(1) } .foregroundStyle(c.label2) } private func badge(_ track: Track) -> String? { let pm = g.playback switch pm.source { case .Stems: let q = g.settings.stemQuality return "\(pm.stemNames.count) STEMS · \(g.stems.effectiveQuality(q) == .Lossless ? "FLAC" : "OPUS \(q.kbps)")" case .Original: return ["ORIGINAL", track.codec?.uppercased()].compactMap { $0 }.filter { !$0.isEmpty }.joined(separator: " · ") case .None: return nil } } } /// Fades a little while pressed, like Apple Music's transport. struct PressFade: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label.opacity(configuration.isPressed ? 0.6 : 1).scaleEffect(configuration.isPressed ? 0.9 : 1) .animation(configuration.isPressed ? .easeOut(duration: 0.1) : .spring(duration: 0.32, bounce: 0.45), value: configuration.isPressed) } } private struct TransportButton: View { let symbol: String let size: CGFloat var box: CGFloat = 72 let action: () -> Void var hold: (() -> Void)? @Environment(\.palette) private var p var body: some View { HoldButton(action: action, hold: hold) { Image(systemName: symbol).font(.system(size: size)).foregroundStyle(p.label).frame(width: box, height: box).contentShape(Rectangle()) } } } /// A PressFade button that runs [hold] on a long press instead of the tap (hold Next to skip the album). struct HoldButton: View { let action: () -> Void var hold: (() -> Void)? @ViewBuilder let label: () -> Label @State private var held = false @State private var fired = 0 var body: some View { Button { if held { held = false } else { action() } } label: { label() } .buttonStyle(PressFade()) .simultaneousGesture(LongPressGesture(minimumDuration: 0.5) .onChanged { _ in held = false } .onEnded { _ in guard let hold else { return } held = true; fired += 1; hold() }) .sensoryFeedback(.impact(weight: .medium), trigger: fired) } } /// Shuffle and repeat, either side of the transport. A dot underneath when on. private struct SideToggle: View { let symbol: String let on: Bool let action: () -> Void var width: CGFloat = 40 @Environment(\.palette) private var p var body: some View { Button(action: action) { VStack(spacing: 4) { Image(systemName: symbol).font(.system(size: 19, weight: .semibold)).contentTransition(.symbolEffect(.replace)) Circle().frame(width: 4, height: 4).opacity(on ? 1 : 0) } .foregroundStyle(on ? p.label : p.label3) .frame(width: width, height: 56) .contentShape(Rectangle()) } .buttonStyle(PressFade()) .sensoryFeedback(.selection, trigger: symbol + "\(on)") } } /// Playback speed picker; shows the current speed. private struct SpeedMenu: View { private var g: AppGraph { .shared } @Environment(\.palette) private var p var body: some View { let pm = g.playback Menu { Picker("Speed", selection: Binding(get: { pm.speed }, set: { pm.setSpeed($0) })) { ForEach(PlaybackManager.speeds, id: \.self) { Text(Self.label($0)).tag($0) } } } label: { Text(Self.label(pm.speed)).font(.system(size: 14, weight: .semibold).monospacedDigit()) .foregroundStyle(pm.speed == 1 ? p.label2 : p.bg) .frame(minWidth: 40, minHeight: 40) .padding(.horizontal, 2) .background(pm.speed == 1 ? .clear : p.label, in: RoundedRectangle(cornerRadius: 10, style: .continuous)) } .menuOrder(.fixed) } static func label(_ v: Double) -> String { (v == v.rounded() ? String(Int(v)) : String(format: "%g", v)) + "×" } } private struct PanelToggle: View { let symbol: String let on: Bool let action: () -> Void @Environment(\.palette) private var p var body: some View { Button(action: action) { Image(systemName: symbol).font(.system(size: 19, weight: .medium)).foregroundStyle(on ? p.bg : p.label2) .frame(width: 40, height: 40) .background(on ? p.label : .clear, in: RoundedRectangle(cornerRadius: 10, style: .continuous)) } .buttonStyle(PressFade()) .sensoryFeedback(.selection, trigger: on) } } /// Round ••• button on a fill, opening the media menu. private struct EllipsisButton: View { let items: [SheetAction] @Environment(Ui.self) private var ui @Environment(\.palette) private var p var body: some View { Button { ui.overlays.actions("", items) } label: { Image(systemName: "ellipsis").font(.system(size: 15, weight: .bold)).foregroundStyle(p.label) .frame(width: 32, height: 32).background(p.fill, in: Circle()) } .buttonStyle(PressFade()) } } /// Big art that eases back when paused, like Apple Music. private struct ArtPanel: View { let thumb: String? let playing: Bool var inset: CGFloat = 28 /// Settings > Appearance, folded Duo only: art against the left edge. var leading = false /// The controls' side padding, which the art's left edge matches when `leading`. var edge: CGFloat = 28 var body: some View { GeometryReader { geo in let side = max(min(geo.size.width, geo.size.height), 1) Artwork(thumb: thumb, size: side, corner: 12) .shadow(color: .black.opacity(playing ? 0.35 : 0.15), radius: playing ? 24 : 10, y: 10) .scaleEffect(playing ? 1 : 0.82, anchor: leading ? .leading : .center) .animation(.spring(response: 0.45, dampingFraction: 0.62), value: playing) .frame(width: geo.size.width, height: geo.size.height, alignment: leading ? .leading : .center) } // Against the left edge, the art starts where the controls below it do. .padding(.leading, leading ? edge : inset).padding(.trailing, inset).padding(.vertical, 8) } } private struct MiniHeader: View { let track: Track var menu = true var compact = false @Environment(Ui.self) private var ui @Environment(\.palette) private var p var body: some View { HStack(spacing: 14) { Artwork(thumb: track.thumb, size: compact ? 52 : 64, corner: 8) VStack(alignment: .leading, spacing: 1) { Text(track.title).font(.headline).foregroundStyle(p.label).lineLimit(1) Text(track.artist).font(.subheadline).foregroundStyle(p.label2).lineLimit(1) } Spacer(minLength: 8) if menu { EllipsisButton(items: ui.menu.track(track)) } } .padding(.horizontal, 24).padding(.vertical, compact ? 6 : 10) } } // ---- seek / volume bar ---------------------------------------------------------------------- /// 0..1 loudness per waveform bucket for the current mix: stems weighted by their faders. func envelope(_ w: Waveform?, _ names: [String], _ mix: [String: StemSetting]) -> [Float]? { guard let w else { return nil } if let stems = w.stems, !stems.isEmpty { let anySolo = names.contains { mix[$0]?.solo == true } var gains: [String: Float] = [:] for n in stems.keys { let s = mix[n] ?? StemSetting() gains[n] = (anySolo && !s.solo) || s.muted ? 0 : stemTaper(s.level) } // Scaled to the full mix's range, so muting a stem still shows as quieter bars. return shape(mixDb(stems, gains), mixDb(stems, nil)) } if let db = w.loudnessDb { return shape(db, db) } return nil } private func mixDb(_ stems: [String: [Float]], _ gains: [String: Float]?) -> [Float] { let n = stems.values.map(\.count).max() ?? 0 return (0.. [Float] { var audible = ref.filter { $0 > -60 } if audible.isEmpty { audible = ref } audible.sort() if audible.isEmpty { return db.map { _ in 0.28 } } func pct(_ p: Float) -> Float { audible[min(Int(p * Float(audible.count)), audible.count - 1)] } let hi = pct(0.99) let span = min(max(hi - pct(0.10), 6), 24) let lo = hi - span return db.map { 0.05 + 0.95 * pow(min(max(($0 - lo) / span, 0), 1), 1.8) } } /// Plexamp's seek bar: the track's loudness as bars, played part highlighted. The bars' height /// follows the master volume, so the volume is drawn over the progress. /// Drag sideways to seek, up/down to change volume, tap to jump. struct ProgressVolumeBar: View { let played: Color let unplayed: Color let centerLabel: String? @Environment(\.palette) private var p @State private var scrub: Double? @State private var axis = 0 // 1 = seek, 2 = volume @State private var startVol: Float = 0 @State private var lastStep = 0 @State private var haptic = 0 @State private var barWidth: Double = 1 private var g: AppGraph { .shared } var body: some View { let pm = g.playback let master = pm.master let env = envelope(pm.waveform, pm.stemNames, pm.mix) let dur = pm.durationMs let volScale = 0.14 + 0.86 * Double(masterTaper(master)).squareRoot().squareRoot() let adjusting = axis == 2 // Bars are discrete and the clock shows seconds, so a few redraws a second look the same // as 120. A drag redraws through [scrub] and the volume through [master]. TimelineView(.animation(minimumInterval: 0.25, paused: !pm.playWhenReady || scrub != nil)) { _ in let pos = scrub.map { Int64($0 * Double(dur)) } ?? pm.positionMs() let frac = dur > 0 ? min(max(Double(pos) / Double(dur), 0), 1) : 0 VStack(spacing: 12) { Canvas { ctx, size in let barW = 2.7, pitch = 4.6 let count = max(8, Int(size.width / pitch)) let gap = (size.width - Double(count) * barW) / Double(count - 1) let maxH = size.height, minH = barW, cy = size.height / 2 if adjusting { let gh = maxH * volScale / 2 var path = Path() path.move(to: CGPoint(x: 0, y: cy - gh)); path.addLine(to: CGPoint(x: size.width, y: cy - gh)) path.move(to: CGPoint(x: 0, y: cy + gh)); path.addLine(to: CGPoint(x: size.width, y: cy + gh)) ctx.stroke(path, with: .color(p.label2.opacity(0.5)), lineWidth: 1) } var playedPath = Path(), restPath = Path() for i in 0.. some Gesture { let pm = g.playback return DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged { v in let t = v.translation if axis == 0, abs(t.width) > 8 || abs(t.height) > 8 { axis = abs(t.width) >= abs(t.height) ? 1 : 2 startVol = pm.master lastStep = Int(startVol * 20) haptic += 1 } switch axis { case 1: scrub = width(v) > 0 ? min(max(v.location.x / width(v), 0), 1) : 0 case 2: let nv = min(max(startVol - Float(t.height / (56 * 4.5)), 0), 1) let step = Int(nv * 20) if step != lastStep { lastStep = step if nv == 0 || nv == 1 { haptic += 1 } } pm.setMaster(nv) default: break } } .onEnded { v in switch axis { case 0: pm.seekTo(Int64(min(max(v.location.x / width(v), 0), 1) * Double(dur))) case 1: if let scrub { pm.seekTo(Int64(scrub * Double(dur))) } default: break } scrub = nil axis = 0 } } private func width(_ v: DragGesture.Value) -> Double { barWidth } } // ---- stem mixer ----------------------------------------------------------------------------- /// Meter levels with a quick attack and ~130 ms fall, timed in seconds so they look the same /// at 60 and 120 Hz. A plain class, so stepping it each frame doesn't invalidate views. private final class Meters { var raw = [Float](repeating: 0, count: MeterHistory.maxStems) var shown: [Float] = [] var last: Int64 = 0 func step(_ engine: StemEngine?, _ n: Int) -> [Float] { if shown.count != n { shown = Array(repeating: 0, count: n) } let now = monoNanos() let fall: Float = last == 0 ? 0 : exp(-Float(now - last) / 1e9 / 0.13) last = now let live = engine?.levelsNow(&raw) ?? false for i in 0.. some View { let current = MixPresets.normalized(pm.mix) let saved = pm.songMix.map(MixPresets.normalized) let isSaved = saved == current PresetChip(text: isSaved ? "Saved" : "Save", icon: isSaved ? "bookmark.fill" : "bookmark", selected: isSaved) { if isSaved { pm.forgetSongMix() ui.overlays.hud("Removed this song's mix", "bookmark.slash") } else { pm.saveSongMix() ui.overlays.hud("Mix saved for this song", "bookmark.fill") } } if let saved, !isSaved { PresetChip(text: "This Song", selected: false) { pm.applyMix(saved) } } } private func newPreset(_ pm: PlaybackManager) { ui.overlays.prompt("Save Preset", "Saves this mix for every song.", "Save", placeholder: "Preset name") { name in let n = name.trimmingCharacters(in: .whitespaces) guard !n.isEmpty else { return } CustomPreset.add(n, pm.mix) ui.overlays.hud("Preset saved", "slider.horizontal.3") } } @ViewBuilder func presetChips(_ pm: PlaybackManager, _ names: [String]) -> some View { let current = MixPresets.normalized(pm.mix) PresetChip(text: "New Preset", icon: "plus", selected: false) { newPreset(pm) } let s = g.settings ForEach(MixPresets.bar(s)) { preset in if let target = preset.build(names) { let on = MixPresets.normalized(target) == current let locked = s.lockedPreset == preset.id // Tapping the mix that's on goes back to the full song. PresetChip(text: preset.label, icon: locked ? "lock.fill" : nil, selected: on) { pm.applyMix(on ? [:] : target) } .contextMenu { if locked { Button("Unlock", systemImage: "lock.open") { s.lockedPreset = "" } } else { Button("Lock for All Songs", systemImage: "lock") { s.lockedPreset = preset.id; pm.applyMix(target) } } Button("Edit Stems", systemImage: "slider.horizontal.3") { ui.showInSettings(.StemEditor) } if preset.custom { Button("Delete Preset", systemImage: "trash", role: .destructive) { CustomPreset.remove(preset.label) } } } } } } static let roomyRow: CGFloat = 66 /// Shortest stacked row: name line, fader and the gap under it. static let roomyMin: CGFloat = 68 /// Shortest side-by-side row, the fader's own height. static let compactMin: CGFloat = 30 static let foldedMin: CGFloat = 40 /// Preset chips plus the bottom gap. static let roomyChrome: CGFloat = 56 static let compactChrome: CGFloat = 56 /// Height that shows the presets and every stem without scrolling, roomy rows. static func roomyHeight(_ stems: Int) -> CGFloat { roomyChrome + CGFloat(stems) * roomyRow } } /// [Chip] in the sheet's own colors: selected is the label color, so it reads on any tint. private struct PresetChip: View { let text: String var icon: String? = nil let selected: Bool let action: () -> Void @Environment(\.palette) private var p var body: some View { Button(action: action) { HStack(spacing: 5) { if let icon { Image(systemName: icon) } Text(text) } .font(.footnote.weight(.semibold)).foregroundStyle(selected ? p.bg : p.label) .padding(.horizontal, 14).frame(height: 32) .background(selected ? p.label : p.fill, in: Capsule()) } .buttonStyle(.plain) .sensoryFeedback(.selection, trigger: selected) } } private struct StemFader: View { let name: String let setting: StemSetting let level: Float let enabled: Bool var height: CGFloat = 46 var roomy = false @Environment(\.palette) private var p private var pm: PlaybackManager { AppGraph.shared.playback } var body: some View { let color = Color(hex: Stems.color(name)) FaderRow(dot: setting.muted ? p.label3 : color, label: Stems.label(name), percent: Int(stemTaper(setting.level) * 100), dim: setting.muted, height: height, stacked: roomy) { // Live level, -48..0 dBFS, drawn bright over the dimmed fill. let f = CGFloat(min(1, max(0, (20 * log10(level + 1e-5) + 48) / 48))) FaderSlider(value: setting.level, fill: setting.muted ? p.label3 : color, detent: StemSetting.unity, enabled: enabled, thumb: roomy ? 26 : 24, meter: enabled && !setting.muted ? f : nil) { pm.setStemLevel(name, $0) } } trailing: { if enabled { ToggleBox(text: "M", on: setting.muted, onColor: p.red, big: roomy) { pm.toggleMute(name) } ToggleBox(text: "S", on: setting.solo, onColor: Color(hex: 0xFFCC00), big: roomy) { pm.toggleSolo(name) } } } } } /// One mixer line: name, fader, level and buttons side by side, so all six stems and the /// master fit without scrolling (the faders grab drags, so the list can't scroll well). struct FaderRow: View { let dot: Color? let label: String let percent: Int let dim: Bool var height: CGFloat = 46 /// Name and level on a line of their own over the fader, so the fader runs the full width. var stacked = false @ViewBuilder let fader: Fader @ViewBuilder let trailing: Trailing @Environment(\.palette) private var p var body: some View { if stacked { // Bottom-aligned with a gap under, so each name sits clear of the meter above it. VStack(alignment: .leading, spacing: 6) { HStack(spacing: 6) { if let dot { Circle().fill(dot).frame(width: 8, height: 8) } Text(label).font(.subheadline.weight(.semibold)).foregroundStyle(dim ? p.label2 : p.label).lineLimit(1) Text("\(percent)%").font(.caption.monospacedDigit()).foregroundStyle(p.label2) Spacer(minLength: 0) } .frame(height: 18) HStack(spacing: 8) { fader; trailing } } // Centred in its slot, so tall rows keep even gaps above and below. .frame(height: height) } else { row } } private var row: some View { HStack(spacing: 8) { HStack(spacing: 6) { if let dot { Circle().fill(dot).frame(width: 8, height: 8) } Text(label).font(.subheadline.weight(.semibold)).foregroundStyle(dim ? p.label2 : p.label) .lineLimit(1).minimumScaleFactor(0.8) } .frame(width: 70, alignment: .leading) fader Text("\(percent)%").font(.caption.monospacedDigit()).foregroundStyle(p.label2) .frame(width: 36, alignment: .trailing) trailing } .frame(height: height) } } struct ToggleBox: View { let text: String let on: Bool let onColor: Color var big = false let action: () -> Void @Environment(\.palette) private var p var body: some View { Button(action: action) { Text(text).font(.footnote.bold()).foregroundStyle(on ? .black : p.label2) .frame(width: big ? 34 : 30, height: big ? 28 : 26) .background(on ? onColor : p.fill, in: RoundedRectangle(cornerRadius: 7, style: .continuous)) } .buttonStyle(.plain) } } /// An iOS-style slider with a colored fill and, optionally, a detent that catches the thumb /// near one value (unity gain) with a tick. struct FaderSlider: View { let value: Float var fill: Color var detent: Float? var enabled = true var thumb: CGFloat = 24 /// 0...1 live level, shown in the full fill color over the dimmed filled part of the track. var meter: CGFloat? let onChange: (Float) -> Void @Environment(\.palette) private var p @State private var start: Float? @State private var caught = false init(value: Float, fill: Color, detent: Float? = nil, enabled: Bool = true, thumb: CGFloat = 24, meter: CGFloat? = nil, onChange: @escaping (Float) -> Void) { self.value = value; self.fill = fill; self.detent = detent; self.enabled = enabled; self.thumb = thumb; self.meter = meter; self.onChange = onChange } private var bar: CGFloat { meter == nil ? (thumb >= 26 ? 5 : 4) : 8 } var body: some View { GeometryReader { geo in let track = max(geo.size.width - thumb, 1) let x = CGFloat(min(max(value, 0), 1)) * track ZStack(alignment: .leading) { Capsule().fill(p.fillStrong).frame(height: bar).padding(.horizontal, thumb / 2) // With a meter the set level is a dim band of the stem's color and the live level // runs over it in the full color, so the two never blend. Capsule().fill(meter == nil ? fill : fill.opacity(0.32)).frame(width: x + thumb / 2, height: bar).padding(.leading, thumb / 2 - 0.01) if let meter, meter > 0.01 { Capsule().fill(fill).frame(width: meter * (x + thumb / 2), height: bar) .shadow(color: fill.opacity(0.6), radius: 3) .padding(.leading, thumb / 2 - 0.01) .animation(.linear(duration: 1 / 30), value: meter) } if let detent { p.label3.frame(width: 2, height: 10).offset(x: CGFloat(detent) * track + thumb / 2 - 1) } Circle().fill(.white).frame(width: thumb, height: thumb) .shadow(color: .black.opacity(0.25), radius: 3, y: 1) .offset(x: x) } .frame(height: geo.size.height) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0).onChanged { g in guard enabled else { return } if start == nil { start = value } var v = min(max((start ?? value) + Float(g.translation.width / track), 0), 1) if let detent { let near = abs(v - detent) < 0.025 if near { v = detent } if near != caught { caught = near } } onChange(v) }.onEnded { _ in start = nil }) } .frame(height: max(36, thumb + 8)) .opacity(enabled ? 1 : 0.4) .sensoryFeedback(.selection, trigger: caught) { _, now in now } } } // ---- deck ----------------------------------------------------------------------------------- /// Unfolded iPhone Duo: the Now Playing tab's side of the hinge, across from docked Now Playing. /// One page at a time (stems, lyrics, the queue or the EQ), each with the whole pane to itself. struct NowPlayingDeck: View { @Environment(Ui.self) private var ui @Environment(\.palette) private var base @AppStorage("duoSwap") private var swap = false private var g: AppGraph { .shared } var body: some View { let c = base let track = g.playback.current?.track GeometryReader { _ in if let track { VStack(spacing: 0) { header(c) Group { switch ui.deckPage { case .Stems: MixerPanel(roomy: true) case .Lyrics: LyricsPanel(track: track) case .Queue: QueuePanel(title: false) case .Eq: DeckEqPanel() } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) .transition(.opacity) } } else { Text("Nothing Playing").font(.title3.weight(.semibold)).foregroundStyle(c.label2) .frame(maxWidth: .infinity, maxHeight: .infinity) } } .padding(.top, 8) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(c.bg.ignoresSafeArea()) .environment(\.palette, c) .modifier(NpScheme(dark: c.dark, docked: true)) .tint(c.label) .animation(.snappy(duration: 0.25), value: ui.deckPage) } /// The page names, and the button that moves Now Playing to the other side of the hinge. private func header(_ c: Palette) -> some View { HStack(spacing: 18) { ViewThatFits(in: .horizontal) { pages(c, spacing: 22) pages(c, spacing: 14) ScrollView(.horizontal) { pages(c, spacing: 18) }.scrollIndicators(.hidden) } Spacer(minLength: 0) if duoSwapEnabled { Button { withAnimation(.spring(duration: 0.4, bounce: 0.1)) { swap.toggle() } } label: { Image(systemName: "arrow.left.arrow.right").font(.body.weight(.semibold)).foregroundStyle(c.label2) .frame(width: 40, height: 32).contentShape(Rectangle()) } .buttonStyle(PressFade()) .accessibilityLabel("Swap Sides") } } .padding(.horizontal, 24).frame(height: 36) } private func pages(_ c: Palette, spacing: CGFloat) -> some View { HStack(spacing: spacing) { ForEach(DeckPage.allCases, id: \.self) { pg in let on = ui.deckPage == pg Button { withAnimation(.snappy(duration: 0.25)) { ui.showDeck(pg) } } label: { Text(pg.title).font(.headline).foregroundStyle(on ? c.label : c.label3) .lineLimit(1).fixedSize().frame(minHeight: 36).contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityAddTraits(on ? .isSelected : []) } } } } // ---- queue ---------------------------------------------------------------------------------- private struct QueuePanel: View { var close: () -> Void = {} /// The deck's header already says "Playing Next", so there it shows just the count. var title = true @Environment(\.palette) private var p private var g: AppGraph { .shared } var body: some View { let pm = g.playback let first = pm.index + 1 let upcoming = Array(pm.queue.dropFirst(max(first, 0))) VStack(spacing: 0) { HStack(spacing: 8) { VStack(alignment: .leading, spacing: 1) { if title { Text("Playing Next").font(.headline).foregroundStyle(p.label) } Text(songs(upcoming.count)).font(title ? .footnote : .subheadline).foregroundStyle(p.label2) } Spacer() QueueToggle(symbol: "shuffle", on: pm.shuffle, action: pm.toggleShuffle) QueueToggle(symbol: pm.repeatMode == .One ? "repeat.1" : "repeat", on: pm.repeatMode != .Off, action: pm.cycleRepeat) if !upcoming.isEmpty { Button("Clear") { pm.clearUpNext() }.foregroundStyle(p.label2).padding(.leading, 6) } } .padding(.horizontal, 24).padding(.vertical, 6) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 20).onEnded { v in if v.translation.height > 50 { close() } }) // Drag the handles to reorder; tap to jump, long press for Play Next or Remove. List { ForEach(Array(upcoming.enumerated()), id: \.element.uid) { i, q in HStack(spacing: 12) { Artwork(thumb: q.track.thumb, size: 44, corner: 5) VStack(alignment: .leading, spacing: 1) { Text(q.track.title).foregroundStyle(p.label).lineLimit(1) Text(q.track.artist).font(.subheadline).foregroundStyle(p.label2).lineLimit(1) } Spacer(minLength: 6) } .frame(height: 60) .contentShape(Rectangle()) .onTapGesture { pm.skipTo(first + i) } .listRowInsets(EdgeInsets(top: 0, leading: 24, bottom: 0, trailing: 16)) .listRowBackground(Color.clear) .listRowSeparator(.hidden) .contextMenu { Button { pm.move(first + i, first) } label: { Label("Play Next", systemImage: "text.line.first.and.arrowtriangle.forward") } Button(role: .destructive) { pm.removeAt(first + i) } label: { Label("Remove", systemImage: "trash") } } } .onMove { from, to in guard let f = from.first else { return } pm.move(first + f, first + (to > f ? to - 1 : to)) } if upcoming.isEmpty { Text("Nothing queued. Use Play Next or Add to Queue on any song.") .font(.subheadline).foregroundStyle(p.label2) .listRowBackground(Color.clear).listRowSeparator(.hidden) .listRowInsets(EdgeInsets(top: 16, leading: 24, bottom: 16, trailing: 24)) } } .listStyle(.plain) .scrollContentBackground(.hidden) .environment(\.editMode, .constant(.active)) } } } private struct QueueToggle: View { let symbol: String let on: Bool let action: () -> Void @Environment(\.palette) private var p var body: some View { Button(action: action) { Image(systemName: symbol).font(.system(size: 15, weight: .semibold)).foregroundStyle(on ? p.bg : p.label2) .frame(width: 36, height: 30) .background(on ? p.label : p.fill, in: RoundedRectangle(cornerRadius: 8, style: .continuous)) } .buttonStyle(.plain) } } // ---- mini player ---------------------------------------------------------------------------- struct MiniPlayer: View { @Environment(Ui.self) private var ui @Environment(\.palette) private var p @Environment(\.displayScale) private var scale private var g: AppGraph { .shared } var body: some View { if let t = g.playback.current?.track { if #available(iOS 26.1, *) { GlassMiniPlayer(track: t) } else { card(t) } } } /// Before iOS 26: a card floating over each tab. @ViewBuilder private func card(_ t: Track) -> some View { let pm = g.playback HStack(spacing: 0) { Artwork(thumb: t.thumb, size: 44, corner: 8).padding(.trailing, 12) VStack(alignment: .leading, spacing: 1) { Text(t.title).font(.subheadline.weight(.semibold)).foregroundStyle(p.label).lineLimit(1) Text(t.artist).font(.footnote).foregroundStyle(p.label2).lineLimit(1) } Spacer(minLength: 6) MiniTransport(color: p.label, next: true) } .padding(.leading, 8).padding(.trailing, 6) .frame(height: 60) .background { // A solid card in the app's own colors instead of system glass. let shape = RoundedRectangle(cornerRadius: 14, style: .continuous) ZStack { shape.fill(p.cell) shape.fill(p.fill.opacity(p.dark ? 0.5 : 0)) shape.strokeBorder(p.separator, lineWidth: 1 / scale) } .shadow(color: .black.opacity(p.dark ? 0.5 : 0.12), radius: 12, y: 4) } .overlay(alignment: .bottom) { // Thin progress line along the bottom of the card. TimelineView(.animation(minimumInterval: 0.5, paused: !pm.playWhenReady)) { _ in let d = pm.durationMs let f = d > 0 ? min(max(Double(pm.positionMs()) / Double(d), 0), 1) : 0 GeometryReader { geo in p.accent.frame(width: geo.size.width * f, height: 2) }.frame(height: 2) } .padding(.horizontal, 14) } .padding(.horizontal, 8) .padding(.bottom, 6) .contentShape(Rectangle()) .onTapGesture { ui.openNowPlaying() } .gesture(DragGesture(minimumDistance: 20).onEnded { v in if v.translation.height < -30 { ui.openNowPlaying() } }) .actionsMenu(ui.menu.track(t)) } } /// iOS 26: lives in the tab bar's Liquid Glass accessory, like Music. Shrinks to title and play when the bar collapses. @available(iOS 26.1, *) private struct GlassMiniPlayer: View { let track: Track @Environment(Ui.self) private var ui @Environment(\.tabViewBottomAccessoryPlacement) private var placement private var inline: Bool { placement == .inline } var body: some View { HStack(spacing: 10) { Artwork(thumb: track.thumb, size: inline ? 28 : 34, corner: 6) VStack(alignment: .leading, spacing: 0) { Text(track.title).font(.subheadline.weight(.semibold)).lineLimit(1) if !inline { Text(track.artist).font(.caption).foregroundStyle(.secondary).lineLimit(1) } } Spacer(minLength: 4) MiniTransport(color: .primary, next: !inline) } .padding(.leading, 10).padding(.trailing, 6) .contentShape(Rectangle()) .onTapGesture { ui.openNowPlaying() } .actionsMenu(ui.menu.track(track)) } } /// Play/pause (spinner while buffering) and next, for the mini player. private struct MiniTransport: View { let color: Color let next: Bool private var g: AppGraph { .shared } var body: some View { let pm = g.playback Button(action: pm.togglePlay) { ZStack { if pm.buffering && pm.playWhenReady { ProgressView() } else { Image(systemName: pm.playWhenReady ? "pause.fill" : "play.fill").font(.system(size: 22)).contentTransition(.symbolEffect(.replace)) } } .animation(.snappy(duration: 0.25), value: pm.playWhenReady) .foregroundStyle(color).frame(width: 44, height: 44).contentShape(Rectangle()) } .buttonStyle(PressFade()) if next { HoldButton(action: pm.next, hold: pm.nextAlbum) { Image(systemName: "forward.fill").font(.system(size: 20)).foregroundStyle(color).frame(width: 44, height: 44).contentShape(Rectangle()) } } } } /// Synced lyrics follow the song (tap a line to jump there); plain ones just scroll. private struct LyricsPanel: View { let track: Track @Environment(\.palette) private var p @State private var lyrics: Lyrics? @State private var loading = true @State private var at = -1 private var g: AppGraph { .shared } var body: some View { VStack(spacing: 0) { // Soft leaves the singer in quietly as a guide. if g.playback.source == .Stems && g.playback.stemNames.contains("vocals") { HStack(spacing: 12) { Text("Vocals").font(.subheadline.weight(.semibold)).foregroundStyle(p.label2) Picker("Vocals", selection: Binding(get: { g.playback.karaokeVocals }, set: { g.playback.setKaraokeVocals($0, engage: true) })) { ForEach(KaraokeVocals.allCases, id: \.self) { Text(String(describing: $0)).tag($0) } } .pickerStyle(.segmented) } .padding(.horizontal, 28).padding(.top, 8) if g.playback.karaokeVocals == .Soft { let db = g.settings.karaokeSoftDb HStack(spacing: 12) { Text("Softness").font(.subheadline.weight(.semibold)).foregroundStyle(p.label2) Slider(value: Binding(get: { Double(db) }, set: { g.playback.setKaraokeSoftDb(Int($0.rounded()), engage: true) }), in: -30...(-3), step: 1) Text("\(db) dB").font(.caption).monospacedDigit().foregroundStyle(p.label2).frame(width: 46, alignment: .trailing) } .padding(.horizontal, 28).padding(.top, 8) } } content } .task(id: track.ratingKey) { await follow() } } @ViewBuilder private var content: some View { Group { if loading { ProgressView() } else if let l = lyrics, !l.lines.isEmpty { lines(l) } else { Text(lyrics?.instrumental == true ? "Instrumental" : "No lyrics found") .font(.title3.weight(.semibold)).foregroundStyle(p.label2) } } .frame(maxWidth: .infinity, maxHeight: .infinity) } private func follow() async { loading = true at = -1 lyrics = await LyricsSource.shared.lyrics(track) loading = false guard let l = lyrics, l.synced else { return } while !Task.isCancelled { // A little early, so the line lights up as it's sung rather than after. let pos = g.playback.positionMs() + 250 let i = (l.lines.lastIndex { $0.ms <= pos }) ?? -1 if i != at { withAnimation(.easeOut(duration: 0.25)) { at = i } } try? await Task.sleep(nanoseconds: 150_000_000) } } private func lines(_ l: Lyrics) -> some View { ScrollViewReader { proxy in ScrollView { VStack(alignment: .leading, spacing: l.synced ? 18 : 6) { ForEach(Array(l.lines.enumerated()), id: \.offset) { i, line in Text(line.text.isEmpty ? "\u{266A}" : line.text) .font(l.synced ? .title2.weight(.bold) : .body) .foregroundStyle(!l.synced || i == at ? p.label : p.label3) .scaleEffect(i == at ? 1.03 : 1, anchor: .leading) .frame(maxWidth: .infinity, alignment: .leading) .contentShape(Rectangle()) .onTapGesture { if l.synced { g.playback.seekTo(line.ms) } } .id(i) } } .padding(.horizontal, 28) .padding(.vertical, l.synced ? 120 : 16) } .scrollIndicators(.hidden) .onChange(of: at) { _, i in if i >= 0 { withAnimation(.easeInOut(duration: 0.4)) { proxy.scrollTo(i, anchor: .center) } } } } } }