Files
RedLintu16andClaude Opus 5.5 64ec524ba0 CrossSound for Apple platforms
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-14 13:28:34 -07:00

320 lines
8.9 KiB
Swift

// WatchLook — CrossSound's look, on a watch: every text and icon in the
// theme's primary red (the phone's RwPalette.allTextPrimary + statusInRed),
// no backgrounds behind buttons, artwork with the 2pt accent border, and a
// 2pt accent border round whatever is playing. Plus the few pieces every
// screen shares — in both watch apps (CrossSound and CrossSound Remote,
// which compiles this file too).
import CrossSoundKit
import RedWhiteUI
import SwiftUI
extension RwPalette {
/// Text, muted text, icons and the status colours all become the primary.
var crossSound: RwPalette {
var q = self
q.text = red
q.textMuted = red
q.contentText = red
q.iconInactive = red
q.success = red
q.warn = red
q.info = red
q.successSoft = redSoft
q.warnSoft = redSoft
q.infoSoft = redSoft
return q
}
}
/// Plants the look for everything below: the palette, red as the foreground
/// and the system tint (back chevrons, titles, progress), and the page
/// background behind the navigation stack.
struct CrossSoundWatchLook: ViewModifier {
@Environment(\.rw) private var p
func body(content: Content) -> some View {
let q = p.crossSound
content
.foregroundStyle(q.red.color)
.tint(q.red.color)
.buttonStyle(.plain)
.environment(\.rw, q)
}
}
extension View {
func crossSoundWatchLook() -> some View { modifier(CrossSoundWatchLook()) }
/// The 2pt accent border on the playing row.
func playingBorder(_ on: Bool) -> some View {
overlay {
if on { Rectangle().strokeBorder(lineWidth: 2) }
}
}
}
/// A cover: square, accent-bordered, the soft panel colour until it loads.
struct Artwork: View {
@Environment(\.rw) private var p
let url: String?
var size: CGFloat = 32
var body: some View {
AsyncImage(url: url.flatMap(URL.init(string:))) { image in
image.resizable().scaledToFill()
} placeholder: {
ZStack {
p.bg2.color
Image(systemName: "music.note")
.font(.system(size: size * 0.4))
}
}
.frame(width: size, height: size)
.clipShape(Rectangle())
.rwBorder(.accent, width: 2)
}
}
/// The small uppercase label over a group of rows.
struct SectionLabel: View {
let text: String
init(_ text: String) { self.text = text }
var body: some View {
Text(text.uppercased())
.rw(.labelSmall)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.top, 10)
.padding(.bottom, 2)
}
}
/// A song: cover, title, artist.
struct TrackRow: View {
let title: String
let artist: String
let artworkUrl: String?
var isCurrent = false
var body: some View {
HStack(spacing: 8) {
Artwork(url: artworkUrl, size: 32)
VStack(alignment: .leading, spacing: 1) {
Text(title)
.rw(.titleSmall)
.lineLimit(2)
Text(artist)
.rw(.labelMedium)
.lineLimit(1)
}
Spacer(minLength: 0)
}
.padding(4)
.playingBorder(isCurrent)
.contentShape(Rectangle())
}
}
/// A row that leads somewhere: an icon and a name.
struct LinkRow: View {
let icon: String
let title: String
var body: some View {
HStack(spacing: 10) {
Image(systemName: icon)
.font(.system(size: 15, weight: .semibold))
.frame(width: 22)
Text(title)
.rw(.titleMedium)
Spacer(minLength: 0)
}
.padding(.vertical, 8)
.contentShape(Rectangle())
}
}
/// A thin progress line with the elapsed and remaining time under it.
struct ProgressLine: View {
@Environment(\.rw) private var p
let positionMs: Int
let durationMs: Int
var body: some View {
VStack(spacing: 2) {
GeometryReader { geo in
let fraction = durationMs > 0 ? min(1, Double(positionMs) / Double(durationMs)) : 0
ZStack(alignment: .leading) {
Rectangle().fill(p.hair.color)
Rectangle().fill(p.red.color).frame(width: geo.size.width * fraction)
}
}
.frame(height: 3)
HStack {
Text(clock(positionMs)).rw(.labelSmall)
Spacer()
Text("-" + clock(max(0, durationMs - positionMs))).rw(.labelSmall)
}
.monospacedDigit()
}
}
private func clock(_ ms: Int) -> String {
let s = ms / 1000
return String(format: "%d:%02d", s / 60, s % 60)
}
}
/// prev · play/pause · next, as bare red glyphs — and, driving another
/// device that says whether its song is liked, a heart after next.
struct TransportRow: View {
let isPlaying: Bool
var enabled = true
/// nil = no heart (this watch's own player has its heart elsewhere; a
/// device that doesn't publish it wouldn't act on the command either).
var liked: Bool? = nil
let onPrev: () -> Void
let onToggle: () -> Void
let onNext: () -> Void
var onLike: () -> Void = {}
var body: some View {
// Three hold 44pt each with the slack between them. Four don't fit that
// way on the smaller watches (a 40mm is 162pt wide), so with the heart
// the glyphs share the row equally instead.
let width: CGFloat? = liked == nil ? 44 : nil
HStack(spacing: 0) {
glyph("backward.fill", size: 18, width: width, label: "Previous", action: onPrev)
if width != nil { Spacer(minLength: 0) }
glyph(
isPlaying ? "pause.fill" : "play.fill", size: 28, width: width,
label: isPlaying ? "Pause" : "Play", action: onToggle)
if width != nil { Spacer(minLength: 0) }
glyph("forward.fill", size: 18, width: width, label: "Next", action: onNext)
if let liked {
glyph(
liked ? "heart.fill" : "heart", size: 18, width: width,
label: liked ? "Unlike" : "Like", action: onLike)
}
}
.disabled(!enabled)
.opacity(enabled ? 1 : 0.4)
}
/// `width` nil = take an equal share of the row.
private func glyph(
_ name: String, size: CGFloat, width: CGFloat?, label: String, action: @escaping () -> Void
) -> some View {
Button(action: action) {
Image(systemName: name)
.font(.system(size: size, weight: .semibold))
.frame(maxWidth: width == nil ? .infinity : nil)
.frame(width: width, height: 40)
.contentShape(Rectangle())
}
.accessibilityLabel(label)
}
}
/// Loading, failed, or the content.
struct Loadable<Value, Content: View>: View {
let state: LoadState<Value>
let retry: () -> Void
@ViewBuilder let content: (Value) -> Content
var body: some View {
switch state {
case .loading:
ProgressView()
.frame(maxWidth: .infinity, minHeight: 80)
case .failed(let message):
VStack(spacing: 8) {
Text(message)
.rw(.labelMedium)
.multilineTextAlignment(.center)
Button("Try again", action: retry)
.font(.system(size: 15, weight: .semibold))
}
.frame(maxWidth: .infinity)
.padding(.vertical, 12)
case .loaded(let value):
content(value)
}
}
}
enum LoadState<Value> {
case loading
case failed(String)
case loaded(Value)
}
// MARK: - Devices
/// A device on the account: what it is and what it's playing.
struct DeviceRow: View {
let device: RemoteDevice
var body: some View {
HStack(spacing: 8) {
Image(systemName: DeviceRow.icon(device.platform))
.font(.system(size: 17, weight: .semibold))
.frame(width: 26)
VStack(alignment: .leading, spacing: 1) {
HStack(spacing: 4) {
Text(device.name)
.rw(.titleSmall)
.lineLimit(1)
.layoutPriority(1)
// "Mac app" / "Desktop app": two apps on one machine read apart.
if let app = device.app {
Text(app)
.rw(.labelSmall)
.lineLimit(1)
}
}
Text(nowPlaying)
.rw(.labelMedium)
.lineLimit(1)
}
Spacer(minLength: 0)
if device.isPlaying {
Image(systemName: "speaker.wave.2.fill")
.font(.system(size: 12))
}
}
.padding(4)
.playingBorder(device.isPlaying)
.contentShape(Rectangle())
}
private var nowPlaying: String {
guard let title = device.title, !title.isEmpty else { return "Nothing playing" }
return [title, device.artist].compactMap { $0 }.joined(separator: " · ")
}
/// The Devices screen's icons (DevicesView.platformIcon).
static func icon(_ platform: String) -> String {
switch platform {
case "desktop": "desktopcomputer"
case "ios": "iphone"
case "watch": "applewatch"
default: "smartphone"
}
}
}
extension RemoteDevice {
/// The last reported playhead, run on by the time since that report while
/// playing (at most 10s, in case the device went quiet). Peers publish about
/// every 2s while playing; this keeps the bar moving smoothly in between.
func position(at date: Date) -> Int {
guard isPlaying, updatedAt > 0 else { return positionMs }
let elapsed = Int(date.timeIntervalSince1970 * 1000) - updatedAt
let shown = positionMs + min(max(elapsed, 0), 10_000)
return durationMs > 0 ? min(shown, durationMs) : shown
}
}