Files
YaMusek/Sources/Views/Player/LyricsPane.swift
T
Thorgathis acd50280bc Yandex Music client for Apple TV
tvOS 17+ SwiftUI client for the unofficial Yandex Music API: rotor stations
with feedback, library, search, artist and album pages, synced lyrics, and
system Now Playing integration.
2026-09-17 20:38:19 +03:00

160 lines
5.3 KiB
Swift

import SwiftUI
/// Synced lyrics, with a run-in before the first line.
struct LyricsPane: View {
let lyrics: Lyrics?
/// Intros longer than this get dots for their whole length; shorter ones
/// only for the last few seconds.
private let longIntro: Double = 5
private let shortLead: Double = 4
/// Words appear this far before the dots run out, so the first line is
/// already on screen and rolls away instead of appearing from nothing.
private let wordsLead: Double = 1
@ObservedObject private var clock = PlayerController.shared.clock
init(lyrics: Lyrics?) {
self.lyrics = lyrics
}
var body: some View {
// Dots and words share an origin, so the first line lands exactly where
// the dots were.
ZStack(alignment: .topLeading) {
if !linesVisible, let range = countdownRange {
CountdownDots(start: range.start, end: range.end)
.transition(.opacity)
}
if linesVisible {
LyricsLines(
lyrics: lyrics,
activeID: lyrics?.activeIndex(at: clock.currentTime),
armed: armed
)
.equatable()
.transition(.opacity)
}
}
.animation(.easeInOut(duration: 0.45), value: linesVisible)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
private var firstLineTime: Double? {
lyrics?.lines.first(where: { !$0.isBlank })?.time
}
private var countdownStart: Double {
guard let first = firstLineTime else { return 0 }
return first > longIntro ? 0 : max(first - shortLead, 0)
}
private var countdownRange: (start: Double, end: Double)? {
guard let first = firstLineTime else { return nil }
let now = clock.currentTime
guard now >= countdownStart, now < first else { return nil }
return (countdownStart, first)
}
private var linesVisible: Bool {
guard let first = firstLineTime else { return true }
return clock.currentTime >= first - wordsLead
}
private var armed: Bool {
guard firstLineTime != nil else { return true }
return clock.currentTime >= countdownStart
}
}
/// Three dots filling one after another across the run-in. Each fills all the
/// way before the next one starts.
private struct CountdownDots: View {
let start: Double
let end: Double
@ObservedObject private var clock = PlayerController.shared.clock
init(start: Double, end: Double) {
self.start = start
self.end = end
}
private var progress: Double {
let span = max(end - start, 0.001)
return min(max((clock.currentTime - start) / span, 0), 1)
}
var body: some View {
HStack(spacing: 22) {
ForEach(0 ..< 3, id: \.self) { index in
let fill = min(max(progress * 3 - Double(index), 0), 1)
Circle()
.fill(.white.opacity(0.18 + 0.77 * fill))
.frame(width: 30, height: 30)
}
}
.padding(.top, 26)
// The clock ticks five times a second; this rides between the ticks.
.animation(.linear(duration: 0.2), value: progress)
}
}
/// The line being sung sits at the top, the lines still to come fade away below
/// it, and everything already sung is gone.
///
/// Not a ScrollView: driving a scroll offset from a timer made the highlight
/// drift out of view.
private struct LyricsLines: View, Equatable {
let lyrics: Lyrics?
let activeID: Int?
let armed: Bool
private let window = 6
/// Equatable collapses five ticks a second into one rebuild per line.
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.activeID == rhs.activeID
&& lhs.armed == rhs.armed
&& lhs.lyrics == rhs.lyrics
}
var body: some View {
rolling
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
private var visible: [LyricsLine] {
guard armed else { return [] }
let sung = (lyrics?.lines ?? []).filter { !$0.isBlank }
guard let anchor = activeID ?? sung.first?.id else { return [] }
return Array(sung.filter { $0.id >= anchor }.prefix(window))
}
private var rolling: some View {
VStack(alignment: .leading, spacing: 34) {
ForEach(visible) { line in
Text(line.text)
.font(.system(size: line.id == activeID ? 96 : 76, weight: .bold))
.foregroundStyle(.white.opacity(opacity(for: line)))
.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .leading)
.transition(.opacity)
}
Spacer(minLength: 0)
}
.animation(.easeInOut(duration: 0.35), value: activeID)
.clipped()
}
/// The sung line is solid; each line further ahead is dimmer than the last.
private func opacity(for line: LyricsLine) -> Double {
guard let activeID else { return 0.4 }
if line.id == activeID { return 1 }
let position = visible.firstIndex(where: { $0.id == line.id }) ?? 0
return max(0.08, 0.32 - Double(position) * 0.07)
}
}