b6fdee8bfc
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>
382 lines
13 KiB
Swift
382 lines
13 KiB
Swift
// LyrebirdRunningProcessesView.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#if canImport(SwiftUI)
|
|
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..<a.count where a[i] !== b[i] { return false }
|
|
return true
|
|
}
|
|
}
|
|
|
|
// MARK: - ObjectIdentifier helper
|
|
|
|
private extension LyrebirdProcess {
|
|
/// Stable identity for SwiftUI `ForEach`. `LyrebirdProcess` is a class, so
|
|
/// `ObjectIdentifier` is the natural identity; not added to the core module
|
|
/// because `ObjectIdentifier` conformance is a UI concern.
|
|
var lyrebirdObjectID: ObjectIdentifier { ObjectIdentifier(self) }
|
|
}
|
|
|
|
// MARK: - Standalone window (macOS)
|
|
|
|
#if os(macOS)
|
|
extension LyrebirdRunningProcessesView {
|
|
|
|
/// Opens a ``LyrebirdWindow`` hosting a running-processes panel.
|
|
///
|
|
/// Use when no parent SwiftUI view is available — a script, REPL, or CLI
|
|
/// demo can call this to surface per-process transport in its own window.
|
|
/// The returned ``LyrebirdWindow`` retains the AppKit window; close it
|
|
/// with `.close()`.
|
|
///
|
|
/// - Parameters:
|
|
/// - eventList: The `LyrebirdEventList` to track.
|
|
/// - engine: The `SynthesisEngine` forwarded to each child
|
|
/// ``LyrebirdProcessView``.
|
|
/// - title: Window title bar text. Defaults to `"<eventList.id> — 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
|
|
|
|
#else
|
|
// Linux/GTK4 counterpart to `LyrebirdUI.LyrebirdRunningProcessesView`: one
|
|
// ``LyrebirdProcessView`` per currently-running `LyrebirdProcess` in an event
|
|
// list. The 10 Hz refresh moves off SwiftOpenUI's off-main `.task` onto a `Timer`
|
|
// on `RunLoop.main` inside an `@Observable` model. The identity-stable
|
|
// reassignment (only swap the array when the running set changes) is preserved so
|
|
// child rows keep their own poll timers and per-row 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``.
|
|
private let linuxRunningProcessesPollIntervalSeconds: TimeInterval = 0.1
|
|
|
|
/// Polls an event list's running processes on a main-thread timer and publishes
|
|
/// an identity-stable snapshot for ``LyrebirdRunningProcessesView``. Internal.
|
|
///
|
|
/// `@unchecked Sendable`: main-thread-confined; the `Timer` block is `@Sendable`,
|
|
/// same rationale as `LyrebirdScopeModel`.
|
|
@Observable
|
|
final class LinuxRunningProcessesModel: @unchecked Sendable {
|
|
|
|
var running: [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: linuxRunningProcessesPollIntervalSeconds,
|
|
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 !LinuxRunningProcessesModel.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..<a.count where a[i] !== b[i] { return false }
|
|
return true
|
|
}
|
|
}
|
|
|
|
/// A panel of the currently-running processes in an event list for the Linux/GTK4
|
|
/// UI. Mirrors `LyrebirdUI.LyrebirdRunningProcessesView`; same public API/init.
|
|
public struct LyrebirdRunningProcessesView: View {
|
|
|
|
public let eventList: LyrebirdEventList
|
|
public let engine: SynthesisEngine
|
|
|
|
@State private var model: LinuxRunningProcessesModel
|
|
|
|
/// Creates a running-processes panel bound to the given event list.
|
|
///
|
|
/// - Parameters:
|
|
/// - eventList: The `LyrebirdEventList` whose running processes are shown.
|
|
/// - engine: The `SynthesisEngine` forwarded to each child
|
|
/// ``LyrebirdProcessView`` for its **Start** button.
|
|
public init(
|
|
eventList: LyrebirdEventList,
|
|
engine: SynthesisEngine
|
|
) {
|
|
self.eventList = eventList
|
|
self.engine = engine
|
|
_model = State(wrappedValue: LinuxRunningProcessesModel(eventList: eventList))
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
header
|
|
Divider()
|
|
if model.running.isEmpty {
|
|
emptyRow
|
|
} else {
|
|
ForEach(model.running, id: \.lyrebirdObjectID) { proc in
|
|
LyrebirdProcessView(process: proc, engine: engine)
|
|
}
|
|
}
|
|
}
|
|
.padding(12)
|
|
.background(Color.lyrebirdControlBackground)
|
|
.frame(minWidth: 320)
|
|
.onAppear { model.start() }
|
|
.onDisappear { model.stop() }
|
|
}
|
|
|
|
// MARK: - Sub-rows
|
|
|
|
private var header: some View {
|
|
HStack(spacing: 8) {
|
|
Text("Running")
|
|
.font(.headline)
|
|
Spacer()
|
|
Text("\(model.running.count)")
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
|
|
private var emptyRow: some View {
|
|
Text("No running processes")
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
}
|
|
|
|
// MARK: - ObjectIdentifier helper
|
|
|
|
private extension LyrebirdProcess {
|
|
/// Stable identity for `ForEach`. `LyrebirdProcess` is a class, so
|
|
/// `ObjectIdentifier` is the natural identity; not added to the core module
|
|
/// because `ObjectIdentifier` conformance is a UI concern.
|
|
var lyrebirdObjectID: ObjectIdentifier { ObjectIdentifier(self) }
|
|
}
|
|
|
|
// MARK: - Standalone window (Linux)
|
|
|
|
extension LyrebirdRunningProcessesView {
|
|
|
|
/// Opens a ``LyrebirdWindow`` hosting a running-processes panel. Requires a
|
|
/// running `GtkApplication` loop.
|
|
///
|
|
/// - Parameters:
|
|
/// - eventList: The `LyrebirdEventList` to track.
|
|
/// - engine: The `SynthesisEngine` forwarded to each child
|
|
/// ``LyrebirdProcessView``.
|
|
/// - title: Window title-bar text. Defaults to `"<eventList.id> — 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.
|
|
@discardableResult
|
|
public static func presentInWindow(
|
|
eventList: LyrebirdEventList,
|
|
engine: SynthesisEngine,
|
|
title: String? = nil,
|
|
width: Double = 360,
|
|
height: Double = 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
|