Files
Lyrebird/Sources/LyrebirdUI/LyrebirdEventListPerformanceView.swift
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

344 lines
11 KiB
Swift

// LyrebirdEventListPerformanceView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if canImport(SwiftUI)
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
#else
// Linux/GTK4 counterpart to `LyrebirdUI.LyrebirdEventListPerformanceView`: a
// ``LyrebirdEventListView`` on top plus a live stack of ``LyrebirdProcessView``
// strips, one per running process. The 10 Hz membership poll moves off
// SwiftOpenUI's off-main `.task` onto a `Timer` on `RunLoop.main` inside an
// `@Observable` model, keeping the identity-stable reassignment so child strips
// keep their own timers/state across ticks.
//
// SwiftOpenUI is always present in this target (see Package.swift).
import SwiftOpenUI
import LyrebirdSynth
import Observation
import Foundation
/// 10 Hz — matches ``LyrebirdProcessView`` / ``LyrebirdEventListView``.
private let linuxEventListPerformancePollIntervalSeconds: TimeInterval = 0.1
/// Polls an event list's running processes on a main-thread timer and publishes
/// an identity-stable snapshot for ``LyrebirdEventListPerformanceView``. Internal.
///
/// `@unchecked Sendable`: main-thread-confined; the `Timer` block is `@Sendable`,
/// same rationale as `LyrebirdScopeModel`.
@Observable
final class LinuxEventListPerformanceModel: @unchecked Sendable {
var runningProcesses: [LyrebirdProcess] = []
@ObservationIgnored private var timer: Timer?
@ObservationIgnored private let eventList: LyrebirdEventList
init(eventList: LyrebirdEventList) {
self.eventList = eventList
}
deinit {
timer?.invalidate()
}
func start() {
guard timer == nil else { return }
refresh()
let t: Timer = Timer.scheduledTimer(
withTimeInterval: linuxEventListPerformancePollIntervalSeconds,
repeats: true
) { [weak self] _ in
self?.refresh()
}
timer = t
}
func stop() {
timer?.invalidate()
timer = nil
}
func refresh() {
let snapshot: [LyrebirdProcess] = eventList.allProcesses.filter { $0.isRunning }
if !snapshot.elementsEqualByIdentity(to: runningProcesses) {
runningProcesses = snapshot
}
}
}
/// A composite event-list performance view for the Linux/GTK4 UI. Mirrors
/// `LyrebirdUI.LyrebirdEventListPerformanceView`; same public API/init as the
/// Apple side.
public struct LyrebirdEventListPerformanceView: View {
public let eventList: LyrebirdEventList
public let engine: SynthesisEngine
@State private var model: LinuxEventListPerformanceModel
/// 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
_model = State(wrappedValue: LinuxEventListPerformanceModel(eventList: eventList))
}
public var body: some View {
VStack(alignment: .leading, spacing: 12) {
LyrebirdEventListView(eventList: eventList)
Divider()
if model.runningProcesses.isEmpty {
emptyState
} else {
ScrollView {
VStack(alignment: .leading, spacing: 8) {
ForEach(model.runningProcesses, id: \.identityKey) { proc in
LyrebirdProcessView(process: proc, engine: engine)
}
}
}
}
}
.padding(12)
.background(Color.lyrebirdControlBackground)
.frame(minWidth: 360, minHeight: 320)
.onAppear { model.start() }
.onDisappear { model.stop() }
}
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)
}
}
// MARK: - Stable identity for ForEach
private extension LyrebirdProcess {
/// `ObjectIdentifier`-based key used for `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 (Linux)
extension LyrebirdEventListPerformanceView {
/// Opens a `LyrebirdWindow` hosting an event-list performance view. Requires
/// a running `GtkApplication` loop.
///
/// - 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.
@discardableResult
public static func presentInWindow(
eventList: LyrebirdEventList,
engine: SynthesisEngine,
title: String? = nil,
width: Double = 420,
height: Double = 520
) -> LyrebirdWindow {
let win: LyrebirdWindow = LyrebirdWindow(
title: title ?? eventList.id,
width: width,
height: height
)
win.open {
LyrebirdEventListPerformanceView(eventList: eventList, engine: engine)
}
return win
}
}
#endif