Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/LooperTimelineView.swift
T
josh 2ce6dda9da Looper UI: re-subscribe polls when the pedal instance changes
The looper timeline and the transport buttons' live captions poll the pedal
via SwiftUI `.task`, which captures the pedal at task-start and never
re-subscribes. When a rig load swaps in a new LooperPedal instance, the
buttons/LEDs (read live in `body`) tracked the new pedal while the polled
state text and captions stayed frozen on the old one — the state indicator and
"Record/Set End/…" captions never updated even though transport worked.

Key the timeline poll on ObjectIdentifier(pedal) (`.task(id:)`) and tag the
transport button row with `.id(ObjectIdentifier(pedal))`, so both re-subscribe
to the current instance. Board edits reuse the same instance (identity stable),
so this only refreshes on an actual instance swap — no per-render churn.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:17:40 -07:00

237 lines
11 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// LooperTimelineView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
/// The looper's loop timeline: a transport-state badge, tappable A–D track dots, a loop-length
/// read-out, a sweeping play-head track, and a one-line state indicator describing what the
/// pedal is doing. Rendered inside the double-width ``PedalView`` for a `LooperPedal`.
///
/// `LooperPedal`'s transport state is plain (non-`@Observable`) RT-adjacent data, so this view
/// polls it on a `.task` loop — the same convention as `TempoView`'s clock read-out.
struct LooperTimelineView: View {
// MARK: - Constants
private static let trackHeight: CGFloat = 16
private static let trackCornerRadius: CGFloat = 4
private static let playheadWidth: CGFloat = 2
private static let sectionSpacing: CGFloat = 6
private static let labelFontSize: CGFloat = 11
private static let trackBrightness: Double = 0.14
private static let trackBorderOpacity: Double = 0.35
private static let filledOpacity: Double = 0.22
private static let pollIntervalNanoseconds: UInt64 = 33_000_000 // ~30 Hz
private static let ledDiameter: CGFloat = 9
private static let badgeInnerSpacing: CGFloat = 4
private static let lengthLabelOpacity: Double = 0.75
private static let borderLineWidth: CGFloat = 1
private static let dotSpacing: CGFloat = 4
private static let dotDiameter: CGFloat = 18
private static let dotLabelFontSize: CGFloat = 10
private static let dotBrightness: Double = 0.16
private static let dotFilledOpacity: Double = 0.55
private static let dotEmptyOpacity: Double = 0.45
private static let activeRingWidth: CGFloat = 1.5
private static let mutedDotOpacity: Double = 0.4
private static let muteSlashOpacity: Double = 0.9
private static let muteSlashWidth: CGFloat = 1.5
private static let indicatorFontSize: CGFloat = 11
private static let indicatorOpacity: Double = 0.85
private static let overdubRingColor: Color = Color.red
private static let dotLabels: [String] = ["A", "B", "C", "D", "E", "F", "G", "H"]
// MARK: - Properties
private let pedal: LooperPedal
@State private var position: Double?
@State private var state: LooperTransportState = .clear
@State private var lengthSeconds: Double = 0.0
@State private var lengthBeats: Double = 0.0
@State private var selectedTrack: Int = 0
@State private var overdubbingTrack: Int?
@State private var trackContent: [Bool] = Array(repeating: false, count: LooperPedal.trackCount)
@State private var trackMuted: [Bool] = Array(repeating: false, count: LooperPedal.trackCount)
@State private var stopArmed: Bool = false
@State private var stateDescription: String = ""
init(pedal: LooperPedal) {
self.pedal = pedal
}
// MARK: - Body
var body: some View {
VStack(spacing: LooperTimelineView.sectionSpacing) {
HStack(spacing: LooperTimelineView.sectionSpacing) {
stateBadge
Spacer(minLength: 0)
trackDots
Spacer(minLength: 0)
lengthLabel
}
track
stateIndicator
}
// Key the poll on the pedal instance: if a rig load / rebuild swaps in a new LooperPedal,
// `.task` alone would keep polling the old (captured) instance and freeze. Restart on change.
.task(id: ObjectIdentifier(pedal)) {
while !Task.isCancelled {
position = pedal.playbackPosition01()
state = pedal.transportState
lengthSeconds = pedal.loopLengthSeconds
lengthBeats = pedal.loopLengthBeats
selectedTrack = pedal.selectedTrackIndex
overdubbingTrack = pedal.overdubbingTrackIndex
trackContent = (0 ..< LooperPedal.trackCount).map { pedal.trackHasContent($0) }
trackMuted = (0 ..< LooperPedal.trackCount).map { pedal.isTrackMuted($0) }
stopArmed = pedal.isStopAtLoopEndArmed
stateDescription = pedal.stateDescription
try? await Task.sleep(nanoseconds: LooperTimelineView.pollIntervalNanoseconds)
}
}
}
// MARK: - Subviews
/// One tappable labelled dot per track: filled when it holds a loop, white-ringed when
/// selected, red-ringed while overdubbing, dimmed with a slash when muted. Tapping selects the
/// track; tapping the already-selected recorded track mutes/unmutes it.
private var trackDots: some View {
HStack(spacing: LooperTimelineView.dotSpacing) {
ForEach(0 ..< LooperPedal.trackCount, id: \.self) { index in
let isSelected: Bool = index == selectedTrack
let isOverdub: Bool = index == overdubbingTrack
let hasContent: Bool = index < trackContent.count && trackContent[index]
let isMuted: Bool = index < trackMuted.count && trackMuted[index]
Button {
pedal.tapTrack(index)
} label: {
Text(LooperTimelineView.dotLabels[index])
.font(.system(size: LooperTimelineView.dotLabelFontSize, weight: .bold, design: .monospaced))
.foregroundStyle(hasContent ? Color.white : Color.white.opacity(LooperTimelineView.dotEmptyOpacity))
.frame(width: LooperTimelineView.dotDiameter, height: LooperTimelineView.dotDiameter)
.background(
Circle().fill(hasContent ? Color.green.opacity(LooperTimelineView.dotFilledOpacity)
: Color(white: LooperTimelineView.dotBrightness))
)
.overlay(muteSlash(isMuted))
.overlay(
Circle().stroke(isOverdub ? LooperTimelineView.overdubRingColor
: (isSelected ? Color.white : Color.clear),
lineWidth: LooperTimelineView.activeRingWidth)
)
.opacity(isMuted ? LooperTimelineView.mutedDotOpacity : 1.0)
}
.buttonStyle(.plain)
}
}
}
/// A diagonal slash drawn over a muted track's dot.
@ViewBuilder
private func muteSlash(_ muted: Bool) -> some View {
if muted {
GeometryReader { geometry in
Path { path in
path.move(to: CGPoint(x: 0, y: geometry.size.height))
path.addLine(to: CGPoint(x: geometry.size.width, y: 0))
}
.stroke(Color.white.opacity(LooperTimelineView.muteSlashOpacity),
lineWidth: LooperTimelineView.muteSlashWidth)
}
}
}
private var stateBadge: some View {
HStack(spacing: LooperTimelineView.badgeInnerSpacing) {
Circle()
.fill(stateColor)
.frame(width: LooperTimelineView.ledDiameter, height: LooperTimelineView.ledDiameter)
Text(stateText)
.font(.system(size: LooperTimelineView.labelFontSize, weight: .semibold, design: .monospaced))
.foregroundStyle(Color.white)
if stopArmed {
Text("→END")
.font(.system(size: LooperTimelineView.labelFontSize, weight: .semibold, design: .monospaced))
.foregroundStyle(Color.orange)
}
}
}
private var lengthLabel: some View {
Text(lengthText)
.font(.system(size: LooperTimelineView.labelFontSize, design: .monospaced))
.foregroundStyle(Color.white.opacity(LooperTimelineView.lengthLabelOpacity))
}
/// One-line description of what the pedal is doing now — replaces the old action-button row.
private var stateIndicator: some View {
Text(stateDescription)
.font(.system(size: LooperTimelineView.indicatorFontSize, weight: .medium))
.foregroundStyle(Color.white.opacity(LooperTimelineView.indicatorOpacity))
.frame(maxWidth: .infinity, alignment: .leading)
}
private var track: some View {
GeometryReader { geometry in
let width: CGFloat = geometry.size.width
let pos: CGFloat = CGFloat(position ?? 0.0)
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: LooperTimelineView.trackCornerRadius)
.fill(Color(white: LooperTimelineView.trackBrightness))
.overlay(
RoundedRectangle(cornerRadius: LooperTimelineView.trackCornerRadius)
.stroke(Color.white.opacity(LooperTimelineView.trackBorderOpacity),
lineWidth: LooperTimelineView.borderLineWidth)
)
if position != nil {
RoundedRectangle(cornerRadius: LooperTimelineView.trackCornerRadius)
.fill(Color.white.opacity(LooperTimelineView.filledOpacity))
.frame(width: max(0, pos * width))
Rectangle()
.fill(Color.white)
.frame(width: LooperTimelineView.playheadWidth)
.offset(x: min(max(0, pos * width), width - LooperTimelineView.playheadWidth))
}
}
}
.frame(height: LooperTimelineView.trackHeight)
}
// MARK: - State mapping
private var stateColor: Color {
switch state {
case .clear: return Color(white: 0.3)
case .armedRecord, .armedClose: return Color.orange
case .recordingMaster: return Color.red
case .playing: return Color.green
case .stopped: return Color.orange
}
}
private var stateText: String {
switch state {
case .clear: return "CLEAR"
case .armedRecord: return "ARM"
case .recordingMaster: return "REC"
case .armedClose: return "END"
case .playing: return "PLAY"
case .stopped: return "STOP"
}
}
private var lengthText: String {
guard lengthSeconds > 0 else { return "--" }
let seconds: String = String(format: "%.2fs", lengthSeconds)
guard lengthBeats > 0 else { return seconds }
return seconds + " · \(Int(lengthBeats)) beat" + (lengthBeats == 1 ? "" : "s")
}
}