feat(panel): 调整资源卡片读数布局并保留趋势平移

逐核显示 CPU 占用,在 GPU 标题展示核数并补充分项读数。无电池设备显示整机功率。

存储与网络将曲线排在详情下方,CPU、GPU、内存和电池保留整卡趋势。数值直接刷新,曲线随采样平滑平移。顶栏展示版本号,底栏调整开机时间和刷新间隔。
This commit is contained in:
2026-11-15 22:03:46 +08:00
parent c886c4227d
commit 6300db3d15
27 changed files with 427 additions and 250 deletions
+10 -5
View File
@@ -1,6 +1,6 @@
import SwiftUI
/// Core-kind columns contain one vertical segmented meter per real core.
/// Core-kind columns contain one continuously filled cell per real core.
struct CPUCoreGrid: View {
let values: [Double]
let kinds: [Int: CPUCoreTopology.Kind]
@@ -85,8 +85,13 @@ struct CPUCoreGrid: View {
private func core(index: Int) -> some View {
let kind = kinds[index]
let measured = values[index].isFinite ? values[index] : nil
return SegmentedCoreBar(value: measured, color: color(kind))
.frame(height: 30)
return RoundedRectangle(cornerRadius: 2)
.fill(color(kind).opacity(0.22))
.overlay(alignment: .bottom) {
Rectangle().fill(color(kind)).frame(height: CPUCoreGridLayout.rowHeight * (measured.map { min(1, max(0, $0)) } ?? 0))
}
.clipShape(.rect(cornerRadius: 2))
.frame(height: CPUCoreGridLayout.rowHeight)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.help(Text("\(kind.map { label($0) } ?? Text("cpu.core")) · \(index + 1): \(MetricFormat.percent(measured))"))
.accessibilityElement(children: .ignore)
@@ -98,8 +103,8 @@ struct CPUCoreGrid: View {
struct CPUCoreGridLayout: Layout {
static let columnSpacing: CGFloat = 3
static let rowSpacing: CGFloat = 4
static let rowHeight: CGFloat = 30
static let minimumCellWidth: CGFloat = 6
static let rowHeight: CGFloat = 11
static let minimumCellWidth: CGFloat = 7
static func rowCounts(count: Int, width: CGFloat) -> [Int] {
guard count > 0 else { return [] }
@@ -10,20 +10,6 @@ extension MenuPanelView {
}.frame(height: 16)
}
func inlineMetric(_ title: LocalizedStringKey, _ value: String) -> some View {
HStack(spacing: 4) {
Text(title).foregroundStyle(.secondary)
MetricText(value: value).fontWeight(.medium)
}
.font(.system(size: 10)).lineLimit(1)
}
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)
@@ -33,6 +19,7 @@ extension MenuPanelView {
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),
@@ -43,8 +30,8 @@ extension MenuPanelView {
}
) {
HStack(spacing: 5) {
inlineMetric("cpu.user", MetricFormat.percent(snapshot.cpu?.user))
inlineMetric("cpu.system", MetricFormat.percent(snapshot.cpu?.system))
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(
@@ -60,24 +47,26 @@ extension MenuPanelView {
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 {
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 { $0.gpu.map { max(0, 1 - $0) } },
third: samples.map(\.gpuRenderer), fourth: samples.map(\.gpuTiler),
color: Color("MetricGPU"), secondColor: Color("MemoryAvailable"),
thirdColor: Color("MetricCPU"), fourthColor: Color("SeriesSystem"),
firstLabel: "gpu.totalUtilization", secondLabel: "gpu.idle", thirdLabel: "gpu.renderer", fourthLabel: "gpu.tiler",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals, stackOrder: [0, 1])
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: 5) {
if let count = snapshot.gpu?.coreCount {
Text(verbatim: String(format: AppLocalization.string("gpu.coreCountFormat", locale: locale), count))
.foregroundStyle(.primary)
}
}.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9).frame(height: 16)
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) {
@@ -97,6 +86,13 @@ extension MenuPanelView {
}
}
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(
@@ -106,6 +102,7 @@ extension MenuPanelView {
} ?? "--",
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),
@@ -168,55 +165,7 @@ extension MenuPanelView {
}
}
var batteryCard: some View {
ResourceCard(
title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"),
value: MetricFormat.percent(snapshot.battery?.charge),
status: snapshot.battery.map { AppLocalization.string($0.externalPower ? "battery.acPower" : "battery.batterySource", locale: locale) },
chart: {
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
)
}
) {
inlineMetric("battery.powerShort", MetricFormat.watts(snapshot.battery?.power))
.frame(height: 16)
if let battery = snapshot.battery {
batteryDetails(battery)
} else {
Text(snapshot.batteryState == .absent ? "panel.noBattery" : "panel.batteryFailed").foregroundStyle(.secondary)
.font(.system(size: 10)).frame(maxWidth: .infinity).frame(height: 32)
}
}
}
private func batteryDetails(_ battery: BatteryMetrics) -> some View {
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 8) {
inlineMetric("battery.healthShort", MetricFormat.percent(battery.health))
inlineMetric("battery.cyclesShort", battery.cycles.map(String.init) ?? "--")
}
HStack(spacing: 4) {
Text("battery.timeShort").foregroundStyle(.secondary)
if battery.externalPower {
Text("battery.notApplicableShort").help(Text("battery.timeNotApplicable"))
} else if let minutes = battery.minutesRemaining {
MetricText(value: "\(minutes) min")
} else {
Text("battery.timeUnknown")
}
}
}
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9)
PowerResourceCard(snapshot: snapshot, samples: samples, chartInterval: chartInterval,
chartBoundaries: chartBoundaries, chartIntervals: chartIntervals)
}
}
+20 -7
View File
@@ -81,7 +81,8 @@ struct MenuPanelView: View {
}
.background(Color("PanelBackground"), in: .rect(cornerRadius: 18))
.clipShape(.rect(cornerRadius: 18))
.transaction { $0.animation = nil; $0.disablesAnimations = true }
.transaction { $0.animation = nil }
.environment(\.panelMotionActive, metrics.isPanelVisible && !metrics.isPaused && !metrics.isSystemSleeping && !metrics.isDisplaySleeping)
.modifier(AppearanceStyle(preferences: preferences))
}
@@ -127,11 +128,23 @@ struct MenuPanelView: View {
}
}
private static let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
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))
HStack(alignment: .top, spacing: 3) {
Text("Kymo").font(.system(size: 17, weight: .semibold))
if let version = Self.version {
Text(verbatim: "v" + version)
.font(.system(size: 9, weight: .semibold)).monospacedDigit()
.padding(.horizontal, 5).padding(.vertical, 1)
.background(.primary.opacity(0.09), in: Capsule())
.offset(y: -2)
.accessibilityLabel(Text("about.version \(version)"))
}
}.fixedSize()
}
}
@@ -147,18 +160,18 @@ struct MenuPanelView: View {
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)
Text(verbatim: HardwareIdentity.displayName)
.lineLimit(1).truncationMode(.middle)
.help(Text(verbatim: HardwareIdentity.displayName))
Spacer(minLength: 8)
HStack(spacing: 4) {
Image(systemName: "timer").help(Text("sampling.panel"))
IntervalControl(milliseconds: Binding(get: { metrics.sampling.panelMilliseconds }, set: { metrics.sampling.panelMilliseconds = $0 }))
IntervalControl(milliseconds: Binding(get: { metrics.sampling.panelMilliseconds }, set: { metrics.sampling.panelMilliseconds = $0 }), fieldWidth: 50)
}
.foregroundStyle(.primary)
.accessibilityLabel(Text("sampling.panel"))
+3 -3
View File
@@ -25,12 +25,12 @@ nonisolated enum PanelLayout {
}
}
/// Structural foreground height determines the full-height background chart.
/// Structural foreground height determines the room available for the trend below the text.
static func cardHeights(coreRows: Int, fanCount: Int?) -> [PanelCard: CGFloat] {
let base = ResourceCardMetrics.minimumHeight
let fanRows = fanCount.map { (max(0, $0) + 1) / 2 } ?? 1
return [.cpu: base + CGFloat(max(0, coreRows - 1)) * 34,
.gpu: base + CGFloat(max(0, fanRows - 1)) * 14,
return [.cpu: base + CGFloat(max(0, coreRows - 1)) * 15,
.gpu: base + CGFloat(max(0, fanRows - 1)) * 18,
.memory: base, .battery: base, .storage: base, .network: base]
}
@@ -0,0 +1,87 @@
import SwiftUI
struct PowerResourceCard: View {
let snapshot: MetricsSnapshot
let samples: [TrendSample]
let chartInterval: TimeInterval
let chartBoundaries: Set<Date>
let chartIntervals: [TimeInterval]
@Environment(\.locale) private var locale
private func batteryPowerLabel(_ battery: BatteryMetrics) -> LocalizedStringKey {
if battery.charging { return "battery.chargePower" }
if !battery.externalPower { return "battery.dischargePower" }
return "battery.power"
}
@ViewBuilder var body: some View {
if snapshot.batteryState == .absent {
ResourceCard(title: "power.system", symbol: "bolt.fill", color: Color("MetricBattery"),
value: MetricFormat.watts(snapshot.systemPower), chart: {
PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.systemPower),
color: Color("MetricBattery"), fixedScale: nil, firstValueFormat: .watts,
firstLabel: "power.system", expectedInterval: chartInterval,
boundaryDates: chartBoundaries, intervals: chartIntervals)
}) {
Text(snapshot.systemPower == nil ? "power.sensorUnavailable" : "panel.noBattery")
.font(.system(size: 10)).foregroundStyle(.secondary)
}
} else {
batteryChargeCard
}
}
private var batteryChargeCard: some View {
ResourceCard(
title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"),
value: MetricFormat.percent(snapshot.battery?.charge),
status: snapshot.battery.map { AppLocalization.string($0.externalPower ? "battery.acPower" : "battery.batterySource", locale: locale) },
chartPlacement: .background,
chart: {
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
)
}
) {
InlineMetric("battery.powerShort", MetricFormat.watts(snapshot.battery?.power))
.frame(height: 16)
if let battery = snapshot.battery {
batteryDetails(battery)
} else {
Text(snapshot.batteryState == .unknown ? "metric.waiting" : "panel.batteryFailed").foregroundStyle(.secondary)
.font(.system(size: 10)).frame(maxWidth: .infinity).frame(height: 32)
}
}
}
private func batteryDetails(_ battery: BatteryMetrics) -> some View {
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 8) {
InlineMetric("battery.healthShort", MetricFormat.percent(battery.health))
InlineMetric("battery.cyclesShort", battery.cycles.map(String.init) ?? "--")
}
HStack(spacing: 4) {
Text("battery.timeShort").foregroundStyle(.secondary)
if battery.externalPower {
Text("battery.notApplicableShort").help(Text("battery.timeNotApplicable"))
} else if let minutes = battery.minutesRemaining {
MetricText(value: "\(minutes) min")
} else {
Text("battery.timeUnknown")
}
}
}
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9)
}
}
@@ -6,11 +6,11 @@ struct StorageDetailsView: View {
let io: DiskIOMetrics?
var body: some View {
VStack(alignment: .leading, spacing: 4) {
VStack(alignment: .leading, spacing: 3) {
field("memory.available", value: MetricFormat.bytes(disk?.available))
ViewThatFits(in: .horizontal) {
HStack(spacing: 6) { read; write }.fixedSize(horizontal: true, vertical: true)
VStack(alignment: .leading, spacing: 4) { read; write }
VStack(alignment: .leading, spacing: 3) { read; write }
}
ViewThatFits(in: .horizontal) {
HStack(spacing: 4) {
@@ -18,7 +18,7 @@ struct StorageDetailsView: View {
Text("·").foregroundStyle(.secondary)
powerOn
}.fixedSize(horizontal: true, vertical: true)
VStack(alignment: .leading, spacing: 4) { life; powerOn }
VStack(alignment: .leading, spacing: 3) { life; powerOn }
}
}
.font(.system(size: 10)).lineLimit(1)
@@ -33,6 +33,6 @@ struct StorageDetailsView: View {
HStack(spacing: 3) {
Text(title).foregroundStyle(.secondary).help(Text(title))
MetricText(value: value).fontWeight(.medium).foregroundStyle(.primary)
}.frame(height: 16)
}.frame(height: 14)
}
}