refactor(menubar): 精简内存卡片并对齐双列底部

This commit is contained in:
2026-11-09 00:05:25 +08:00
parent 28112bcf2e
commit 1afe82b233
15 changed files with 119 additions and 40 deletions
+1 -21
View File
@@ -7,7 +7,7 @@ struct MemoryDetailsView: View {
private let labels: [LocalizedStringKey] = ["memory.app", "memory.wired", "memory.compressed", "memory.available"]
var body: some View {
VStack(alignment: .leading, spacing: 4) {
VStack(alignment: .leading, spacing: 3) {
CardSeparator()
CapacityBar(values: values.map { Double($0) }, colors: colors, total: Double(memory.total))
HStack(spacing: 4) {
@@ -15,29 +15,9 @@ struct MemoryDetailsView: View {
CapacityField(title: labels[index], value: MetricFormat.memory(values[index]), color: colors[index])
}
}
CardSeparator()
HStack(alignment: .top, spacing: 8) {
CardField(title: "memory.pressure") { pressure }
Divider().frame(height: 32)
CardField(title: "memory.cached") {
MetricText(value: MetricFormat.memory(memory.cached))
}
Divider().frame(height: 32)
CardField(title: "memory.swap") {
MetricText(value: MetricFormat.memory(memory.swapUsed))
}
}.font(PanelStyle.label).lineLimit(1)
}
.accessibilityElement(children: .contain)
.accessibilityLabel(Text("memory.distribution"))
}
@ViewBuilder private var pressure: some View {
switch memory.pressure {
case 1: Text("pressure.normal").foregroundStyle(Color("MetricCPU"))
case 2: Text("pressure.warning").foregroundStyle(.orange)
case 4: Text("pressure.critical").foregroundStyle(.red)
default: Text(verbatim: "--").foregroundStyle(.secondary)
}
}
}
@@ -0,0 +1,27 @@
import SwiftUI
/// Presentation only: reuse the current snapshot without additional sampling on hover.
struct MemoryHeaderInfo {
let memory: MemoryMetrics?
let locale: Locale
var bundle: Bundle = .main
private var pressure: (key: String?, color: Color) {
switch memory?.pressure {
case 1: ("pressure.normal", Color("MetricCPU"))
case 2: ("pressure.warning", .orange)
case 4: ("pressure.critical", .red)
default: (nil, .secondary)
}
}
var title: String { pressure.key.map(localized) ?? "--" }
var color: Color { pressure.color }
var help: String {
"\(localized("memory.pressure")): \(title)\n"
+ "\(localized("memory.cached")): \(MetricFormat.memory(memory?.cached))\n"
+ "\(localized("memory.swap")): \(MetricFormat.memory(memory?.swapUsed))"
}
private func localized(_ key: String) -> String { AppLocalization.string(key, locale: locale, bundle: bundle) }
}
@@ -79,12 +79,14 @@ extension MenuPanelView {
}
var memoryCard: some View {
ResourceCard(
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)
} ?? "--",
status: snapshot.memory.map { MetricFormat.percent($0.usage) }
status: snapshot.memory.map { MetricFormat.percent($0.usage) },
detail: header.title, detailColor: header.color, detailHelp: header.help
) {
PanelTrendChart(
dates: samples.map(\.date),
+1 -1
View File
@@ -25,7 +25,7 @@ nonisolated enum PanelLayout {
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: 228, .battery: 170, .storage: 170, .network: 208]
.memory: 180, .battery: 170, .storage: 170, .network: 208]
}
static func columnHeights(columns: Int, heights: [PanelCard: CGFloat]) -> [CGFloat] {