Files
Kymo/kymo/Features/MenuBar/EnergyProcessSection.swift
T

65 lines
2.6 KiB
Swift

import SwiftUI
/// A separate observation scope: energy completion does not replace the resource snapshot.
struct EnergyProcessSection: View {
let metrics: MetricsStore
private var snapshot: ProcessEnergySnapshot? { metrics.energy }
private var rows: [ProcessEnergyUsage] { snapshot?.rows ?? [] }
var body: some View {
VStack(alignment: .leading, spacing: 4) {
HStack {
Text("energy.cpuProcesses").font(PanelStyle.section)
if snapshot?.state == .partial {
Text("energy.partial").font(.system(size: 8)).foregroundStyle(.secondary)
.help(Text("energy.coverage \(snapshot?.measured ?? 0) \(snapshot?.candidates ?? 0)"))
}
Spacer()
Text("energy.averagePower").foregroundStyle(.secondary)
}.font(PanelStyle.label)
if rows.isEmpty {
Text(statusKey).foregroundStyle(.secondary).font(PanelStyle.label)
} else {
ForEach(rows) { usage in
EnergyProcessListRow(usage: usage, maximum: rows.first?.watts ?? 0)
}
}
}
.help(Text("energy.scopeHelp"))
}
private var statusKey: LocalizedStringKey {
switch snapshot?.state {
case .unsupported: "energy.unsupported"
case .unavailable: "energy.unavailable"
default: "energy.waiting"
}
}
}
private struct EnergyProcessListRow: View {
let usage: ProcessEnergyUsage
let maximum: Double
private var progress: Double { maximum > 0 ? min(1, max(0, usage.watts / maximum)) : 0 }
var body: some View {
HStack(spacing: 5) {
ProcessIcon(process: usage.process)
Text(verbatim: usage.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)
Text(verbatim: usage.formattedPower)
.foregroundStyle(Color("MetricCPU")).font(PanelStyle.value)
.monospacedDigit().frame(width: 57, alignment: .trailing)
}
.font(PanelStyle.label).frame(height: 16)
.help(Text("energy.window \(usage.seconds.formatted(.number.precision(.fractionLength(1))))"))
.accessibilityElement(children: .combine)
}
}