// 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 selectedDotBrightness: Double = 0.32 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)) // Selected (or recorded) tracks show a bright letter; other empty tracks dim. .foregroundStyle((hasContent || isSelected) ? Color.white : Color.white.opacity(LooperTimelineView.dotEmptyOpacity)) .frame(width: LooperTimelineView.dotDiameter, height: LooperTimelineView.dotDiameter) .background( Circle().fill(hasContent ? Color.green.opacity(LooperTimelineView.dotFilledOpacity) : (isSelected ? Color(white: LooperTimelineView.selectedDotBrightness) : 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") } }