Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions App/Dashboard/ClusterDashboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions App/Dashboard/DashboardComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions App/Dashboard/MemorySection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
Expand All @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [])

Expand Down
Loading