// LyrebirdRunningProcessesView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdSynth /// A panel that renders one ``LyrebirdProcessView`` per currently-running /// `LyrebirdProcess` owned by a `LyrebirdEventList`. /// /// Processes appear when they start (transition to running/releasing) and /// disappear when they finish (auto-free or are killed). Identity is tracked /// via `ObjectIdentifier`, not by `LyrebirdProcess.id`, so two processes that /// share an `id` string still render as distinct rows. /// /// The list of running processes is refreshed on a 10 Hz poll loop, matching /// the cadence used by ``LyrebirdProcessView`` and ``LyrebirdEventListView``. /// The poll only re-assigns the `@State` snapshot when the set of running /// identities changes — otherwise each child ``LyrebirdProcessView`` keeps its /// own polling task running uninterrupted and per-row UI state (slider drag, /// focus) survives ticks. /// /// ## Hosting /// /// Embed inside any SwiftUI parent that already has an event list and engine /// in scope: /// /// ```swift /// VStack(alignment: .leading, spacing: 12) { /// if let eventList = controller.eventList { /// LyrebirdEventListView(eventList: eventList) /// LyrebirdRunningProcessesView(eventList: eventList, engine: controller.engine) /// Spacer() /// } /// } /// ``` /// /// On macOS, ``presentInWindow(eventList:engine:title:width:height:)`` opens /// a standalone ``LyrebirdWindow``. /// /// ## Threading /// /// The view body and refresh loop run on the main thread. /// `LyrebirdEventList.allProcesses` and `LyrebirdProcess.isRunning` are /// documented as safe to read from any thread — the main-thread read here is /// trivially valid. public struct LyrebirdRunningProcessesView: View { public let eventList: LyrebirdEventList public let engine: SynthesisEngine /// Snapshot of currently-running processes. Identity-stable across refresh /// ticks: only re-assigned when the set of running identities changes. @State private var running: [LyrebirdProcess] = [] /// 10 Hz — matches ``LyrebirdProcessView`` and ``LyrebirdEventListView``. private static let pollIntervalNs: UInt64 = 100_000_000 /// Creates a running-processes panel bound to the given event list. /// /// - Parameters: /// - eventList: The `LyrebirdEventList` whose running processes will be /// rendered. Only processes registered with this list are visible — /// standalone processes (those not owned by an event list) do not /// appear here. /// - engine: The `SynthesisEngine` forwarded to each child /// ``LyrebirdProcessView`` for its **Start** button. public init( eventList: LyrebirdEventList, engine: SynthesisEngine ) { self.eventList = eventList self.engine = engine } public var body: some View { VStack(alignment: .leading, spacing: 8) { header Divider() if running.isEmpty { emptyRow } else { ForEach(running, id: \.lyrebirdObjectID) { proc in LyrebirdProcessView(process: proc, engine: engine) } } } .padding(12) .background(Color.lyrebirdControlBackground) .frame(minWidth: 320) .task { // Seed once before the poll loop so the panel is correct on first // appearance (e.g. when re-hosted while processes are already running). refresh() while !Task.isCancelled { refresh() try? await Task.sleep(nanoseconds: LyrebirdRunningProcessesView.pollIntervalNs) } } } // MARK: - Sub-rows private var header: some View { HStack(spacing: 8) { Text("Running") .font(.headline) Spacer() Text("\(running.count)") .font(.caption.monospaced()) .foregroundStyle(.secondary) } } private var emptyRow: some View { Text("No running processes") .font(.caption.monospaced()) .foregroundStyle(.secondary) .frame(maxWidth: .infinity, alignment: .leading) } // MARK: - Refresh private func refresh() { let snapshot: [LyrebirdProcess] = eventList.allProcesses.filter { $0.isRunning } if !LyrebirdRunningProcessesView.sameIdentities(snapshot, running) { running = snapshot } } /// `true` when two arrays contain the same `LyrebirdProcess` instances in /// the same order. Identity-only — does not inspect process state. private static func sameIdentities( _ a: [LyrebirdProcess], _ b: [LyrebirdProcess] ) -> Bool { guard a.count == b.count else { return false } for i: Int in 0.. — Running"`. /// - width: Initial window width in points. Defaults to `360`. /// - height: Initial window height in points. Defaults to `420`. /// - 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 = 360, height: CGFloat = 420 ) -> LyrebirdWindow { let win: LyrebirdWindow = LyrebirdWindow( title: title ?? "\(eventList.id) — Running", width: width, height: height ) win.open { LyrebirdRunningProcessesView(eventList: eventList, engine: engine) } return win } } #endif