SPB Git

Repository Public

MenuBar.swift 6.0 KB 124 lines swift

1import MCCCore2import SwiftUI34/// Menu bar icon: its shape changes with the state (never colour alone) and VoiceOver reads the summary.5struct MenuBarLabel: View {6    let app: AppModel7    var body: some View {8        let glance = ClusterGlance(session: app.session)9        let symbol: String = {10            if app.session.phase != .signedIn { return "server.rack" }11            if app.session.isOffline { return "bolt.slash" }12            if glance.worstSeverity == .critical { return "exclamationmark.octagon" }13            if glance.firing > 0 || (glance.nodes?.offline ?? 0) > 0 { return "exclamationmark.triangle" }14            return "server.rack"15        }()16        Label {17            Text(glance.firing > 0 ? "\(glance.firing)" : "")18        } icon: {19            Image(systemName: symbol)20        }21        .labelStyle(.titleAndIcon)22        .accessibilityLabel("MacLustr Command Center")23        .accessibilityValue(app.session.phase == .signedIn ? glance.spoken : tr("menubar.signed_out"))24    }25}2627/// Cluster health at a glance; works with the main window closed.28struct MenuBarView: View {29    @Environment(AppModel.self) private var app30    @Environment(\.mccTheme) private var theme3132    var body: some View {33        VStack(alignment: .leading, spacing: Tokens.Space.s4) {34            HStack {35                Text("MacLustr Command Center").font(.headline).foregroundStyle(theme.textPrimary)36                Spacer()37                if app.session.phase == .signedIn {38                    let glance = ClusterGlance(session: app.session)39                    StatusBadge(glance.connectionLabel, tone: glance.connectionTone, compact: true)40                }41            }42            if app.session.phase == .signedIn { signedIn } else { signedOut }43            Divider()44            HStack {45                Button(tr("menubar.open")) { app.show(app.selection) }.keyboardShortcut("o")46                Spacer()47                Button(tr("menubar.quit")) { NSApp.terminate(nil) }.keyboardShortcut("q")48            }49        }50        .padding(Tokens.Space.s5)51        .frame(width: 340)52        .background(theme.surface)53        .task { await refresh() }54    }5556    private func refresh() async {57        guard app.session.phase == .signedIn else { return }58        await app.session.cluster.overview.loadIfNeeded(maxAge: 30)59        await app.session.nodes.list.loadIfNeeded(maxAge: 30)60        if app.session.can("alerts.read") { await app.session.alerts.alerts.loadIfNeeded(maxAge: 30) }61    }6263    @ViewBuilder private var signedOut: some View {64        Text(tr("menubar.signed_out")).font(.callout).foregroundStyle(theme.textSecondary)65    }6667    @ViewBuilder private var signedIn: some View {68        let glance = ClusterGlance(session: app.session)69        if let n = glance.nodes {70            VStack(alignment: .leading, spacing: Tokens.Space.s2) {71                Text(tr("menubar.nodes")).font(.caption).foregroundStyle(theme.textSecondary)72                HStack(spacing: Tokens.Space.s3) {73                    StatusBadge("\(n.fresh) \(API.Freshness.fresh.label.lowercased())", tone: .ok, compact: true)74                    StatusBadge("\(n.stale) \(API.Freshness.stale.label.lowercased())", tone: n.stale > 0 ? .stale : .neutral, compact: true)75                    StatusBadge("\(n.offline) \(API.Freshness.offline.label.lowercased())", tone: n.offline > 0 ? .offline : .neutral, compact: true)76                }77                if n.maintenance > 0 { StatusLabel(tr("menubar.maintenance", n.maintenance), tone: .maintenance).font(.caption) }78            }79            .accessibilityElement(children: .combine)80        } else {81            Text(tr("menubar.nodes.unknown")).font(.callout).foregroundStyle(theme.textSecondary)82        }83        VStack(alignment: .leading, spacing: Tokens.Space.s2) {84            Text(tr("menubar.alerts")).font(.caption).foregroundStyle(theme.textSecondary)85            if !glance.alertsKnown {86                Text(tr("glance.alerts.unknown")).font(.callout).foregroundStyle(theme.textTertiary)87            } else if glance.firing == 0 {88                StatusLabel(tr("menubar.alerts.none"), tone: .ok).font(.callout)89            } else {90                HStack(spacing: Tokens.Space.s3) {91                    ForEach([API.Severity.critical, .warning, .info], id: \.self) { sev in92                        if let c = glance.firingBySeverity[sev], c > 0 { StatusBadge("\(c) \(sev.label.lowercased())", tone: sev.tone, compact: true) }93                    }94                }95                ForEach((app.session.alerts.alerts.value ?? []).filter { $0.state == .firing }.sorted { $0.severity.rank > $1.severity.rank }.prefix(4), id: \.id) { alert in96                    Button { app.show(.alerts, focus: .alert(alert.id)) } label: {97                        HStack(spacing: Tokens.Space.s2) {98                            Image(systemName: alert.severity.tone.symbol).foregroundStyle(alert.severity.tone.color(theme)).accessibilityHidden(true)99                            Text([alert.nodeAlias, alert.summary].compactMap { $0 }.joined(separator: " — ")).lineLimit(1)100                            Spacer(minLength: 0)101                        }102                    }103                    .buttonStyle(.plain)104                    .font(.callout)105                    .accessibilityLabel("\(alert.severity.label), \(alert.nodeAlias ?? ""), \(alert.summary)")106                }107            }108        }109        HStack(spacing: Tokens.Space.s3) {110            Button(tr("nav.alerts")) { app.show(.alerts) }111            Button(tr("nav.nodes")) { app.show(.nodes) }112            Button(tr("nav.maintenance")) { app.show(.maintenance) }113        }114        .controlSize(.small)115        if let t = glance.lastUpdate {116            Text(tr("glance.updated", Format.time(t, seconds: true))).font(.caption).foregroundStyle(theme.textTertiary)117                .accessibilityIdentifier("menubar-updated")118        }119        if let since = app.session.offlineSince {120            Text(tr("offline.banner.since", Format.time(since))).font(.caption).foregroundStyle(theme.textTertiary)121        }122    }123}124
Files main
Menu

Theme