250 lines
11 KiB
Swift
250 lines
11 KiB
Swift
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(\.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<Date> { 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..<reservedFanCount).filter { id in !fans.contains { $0.id == id } }
|
|
return (fans + missingIDs.prefix(reservedFanCount - fans.count).map { FanMetrics(id: $0, rpm: nil) }).sorted { $0.id < $1.id }
|
|
}
|
|
var cardContentWidth: CGFloat { max(0, (measuredCardWidth ?? PanelLayout.cardWidth(size: panelSize)) - 20) }
|
|
var columns: Int { PanelLayout.columns(width: panelSize.width) }
|
|
private var rowHeights: [CGFloat] {
|
|
let groups = CPUCoreGrid.groups(count: snapshot.cpu?.cores.count ?? 0, kinds: CPUCoreTopology.cores)
|
|
let widths = CPUCoreGrid.groupWidths(groups: groups, width: max(0, (measuredCardWidth ?? PanelLayout.cardWidth(size: panelSize)) - 20))
|
|
let coreRows = zip(groups, widths).map { CPUCoreGridLayout.rowCounts(count: $0.0.count, width: $0.1).count }.max() ?? 0
|
|
return PanelLayout.rowHeights(columns: columns, coreRows: coreRows,
|
|
fanCount: max(reservedFanCount, snapshot.fans?.count ?? 0))
|
|
}
|
|
var network: [NetworkInterfaceMetrics] {
|
|
(snapshot.network ?? []).filter { selectedInterface.isEmpty ? $0.isIncludedInTotal : $0.identity == selectedInterface }
|
|
}
|
|
func total(_ key: KeyPath<NetworkInterfaceMetrics, Double?>) -> 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, rows: rowHeights) {
|
|
ScrollView { resourceRows }
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else {
|
|
resourceRows
|
|
.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))
|
|
.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 resourceRows: some View {
|
|
VStack(spacing: PanelStyle.gap) {
|
|
ForEach(rowHeights.indices, id: \.self) { row in
|
|
HStack(spacing: PanelStyle.gap) {
|
|
ForEach(0..<columns, id: \.self) { column in
|
|
resourceCard(at: row * columns + column)
|
|
.frame(maxWidth: .infinity)
|
|
.frame(height: rowHeights[row])
|
|
.onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width in
|
|
if row == 0 && column == 0 { measuredCardWidth = width }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder private func resourceCard(at index: Int) -> some View {
|
|
if PanelResourceAudit.omitted == "resources" {
|
|
Color.clear
|
|
} else {
|
|
switch index {
|
|
case 0: cpuCard
|
|
case 1: batteryCard
|
|
case 2: gpuCard
|
|
case 3: diskCard
|
|
case 4: memoryCard
|
|
case 5: networkCard
|
|
default: EmptyView()
|
|
}
|
|
}
|
|
}
|
|
|
|
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<Content: View>: 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) }
|
|
}
|