feat(ui): 统一实现卡片背景趋势与大数值布局

This commit is contained in:
2026-11-15 15:40:29 +08:00
parent 397156249b
commit 3c1a2baace
25 changed files with 447 additions and 268 deletions
@@ -8,7 +8,6 @@ struct MemoryDetailsView: View {
var body: some View {
VStack(alignment: .leading, spacing: 3) {
CardSeparator()
CapacityBar(values: values.map { Double($0) }, colors: colors, total: Double(memory.total))
HStack(spacing: 4) {
ForEach(0..<4) { index in
@@ -6,50 +6,51 @@ extension MenuPanelView {
ResourceCard(
title: "metric.network", symbol: "network", color: Color("MetricNetwork"), value: "↓ " + MetricFormat.rate(total(\.download)),
secondaryValue: "↑ " + MetricFormat.rate(total(\.upload)), valueColor: Color("MetricMemory"),
secondaryValueColor: Color("SeriesUpload")
secondaryValueColor: Color("SeriesUpload"), headerAccessory: AnyView(networkInterfacePicker),
chart: {
PanelTrendChart(
dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricMemory"),
secondColor: Color("SeriesUpload"), fixedScale: nil, firstLabel: "network.download", secondLabel: "network.upload",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: networkIntervals, visibleSeriesCount: 2
).id(selectedInterface)
}
) {
PanelTrendChart(
dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricMemory"),
secondColor: Color("SeriesUpload"), fixedScale: nil, firstLabel: "network.download", secondLabel: "network.upload",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: networkIntervals, showsLegendValues: false
).id(selectedInterface)
CardSeparator()
HStack(alignment: .top, spacing: 8) {
HStack(spacing: 12) {
networkTotal(
"network.received", value: network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.received }),
symbol: "arrow.down", color: Color("MetricMemory"))
Divider().frame(height: 32)
networkTotal(
"network.sent", value: network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.sent }),
symbol: "arrow.up", color: Color("SeriesUpload"))
Divider().frame(height: 32)
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 4) {
Circle().fill(networkStatusColor).frame(width: 5, height: 5)
Picker("panel.interface", selection: $selectedInterface) {
Text("network.all").tag("")
ForEach(snapshot.network ?? [], id: \.identity) { item in
Text(verbatim: item.name).tag(item.identity)
}
}.labelsHidden().controlSize(.mini).fixedSize()
}
Text(networkStatusLabel).help(Text(networkStatusLabel))
}.frame(maxWidth: .infinity, alignment: .leading)
}.font(PanelStyle.label).lineLimit(1)
CardSeparator()
HStack(spacing: 8) {
}
VStack(spacing: 4) {
networkAddress("network.public", address: publicIPAddress)
Divider().frame(height: 15)
networkAddress(
"network.local",
address: addressInterface?.addresses.first(where: { !$0.contains(":") }) ?? addressInterface?.addresses.first)
}.font(PanelStyle.label)
.modifier(CardInset())
.accessibilityElement(children: .contain)
.accessibilityLabel(Text("network.addresses"))
}
.font(PanelStyle.label)
.accessibilityElement(children: .contain)
.accessibilityLabel(Text("network.addresses"))
}
.modifier(PanelPublicIPRefresh(metrics: metrics, address: $publicIPAddress))
}
private var networkInterfacePicker: some View {
HStack(spacing: 4) {
Circle().fill(networkStatusColor).frame(width: 5, height: 5)
Picker("panel.interface", selection: $selectedInterface) {
Text("network.all").tag("")
ForEach(snapshot.network ?? [], id: \.identity) { item in
Text(verbatim: item.name).tag(item.identity)
}
}.labelsHidden().controlSize(.mini).frame(maxWidth: 100)
}
.help(Text(networkStatusLabel))
.accessibilityElement(children: .contain)
.accessibilityValue(Text(networkStatusLabel))
}
func networkAddress(_ title: LocalizedStringKey, address: String?) -> some View {
HStack(spacing: 3) {
Text(title).foregroundStyle(.secondary)
@@ -10,6 +10,15 @@ extension MenuPanelView {
}.frame(height: 14)
}
func inlineMetric(_ title: LocalizedStringKey, _ value: String) -> some View {
HStack(spacing: 4) {
Text(title).foregroundStyle(.secondary)
MetricText(value: value).fontWeight(.medium)
}
.font(.system(size: 11)).lineLimit(1).minimumScaleFactor(0.8)
.frame(maxWidth: .infinity, alignment: .leading)
}
func batteryPowerLabel(_ battery: BatteryMetrics) -> LocalizedStringKey {
if battery.charging { return "battery.chargePower" }
if !battery.externalPower { return "battery.dischargePower" }
@@ -24,15 +33,20 @@ extension MenuPanelView {
value: MetricFormat.percent(snapshot.cpu?.usage),
status: temperature.map { "\(Int($0.rounded()))°C" },
statusWarning: highTemperature,
warningText: highTemperature ? Text("cpu.temperature.high", tableName: "CPUDetails") : nil
warningText: highTemperature ? Text("cpu.temperature.high", tableName: "CPUDetails") : nil,
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)
}
) {
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()
HStack(spacing: 12) {
inlineMetric("cpu.user", MetricFormat.percent(snapshot.cpu?.user))
inlineMetric("cpu.system", MetricFormat.percent(snapshot.cpu?.system))
}.frame(height: 14)
if let cpu = snapshot.cpu {
CPUCoreGrid(
values: cpu.cores, kinds: CPUCoreTopology.cores,
@@ -48,17 +62,22 @@ extension MenuPanelView {
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" },
detail: snapshot.gpu?.coreCount.map { String(format: AppLocalization.string("gpu.coreCountFormat", locale: locale), $0) }
detail: snapshot.gpu?.coreCount.map { String(format: AppLocalization.string("gpu.coreCountFormat", locale: locale), $0) },
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)
}
) {
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)
HStack(spacing: 12) {
inlineMetric("gpu.renderer", MetricFormat.percent(snapshot.gpu?.renderer))
inlineMetric("gpu.tiler", MetricFormat.percent(snapshot.gpu?.tiler))
}.frame(height: 14)
let fans = displayedFans
if !fans.isEmpty {
CardSeparator()
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 10), count: min(2, fans.count)), spacing: 4) {
ForEach(fans) { fan in
FanSpeedIndicator(
@@ -68,12 +87,9 @@ extension MenuPanelView {
.frame(height: 16)
}
}
.modifier(CardInset())
} else if snapshot.fans == nil {
CardSeparator()
Text("metric.waiting").font(PanelStyle.label)
.frame(maxWidth: .infinity, alignment: .leading).frame(height: 16)
.modifier(CardInset())
}
}
}
@@ -86,22 +102,22 @@ extension MenuPanelView {
String(format: "%.1f / %.0f GiB", Double($0.used) / 1_073_741_824, Double($0.total) / 1_073_741_824)
} ?? "--",
status: snapshot.memory.map { MetricFormat.percent($0.usage) },
detail: header.title, detailColor: header.color, detailHelp: header.help
detail: header.title, detailColor: header.color, detailHelp: header.help,
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: "gpu.totalUtilization",
secondLabel: "memory.app", thirdLabel: "memory.wired", fourthLabel: "memory.compressed",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
}
) {
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,
showsLegendValues: cardContentWidth >= 340)
if let memory = snapshot.memory {
MemoryDetailsView(memory: memory)
} else {
CardSeparator()
Text("metric.waiting").foregroundStyle(.secondary)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
@@ -114,15 +130,19 @@ extension MenuPanelView {
String(
format: "%.0f / %.0f GB", Double($0.total - min($0.total, $0.available)) / 1_000_000_000,
Double($0.total) / 1_000_000_000)
} ?? "--"
} ?? "--",
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
)
}
) {
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
)
HStack(spacing: 12) {
inlineMetric("disk.read", MetricFormat.rate(snapshot.diskIO?.read))
inlineMetric("disk.write", MetricFormat.rate(snapshot.diskIO?.written))
}.frame(height: 14)
StorageDetailsView(disk: snapshot.disk, health: snapshot.diskHealth) {
showVolumes.toggle()
}
@@ -150,24 +170,27 @@ extension MenuPanelView {
var batteryCard: some View {
ResourceCard(
title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"),
value: MetricFormat.percent(snapshot.battery?.charge)
value: MetricFormat.percent(snapshot.battery?.charge),
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
)
}
) {
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()
inlineMetric(snapshot.battery.map(batteryPowerLabel) ?? "battery.power", MetricFormat.watts(snapshot.battery?.power))
.frame(height: 14)
if let battery = snapshot.battery {
batteryDetails(battery)
} else {
+1 -1
View File
@@ -29,7 +29,7 @@ struct MenuPanelView: View {
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 cardContentWidth: CGFloat { max(0, (measuredCardWidth ?? PanelLayout.cardWidth(size: panelSize)) - ResourceCardMetrics.padding * 2) }
var columns: Int { PanelLayout.columns(width: panelSize.width) }
private var cardHeights: [PanelCard: CGFloat] {
let coreCount = max(CPUCoreTopology.cores.count, ProcessInfo.processInfo.processorCount, snapshot.cpu?.cores.count ?? 0)
+5 -4
View File
@@ -25,12 +25,13 @@ nonisolated enum PanelLayout {
}
}
/// Fixed content rows with a 48pt plot. Nil fans reserve a discovery row.
/// Foreground details and the edge-to-edge background trend have separate budgets.
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: 152 + CGFloat(max(1, coreRows)) * 26,
.gpu: fanRows == 0 ? 124 : 144 + CGFloat(fanRows) * 20,
.memory: 180, .battery: 170, .storage: 170, .network: 208]
return [.cpu: base + CGFloat(max(0, coreRows - 2)) * 26,
.gpu: base + CGFloat(max(0, fanRows - 1)) * 20,
.memory: base, .battery: base, .storage: base, .network: base]
}
static func rowHeights(columns: Int, heights: [PanelCard: CGFloat]) -> [CGFloat] {
@@ -46,6 +46,10 @@ import AppKit
}
}
mark("interactive")
if ProcessInfo.processInfo.environment["KYMO_AUDIT_CAPTURE"] == "1" {
await wait(10)
mark("open1.10")
}
return
}
let quick = ProcessInfo.processInfo.environment["KYMO_AUDIT_QUICK"] == "1"
@@ -7,7 +7,6 @@ struct StorageDetailsView: View {
var body: some View {
VStack(alignment: .leading, spacing: 4) {
CardSeparator()
HStack(spacing: 8) {
VStack(spacing: 4) {
field("memory.available", value: MetricFormat.bytes(disk?.available))