diff --git a/App/Dashboard/ClusterDashboard.swift b/App/Dashboard/ClusterDashboard.swift index 5e463ec..6284cdc 100644 --- a/App/Dashboard/ClusterDashboard.swift +++ b/App/Dashboard/ClusterDashboard.swift @@ -66,9 +66,9 @@ struct ClusterDashboard: View { DialTile( title: "Memory", detail: memory.pressure.level.name, - value: memory.used.formattedCompact(), - fraction: memory.usage.fraction, - tint: DashboardSection.memory.tint + value: memory.pressure.percent.formatted(), + fraction: memory.pressure.percent.fraction, + tint: memory.pressure.level.tint ) .showsDetails(.memory, cornerRadius: 14) } diff --git a/App/Dashboard/DashboardComponents.swift b/App/Dashboard/DashboardComponents.swift index 25aa95e..e5d7599 100644 --- a/App/Dashboard/DashboardComponents.swift +++ b/App/Dashboard/DashboardComponents.swift @@ -22,6 +22,13 @@ enum DashboardPalette { ? NSColor(srgbRed: 0xC9 / 255, green: 0x85 / 255, blue: 0, alpha: 1) : NSColor(srgbRed: 0xED / 255, green: 0xA1 / 255, blue: 0, alpha: 1) }) + /// High memory pressure. System yellow is unreadable as text on light backgrounds, so + /// light mode uses a darker amber. + static let warning = Color(nsColor: NSColor(name: nil) { appearance in + appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua + ? .systemYellow + : NSColor(srgbRed: 0x9A / 255, green: 0x67 / 255, blue: 0, alpha: 1) + }) static let track = Color.secondary.opacity(0.22) static let grid = Color.secondary.opacity(0.3) } @@ -516,6 +523,20 @@ extension MemoryPressure.Level { case .critical: "Critical" } } + + /// Normal keeps the section's color; higher levels follow Activity Monitor's yellow and red. + var tint: Color { + switch self { + case .normal: DashboardSection.memory.tint + case .warning: DashboardPalette.warning + case .critical: .red + } + } + + /// Headline values stay the primary color until pressure rises. + var valueStyle: Color { + self == .normal ? .primary : tint + } } extension View { diff --git a/App/Dashboard/MemorySection.swift b/App/Dashboard/MemorySection.swift index 0218e92..ae20640 100644 --- a/App/Dashboard/MemorySection.swift +++ b/App/Dashboard/MemorySection.swift @@ -16,9 +16,10 @@ struct MemorySection: View { memory.pressure.level == .normal ? nil : "\(memory.pressure.level.name) pressure" ) ) { - Sparkline(history, color: DashboardSection.memory.tint) { $0.fraction } + Sparkline(history, color: memory.pressure.level.tint) { $0.fraction } } value: { - Text(memory.usage.formatted()) + Text(memory.pressure.percent.formatted()) + .foregroundStyle(memory.pressure.level.valueStyle) } } } @@ -32,10 +33,10 @@ struct MemoryDetails: View { VStack(alignment: .leading, spacing: 6) { DetailsHeader( section: .memory, - value: memory.used.formatted(.memory), - summary: "of \(memory.total.formatted(.memory)) · \(memory.pressure.level.name) pressure" + value: memory.pressure.percent.formatted(), + summary: "\(memory.pressure.level.name) pressure" ) - Sparkline(history, color: DashboardSection.memory.tint, window: history.capacity, showsGrid: true) { $0.fraction } + Sparkline(history, color: memory.pressure.level.tint, window: history.capacity, showsGrid: true) { $0.fraction } .frame(height: 64) .accessibilityHidden(true) .padding(.bottom, 2) @@ -49,7 +50,7 @@ struct MemoryDetails: View { LegendRow(color: DashboardPalette.secondary, label: "Wired", value: memory.wired.formatted(.memory)) LegendRow(color: DashboardPalette.tertiary, label: "Compressed", value: memory.compressed.formatted(.memory)) DetailRow(label: "Cached files", value: memory.cached.formatted(.memory)) - DetailRow(label: "Pressure", value: "\(memory.pressure.percent.formatted()) · \(memory.pressure.level.name)") + DetailRow(label: "Memory used", value: "\(memory.used.formatted(.memory)) of \(memory.total.formatted(.memory))") if let swap = memory.swapUsed { DetailRow(label: "Swap used", value: swap.bytes == 0 ? "None" : swap.formatted(.memory)) } diff --git a/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift b/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift index 8e2144e..8192f94 100644 --- a/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift +++ b/Packages/BarometryKit/Sources/BarometryKit/MenuBar/MenuBarWidgetContent.swift @@ -51,9 +51,9 @@ public enum MenuBarWidgetContent: Equatable, Sendable { case (.gpu, .text): return .readouts([percent(gpuHistory.last)]) case (.memory, .graphical): - return .gauge(memory?.usage.fraction ?? 0) + return .gauge(memory?.pressure.percent.fraction ?? 0) case (.memory, .text): - return .readouts([percent(memory?.usage)]) + return .readouts([percent(memory?.pressure.percent)]) case (.network, _): return .lines( top: network?.upload.formatted(locale: locale) ?? missing, diff --git a/Packages/BarometryKit/Sources/BarometryKit/Model/MemorySample.swift b/Packages/BarometryKit/Sources/BarometryKit/Model/MemorySample.swift index 34cf8b6..d32cb17 100644 --- a/Packages/BarometryKit/Sources/BarometryKit/Model/MemorySample.swift +++ b/Packages/BarometryKit/Sources/BarometryKit/Model/MemorySample.swift @@ -76,10 +76,6 @@ public struct MemorySample: Equatable, Sendable { app + wired + compressed } - public var usage: Percent { - Percent(Double(used.bytes), of: Double(total.bytes)) - } - /// Breaks page counts down the same way Activity Monitor does. init(pages: VMPageCounts, pageSize: UInt64, total: ByteCount, pressure: MemoryPressure, swapUsed: ByteCount? = nil) { func bytes(_ pageCount: UInt64) -> ByteCount { diff --git a/Packages/BarometryKit/Sources/BarometryKit/SystemMonitor.swift b/Packages/BarometryKit/Sources/BarometryKit/SystemMonitor.swift index 7bc5049..13ada2f 100644 --- a/Packages/BarometryKit/Sources/BarometryKit/SystemMonitor.swift +++ b/Packages/BarometryKit/Sources/BarometryKit/SystemMonitor.swift @@ -201,7 +201,7 @@ public final class SystemMonitor { gpuHistory.append(gpu.utilization) } if let memory = snapshot.memory { - memoryHistory.append(memory.usage) + memoryHistory.append(memory.pressure.percent) } if let power = snapshot.sensors?.systemPower { powerHistory.append(power) diff --git a/Packages/BarometryKit/Tests/BarometryKitTests/MemorySampleTests.swift b/Packages/BarometryKit/Tests/BarometryKitTests/MemorySampleTests.swift index 99013c9..5b1b536 100644 --- a/Packages/BarometryKit/Tests/BarometryKitTests/MemorySampleTests.swift +++ b/Packages/BarometryKit/Tests/BarometryKitTests/MemorySampleTests.swift @@ -17,7 +17,6 @@ struct MemorySampleTests { #expect(sample.compressed == ByteCount(bytes: 10_000)) #expect(sample.cached == ByteCount(bytes: 50_000)) #expect(sample.used == ByteCount(bytes: 130_000)) - #expect(sample.usage == Percent(0.13)) } @Test func clampsAppMemoryWhenPurgeableExceedsInternal() { diff --git a/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift b/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift index 0384ba4..e5947e0 100644 --- a/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift +++ b/Packages/BarometryKit/Tests/BarometryKitTests/MenuBarWidgetContentTests.swift @@ -103,6 +103,21 @@ struct MenuBarWidgetContentTests { #expect(content(.cpu, style: .text, cpuHistory: history) == .readouts([Readout(label: "CPU", value: "22%", widthTemplate: "100%")])) } + @Test func memoryShowsPressureRatherThanUsedMemory() { + // 50% of memory used, but only 12% pressure. + let memory = MemorySample( + total: ByteCount(bytes: 1_000), + app: ByteCount(bytes: 500), + wired: .zero, + compressed: .zero, + cached: .zero, + pressure: MemoryPressure(level: .normal, percent: Percent(0.12)) + ) + + #expect(content(.memory, memory: memory) == .gauge(0.12)) + #expect(content(.memory, style: .text, memory: memory) == .readouts([Readout(label: "MEM", value: "12%", widthTemplate: "100%")])) + } + @Test func textSensorsSplitIntoTemperatureAndPower() { let sensors = SensorSample(cpuTemperature: Temperature(celsius: 50), gpuTemperature: nil, systemPower: Power(watts: 9.41), fans: [])