302 lines
10 KiB
Swift
302 lines
10 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
/// Hover / long-press / right-click panel: the full player, plus pickers for
|
|
/// your playlists, the Discover shelves and the current queue.
|
|
struct CrossSoundPanel: View {
|
|
var client: CrossSoundClient
|
|
let dismiss: () -> Void
|
|
|
|
private enum Tab: String, CaseIterable, Identifiable {
|
|
case playlists = "Playlists"
|
|
case discover = "Discover"
|
|
case queue = "Queue"
|
|
|
|
var id: String { rawValue }
|
|
}
|
|
|
|
@State private var tab: Tab = .playlists
|
|
@State private var scrubbing = false
|
|
@State private var scrubValue: Double = 0
|
|
@State private var hoveredRow: String?
|
|
|
|
private var track: CrossSoundNowPlaying { client.nowPlaying }
|
|
|
|
var body: some View {
|
|
TimelineView(.periodic(from: .now, by: 1.0)) { _ in
|
|
let _ = client.tick(minimumInterval: 1.0)
|
|
let _ = reloadIfIdle()
|
|
VStack(spacing: 0) {
|
|
if client.isConnected {
|
|
player
|
|
Divider()
|
|
picker
|
|
list
|
|
} else {
|
|
offline
|
|
}
|
|
}
|
|
.frame(width: 360, height: 460)
|
|
}
|
|
.onAppear { load(for: tab) }
|
|
.onChange(of: tab) { _, newTab in load(for: newTab) }
|
|
}
|
|
|
|
// MARK: Offline
|
|
|
|
private var offline: some View {
|
|
VStack(spacing: 10) {
|
|
Image(systemName: "music.note")
|
|
.font(.largeTitle)
|
|
.foregroundStyle(.tertiary)
|
|
Text("CrossSound is not running")
|
|
.font(.headline)
|
|
Text("Open CrossSound on this Mac. The widget talks to it on 127.0.0.1, the same local API the Stream Deck plugin uses.")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.multilineTextAlignment(.center)
|
|
.padding(.horizontal, 24)
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
|
|
// MARK: Player
|
|
|
|
private var player: some View {
|
|
VStack(spacing: 12) {
|
|
HStack(alignment: .top, spacing: 12) {
|
|
cover
|
|
VStack(alignment: .leading, spacing: 3) {
|
|
Text(track.hasTrack ? track.title : "Nothing playing")
|
|
.font(.headline)
|
|
.lineLimit(2)
|
|
Text(track.artist)
|
|
.font(.callout)
|
|
.foregroundStyle(.secondary)
|
|
.lineLimit(1)
|
|
}
|
|
Spacer(minLength: 0)
|
|
Button {
|
|
client.toggleLike()
|
|
} label: {
|
|
Image(systemName: track.isLiked ? "heart.fill" : "heart")
|
|
.font(.title3)
|
|
.foregroundStyle(track.isLiked ? Color.red : Color.secondary)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.disabled(!track.hasTrack)
|
|
.help(track.isLiked ? "Unlike" : "Like")
|
|
}
|
|
|
|
progress
|
|
transport
|
|
}
|
|
.padding(16)
|
|
}
|
|
|
|
private var cover: some View {
|
|
Group {
|
|
if let artwork = client.artwork {
|
|
Image(nsImage: artwork)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fill)
|
|
} else {
|
|
ZStack {
|
|
Color.secondary.opacity(0.12)
|
|
Image(systemName: "music.note")
|
|
.font(.title2)
|
|
.foregroundStyle(.tertiary)
|
|
}
|
|
}
|
|
}
|
|
.frame(width: 84, height: 84)
|
|
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
|
|
}
|
|
|
|
private var progress: some View {
|
|
VStack(spacing: 2) {
|
|
Slider(
|
|
value: Binding(
|
|
get: { scrubbing ? scrubValue : track.position },
|
|
set: { scrubValue = $0 }
|
|
),
|
|
in: 0...max(track.durationSeconds, 1),
|
|
onEditingChanged: { editing in
|
|
scrubbing = editing
|
|
if !editing { client.seek(to: scrubValue) }
|
|
}
|
|
)
|
|
.disabled(!track.hasTrack || track.durationSeconds <= 0)
|
|
HStack {
|
|
Text(track.elapsedText)
|
|
Spacer()
|
|
Text(track.durationText)
|
|
}
|
|
.font(.caption2)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
|
|
private var transport: some View {
|
|
HStack(spacing: 18) {
|
|
iconButton("shuffle", active: track.shuffle, size: .callout) { client.toggleShuffle() }
|
|
Spacer(minLength: 0)
|
|
iconButton("backward.end.fill", size: .title3) { client.previous() }
|
|
Button {
|
|
client.playPause()
|
|
} label: {
|
|
Image(systemName: track.isPlaying ? "pause.circle.fill" : "play.circle.fill")
|
|
.font(.system(size: 38))
|
|
.foregroundStyle(.primary)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.help(track.isPlaying ? "Pause" : "Play")
|
|
iconButton("forward.end.fill", size: .title3) { client.next() }
|
|
Spacer(minLength: 0)
|
|
iconButton(repeatIcon, active: track.repeatMode != "none", size: .callout) { client.cycleRepeat() }
|
|
}
|
|
}
|
|
|
|
private var repeatIcon: String { track.repeatMode == "one" ? "repeat.1" : "repeat" }
|
|
|
|
private func iconButton(
|
|
_ symbol: String,
|
|
active: Bool = false,
|
|
size: Font,
|
|
action: @escaping () -> Void
|
|
) -> some View {
|
|
Button(action: action) {
|
|
Image(systemName: symbol)
|
|
.font(size)
|
|
.foregroundStyle(active ? Color.accentColor : Color.secondary)
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
|
|
// MARK: Lists
|
|
|
|
private var picker: some View {
|
|
Picker("", selection: $tab) {
|
|
ForEach(Tab.allCases) { item in
|
|
Text(item.rawValue).tag(item)
|
|
}
|
|
}
|
|
.pickerStyle(.segmented)
|
|
.labelsHidden()
|
|
.padding(.horizontal, 12)
|
|
.padding(.vertical, 8)
|
|
}
|
|
|
|
private var list: some View {
|
|
Group {
|
|
switch tab {
|
|
case .playlists:
|
|
rows(client.playlists, state: client.playlistsLoad)
|
|
case .discover:
|
|
rows(client.discover, state: client.discoverLoad)
|
|
case .queue:
|
|
rows(queueRows, state: queueRows.isEmpty ? .failed("Queue is empty") : .loaded)
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
|
|
private var queueRows: [CrossSoundBrowseRow] {
|
|
track.queue.map { item in
|
|
CrossSoundBrowseRow(
|
|
id: "queue-\(item.index)-\(item.id)",
|
|
title: item.title,
|
|
subtitle: item.index == track.queueIndex ? "Now playing" : item.artist,
|
|
action: .queueIndex(item.index)
|
|
)
|
|
}
|
|
}
|
|
|
|
private func rows(_ items: [CrossSoundBrowseRow], state: CrossSoundLoad) -> some View {
|
|
Group {
|
|
switch state {
|
|
case .loading, .idle:
|
|
VStack(spacing: 8) {
|
|
ProgressView().controlSize(.small)
|
|
Text("Loading…")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
case let .failed(message):
|
|
Text(message)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
case .loaded:
|
|
ScrollView(.vertical, showsIndicators: false) {
|
|
LazyVStack(spacing: 2) {
|
|
ForEach(items) { row in
|
|
rowView(row)
|
|
}
|
|
}
|
|
.padding(.horizontal, 10)
|
|
.padding(.bottom, 10)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func rowView(_ row: CrossSoundBrowseRow) -> some View {
|
|
Button {
|
|
client.perform(row.action)
|
|
} label: {
|
|
HStack(spacing: 8) {
|
|
VStack(alignment: .leading, spacing: 1) {
|
|
Text(row.title)
|
|
.font(.callout)
|
|
.lineLimit(1)
|
|
if !row.subtitle.isEmpty {
|
|
Text(row.subtitle)
|
|
.font(.caption2)
|
|
.foregroundStyle(.secondary)
|
|
.lineLimit(1)
|
|
}
|
|
}
|
|
Spacer(minLength: 0)
|
|
Image(systemName: "play.fill")
|
|
.font(.caption2)
|
|
.foregroundStyle(.secondary)
|
|
.opacity(hoveredRow == row.id ? 1 : 0)
|
|
}
|
|
.padding(.horizontal, 8)
|
|
.padding(.vertical, 6)
|
|
.background(
|
|
RoundedRectangle(cornerRadius: 6, style: .continuous)
|
|
.fill(Color.secondary.opacity(hoveredRow == row.id ? 0.14 : 0))
|
|
)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.onHover { inside in
|
|
hoveredRow = inside ? row.id : (hoveredRow == row.id ? nil : hoveredRow)
|
|
}
|
|
}
|
|
|
|
/// Losing the app clears the cached lists; pick them back up once it returns.
|
|
/// A failed load stays failed until the tab changes, so this cannot spin.
|
|
private func reloadIfIdle() {
|
|
guard client.isConnected else { return }
|
|
switch tab {
|
|
case .playlists where client.playlistsLoad == .idle:
|
|
client.loadPlaylists()
|
|
case .discover where client.discoverLoad == .idle:
|
|
client.loadDiscover()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
private func load(for tab: Tab) {
|
|
switch tab {
|
|
case .playlists: client.loadPlaylists()
|
|
case .discover: client.loadDiscover()
|
|
case .queue: break
|
|
}
|
|
}
|
|
}
|