Files
Lyrebird/Sources/LyrebirdUI/LyrebirdMessageBanner.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

242 lines
8.2 KiB
Swift

// LyrebirdMessageBanner.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if canImport(SwiftUI)
import SwiftUI
import LyrebirdSynth
/// Compact banner showing the most recent few messages from a
/// `LyrebirdMessageCenter`.
///
/// Designed for embedding inside larger control panels (e.g.
/// ``LyrebirdEngineStatusView``). Renders up to ``lineCount`` most-recent
/// messages with a tier-coloured left edge stripe. Tapping the banner
/// invokes the optional ``onOpenFull`` callback so the host can open the
/// full ``LyrebirdMessageView`` in a window or sheet.
public struct LyrebirdMessageBanner: View {
@Bindable private var center: LyrebirdMessageCenter
/// Number of recent messages shown when present. Defaults to `3`.
public let lineCount: Int
/// Callback invoked when the user taps the banner. Typically opens the
/// full message view in a window. `nil` makes the banner non-interactive.
public let onOpenFull: (() -> Void)?
/// Creates a banner bound to `LyrebirdMessageCenter.shared`.
///
/// - Parameters:
/// - lineCount: How many recent messages to show. Defaults to `3`.
/// - onOpenFull: Tap action. Defaults to `nil`.
@MainActor
public init(
lineCount: Int = 3,
onOpenFull: (() -> Void)? = nil
) {
self.center = LyrebirdMessageCenter.shared
self.lineCount = lineCount
self.onOpenFull = onOpenFull
}
/// Creates a banner bound to the given center.
///
/// - Parameters:
/// - center: Center to observe.
/// - lineCount: How many recent messages to show. Defaults to `3`.
/// - onOpenFull: Tap action. Defaults to `nil`.
public init(
center: LyrebirdMessageCenter,
lineCount: Int = 3,
onOpenFull: (() -> Void)? = nil
) {
self.center = center
self.lineCount = lineCount
self.onOpenFull = onOpenFull
}
public var body: some View {
let recent: [LyrebirdMessage] = recentMessages
return Group {
if recent.isEmpty {
emptyState
} else {
content(recent: recent)
}
}
}
private var emptyState: some View {
HStack(spacing: 6) {
Image(systemName: "tray")
.font(.caption)
.foregroundStyle(.secondary)
Text("No messages")
.font(.caption)
.foregroundStyle(.secondary)
Spacer()
}
.padding(6)
.background(
RoundedRectangle(cornerRadius: 4)
.fill(Color.secondary.opacity(0.05))
)
}
private func content(recent: [LyrebirdMessage]) -> some View {
Button(action: { onOpenFull?() }) {
VStack(alignment: .leading, spacing: 3) {
ForEach(recent) { msg in
bannerRow(msg)
}
}
.padding(6)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 4)
.fill(Color.secondary.opacity(0.05))
)
}
.buttonStyle(.plain)
.disabled(onOpenFull == nil)
}
private func bannerRow(_ msg: LyrebirdMessage) -> some View {
HStack(alignment: .firstTextBaseline, spacing: 6) {
Capsule()
.fill(LyrebirdMessageTierStyle.color(for: msg.tier))
.frame(width: 3, height: 11)
Text(msg.text)
.font(.caption)
.lineLimit(1)
.truncationMode(.tail)
.foregroundStyle(.primary)
Spacer(minLength: 0)
}
}
private var recentMessages: [LyrebirdMessage] {
let all: [LyrebirdMessage] = center.messages
guard all.count > lineCount else { return all }
let start: Int = all.count - lineCount
return Array(all[start..<all.count])
}
}
#else
// Linux/GTK4 counterpart to `LyrebirdUI.LyrebirdMessageBanner`: a compact banner
// showing the most recent few messages from a `LyrebirdMessageCenter`, with a
// tier-coloured left stripe, that runs `onOpenFull` on tap. Live updates come
// from `@Bindable` on the `@Observable` center (reads inside the body are
// observation-tracked, same as the Apple side — no polling). SF-Symbol art and
// `RoundedRectangle` chrome are dropped; the layout is `HStack`/`VStack` + `Text`.
//
// SwiftOpenUI is always present in this target (see Package.swift).
import SwiftOpenUI
import LyrebirdSynth
/// A compact recent-messages banner for the Linux/GTK4 UI. Mirrors
/// `LyrebirdUI.LyrebirdMessageBanner`: up to ``lineCount`` recent messages with
/// a tier-coloured stripe; tapping invokes ``onOpenFull``.
public struct LyrebirdMessageBanner: View {
@Bindable private var center: LyrebirdMessageCenter
/// Number of recent messages shown when present. Defaults to `3`.
public let lineCount: Int
/// Tap action. `nil` makes the banner non-interactive.
public let onOpenFull: (() -> Void)?
// Presentation constants (points).
private let stripeWidth: Double = 3
private let stripeHeight: Double = 11
private let cornerRadiusPoints: Double = 4
/// Creates a banner bound to `LyrebirdMessageCenter.shared`.
///
/// - Parameters:
/// - lineCount: How many recent messages to show. Defaults to `3`.
/// - onOpenFull: Tap action. Defaults to `nil`.
@MainActor
public init(
lineCount: Int = 3,
onOpenFull: (() -> Void)? = nil
) {
self.center = LyrebirdMessageCenter.shared
self.lineCount = lineCount
self.onOpenFull = onOpenFull
}
/// Creates a banner bound to the given center.
///
/// - Parameters:
/// - center: Center to observe.
/// - lineCount: How many recent messages to show. Defaults to `3`.
/// - onOpenFull: Tap action. Defaults to `nil`.
public init(
center: LyrebirdMessageCenter,
lineCount: Int = 3,
onOpenFull: (() -> Void)? = nil
) {
self.center = center
self.lineCount = lineCount
self.onOpenFull = onOpenFull
}
public var body: some View {
// A single static Button keeps state changes on the backend's narrow
// in-place mutation path (see ParameterButton's Linux body for the
// rationale). The empty state and the populated rows both live inside
// the one button; `press()` re-reads at click time.
Button(action: { onOpenFull?() }) {
VStack(alignment: .leading, spacing: 3) {
let recent: [LyrebirdMessage] = recentMessages
if recent.isEmpty {
Text("No messages")
.font(.caption)
.foregroundStyle(.secondary)
} else {
ForEach(recent) { msg in
bannerRow(msg)
}
}
}
.padding(6)
}
.buttonStyle(.plain)
.background(Color.lyrebirdControlBackground)
.cornerRadius(cornerRadiusPoints)
.disabled(onOpenFull == nil)
}
private func bannerRow(_ msg: LyrebirdMessage) -> some View {
HStack(spacing: 6) {
Capsule()
.fill(LyrebirdMessageTierStyle.color(for: msg.tier))
.frame(width: stripeWidth, height: stripeHeight)
Text(msg.text)
.font(.caption)
.lineLimit(1)
.truncationMode(.tail)
Spacer(minLength: 0)
}
}
private var recentMessages: [LyrebirdMessage] {
// `LyrebirdMessageCenter` is @MainActor; the SwiftOpenUI body runs on the
// GTK main thread, so `assumeIsolated` reaches `messages` safely. Bind the
// center to a local first so the `@MainActor` closure captures the
// (Sendable) center, not `self` (the non-Sendable View).
let c: LyrebirdMessageCenter = center
let all: [LyrebirdMessage] = MainActor.assumeIsolated { c.messages }
guard all.count > lineCount else { return all }
let start: Int = all.count - lineCount
return Array(all[start..<all.count])
}
}
#endif