139 lines
4.9 KiB
Swift
139 lines
4.9 KiB
Swift
import AppKit
|
|
import DockDoorWidgetSDK
|
|
import SwiftUI
|
|
|
|
/// The dock slot itself: cover art, and in a double slot the track text too.
|
|
/// Everything scales off the shortest side, per the house layout rules.
|
|
struct CrossSoundWidgetView: View {
|
|
let size: CGSize
|
|
let isVertical: Bool
|
|
let pluginId: String
|
|
var client: CrossSoundClient
|
|
|
|
private var dim: CGFloat { min(size.width, size.height) }
|
|
|
|
private var isExtended: Bool {
|
|
isVertical
|
|
? size.height > size.width * 1.5
|
|
: size.width > size.height * 1.5
|
|
}
|
|
|
|
private var showArtwork: Bool {
|
|
WidgetDefaults.bool(key: "showArtwork", widgetId: pluginId, default: true)
|
|
}
|
|
|
|
private var showHeart: Bool {
|
|
WidgetDefaults.bool(key: "showHeart", widgetId: pluginId, default: true)
|
|
}
|
|
|
|
private var dockText: CrossSoundDockText { CrossSoundDockText.current(pluginId: pluginId) }
|
|
|
|
private var track: CrossSoundNowPlaying { client.nowPlaying }
|
|
|
|
var body: some View {
|
|
// A one-second tick is enough for title, artist and play state; the
|
|
// client throttles the actual request.
|
|
TimelineView(.periodic(from: .now, by: 1.0)) { _ in
|
|
let _ = client.tick(minimumInterval: 1.0)
|
|
Group {
|
|
if isExtended {
|
|
extendedLayout
|
|
} else {
|
|
compactLayout
|
|
}
|
|
}
|
|
.padding(dim * 0.06)
|
|
}
|
|
}
|
|
|
|
// MARK: Layouts
|
|
|
|
private var compactLayout: some View {
|
|
ZStack(alignment: .bottomTrailing) {
|
|
cover(side: dim * WidgetMetrics.contentScale)
|
|
if showHeart, track.isLiked, client.isConnected {
|
|
Image(systemName: "heart.fill")
|
|
.font(.system(size: dim * 0.2))
|
|
.foregroundStyle(.red)
|
|
.shadow(radius: 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
private var extendedLayout: some View {
|
|
Group {
|
|
if isVertical {
|
|
VStack(spacing: dim * WidgetMetrics.spacingScale) {
|
|
cover(side: dim * 0.52)
|
|
labels(alignment: .center)
|
|
}
|
|
} else {
|
|
HStack(spacing: dim * 0.1) {
|
|
cover(side: dim * WidgetMetrics.contentScale)
|
|
labels(alignment: .leading)
|
|
Spacer(minLength: 0)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Pieces
|
|
|
|
/// Cover art when there is one, otherwise the app glyph. Dimmed while paused
|
|
/// so the dock shows playback state at a glance.
|
|
private func cover(side: CGFloat) -> some View {
|
|
Group {
|
|
if let artwork = client.artwork, showArtwork {
|
|
Image(nsImage: artwork)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fill)
|
|
.frame(width: side, height: side)
|
|
.clipShape(RoundedRectangle(cornerRadius: side * 0.18, style: .continuous))
|
|
} else {
|
|
Image(systemName: "music.note")
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.frame(width: side * WidgetMetrics.sfSymbolScale)
|
|
.foregroundStyle(client.isConnected ? .secondary : .tertiary)
|
|
.frame(width: side, height: side)
|
|
}
|
|
}
|
|
.opacity(track.isPlaying ? 1 : 0.55)
|
|
}
|
|
|
|
private func labels(alignment: HorizontalAlignment) -> some View {
|
|
VStack(alignment: alignment, spacing: dim * 0.03) {
|
|
if dockText == .hidden || !client.isConnected {
|
|
Text(client.isConnected ? "CrossSound" : "Offline")
|
|
.font(.system(size: dim * 0.2, weight: .medium))
|
|
.foregroundStyle(.tertiary)
|
|
.lineLimit(1)
|
|
} else if track.hasTrack {
|
|
Text(track.title)
|
|
.font(.system(size: dim * 0.22, weight: .semibold))
|
|
.foregroundStyle(.primary)
|
|
.lineLimit(1)
|
|
if dockText == .titleAndArtist {
|
|
HStack(spacing: dim * 0.05) {
|
|
if showHeart, track.isLiked {
|
|
Image(systemName: "heart.fill")
|
|
.font(.system(size: dim * 0.15))
|
|
.foregroundStyle(.red)
|
|
}
|
|
Text(track.artist)
|
|
.font(.system(size: dim * 0.18, weight: .medium))
|
|
.foregroundStyle(.secondary)
|
|
.lineLimit(1)
|
|
}
|
|
}
|
|
} else {
|
|
Text("Nothing playing")
|
|
.font(.system(size: dim * 0.2, weight: .medium))
|
|
.foregroundStyle(.tertiary)
|
|
.lineLimit(1)
|
|
}
|
|
}
|
|
.multilineTextAlignment(alignment == .center ? .center : .leading)
|
|
}
|
|
}
|