// LyrebirdEventListPerformanceView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdSynth /// A composite performance view: ``LyrebirdEventListView`` on top, plus a /// dynamically-populated stack of ``LyrebirdProcessView`` strips for every /// process currently running in the list. /// /// As the user fires events (or as an external timeline advances the list), /// processes start and stop. This view polls /// `LyrebirdEventList.allProcesses` at 10 Hz, filters by /// `LyrebirdProcess.isRunning`, and renders a control strip for each. When /// a process finishes (envelope reaches its natural end, `LyrebirdEventList` /// releases it, etc.) its strip disappears on the next poll. /// /// ## Hosting /// /// Embed inside any SwiftUI parent: /// /// ```swift /// LyrebirdEventListPerformanceView(eventList: list, engine: engine) /// .padding() /// ``` /// /// On macOS, `presentInWindow(eventList:engine:title:width:height:)` opens a /// standalone `LyrebirdWindow`: /// /// ```swift /// let win = LyrebirdEventListPerformanceView.presentInWindow( /// eventList: list, engine: engine /// ) /// // … later … /// win.close() /// ``` public struct LyrebirdEventListPerformanceView: View { public let eventList: LyrebirdEventList public let engine: SynthesisEngine @State private var runningProcesses: [LyrebirdProcess] = [] private static let pollIntervalNs: UInt64 = 100_000_000 /// Creates a performance view bound to the given event list. /// /// - Parameters: /// - eventList: The `LyrebirdEventList` to drive. /// - engine: The `SynthesisEngine` that hosts both the list and any /// ad-hoc `Start` actions on individual process strips. public init( eventList: LyrebirdEventList, engine: SynthesisEngine ) { self.eventList = eventList self.engine = engine } public var body: some View { VStack(alignment: .leading, spacing: 12) { LyrebirdEventListView(eventList: eventList) Divider() if runningProcesses.isEmpty { emptyState } else { ScrollView { LazyVStack(alignment: .leading, spacing: 8) { ForEach(runningProcesses, id: \.identityKey) { proc in LyrebirdProcessView(process: proc, engine: engine) } } } } } .padding(12) .background(Color.lyrebirdControlBackground) .frame(minWidth: 360, minHeight: 320) .task { while !Task.isCancelled { refresh() try? await Task.sleep(nanoseconds: LyrebirdEventListPerformanceView.pollIntervalNs) } } } private var emptyState: some View { VStack(alignment: .leading, spacing: 4) { Text("No processes running") .font(.caption) .foregroundStyle(.secondary) Text("Press Next to fire the queued event.") .font(.caption2) .foregroundStyle(.secondary) } .frame(maxWidth: .infinity, alignment: .leading) } private func refresh() { let snapshot: [LyrebirdProcess] = eventList.allProcesses.filter { $0.isRunning } // Avoid issuing identity-changes on every poll when the membership is // unchanged — cheaper for SwiftUI's diffing pass. if !snapshot.elementsEqualByIdentity(to: runningProcesses) { runningProcesses = snapshot } } } // MARK: - Stable identity for ForEach private extension LyrebirdProcess { /// `ObjectIdentifier`-based key used for SwiftUI's `ForEach` so processes /// with duplicate user-supplied `id` strings still render distinct views. var identityKey: ObjectIdentifier { ObjectIdentifier(self) } } private extension Array where Element == LyrebirdProcess { func elementsEqualByIdentity(to other: [LyrebirdProcess]) -> Bool { guard count == other.count else { return false } for (a, b) in zip(self, other) where a !== b { return false } return true } } // MARK: - Standalone window (macOS) #if os(macOS) extension LyrebirdEventListPerformanceView { /// Opens a `LyrebirdWindow` hosting an event-list performance view. /// /// - Parameters: /// - eventList: The `LyrebirdEventList` to drive. /// - engine: The `SynthesisEngine` for individual process Start actions. /// - title: Window title bar text. Defaults to the event list ID. /// - width: Initial window width in points. Defaults to `420`. /// - height: Initial window height in points. Defaults to `520`. /// - Returns: The `LyrebirdWindow` hosting the view. Caller retains it to /// keep the window alive. @MainActor @discardableResult public static func presentInWindow( eventList: LyrebirdEventList, engine: SynthesisEngine, title: String? = nil, width: CGFloat = 420, height: CGFloat = 520 ) -> LyrebirdWindow { let win: LyrebirdWindow = LyrebirdWindow( title: title ?? eventList.id, width: width, height: height ) win.open { LyrebirdEventListPerformanceView(eventList: eventList, engine: engine) } return win } } #endif