303 lines
15 KiB
Swift
303 lines
15 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
extension MenuPanelView {
|
|
func line(_ key: LocalizedStringKey, _ value: String) -> some View {
|
|
HStack(alignment: .firstTextBaseline) {
|
|
Text(key).font(PanelStyle.label).foregroundStyle(.secondary)
|
|
Spacer(minLength: 6)
|
|
AnimatedMetricText(value: value).font(PanelStyle.value)
|
|
}.frame(height: 14)
|
|
}
|
|
|
|
func batteryPowerLabel(_ battery: BatteryMetrics) -> LocalizedStringKey {
|
|
if battery.charging { return "battery.chargePower" }
|
|
if !battery.externalPower { return "battery.dischargePower" }
|
|
return "battery.power"
|
|
}
|
|
|
|
var cpuCard: some View {
|
|
let temperature = snapshot.cpuTemperature
|
|
let highTemperature = CPUTemperatureMonitor.isHighTemperature(temperature)
|
|
return ResourceCard(
|
|
title: "metric.cpu", symbol: "cpu", color: Color("MetricCPU"),
|
|
value: MetricFormat.percent(snapshot.cpu?.usage),
|
|
status: temperature.map { "\(Int($0.rounded()))°C" },
|
|
statusWarning: highTemperature,
|
|
statusHelp: temperature == nil ? nil : Text("cpu.temperature.help", tableName: "CPUDetails"),
|
|
warningText: highTemperature ? Text("cpu.temperature.high", tableName: "CPUDetails") : nil
|
|
) {
|
|
PanelTrendChart(
|
|
dates: samples.map(\.date), first: samples.map(\.cpu),
|
|
second: samples.map(\.cpuUser), third: samples.map(\.cpuSystem),
|
|
color: Color("MetricCPU"), secondColor: Color("SeriesUser"), thirdColor: Color("SeriesSystem"),
|
|
firstLabel: "gpu.totalUtilization", secondLabel: "cpu.user", thirdLabel: "cpu.system", expectedInterval: chartInterval,
|
|
boundaryDates: chartBoundaries, intervals: chartIntervals)
|
|
CardSeparator()
|
|
if let cpu = snapshot.cpu {
|
|
Spacer(minLength: 0)
|
|
CPUCoreGrid(
|
|
values: cpu.cores, kinds: CPUCoreTopology.cores, interval: chartInterval,
|
|
availableWidth: cardContentWidth)
|
|
|
|
} else {
|
|
Text("metric.waiting").foregroundStyle(.secondary)
|
|
}
|
|
Spacer(minLength: 0)
|
|
CardSeparator()
|
|
HStack(spacing: 10) {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
HStack {
|
|
Text("cpu.load.oneMinute", tableName: "CPUDetails").foregroundStyle(.secondary)
|
|
Spacer(minLength: 3)
|
|
AnimatedMetricText(
|
|
value: snapshot.load.first.map { "\(MetricFormat.decimal($0)) / \(snapshot.cpu?.cores.count ?? 0)" } ?? "--")
|
|
}
|
|
MetricProgressBar(
|
|
value: (snapshot.load.first ?? 0) / Double(max(1, snapshot.cpu?.cores.count ?? 0)), color: Color("MetricCPU")
|
|
)
|
|
.frame(height: 5).accessibilityHidden(true)
|
|
}.frame(maxWidth: .infinity).help(Text("cpu.load.help", tableName: "CPUDetails"))
|
|
Rectangle().fill(.primary.opacity(0.08)).frame(width: 1, height: 25)
|
|
VStack(alignment: .leading, spacing: 3) {
|
|
Text("panel.uptime").foregroundStyle(.secondary)
|
|
AnimatedMetricText(value: String(format: "%.1f h", snapshot.uptime / 3600))
|
|
.fontWeight(.medium)
|
|
}
|
|
}.font(PanelStyle.label).lineLimit(1)
|
|
|
|
}
|
|
}
|
|
var gpuCard: some View {
|
|
let summary = GPUUtilizationSummary(samples: samples)
|
|
return ResourceCard(
|
|
title: "metric.gpu", symbol: "square.grid.2x2", color: Color("MetricGPU"), value: MetricFormat.percent(snapshot.gpuUsage),
|
|
status: snapshot.gpuTemperature.map { "\(Int($0.rounded()))°C" },
|
|
statusHelp: Text("gpu.temperatureHelp"),
|
|
detail: snapshot.gpu?.coreCount.map { String(format: String(localized: "gpu.coreCountFormat"), $0) }
|
|
) {
|
|
PanelTrendChart(
|
|
dates: samples.map(\.date), first: samples.map(\.gpu),
|
|
second: samples.map(\.gpuRenderer), third: samples.map(\.gpuTiler),
|
|
color: Color("MetricGPU"), secondColor: Color("MetricCPU"), thirdColor: Color("SeriesSystem"),
|
|
firstLabel: "gpu.totalUtilization", secondLabel: "gpu.renderer", thirdLabel: "gpu.tiler",
|
|
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
|
|
CardSeparator()
|
|
gpuActivity("gpu.rendererActivity", value: snapshot.gpu?.renderer, color: Color("MetricCPU"))
|
|
gpuActivity("gpu.tilerActivity", value: snapshot.gpu?.tiler, color: Color("SeriesSystem"))
|
|
CardSeparator()
|
|
Spacer(minLength: 0)
|
|
GPUStatisticsView(
|
|
inUse: snapshot.gpu?.inUseMemory, allocated: snapshot.gpu?.allocatedMemory,
|
|
average: summary.average, peak: summary.peak
|
|
)
|
|
.help(snapshot.gpuUsage == nil ? Text("gpu.unavailable") : Text("gpu.summaryHelp"))
|
|
Spacer(minLength: 0)
|
|
let fans = displayedFans
|
|
if !fans.isEmpty {
|
|
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 10), count: min(2, fans.count)), spacing: 5) {
|
|
ForEach(fans) { fan in
|
|
FanSpeedIndicator(
|
|
title: fans.count == 2
|
|
? (fan.id == 0 ? "gpu.fanLeft" : "gpu.fanRight")
|
|
: LocalizedStringKey("\(String(localized: "gpu.fan")) \(fan.id + 1)"), rpm: fan.rpm)
|
|
}
|
|
}
|
|
.modifier(CardInset())
|
|
}
|
|
}
|
|
}
|
|
|
|
func gpuActivity(_ title: LocalizedStringKey, value: Double?, color: Color) -> some View {
|
|
HStack(spacing: 8) {
|
|
Text(title).font(PanelStyle.label).frame(width: 100, alignment: .leading).help(Text(title))
|
|
MetricProgressBar(value: value ?? 0, color: color).frame(height: 5)
|
|
.animation(reduceMotion ? nil : .easeInOut(duration: min(0.35, chartInterval * 0.8)), value: value)
|
|
AnimatedMetricText(value: MetricFormat.percent(value)).font(PanelStyle.value).frame(width: 30, alignment: .trailing)
|
|
}.font(PanelStyle.label).help(Text("gpu.pipelineHelp"))
|
|
}
|
|
var memoryCard: some View {
|
|
ResourceCard(
|
|
title: "metric.memory", symbol: "memorychip", color: Color("MemoryApplication"),
|
|
value: snapshot.memory.map {
|
|
String(format: "%.1f / %.0f GiB", Double($0.used) / 1_073_741_824, Double($0.total) / 1_073_741_824)
|
|
} ?? "--"
|
|
) {
|
|
PanelTrendChart(
|
|
dates: samples.map(\.date),
|
|
first: samples.map { $0.memory.map { $0 * Double(snapshot.memory?.total ?? 1) } },
|
|
second: samples.map(\.memoryApp), third: samples.map(\.memoryWired), fourth: samples.map(\.memoryCompressed),
|
|
color: Color("MetricCPU"), secondColor: Color("MemoryApplication"),
|
|
thirdColor: Color("MemoryWired"), fourthColor: Color("MemoryCompressed"), fixedScale: Double(snapshot.memory?.total ?? 1),
|
|
scaleLabel: MetricFormat.memory(snapshot.memory?.total), firstValueFormat: .memory, firstLabel: "gpu.totalUtilization",
|
|
secondLabel: "memory.app", thirdLabel: "memory.wired", fourthLabel: "memory.compressed",
|
|
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
|
|
if let memory = snapshot.memory {
|
|
MemoryDetailsView(memory: memory)
|
|
} else {
|
|
CardSeparator()
|
|
Text("metric.waiting").foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
}
|
|
}
|
|
var diskCard: some View {
|
|
ResourceCard(
|
|
title: "metric.disk", symbol: "internaldrive", color: Color("MetricDisk"),
|
|
value: snapshot.disk.map {
|
|
String(
|
|
format: "%.0f / %.0f GB", Double($0.total - min($0.total, $0.available)) / 1_000_000_000,
|
|
Double($0.total) / 1_000_000_000)
|
|
} ?? "--"
|
|
) {
|
|
|
|
PanelTrendChart(
|
|
dates: samples.map(\.date), first: samples.map(\.read), second: samples.map(\.write), color: Color("MetricDisk"),
|
|
secondColor: Color("MetricNetwork"), fixedScale: nil, firstLabel: "disk.read", secondLabel: "disk.write",
|
|
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals
|
|
)
|
|
.help(Text("disk.physicalIO"))
|
|
StorageDetailsView(disk: snapshot.disk, health: snapshot.diskHealth) {
|
|
showVolumes.toggle()
|
|
}
|
|
.popover(isPresented: $showVolumes) {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Text("disk.homeVolume")
|
|
Text("panel.smartScope").foregroundStyle(.secondary)
|
|
Group {
|
|
ForEach(snapshot.volumes ?? [], id: \.path) { volume in
|
|
VStack(alignment: .leading) {
|
|
Text(verbatim: volume.name).lineLimit(1).truncationMode(.middle)
|
|
line(
|
|
"memory.available",
|
|
"\(MetricFormat.bytes(volume.capacity.available)) / \(MetricFormat.bytes(volume.capacity.total))")
|
|
}
|
|
}
|
|
Text("panel.apfsScope").font(.system(size: 9)).foregroundStyle(.secondary)
|
|
}
|
|
}.padding(12).frame(width: 330)
|
|
}
|
|
|
|
}
|
|
}
|
|
var batteryCard: some View {
|
|
ResourceCard(
|
|
title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"),
|
|
value: MetricFormat.percent(snapshot.battery?.charge)
|
|
) {
|
|
PanelTrendChart(
|
|
dates: samples.map(\.date),
|
|
first: samples.map(\.battery),
|
|
second: samples.map(\.batteryPower),
|
|
color: Color("MetricBattery"),
|
|
secondColor: Color("SeriesUser"),
|
|
fixedScale: 1,
|
|
secondSeriesUsesAutonomousScale: true,
|
|
secondValueFormat: .watts,
|
|
firstLabel: "metric.battery",
|
|
secondLabel: snapshot.battery.map(batteryPowerLabel) ?? "battery.power",
|
|
expectedInterval: chartInterval,
|
|
boundaryDates: chartBoundaries,
|
|
intervals: chartIntervals
|
|
)
|
|
CardSeparator()
|
|
if let battery = snapshot.battery {
|
|
batteryDetails(battery)
|
|
} else {
|
|
Text(snapshot.batteryState == .absent ? "panel.noBattery" : "panel.batteryFailed").foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
}
|
|
}
|
|
private func batteryDetails(_ battery: BatteryMetrics) -> some View {
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
HStack(alignment: .top, spacing: 10) {
|
|
VStack(spacing: 4) {
|
|
line("battery.health", MetricFormat.percent(battery.health))
|
|
HStack {
|
|
Text("battery.powerSource").foregroundStyle(.secondary)
|
|
Spacer(minLength: 4)
|
|
Text(battery.externalPower ? "battery.acPower" : "battery.batterySource").font(PanelStyle.value)
|
|
}.frame(height: 14)
|
|
}
|
|
Rectangle().fill(.primary.opacity(0.08)).frame(width: 1, height: 32)
|
|
VStack(spacing: 4) {
|
|
line("battery.cycles", battery.cycles.map(String.init) ?? "--")
|
|
HStack {
|
|
Text("battery.timeEstimate").foregroundStyle(.secondary).help(Text("battery.timeEstimate"))
|
|
Spacer(minLength: 4)
|
|
if battery.externalPower {
|
|
Text("battery.timeNotApplicable").font(PanelStyle.value).help(Text("battery.timeNotApplicable"))
|
|
} else if let minutes = battery.minutesRemaining {
|
|
AnimatedMetricText(value: "\(minutes) min").font(PanelStyle.value)
|
|
} else {
|
|
Text("battery.timeUnknown").font(PanelStyle.value)
|
|
}
|
|
}.frame(height: 14)
|
|
}
|
|
}
|
|
.font(PanelStyle.label)
|
|
.lineLimit(1)
|
|
.frame(height: 32)
|
|
Spacer(minLength: 0)
|
|
CardSeparator()
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
HStack {
|
|
Text("battery.highestEnergyProcess").font(PanelStyle.section)
|
|
Spacer()
|
|
Text("battery.cpuUsage").foregroundStyle(.secondary)
|
|
}
|
|
.font(PanelStyle.label)
|
|
let energyProcesses = snapshot.processes?.highestCPUProcesses(limit: 3) ?? []
|
|
if energyProcesses.isEmpty {
|
|
Text("metric.waiting").foregroundStyle(.secondary)
|
|
} else {
|
|
ForEach(energyProcesses) { process in
|
|
EnergyProcessListRow(process: process)
|
|
}
|
|
}
|
|
}
|
|
// Reserve the header and all three rows before process sampling completes.
|
|
.frame(height: 74, alignment: .top)
|
|
.lineLimit(1)
|
|
.help(Text("battery.energyProxyHelp"))
|
|
|
|
}.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
|
}
|
|
|
|
}
|
|
|
|
struct EnergyProcessListRow: View {
|
|
let process: ProcessMetrics
|
|
|
|
private var progress: Double {
|
|
guard let value = process.cpu, value.isFinite else { return 0 }
|
|
return min(max(value, 0), 1)
|
|
}
|
|
|
|
var body: some View {
|
|
HStack(spacing: 5) {
|
|
ProcessIcon(process: process)
|
|
Text(verbatim: process.name).lineLimit(1).truncationMode(.middle)
|
|
.frame(width: 66, alignment: .leading)
|
|
GeometryReader { proxy in
|
|
Capsule().fill(.primary.opacity(0.07))
|
|
.overlay(alignment: .leading) {
|
|
Capsule().fill(Color("MetricCPU"))
|
|
.frame(width: proxy.size.width * progress, height: 4)
|
|
.clipped()
|
|
}
|
|
.clipShape(Capsule())
|
|
}
|
|
.frame(height: 4)
|
|
.animation(.easeInOut(duration: 0.35), value: process.cpu)
|
|
AnimatedMetricText(value: process.cpu.map { String(format: "%.1f%%", $0 * 100) } ?? "--")
|
|
.foregroundStyle(Color("MetricCPU")).font(PanelStyle.value)
|
|
.monospacedDigit().frame(width: 39, alignment: .trailing)
|
|
}
|
|
.font(PanelStyle.label)
|
|
.frame(height: 16)
|
|
|
|
}
|
|
}
|