import SwiftUI import AppKit struct MenuPanelView: View { let metrics: MetricsStore let preferences: AppearancePreferences let keepAwake: KeepAwakeService let settingsPresentation: SettingsPresentation var presentation = PanelPresentation() var onOpenSettings: () -> Void = {} var onPreferredHeight: (CGFloat) -> Void = { _ in } @Environment(\.locale) var locale @State private var panelSize = PanelLayout.contentSize(visible: NSScreen.main?.visibleFrame ?? CGRect(x: 0, y: 0, width: 1000, height: 760)) @State var selectedInterface = "" @State var publicIPAddress: String? @State var showVolumes = false @State private var processState = ProcessTableState() var snapshot: MetricsSnapshot { metrics.snapshot } var samples: [TrendSample] { metrics.history.samples } var chartIntervals: [TimeInterval] { samples.map(\.expectedInterval) } var chartInterval: TimeInterval { Double(metrics.effectiveMilliseconds) / 1000 } var chartBoundaries: Set { Set(samples.filter(\.breakBefore).map(\.date)) } @State private var reservedFanCount = 0 @State private var measuredCardWidth: CGFloat? var displayedFans: [FanMetrics] { let fans = snapshot.fans ?? [] guard fans.count < reservedFanCount else { return fans } let missingIDs = (0.. 0 ? max(reservedFanCount, snapshot.fans?.count ?? 0) : snapshot.fans?.count return PanelLayout.cardHeights(coreRows: coreRows, fanCount: fanCount) } private var preferredHeight: CGFloat { PanelLayout.preferredHeight(width: panelSize.width, heights: cardHeights) } var network: [NetworkInterfaceMetrics] { (snapshot.network ?? []).filter { selectedInterface.isEmpty ? $0.isIncludedInTotal : $0.identity == selectedInterface } } func total(_ key: KeyPath) -> Double? { let rates = network.compactMap { $0[keyPath: key] } return !network.isEmpty && rates.count == network.count ? rates.reduce(0, +) : nil } var body: some View { VStack(spacing: PanelStyle.gap) { if PanelResourceAudit.omitted != "header" { header } if presentation.page == .overview { if PanelLayout.needsResourceScroll(size: panelSize, heights: cardHeights) { ScrollView { resourceColumns } .frame(maxWidth: .infinity, maxHeight: .infinity) } else { resourceColumns .frame(maxHeight: .infinity, alignment: .top) } } else if presentation.isOpen && PanelResourceAudit.omitted != "processes" { PanelProcessSection(metrics: metrics, state: processState, wide: panelSize.width >= 800) .frame(maxWidth: .infinity, maxHeight: .infinity) } footer } .padding(PanelStyle.padding) .frame(width: panelSize.width, height: panelSize.height) .background(PanelWindowObserver { size in if panelSize != size { panelSize = size } }.frame(width: 0, height: 0)) .task(id: preferredHeight) { // Resize only for structural changes, outside the current layout pass. await Task.yield() guard !Task.isCancelled else { return } onPreferredHeight(preferredHeight) } .onAppear { reservedFanCount = snapshot.fans?.count ?? 0 } .onChange(of: snapshot.fans?.count) { _, count in reservedFanCount = max(reservedFanCount, count ?? 0) } .onChange(of: snapshot.network?.map(\.identity)) { _, keys in if !selectedInterface.isEmpty && !(keys ?? []).contains(selectedInterface) { selectedInterface = "" } } .background(Color("PanelBackground"), in: .rect(cornerRadius: 18)) .clipShape(.rect(cornerRadius: 18)) .transaction { $0.animation = nil; $0.disablesAnimations = true } .modifier(AppearanceStyle(preferences: preferences)) } private var resourceColumns: some View { let groups = PanelLayout.cardColumns(columns: columns) return HStack(alignment: .top, spacing: PanelStyle.gap) { ForEach(groups.indices, id: \.self) { column in VStack(spacing: PanelStyle.gap) { ForEach(groups[column], id: \.self) { card in resourceCard(card) .frame(maxWidth: .infinity) .frame(height: cardHeights[card]) .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width in if card == .cpu { measuredCardWidth = width } } } } .frame(maxWidth: .infinity, alignment: .top) } } } @ViewBuilder private func resourceCard(_ card: PanelCard) -> some View { if PanelResourceAudit.omitted == "resources" { Color.clear } else { switch card { case .cpu: cpuCard case .battery: batteryCard case .gpu: gpuCard case .storage: diskCard case .memory: memoryCard case .network: networkCard } } } private var brand: some View { HStack(spacing: 6) { Image("BrandMark").renderingMode(.template).resizable().scaledToFit() .frame(width: 22, height: 22) Text("Kymo").font(.system(size: 17, weight: .semibold)) } } private var pagePicker: some View { NeutralTabPicker(title: "panel.page", selection: Binding(get: { presentation.page }, set: { presentation.page = $0 }), options: [(.overview, "panel.overview"), (.processes, "panel.processes")]) .frame(width: 164) .accessibilityIdentifier("panel.tabs") } private var header: some View { VStack(spacing: 4) { HStack(spacing: 8) { brand Spacer(minLength: 8) controls } .frame(height: 32) .overlay { if panelSize.width >= 560 { pagePicker } } if panelSize.width < 560 { pagePicker.frame(maxWidth: .infinity).frame(height: 28) } } .frame(height: PanelLayout.headerHeight(width: panelSize.width)) .padding(.horizontal, 4) } private var footer: some View { HStack { Text(verbatim: HardwareIdentity.displayName) .lineLimit(1).truncationMode(.middle) .help(Text(verbatim: HardwareIdentity.displayName)) Spacer(minLength: 8) HStack(spacing: 4) { Text("panel.uptime") MetricText(value: String(format: "%.1f h", snapshot.uptime / 3600)) }.fixedSize() Divider().frame(height: 14) HStack(spacing: 4) { Image(systemName: "timer").help(Text("sampling.panel")) IntervalControl(milliseconds: Binding(get: { metrics.sampling.panelMilliseconds }, set: { metrics.sampling.panelMilliseconds = $0 })) } .foregroundStyle(.primary) .accessibilityLabel(Text("sampling.panel")) } .font(PanelStyle.label).foregroundStyle(.secondary) .frame(height: PanelLayout.footerHeight).padding(.horizontal, 4) } private var controls: some View { HStack(spacing: 8) { HStack(spacing: 2) { Button { presentation.isPinned.toggle() } label: { Image(systemName: presentation.isPinned ? "pin.fill" : "pin") .frame(width: 20, height: 20) .foregroundStyle(presentation.isPinned ? Color.blue : Color.primary) } .help(Text(presentation.isPinned ? "panel.unpin" : "panel.pin")) .accessibilityLabel(Text(presentation.isPinned ? "panel.unpin" : "panel.pin")) .accessibilityValue(Text(presentation.isPinned ? "panel.pinned" : "panel.unpinned")) Button { keepAwake.toggle() } label: { Image(systemName: keepAwake.isEnabled ? "cup.and.saucer.fill" : "cup.and.saucer") .frame(width: 20, height: 20) .foregroundStyle(keepAwake.isEnabled ? Color.blue : 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.setActivationPolicy(.regular) settingsPresentation.request() onOpenSettings() } 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(PanelControlButtonStyle()) .padding(4) .background(.primary.opacity(0.04), in: .capsule) } .controlSize(.small) .padding(.horizontal, 2).padding(.vertical, 0) } } private struct PanelControlButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { PanelControlButtonBody(isPressed: configuration.isPressed) { configuration.label } } } private struct PanelControlButtonBody: View { let isPressed: Bool @ViewBuilder var content: () -> Content @State private var isHovered = false var body: some View { content() .frame(width: 26, height: 24) .background { Circle().fill(.primary.opacity(isPressed ? 0.16 : (isHovered ? 0.08 : 0))) } .contentShape(Circle()) .onHover { isHovered = $0 } } } /// Keep process publications out of the resource-card observation scope. private struct PanelProcessSection: View { let metrics: MetricsStore let state: ProcessTableState let wide: Bool var body: some View { ProcessTableView(snapshot: metrics.processSnapshot, energy: metrics.energy, state: state, wide: wide) } }