import SwiftUI /// The player screen. /// /// Every width and height here is a constant. Whether a track has lyrics comes /// from its metadata, which arrives with the track, so the two columns are /// placed once per song and never move while it plays. struct NowPlayingView: View { @EnvironmentObject private var player: PlayerController private enum Control: Hashable { case chrome, scrubber, previous, play, next, like } private let stageHeight: CGFloat = 620 private let detailsWidth: CGFloat = 430 private let lyricsWidth: CGFloat = 1150 @State private var likeBusy = false @FocusState private var focus: Control? var body: some View { ZStack { BlurredBackdrop(url: player.current?.coverURL(size: 200)) if let track = player.current { content(for: track) } else { idle } } } private var idle: some View { VStack(spacing: 16) { Image(systemName: "music.note") .font(.system(size: 80)) .foregroundStyle(.secondary) Text("Ничего не играет") .font(.title2) .foregroundStyle(.secondary) } } // MARK: - Layout private func content(for track: Track) -> some View { VStack(spacing: 0) { // The tab bar floats over the content: this strip reserves room for // it and gives focus a step on the way back up to it. Color.clear .frame(height: 96) .contentShape(Rectangle()) .focusable() .focused($focus, equals: .chrome) HStack(alignment: .top, spacing: showsLyrics ? 72 : 0) { details(for: track) .frame(width: detailsWidth, height: stageHeight, alignment: .top) .clipped() LyricsPane(lyrics: player.lyricsState == .ready ? player.lyrics : nil) .frame( width: showsLyrics ? lyricsWidth : 0, height: stageHeight, alignment: .topLeading ) .clipped() .opacity(showsLyrics ? 1 : 0) } .frame( maxWidth: .infinity, maxHeight: .infinity, alignment: showsLyrics ? .topLeading : .top ) transportBar } .padding(.bottom, 24) } private var showsLyrics: Bool { player.current?.lyricsInfo?.hasSynced ?? true } private func details(for track: Track) -> some View { VStack(alignment: .leading, spacing: 24) { CoverImage(url: track.coverURL(size: 400), corner: 18) .frame(width: 390, height: 390) VStack(alignment: .leading, spacing: 8) { sourceLabel Text(track.displayTitle) .font(.system(size: 40, weight: .bold)) .lineLimit(2) Text(track.artistNames) .font(.system(size: 26)) .foregroundStyle(.white.opacity(0.65)) .lineLimit(1) } Spacer(minLength: 0) } } @ViewBuilder private var sourceLabel: some View { switch player.source { case let .rotor(_, name): Label(name.uppercased(), systemImage: "dot.radiowaves.left.and.right") .font(.caption.weight(.semibold)) .foregroundStyle(Color.ymAccent) case let .collection(name): Text(name.uppercased()) .font(.caption.weight(.semibold)) .foregroundStyle(Color.ymAccent) case .none: EmptyView() } } // MARK: - Transport private var transportBar: some View { VStack(alignment: .trailing, spacing: 16) { HStack(spacing: 20) { Spacer(minLength: 0) Button { player.previous() } label: { Image(systemName: "backward.fill") } .buttonStyle(GlyphButtonStyle()) .focused($focus, equals: .previous) Button { player.togglePlayPause() } label: { Image(systemName: player.isPlaying ? "pause.fill" : "play.fill") } .buttonStyle(GlyphButtonStyle(prominent: true)) .focused($focus, equals: .play) Button { player.next() } label: { Image(systemName: "forward.fill") } .buttonStyle(GlyphButtonStyle()) .focused($focus, equals: .next) Button { toggleLike() } label: { Image(systemName: isLiked ? "heart.fill" : "heart") } .buttonStyle(GlyphButtonStyle(tinted: isLiked)) .focused($focus, equals: .like) .disabled(likeBusy) } Scrubber( seek: { player.seek(to: $0) }, togglePlayPause: { player.togglePlayPause() } ) .focused($focus, equals: .scrubber) } .focusSection() } private var isLiked: Bool { player.isLiked(player.current) } private func toggleLike() { likeBusy = true Task { await player.toggleLikeCurrent() likeBusy = false } } } /// A glyph that takes a solid disc on focus. No scaling: growing a focused view /// makes tvOS swap in a snapshot of the screen. private struct GlyphButtonStyle: ButtonStyle { var tinted = false var prominent = false func makeBody(configuration: Configuration) -> some View { StyleBody(configuration: configuration, tinted: tinted, prominent: prominent) } private struct StyleBody: View { @Environment(\.isFocused) private var isFocused @Environment(\.isEnabled) private var isEnabled let configuration: ButtonStyleConfiguration let tinted: Bool let prominent: Bool private var foreground: Color { if isFocused { return .black } return tinted ? .ymAccent : .white } var body: some View { configuration.label .font(.system(size: prominent ? 34 : 28, weight: .semibold)) .foregroundStyle(foreground) .opacity(isEnabled ? (configuration.isPressed ? 0.7 : 1) : 0.3) .frame(width: prominent ? 88 : 72, height: prominent ? 88 : 72) .background { Circle().fill( isFocused ? AnyShapeStyle(.white) : AnyShapeStyle(.white.opacity(prominent ? 0.22 : 0.12)) ) } .animation(.easeOut(duration: 0.15), value: isFocused) } } }