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 = {} @Environment(\.accessibilityReduceMotion) var reduceMotion @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 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..) -> 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) { header if PanelLayout.needsResourceScroll(size: panelSize, rows: rowHeights) { ScrollView { resourceRows } .frame(maxHeight: PanelLayout.resourceHeight(rows: rowHeights)) } else { resourceRows } if metrics.isPanelVisible { ProcessTableView(snapshot: snapshot.processes, wide: panelSize.width >= 900).frame(maxWidth: .infinity, maxHeight: .infinity) .frame(minHeight: 170) } } .padding(PanelStyle.padding) .frame(width: panelSize.width, height: panelSize.height) .background(PanelWindowObserver { size in if panelSize != size { panelSize = size } }.frame(width: 0, height: 0)) .onAppear { metrics.setPanelVisible(true) reservedFanCount = snapshot.fans?.count ?? 0 } .onChange(of: snapshot.fans?.count) { _, count in reservedFanCount = max(reservedFanCount, count ?? 0) } .onDisappear { metrics.setPanelVisible(false) } .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)) .modifier(AppearanceStyle(preferences: preferences)) } private var resourceRows: some View { VStack(spacing: PanelStyle.gap) { ForEach(rowHeights.indices, id: \.self) { row in HStack(spacing: PanelStyle.gap) { ForEach(0.. some View { switch index { case 0: cpuCard case 1: gpuCard case 2: batteryCard case 3: memoryCard case 4: diskCard case 5: networkCard default: EmptyView() } } private var header: some View { HStack(spacing: 10) { Image("BrandMark").renderingMode(.template) HStack(alignment: .firstTextBaseline, spacing: 2) { Text("Kymo").font(.system(size: 18, weight: .semibold)) Text(verbatim: "v\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "--")") .font(.system(size: 8, weight: .medium)) .foregroundStyle(.secondary) .baselineOffset(7) } if panelSize.width >= 900 { Rectangle() .fill(.secondary.opacity(0.32)) .frame(width: 1, height: 16) .accessibilityHidden(true) Text(verbatim: HardwareIdentity.displayName) .font(.system(size: 11)).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle) } Spacer(minLength: 8) controls }.padding(.horizontal, 4) } private var controls: some View { HStack(spacing: 8) { HStack(spacing: 2) { HStack(spacing: 4) { Image(systemName: "timer").help(Text("sampling.panel")) IntervalControl(milliseconds: Binding(get: { metrics.sampling.panelMilliseconds }, set: { metrics.sampling.panelMilliseconds = $0 })) }.padding(.horizontal, 6).accessibilityLabel(Text("sampling.panel")) Divider().frame(height: 18).padding(.horizontal, 3) Button { presentation.isPinned.toggle() } label: { Image(systemName: presentation.isPinned ? "pin.fill" : "pin") .frame(width: 20, height: 20) .foregroundStyle(presentation.isPinned ? preferences.tintColor : 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 ? 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.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) .glassEffect(.regular, 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 } } }