// 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 label, a loop-length read-out, and a track /// with a sweeping play-head. 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 — rather /// than relying on SwiftUI change tracking. 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 emptyLedBrightness: Double = 0.3 private static let borderLineWidth: CGFloat = 1 // MARK: - Properties private let pedal: LooperPedal @State private var position: Double? @State private var state: LooperTransportState = .empty @State private var lengthSeconds: Double = 0.0 @State private var lengthBeats: Double = 0.0 init(pedal: LooperPedal) { self.pedal = pedal } // MARK: - Body var body: some View { VStack(spacing: LooperTimelineView.sectionSpacing) { HStack(spacing: LooperTimelineView.sectionSpacing) { stateBadge Spacer(minLength: 0) lengthLabel } track } .task { // Poll the pedal's transport read-outs; it isn't @Observable (RT-adjacent state). while !Task.isCancelled { position = pedal.playbackPosition01() state = pedal.transportState lengthSeconds = pedal.loopLengthSeconds lengthBeats = pedal.loopLengthBeats try? await Task.sleep(nanoseconds: LooperTimelineView.pollIntervalNanoseconds) } } } // MARK: - Subviews 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) } } private var lengthLabel: some View { Text(lengthText) .font(.system(size: LooperTimelineView.labelFontSize, design: .monospaced)) .foregroundStyle(Color.white.opacity(LooperTimelineView.lengthLabelOpacity)) } 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) ) // Filled region up to the play-head, only while playing. 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 .empty: return Color(white: LooperTimelineView.emptyLedBrightness) case .armedRecord, .armedStop: return Color.orange case .recording: return Color.red case .playing: return Color.green } } private var stateText: String { switch state { case .empty: return "EMPTY" case .armedRecord: return "ARM" case .recording: return "REC" case .armedStop: return "END" case .playing: return "PLAY" } } 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") } }