refactor(menubar): 精简内存卡片并对齐双列底部
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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] {
|
||||
|
||||
@@ -12,6 +12,8 @@ struct ResourceCard<Content: View>: View {
|
||||
var statusWarning = false
|
||||
var warningText: Text? = nil
|
||||
var detail: String? = nil
|
||||
var detailColor: Color? = nil
|
||||
var detailHelp: String? = nil
|
||||
@ViewBuilder var content: () -> Content
|
||||
|
||||
var body: some View {
|
||||
@@ -40,10 +42,13 @@ struct ResourceCard<Content: View>: View {
|
||||
}
|
||||
if let detail {
|
||||
Text(verbatim: detail)
|
||||
.font(.system(size: 10, weight: .medium)).foregroundStyle(.secondary)
|
||||
.font(.system(size: 10, weight: .medium)).foregroundStyle(detailColor ?? .secondary)
|
||||
.padding(.horizontal, 6).frame(height: 16)
|
||||
.background(.primary.opacity(0.06), in: Capsule())
|
||||
.background((detailColor ?? .primary).opacity(detailColor == nil ? 0.06 : 0.14), in: Capsule())
|
||||
.lineLimit(1)
|
||||
.fixedSize(horizontal: true, vertical: false)
|
||||
.help(Text(verbatim: detailHelp ?? detail))
|
||||
.accessibilityLabel(Text(verbatim: detailHelp ?? detail))
|
||||
}
|
||||
Spacer(minLength: 0)
|
||||
HStack(alignment: .center, spacing: 6) {
|
||||
|
||||
Reference in New Issue
Block a user