// LyrebirdMessageBanner.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT 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..