Skip to content

Free, open-source macOS cleaner. CleanMyMac alternative with zero telemetry. Native SwiftUI, scheduled auto-cleaning, Xcode/Homebrew/system cache cleanup. MIT licensed.

fix(performance): keep the last process sample on screen while sampling is paused

momenbasele995224e7105
diff --git a/PureMac/Services/ProcessResourceMonitor.swift b/PureMac/Services/ProcessResourceMonitor.swift
index 859564d..08aa030 100644
--- a/PureMac/Services/ProcessResourceMonitor.swift
+++ b/PureMac/Services/ProcessResourceMonitor.swift
@@ -202,16 +202,13 @@ final class ProcessResourceMonitor: ObservableObject {
isSampling = active
samplingTask?.cancel()
samplingTask = nil
- consumers = []
- icons.removeAll()
- names.removeAll()
- protectedProcessCount = 0
- changedProcessCount = 0
+ guard active else { return }
+ // The last sample stays on screen while paused. Resuming starts a new
+ // session-scoped sampler, so the baseline resets and a rapid hide/show
+ // cannot feed the next interval from an old task's counters.
isWarmingUp = true
samplingFailed = false
- guard active else { return }
- // Session-scoped sampler releases its counters when cancelled. A rapid
- // hide/show cannot reset the next session's baseline from an old task.
+ changedProcessCount = 0
let sampler = ProcessResourceSampler()
samplingTask = Task { [weak self, sampler] in
while !Task.isCancelled {
diff --git a/PureMac/Views/Tools/ProcessResourcesView.swift b/PureMac/Views/Tools/ProcessResourcesView.swift
index 8b5bf61..5bf47b4 100644
--- a/PureMac/Views/Tools/ProcessResourcesView.swift
+++ b/PureMac/Views/Tools/ProcessResourcesView.swift
@@ -49,15 +49,18 @@ struct ProcessResourcesView: View {
.padding(9)
.background(Color.primary.opacity(0.035), in: RoundedRectangle(cornerRadius: 8))
+ // Pausing keeps the last sample on screen under the notice.
if !isActive {
Label("Live updates paused", systemImage: "pause.circle")
.font(.system(size: 12))
.foregroundStyle(.secondary)
- } else if monitor.samplingFailed {
+ }
+
+ if isActive && monitor.samplingFailed {
Label("Process information is unavailable. Try Activity Monitor for more detail.", systemImage: "exclamationmark.triangle")
.font(.system(size: 12))
.foregroundStyle(.secondary)
- } else if monitor.consumers.isEmpty && monitor.isWarmingUp {
+ } else if isActive && monitor.consumers.isEmpty && monitor.isWarmingUp {
HStack(spacing: 9) {
ProgressView().controlSize(.small)
Text("Measuring apps and processes…")
@@ -66,10 +69,12 @@ struct ProcessResourcesView: View {
}
.padding(.vertical, 10)
} else if visibleConsumers.isEmpty {
- Text("No matching apps or processes")
- .font(.system(size: 12))
- .foregroundStyle(.secondary)
- .padding(.vertical, 10)
+ if isActive || !monitor.consumers.isEmpty {
+ Text("No matching apps or processes")
+ .font(.system(size: 12))
+ .foregroundStyle(.secondary)
+ .padding(.vertical, 10)
+ }
} else {
HStack {
Text("Name")
diff --git a/PureMacTests/MonitoringLifecycleTests.swift b/PureMacTests/MonitoringLifecycleTests.swift
index 5815a76..592d05b 100644
--- a/PureMacTests/MonitoringLifecycleTests.swift
+++ b/PureMacTests/MonitoringLifecycleTests.swift
@@ -47,17 +47,22 @@ final class MonitoringLifecycleTests: XCTestCase {
XCTAssertEqual(samples, stoppedCount)
}
- func testProcessMonitorStopsAndReleasesItsVisibleData() async throws {
+ func testPausingKeepsTheLastSampleAndResumingResetsTheBaseline() async throws {
let monitor = ProcessResourceMonitor()
monitor.setActive(true)
for _ in 0..<100 where monitor.consumers.isEmpty {
try await Task.sleep(nanoseconds: 20_000_000)
}
XCTAssertFalse(monitor.consumers.isEmpty, "Native read-only sample should include this test host")
+ let shown = monitor.consumers.map(\.id)
monitor.setActive(false)
XCTAssertFalse(monitor.isSampling)
- XCTAssertTrue(monitor.consumers.isEmpty)
+ XCTAssertEqual(monitor.consumers.map(\.id), shown, "Pausing keeps the last sample on screen")
try await Task.sleep(nanoseconds: 100_000_000)
- XCTAssertTrue(monitor.consumers.isEmpty, "Cancelled work must not publish after hiding")
+ XCTAssertEqual(monitor.consumers.map(\.id), shown, "Cancelled work must not publish after pausing")
+ monitor.setActive(true)
+ XCTAssertTrue(monitor.isWarmingUp, "Resuming starts from a fresh baseline")
+ XCTAssertEqual(monitor.consumers.map(\.id), shown, "The last sample stays until the next one arrives")
+ monitor.setActive(false)
}
}