import SwiftUI import AppKit struct MenuPanelView: View { let metrics: MetricsStore let preferences: AppearancePreferences let keepAwake: KeepAwakeService @Environment(\.openSettings) private var openSettings var body: some View { VStack(spacing: 4) { VStack(spacing: 2) { MetricRow(title: "metric.cpu", symbol: "cpu", value: MetricFormat.percent(metrics.snapshot.cpu?.usage), usage: metrics.snapshot.cpu?.usage, history: metrics.history.samples.map(\.cpu), accent: preferences.tintColor) { cpuDetail } MetricRow(title: "metric.gpu", symbol: "square.grid.2x2", value: MetricFormat.percent(metrics.snapshot.gpuUsage), usage: metrics.snapshot.gpuUsage, history: metrics.history.samples.map(\.gpu), accent: preferences.tintColor) { Text("gpu.unavailable").foregroundStyle(.secondary) } MetricRow(title: "metric.memory", symbol: "memorychip", value: MetricFormat.percent(metrics.snapshot.memory?.usage), usage: metrics.snapshot.memory?.usage, history: metrics.history.samples.map(\.memory), accent: preferences.tintColor) { memoryDetail } } VStack(spacing: 2) { MetricRow(title: "metric.network", symbol: "network", value: networkValue, usage: metrics.snapshot.downloadRate.map { _ in 0 }, history: networkHistory, accent: preferences.tintColor) { ScrollView { VStack(spacing: 6) { switch metrics.snapshot.networkPath { case .available: Text("network.pathAvailable") case .unavailable: Text("network.pathUnavailable") case .requiresConnection: Text("network.pathRequiresConnection") case nil: Text("metric.waiting") } if let interfaces = metrics.snapshot.network { if interfaces.isEmpty { Text("network.noLink") } ForEach(interfaces, id: \.name) { item in VStack(spacing: 4) { Text(verbatim: item.name).fontWeight(.semibold) detailLine("network.download", rate(item.download)) detailLine("network.upload", rate(item.upload)) detailLine("network.received", MetricFormat.bytes(item.received)) detailLine("network.sent", MetricFormat.bytes(item.sent)) } } } else { Text("metric.waiting") } } }.frame(maxHeight: 280) } MetricRow(title: "metric.disk", symbol: "internaldrive", value: MetricFormat.percent(metrics.snapshot.disk?.usage), usage: metrics.snapshot.disk?.usage, history: metrics.history.samples.map(\.disk), accent: preferences.tintColor) { ScrollView { VStack(spacing: 6) { Text("disk.homeVolume").foregroundStyle(.secondary) detailLine("disk.total", MetricFormat.bytes(metrics.snapshot.disk?.total)) detailLine("memory.available", MetricFormat.bytes(metrics.snapshot.disk?.available)) detailLine("disk.remainingLife", MetricFormat.percent(metrics.snapshot.diskHealth?.remainingLife)) HStack { Text("disk.powerOnHours").foregroundStyle(.secondary) Spacer() if let hours = metrics.snapshot.diskHealth?.powerOnHours { Text("disk.hours \(hours)").monospacedDigit() } else { Text(verbatim: "--") } } Divider() Text("disk.physicalIO").foregroundStyle(.secondary) detailLine("disk.read", rate(metrics.snapshot.diskIO?.read)) detailLine("disk.write", rate(metrics.snapshot.diskIO?.written)) Divider() Text("disk.volumes").foregroundStyle(.secondary) if let volumes = metrics.snapshot.volumes { ForEach(volumes, id: \.path) { volume in VStack(spacing: 4) { Text(verbatim: volume.name).fontWeight(.semibold) detailLine("disk.total", MetricFormat.bytes(volume.capacity.total)) detailLine("memory.available", MetricFormat.bytes(volume.capacity.available)) } } } else { Text("metric.waiting") } } }.frame(maxHeight: 280) } if let battery = metrics.snapshot.battery { MetricRow(title: "metric.battery", symbol: battery.charging ? "battery.100percent.bolt" : "battery.100percent", value: MetricFormat.percent(battery.charge), usage: battery.charge.map { _ in 0 }, history: metrics.history.samples.map(\.battery), accent: preferences.tintColor) { VStack(spacing: 6) { Text(battery.charging ? "battery.charging" : battery.externalPower ? "battery.external" : "battery.discharging") if battery.externalPower && !battery.charging { HStack { Text("battery.timeEstimate").foregroundStyle(.secondary) Spacer() Text("battery.timeNotApplicable") } } else if let minutes = battery.minutesRemaining { detailLine(battery.charging ? "battery.timeFull" : "battery.timeEmpty", "\(minutes) min") } else { HStack { Text(battery.charging ? "battery.timeFull" : "battery.timeEmpty").foregroundStyle(.secondary) Spacer() Text("battery.timeUnknown") } } detailLine("battery.cycles", battery.cycles.map(String.init) ?? "--") detailLine("battery.health", MetricFormat.percent(battery.health)) } } } } Divider().padding(.horizontal, 10) HStack { Text("system.load") Text(verbatim: metrics.snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")) Spacer(minLength: 4) Image(systemName: "clock") Text(verbatim: String(format: "%.0fh", floor(metrics.snapshot.uptime / 3600))) } .font(.system(size: 10)).monospacedDigit().foregroundStyle(.secondary) .padding(.horizontal, 10).padding(.vertical, 8) footer } .padding(10) .frame(width: 320) .modifier(AppearanceStyle(preferences: preferences)) } // Shared scale for the whole visible history; never label throughput as utilization. private var networkHistory: [Double?] { let values = metrics.history.samples.map(\.download) let scale = max(1_000, values.compactMap { $0 }.max() ?? 1_000) return values.map { $0.map { $0 / scale } } } private var networkValue: String { guard let total = metrics.snapshot.downloadRate else { return "--" } return total >= 1_000_000 ? String(format: "%.1fM↓", total / 1_000_000) : String(format: "%.0fK↓", total / 1000) } private func rate(_ bytes: Double?) -> String { MetricFormat.rate(bytes) } @ViewBuilder private var cpuDetail: some View { if let cpu = metrics.snapshot.cpu { VStack(alignment: .leading, spacing: 8) { detailLine("cpu.user", MetricFormat.percent(cpu.user)) detailLine("cpu.system", MetricFormat.percent(cpu.system)) LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 4), count: 7), spacing: 4) { ForEach(Array(cpu.cores.enumerated()), id: \.offset) { index, usage in RoundedRectangle(cornerRadius: 4) .fill(preferences.tintColor.opacity(0.12 + 0.88 * usage)) .frame(height: 18) .overlay(Text(verbatim: "\(index + 1)").font(.system(size: 9)).foregroundStyle(.primary)) .help(MetricFormat.percent(usage)) .accessibilityLabel(Text("cpu.core")) .accessibilityValue(Text(verbatim: "\(index + 1): \(MetricFormat.percent(usage))")) } } } } else { Text("metric.waiting").foregroundStyle(.secondary) } } @ViewBuilder private var memoryDetail: some View { if let memory = metrics.snapshot.memory { VStack(spacing: 6) { detailLine("memory.used", MetricFormat.bytes(memory.used)) detailLine("memory.available", MetricFormat.bytes(memory.total - memory.used)) detailLine("memory.app", MetricFormat.bytes(memory.used - min(memory.used, memory.wired + memory.compressed))) detailLine("memory.wired", MetricFormat.bytes(memory.wired)) detailLine("memory.compressed", MetricFormat.bytes(memory.compressed)) detailLine("memory.swap", MetricFormat.bytes(memory.swapUsed)) HStack { Text("memory.pressure").foregroundStyle(.secondary) Spacer() switch memory.pressure { case 1: Text("pressure.normal") case 2: Text("pressure.warning").foregroundStyle(.orange) case 4: Text("pressure.critical").foregroundStyle(.red) default: Text(verbatim: "--") } } } } else { Text("metric.waiting").foregroundStyle(.secondary) } } private func detailLine(_ key: LocalizedStringKey, _ value: String) -> some View { HStack { Text(key).foregroundStyle(.secondary); Spacer(); Text(verbatim: value).monospacedDigit() } } private var footer: some View { HStack(spacing: 8) { VStack(alignment: .leading, spacing: 2) { Text(verbatim: "Kymo").font(.system(size: 12, weight: .semibold)) Text(verbatim: "v\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "--")") .font(.system(size: 9)).foregroundStyle(.tertiary) } Spacer() HStack(spacing: 2) { Button { keepAwake.toggle() } label: { Image(systemName: keepAwake.isEnabled ? "cup.and.saucer.fill" : "cup.and.saucer") .frame(width: 20, height: 20) .foregroundStyle(keepAwake.isEnabled ? preferences.tintColor : Color.primary) } .help(keepAwake.didFail ? Text("awake.failed") : (keepAwake.isEnabled ? Text("awake.disable") : Text("awake.enable"))) .accessibilityLabel(keepAwake.isEnabled ? Text("awake.disable") : Text("awake.enable")) .accessibilityValue(keepAwake.isEnabled ? Text("awake.on") : Text("awake.off")) Button { metrics.togglePause() } label: { Image(systemName: metrics.isPaused ? "play.fill" : "pause.fill").frame(width: 20, height: 20) } .help(metrics.isPaused ? Text("action.resume") : Text("action.pause")) .accessibilityLabel(metrics.isPaused ? Text("action.resume") : Text("action.pause")) Button { NSApp.activate(); openSettings() } label: { Image(systemName: "gearshape").frame(width: 20, height: 20) } .help(Text("action.settings")).accessibilityLabel(Text("action.settings")) Divider().frame(height: 18).padding(.horizontal, 3) Button { NSApp.terminate(nil) } label: { Image(systemName: "power").frame(width: 20, height: 20) } .help(Text("action.quit")).accessibilityLabel(Text("action.quit")) } .buttonStyle(FooterButtonStyle()) .padding(4) .glassEffect(.regular, in: .capsule) } .controlSize(.small) .padding(.horizontal, 8).padding(.vertical, 6) } } private struct FooterButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { FooterButtonBody(isPressed: configuration.isPressed) { configuration.label } } } private struct FooterButtonBody: View { let isPressed: Bool @ViewBuilder var content: () -> Content @State private var isHovered = false var body: some View { content() .frame(width: 34, height: 34) .background { Circle().fill(.primary.opacity(isPressed ? 0.16 : (isHovered ? 0.08 : 0))) } .contentShape(Circle()) .onHover { isHovered = $0 } } }