1.0.0 release
This commit is contained in:
@@ -0,0 +1,275 @@
|
||||
// LyrebirdMessageView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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] {
|
||||
center.messages.filter { enabledTiers.contains($0.tier) }
|
||||
}
|
||||
|
||||
private var emptyStateText: String {
|
||||
if center.messages.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 center.messages 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
|
||||
Reference in New Issue
Block a user