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>
576 lines
18 KiB
Swift
576 lines
18 KiB
Swift
// LyrebirdMessageView.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#if canImport(SwiftUI)
|
|
import SwiftUI
|
|
import LyrebirdSynth
|
|
|
|
/// Full scrollable view over a `LyrebirdMessageCenter` ring.
|
|
///
|
|
/// Top bar: per-tier filter toggles (with live counts), Clear button, total
|
|
/// count badge. Body: a scrolling list of rows showing timestamp, tier chip,
|
|
/// optional source caption, and message text.
|
|
///
|
|
/// ## Hosting
|
|
///
|
|
/// Embed inside any SwiftUI parent:
|
|
///
|
|
/// ```swift
|
|
/// LyrebirdMessageView()
|
|
/// .padding()
|
|
/// ```
|
|
///
|
|
/// Or bind to a non-shared center:
|
|
///
|
|
/// ```swift
|
|
/// LyrebirdMessageView(center: myCenter)
|
|
/// ```
|
|
///
|
|
/// On macOS, `presentInWindow(...)` opens a standalone `LyrebirdWindow`.
|
|
///
|
|
/// ## Threading
|
|
///
|
|
/// `LyrebirdMessageCenter` is `@MainActor`; SwiftUI view bodies run on the
|
|
/// main actor too, so all reads are safe.
|
|
public struct LyrebirdMessageView: View {
|
|
|
|
@Bindable private var center: LyrebirdMessageCenter
|
|
|
|
@State private var enabledTiers: Set<LyrebirdMessageTier> =
|
|
Set(LyrebirdMessageTier.allCases)
|
|
|
|
/// Creates a message view bound to `LyrebirdMessageCenter.shared`.
|
|
@MainActor
|
|
public init() {
|
|
self.center = LyrebirdMessageCenter.shared
|
|
}
|
|
|
|
/// Creates a message view bound to the given center.
|
|
///
|
|
/// - Parameter center: The center to observe.
|
|
public init(center: LyrebirdMessageCenter) {
|
|
self.center = center
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
headerRow
|
|
filterRow
|
|
Divider()
|
|
listBody
|
|
}
|
|
.padding(12)
|
|
.background(Color.lyrebirdControlBackground)
|
|
.frame(minWidth: 360, minHeight: 200)
|
|
}
|
|
|
|
// MARK: - Sub-rows
|
|
|
|
private var headerRow: some View {
|
|
HStack(spacing: 8) {
|
|
Text("Messages")
|
|
.font(.headline)
|
|
Spacer()
|
|
Text("\(center.messages.count)")
|
|
.font(.caption.monospaced())
|
|
.foregroundStyle(.secondary)
|
|
Button("Clear") { center.clear() }
|
|
.disabled(center.messages.isEmpty)
|
|
}
|
|
}
|
|
|
|
/// Current messages. On Apple the body is `@MainActor`, so this is a plain
|
|
/// read (the Linux counterpart hops via `assumeIsolated`).
|
|
private var messagesSnapshot: [LyrebirdMessage] {
|
|
center.messages
|
|
}
|
|
|
|
private var filterRow: some View {
|
|
HStack(spacing: 8) {
|
|
ForEach(LyrebirdMessageTier.allCases, id: \.self) { tier in
|
|
TierFilterChip(
|
|
tier: tier,
|
|
count: count(of: tier),
|
|
isEnabled: enabledTiers.contains(tier),
|
|
toggle: { toggle(tier) }
|
|
)
|
|
}
|
|
Spacer()
|
|
}
|
|
}
|
|
|
|
private var listBody: some View {
|
|
let visible: [LyrebirdMessage] = filteredMessages
|
|
return Group {
|
|
if visible.isEmpty {
|
|
Text(emptyStateText)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
|
|
} else {
|
|
ScrollViewReader { proxy in
|
|
ScrollView {
|
|
LazyVStack(alignment: .leading, spacing: 4) {
|
|
ForEach(visible) { msg in
|
|
MessageRow(message: msg)
|
|
.id(msg.id)
|
|
}
|
|
}
|
|
.padding(.vertical, 4)
|
|
}
|
|
.onChange(of: visible.last?.id) { _, newest in
|
|
if let newest: UUID = newest {
|
|
withAnimation(.linear(duration: 0.12)) {
|
|
proxy.scrollTo(newest, anchor: .bottom)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private var filteredMessages: [LyrebirdMessage] {
|
|
messagesSnapshot.filter { enabledTiers.contains($0.tier) }
|
|
}
|
|
|
|
private var emptyStateText: String {
|
|
if messagesSnapshot.isEmpty { return "No messages." }
|
|
return "No messages match the current filter."
|
|
}
|
|
|
|
private func count(of tier: LyrebirdMessageTier) -> Int {
|
|
var n: Int = 0
|
|
for msg: LyrebirdMessage in messagesSnapshot where msg.tier == tier {
|
|
n += 1
|
|
}
|
|
return n
|
|
}
|
|
|
|
private func toggle(_ tier: LyrebirdMessageTier) {
|
|
if enabledTiers.contains(tier) {
|
|
enabledTiers.remove(tier)
|
|
} else {
|
|
enabledTiers.insert(tier)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Sub-views
|
|
|
|
private struct TierFilterChip: View {
|
|
let tier: LyrebirdMessageTier
|
|
let count: Int
|
|
let isEnabled: Bool
|
|
let toggle: () -> Void
|
|
|
|
var body: some View {
|
|
Button(action: toggle) {
|
|
HStack(spacing: 4) {
|
|
Circle()
|
|
.fill(LyrebirdMessageTierStyle.color(for: tier))
|
|
.frame(width: 8, height: 8)
|
|
.opacity(isEnabled ? 1.0 : 0.35)
|
|
Text(LyrebirdMessageTierStyle.shortLabel(for: tier))
|
|
.font(.caption.monospaced())
|
|
Text("\(count)")
|
|
.font(.caption2.monospaced())
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.padding(.horizontal, 6)
|
|
.padding(.vertical, 3)
|
|
.background(
|
|
RoundedRectangle(cornerRadius: 4)
|
|
.fill(isEnabled
|
|
? LyrebirdMessageTierStyle.color(for: tier).opacity(0.15)
|
|
: Color.secondary.opacity(0.08))
|
|
)
|
|
.opacity(isEnabled ? 1.0 : 0.55)
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
}
|
|
|
|
private struct MessageRow: View {
|
|
let message: LyrebirdMessage
|
|
|
|
var body: some View {
|
|
HStack(alignment: .firstTextBaseline, spacing: 8) {
|
|
Text(MessageRow.formatTime(message.timestamp))
|
|
.font(.caption.monospaced())
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: 80, alignment: .leading)
|
|
Capsule()
|
|
.fill(LyrebirdMessageTierStyle.color(for: message.tier))
|
|
.frame(width: 3, height: 14)
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
if let src: String = message.source, !src.isEmpty {
|
|
Text(src)
|
|
.font(.caption2.monospaced())
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
Text(message.text)
|
|
.font(.callout)
|
|
.foregroundStyle(.primary)
|
|
.textSelection(.enabled)
|
|
}
|
|
Spacer(minLength: 0)
|
|
}
|
|
.padding(.horizontal, 4)
|
|
}
|
|
|
|
private static let timeFormatter: DateFormatter = {
|
|
let f: DateFormatter = DateFormatter()
|
|
f.dateFormat = "HH:mm:ss.SSS"
|
|
return f
|
|
}()
|
|
|
|
private static func formatTime(_ date: Date) -> String {
|
|
timeFormatter.string(from: date)
|
|
}
|
|
}
|
|
|
|
// MARK: - Standalone window (macOS)
|
|
|
|
#if os(macOS)
|
|
extension LyrebirdMessageView {
|
|
|
|
/// Opens a `LyrebirdWindow` hosting a message view bound to the shared
|
|
/// `LyrebirdMessageCenter`.
|
|
///
|
|
/// - Parameters:
|
|
/// - title: Window title bar text. Defaults to `"Messages"`.
|
|
/// - width: Initial width in points. Defaults to `460`.
|
|
/// - height: Initial height in points. Defaults to `360`.
|
|
/// - Returns: The `LyrebirdWindow` hosting the view. Caller retains it to
|
|
/// keep the window alive.
|
|
@MainActor
|
|
@discardableResult
|
|
public static func presentInWindow(
|
|
title: String = "Messages",
|
|
width: CGFloat = 460,
|
|
height: CGFloat = 360
|
|
) -> LyrebirdWindow {
|
|
presentInWindow(
|
|
center: LyrebirdMessageCenter.shared,
|
|
title: title,
|
|
width: width,
|
|
height: height
|
|
)
|
|
}
|
|
|
|
@MainActor
|
|
@discardableResult
|
|
public static func presentInWindow(
|
|
center: LyrebirdMessageCenter,
|
|
title: String = "Messages",
|
|
width: CGFloat = 460,
|
|
height: CGFloat = 360
|
|
) -> LyrebirdWindow {
|
|
let win: LyrebirdWindow = LyrebirdWindow(title: title, width: width, height: height)
|
|
win.open {
|
|
LyrebirdMessageView(center: center)
|
|
}
|
|
return win
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#else
|
|
// Linux/GTK4 counterpart to `LyrebirdUI.LyrebirdMessageView`: a full scrollable
|
|
// view over a `LyrebirdMessageCenter` ring — a header (title, count, Clear), a
|
|
// per-tier filter row with live counts, and a scrolling list of rows. Live
|
|
// updates come from `@Bindable` on the `@Observable` center (body reads are
|
|
// observation-tracked, same as the Apple side). Text is drawn with real `Text`
|
|
// widgets, so the readout is complete.
|
|
//
|
|
// Dropped vs the Apple side (document per project rule): the auto-scroll to the
|
|
// newest row (`ScrollViewReader`/`onChange`/`withAnimation`) and per-row text
|
|
// selection are omitted; the list simply scrolls. `presentInWindow` opens a
|
|
// detached GTK window (needs a running `GtkApplication`).
|
|
//
|
|
// SwiftOpenUI is always present in this target (see Package.swift).
|
|
import SwiftOpenUI
|
|
import LyrebirdSynth
|
|
import Foundation // DateFormatter for the row timestamp
|
|
|
|
/// Full scrollable view over a `LyrebirdMessageCenter` ring for the Linux/GTK4
|
|
/// UI. Mirrors `LyrebirdUI.LyrebirdMessageView` (minus auto-scroll and text
|
|
/// selection); same public API/init as the Apple side.
|
|
public struct LyrebirdMessageView: View {
|
|
|
|
@Bindable private var center: LyrebirdMessageCenter
|
|
|
|
@State private var enabledTiers: Set<LyrebirdMessageTier> =
|
|
Set(LyrebirdMessageTier.allCases)
|
|
|
|
// Presentation constants (points).
|
|
private let minPanelWidth: Double = 360
|
|
private let minPanelHeight: Double = 200
|
|
private let timeColumnWidth: Double = 80
|
|
private let stripeWidth: Double = 3
|
|
private let stripeHeight: Double = 14
|
|
|
|
/// Creates a message view bound to `LyrebirdMessageCenter.shared`.
|
|
@MainActor
|
|
public init() {
|
|
self.center = LyrebirdMessageCenter.shared
|
|
}
|
|
|
|
/// Creates a message view bound to the given center.
|
|
///
|
|
/// - Parameter center: The center to observe.
|
|
public init(center: LyrebirdMessageCenter) {
|
|
self.center = center
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
headerRow
|
|
filterRow
|
|
Divider()
|
|
listBody
|
|
}
|
|
.padding(12)
|
|
.background(Color.lyrebirdControlBackground)
|
|
.frame(minWidth: minPanelWidth, minHeight: minPanelHeight)
|
|
}
|
|
|
|
// MARK: - Sub-rows
|
|
|
|
// `LyrebirdMessageCenter` is @MainActor; the SwiftOpenUI body / actions run on
|
|
// the GTK main thread, so `assumeIsolated` reaches `messages` / `clear()`
|
|
// safely. The center is bound to a local first so the `@MainActor` closure
|
|
// captures the (Sendable) center rather than `self` (the non-Sendable View).
|
|
private var messagesSnapshot: [LyrebirdMessage] {
|
|
let c: LyrebirdMessageCenter = center
|
|
return MainActor.assumeIsolated { c.messages }
|
|
}
|
|
|
|
private var headerRow: some View {
|
|
let all: [LyrebirdMessage] = messagesSnapshot
|
|
let c: LyrebirdMessageCenter = center
|
|
return HStack(spacing: 8) {
|
|
Text("Messages")
|
|
.font(.headline)
|
|
Spacer()
|
|
Text("\(all.count)")
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
// action:label: form — the string-title Button's action does not
|
|
// fire on GTK4 here (same as the EngineStatus Stop button).
|
|
Button {
|
|
MainActor.assumeIsolated { c.clear() }
|
|
} label: {
|
|
Text("Clear")
|
|
}
|
|
.disabled(all.isEmpty)
|
|
}
|
|
}
|
|
|
|
private var filterRow: some View {
|
|
HStack(spacing: 8) {
|
|
ForEach(LyrebirdMessageTier.allCases, id: \.self) { tier in
|
|
TierFilterChip(
|
|
tier: tier,
|
|
count: count(of: tier),
|
|
isEnabled: enabledTiers.contains(tier),
|
|
toggle: { toggle(tier) }
|
|
)
|
|
}
|
|
Spacer()
|
|
}
|
|
}
|
|
|
|
private var listBody: some View {
|
|
let visible: [LyrebirdMessage] = filteredMessages
|
|
return ScrollView {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
if visible.isEmpty {
|
|
Text(emptyStateText)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
} else {
|
|
ForEach(visible) { msg in
|
|
MessageRow(
|
|
message: msg,
|
|
timeColumnWidth: timeColumnWidth,
|
|
stripeWidth: stripeWidth,
|
|
stripeHeight: stripeHeight
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private var filteredMessages: [LyrebirdMessage] {
|
|
messagesSnapshot.filter { enabledTiers.contains($0.tier) }
|
|
}
|
|
|
|
private var emptyStateText: String {
|
|
if messagesSnapshot.isEmpty { return "No messages." }
|
|
return "No messages match the current filter."
|
|
}
|
|
|
|
private func count(of tier: LyrebirdMessageTier) -> Int {
|
|
var n: Int = 0
|
|
for msg: LyrebirdMessage in messagesSnapshot where msg.tier == tier {
|
|
n += 1
|
|
}
|
|
return n
|
|
}
|
|
|
|
private func toggle(_ tier: LyrebirdMessageTier) {
|
|
if enabledTiers.contains(tier) {
|
|
enabledTiers.remove(tier)
|
|
} else {
|
|
enabledTiers.insert(tier)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Sub-views
|
|
|
|
private struct TierFilterChip: View {
|
|
let tier: LyrebirdMessageTier
|
|
let count: Int
|
|
let isEnabled: Bool
|
|
let toggle: () -> Void
|
|
|
|
private let dotSize: Double = 8
|
|
private let cornerRadiusPoints: Double = 4
|
|
private let disabledDotOpacity: Double = 0.35
|
|
private let disabledChipOpacity: Double = 0.55
|
|
|
|
var body: some View {
|
|
Button(action: toggle) {
|
|
HStack(spacing: 4) {
|
|
Circle()
|
|
.fill(LyrebirdMessageTierStyle.color(for: tier))
|
|
.frame(width: dotSize, height: dotSize)
|
|
.opacity(isEnabled ? 1.0 : disabledDotOpacity)
|
|
Text(LyrebirdMessageTierStyle.shortLabel(for: tier))
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
Text("\(count)")
|
|
.font(.caption2)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.padding(.horizontal, 6)
|
|
.padding(.vertical, 3)
|
|
}
|
|
.buttonStyle(.plain)
|
|
.background(Color.lyrebirdControlBackground)
|
|
.cornerRadius(cornerRadiusPoints)
|
|
.opacity(isEnabled ? 1.0 : disabledChipOpacity)
|
|
}
|
|
}
|
|
|
|
private struct MessageRow: View {
|
|
let message: LyrebirdMessage
|
|
let timeColumnWidth: Double
|
|
let stripeWidth: Double
|
|
let stripeHeight: Double
|
|
|
|
var body: some View {
|
|
HStack(spacing: 8) {
|
|
Text(MessageRow.formatTime(message.timestamp))
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: timeColumnWidth, alignment: .leading)
|
|
Capsule()
|
|
.fill(LyrebirdMessageTierStyle.color(for: message.tier))
|
|
.frame(width: stripeWidth, height: stripeHeight)
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
if let src: String = message.source, !src.isEmpty {
|
|
Text(src)
|
|
.font(.caption2)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
Text(message.text)
|
|
.font(.callout)
|
|
.foregroundStyle(.primary)
|
|
}
|
|
Spacer(minLength: 0)
|
|
}
|
|
.padding(.horizontal, 4)
|
|
}
|
|
|
|
private static let timeFormatter: DateFormatter = {
|
|
let f: DateFormatter = DateFormatter()
|
|
f.dateFormat = "HH:mm:ss.SSS"
|
|
return f
|
|
}()
|
|
|
|
private static func formatTime(_ date: Date) -> String {
|
|
timeFormatter.string(from: date)
|
|
}
|
|
}
|
|
|
|
// MARK: - Standalone window (Linux)
|
|
|
|
extension LyrebirdMessageView {
|
|
|
|
/// Opens a `LyrebirdWindow` hosting a message view bound to the shared
|
|
/// `LyrebirdMessageCenter`. Requires a running `GtkApplication` loop.
|
|
///
|
|
/// - Important: Call on the GTK main thread (as the "Open Window" buttons and
|
|
/// any top-level demo/`main()` code do). It reaches the `@MainActor`
|
|
/// `LyrebirdMessageCenter.shared` via `assumeIsolated`, which traps if
|
|
/// invoked off the main thread — from a background/script thread, use the
|
|
/// `presentInWindow(center:...)` overload with an explicitly-resolved center.
|
|
///
|
|
/// - Parameters:
|
|
/// - title: Window title-bar text. Defaults to `"Messages"`.
|
|
/// - width: Initial width in points. Defaults to `460`.
|
|
/// - height: Initial height in points. Defaults to `360`.
|
|
/// - Returns: The `LyrebirdWindow` hosting the view.
|
|
@discardableResult
|
|
public static func presentInWindow(
|
|
title: String = "Messages",
|
|
width: Double = 460,
|
|
height: Double = 360
|
|
) -> LyrebirdWindow {
|
|
// `LyrebirdMessageCenter.shared` is `@MainActor`; this runs on the GTK
|
|
// main thread, so `assumeIsolated` reaches it (no `self` captured — no
|
|
// sending-race), same hop the messages section uses.
|
|
let center: LyrebirdMessageCenter = MainActor.assumeIsolated {
|
|
LyrebirdMessageCenter.shared
|
|
}
|
|
return presentInWindow(
|
|
center: center,
|
|
title: title,
|
|
width: width,
|
|
height: height
|
|
)
|
|
}
|
|
|
|
/// Opens a `LyrebirdWindow` hosting a message view bound to `center`.
|
|
@discardableResult
|
|
public static func presentInWindow(
|
|
center: LyrebirdMessageCenter,
|
|
title: String = "Messages",
|
|
width: Double = 460,
|
|
height: Double = 360
|
|
) -> LyrebirdWindow {
|
|
let win: LyrebirdWindow = LyrebirdWindow(title: title, width: width, height: height)
|
|
win.open {
|
|
LyrebirdMessageView(center: center)
|
|
}
|
|
return win
|
|
}
|
|
}
|
|
#endif
|