Files
Kymo/kymo/Features/MenuBar/MenuPanelView+ResourceCards.swift
wangairnan b9abb970d9 feat(menubar): 用分色进度柱展示电量、供电与睡眠历史
记录系统睡眠和唤醒区间,保留真实采样空档。电量柱按固定间距从右侧滑入,以蓝色、绿色和橙黄色分别标记外接电源、电池和睡眠,支持悬浮查看历史。
2026-11-16 11:07:07 +08:00

173 lines
9.1 KiB
Swift

import AppKit
import SwiftUI
extension MenuPanelView {
func line(_ key: LocalizedStringKey, _ value: String) -> some View {
HStack(alignment: .firstTextBaseline, spacing: 4) {
Text(key).font(.system(size: 10)).foregroundStyle(.secondary)
Spacer(minLength: 6)
MetricText(value: value).font(.system(size: 10, weight: .medium))
}.frame(height: 16)
}
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,
warningText: highTemperature ? Text("cpu.temperature.high", tableName: "CPUDetails") : nil,
chartPlacement: .background,
chart: {
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, stackOrder: [2, 1])
}
) {
HStack(spacing: 5) {
InlineMetric("cpu.user", MetricFormat.percent(snapshot.cpu?.user))
InlineMetric("cpu.system", MetricFormat.percent(snapshot.cpu?.system))
}.frame(height: 16)
if let cpu = snapshot.cpu {
CPUCoreGrid(
values: cpu.cores, kinds: CPUCoreTopology.cores,
availableWidth: cardContentWidth)
} else {
Text("metric.waiting").foregroundStyle(.secondary)
}
}
}
var gpuCard: some View {
return ResourceCard(
title: "metric.gpu", symbol: "square.grid.2x2", color: Color("MetricGPU"), value: MetricFormat.percent(snapshot.gpuUsage),
titleBadge: snapshot.gpu?.coreCount.map {
$0 == 1 ? AppLocalization.string("gpu.oneCore", locale: locale)
: String(format: AppLocalization.string("gpu.coreCountFormat", locale: locale), $0)
},
status: snapshot.gpuTemperature.map { "\(Int($0.rounded()))°C" },
chartPlacement: .background,
chart: {
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,
visibleSeriesCount: 3, filledSeriesCount: 1)
}
) {
HStack(spacing: 8) {
gpuStage("gpu.renderer", snapshot.gpu?.renderer, Color("MetricCPU"))
gpuStage("gpu.tiler", snapshot.gpu?.tiler, Color("SeriesSystem"))
}.frame(height: 16)
let fans = displayedFans
if !fans.isEmpty {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 5, alignment: .leading), count: min(2, fans.count)), spacing: 2) {
ForEach(fans) { fan in
FanSpeedIndicator(
title: fans.count == 2
? (fan.id == 0 ? Text("gpu.fanLeft") : Text("gpu.fanRight"))
: Text("gpu.fanNumber \(fan.id + 1)"), rpm: fan.rpm,
isActive: metrics.isPanelVisible && !metrics.isPaused && !metrics.isSystemSleeping && !metrics.isDisplaySleeping)
.frame(height: 16)
}
}
} else if snapshot.fans == nil {
Text("metric.waiting").font(.system(size: 10))
.frame(maxWidth: .infinity, alignment: .leading).frame(height: 16)
}
}
}
private func gpuStage(_ title: LocalizedStringKey, _ usage: Double?, _ tint: Color) -> some View {
HStack(spacing: 3) {
Circle().fill(tint).frame(width: 4, height: 4)
InlineMetric(title, MetricFormat.percent(usage))
}
}
var memoryCard: some View {
let header = MemoryHeaderInfo(memory: snapshot.memory, locale: locale)
return 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)
} ?? "--",
detail: header.badgeTitle, detailColor: snapshot.memory?.pressure == 1 ? Color("MetricBattery") : header.color,
detailHelp: header.help + "\n" + MetricFormat.percent(snapshot.memory?.usage),
chartPlacement: .background,
chart: {
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("MemoryApplication"), secondColor: Color("MemoryApplication"),
thirdColor: Color("MemoryWired"), fourthColor: Color("MemoryCompressed"), fixedScale: Double(snapshot.memory?.total ?? 1),
firstValueFormat: .memory, firstLabel: "memory.used",
secondLabel: "memory.app", thirdLabel: "memory.wired", fourthLabel: "memory.compressed",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals, stackOrder: [1, 2, 3])
}
) {
if let memory = snapshot.memory {
MemoryDetailsView(memory: memory)
} else {
Text("metric.waiting").foregroundStyle(.secondary)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
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)
} ?? "--",
headerAccessory: AnyView(Button { showVolumes.toggle() } label: {
HStack(spacing: 2) { Text("disk.localVolumes"); Image(systemName: "chevron.right").font(.system(size: 7)) }
}.font(.system(size: 10)).buttonStyle(.plain).help(Text("disk.viewDetails"))),
chart: {
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, visibleSeriesCount: 2, projection: .split(upwardSeries: 0)
)
}
) {
StorageDetailsView(disk: snapshot.disk, health: snapshot.diskHealth, io: snapshot.diskIO)
.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: 10)).foregroundStyle(.secondary)
}
}.padding(12).frame(width: 330)
.environment(\.locale, locale)
}
}
}
var batteryCard: some View {
PowerResourceCard(snapshot: snapshot, samples: samples, sleepIntervals: metrics.history.sleepIntervals, chartInterval: chartInterval,
chartBoundaries: chartBoundaries, chartIntervals: chartIntervals)
}
}