Files
TopManager/TopManager/Views/MenuBarView.swift
hariel1985 6f60dd7ad8 Pause view updates while no window is visible; fix zero-value flicker
The window left open all day grew the app by ~360 MB in 24 h even after
the .tabItem fix. Isolating each piece in lab builds showed the growth is
the sum of several small framework leaks on macOS 26.6 that fire on every
view update: SwiftUI Table cell updates, AppKit text-cell KVO
dependencies (NSKeyValueDependency) and an ObservationRegistrar created
inside DesignLibrary. With the window closed every counter stayed flat;
minimized or fully covered, SwiftUI kept updating and kept leaking.

- WindowVisibility tracks whether any TopManager window is on screen
  (occlusion state; the status item's host window is ignored).
- SystemMonitor records into an always-current LiveState and only
  mirrors it into the @Published properties while something is visible.
  Resuming publishes everything at once, so views catch up immediately.
  Metrics history, alerts and the menu bar keep running while paused.
- MenuBarStatus carries the pre-formatted menu-bar strings, republished
  only when the visible text changes.
- AlertCenter assigns healthScore/diagnosis only when they change.
- Monitoring now starts in applicationDidFinishLaunching instead of the
  main window's onAppear, so it also runs when the app launches with no
  window (state restoration after the window was closed).

Also fixes a visible bug: ProcessMonitor's lightweight refresh (2 of
every 3 fetches) emitted 0 for memory, CPU and threads instead of the
last known values, so ~90% of rows showed "Zero KB" most of the time.

Verified in an isolated lab instance: minimized or covered, all leak
counters stay flat while the menu-bar title keeps updating; a partially
visible window updates every tick. Growth while the window is actually
visible (~190 KB/min) is unchanged — those leaks are in Apple's
frameworks. 58/58 tests pass, including a regression test that fails on
the old code (472 of 487 own processes reported 0 memory).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-16 13:53:10 +02:00

180 sor
6.0 KiB
Swift

import SwiftUI
/// The compact menu-bar title. Renders whichever metric the user picked.
/// Reads `MenuBarStatus`, not `SystemMonitor`, so it keeps updating while the
/// monitor's view publishing is paused.
struct MenuBarLabel: View {
@ObservedObject var status: MenuBarStatus
@ObservedObject var settings: AppSettings
@ObservedObject var alerts: AlertCenter
var body: some View {
switch settings.menuBarMetric {
case .cpu:
textOrIcon(status.cpuText, icon: "cpu")
case .memory:
textOrIcon(status.memoryText, icon: "memorychip")
case .health:
Text("♥ \(alerts.healthScore)").monospacedDigit()
case .download:
textOrIcon(status.downloadText, icon: "arrow.down")
}
}
@ViewBuilder private func textOrIcon(_ text: String?, icon: String) -> some View {
if let text { Text(text).monospacedDigit() } else { Image(systemName: icon) }
}
}
struct MenuBarView: View {
@EnvironmentObject var monitor: SystemMonitor
@EnvironmentObject var alertCenter: AlertCenter
private var topCPU: [ProcessItem] {
monitor.processes.sorted { $0.cpuUsage > $1.cpuUsage }.prefix(3).map { $0 }
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: "heart.text.square")
.frame(width: 16)
.foregroundColor(healthColor)
Text("Health:")
Spacer()
Text("\(alertCenter.healthScore)/100")
.monospacedDigit()
.foregroundColor(healthColor)
if alertCenter.activeAlertCount > 0 {
Image(systemName: "bell.badge.fill")
.foregroundColor(.orange)
}
}
Divider()
if let cpu = monitor.cpuInfo {
HStack {
Image(systemName: "cpu")
.frame(width: 16)
Text("CPU:")
Spacer()
Text(String(format: "%.1f%%", cpu.globalUsage))
.monospacedDigit()
}
}
if let mem = monitor.memoryInfo {
HStack {
Image(systemName: "memorychip")
.frame(width: 16)
Text("Memory:")
Spacer()
Text(String(format: "%.1f%%", mem.usagePercentage))
.monospacedDigit()
}
}
if let net = monitor.networkInfo {
HStack {
Image(systemName: "arrow.down.circle")
.frame(width: 16)
Text("Download:")
Spacer()
Text(formatBytesPerSecond(net.totalDownloadRate))
.monospacedDigit()
}
HStack {
Image(systemName: "arrow.up.circle")
.frame(width: 16)
Text("Upload:")
Spacer()
Text(formatBytesPerSecond(net.totalUploadRate))
.monospacedDigit()
}
}
if let gpu = monitor.gpuInfo {
HStack {
Image(systemName: "gpu")
.frame(width: 16)
Text("GPU VRAM:")
Spacer()
Text(formatBytes(gpu.vramUsed))
.monospacedDigit()
}
}
if let power = monitor.powerInfo, power.hasBattery {
HStack {
Image(systemName: power.isCharging ? "battery.100.bolt" : "battery.100")
.frame(width: 16)
Text("Battery:")
Spacer()
Text("\(power.currentCharge)%")
.monospacedDigit()
if let toEmpty = power.timeToEmpty {
Text("(\(BatteryMath.formatMinutes(toEmpty)))")
.foregroundColor(.secondary)
}
}
}
if !topCPU.isEmpty {
Divider()
Text("Top CPU Consumers")
.font(.caption)
.foregroundColor(.secondary)
ForEach(topCPU) { process in
HStack(spacing: 6) {
Text(process.name)
.lineLimit(1)
.truncationMode(.middle)
Spacer()
Text(String(format: "%.0f%%", process.cpuUsage))
.monospacedDigit()
.foregroundColor(.secondary)
Button {
_ = monitor.terminateProcess(process.pid, expectedStartTime: process.startTime)
} label: {
Image(systemName: "xmark.circle")
}
.buttonStyle(.borderless)
.help("Quit \(process.name)")
}
}
}
Divider()
Button("Open TopManager") {
NSApp.activate(ignoringOtherApps: true)
if let window = NSApp.windows.first(where: { $0.canBecomeMain }) {
window.makeKeyAndOrderFront(nil)
}
}
Button("Quit") {
NSApplication.shared.terminate(nil)
}
.keyboardShortcut("q")
}
.padding(12)
.frame(width: 240)
}
private var healthColor: Color {
switch alertCenter.healthScore {
case 85...: return .green
case 70..<85: return .mint
case 50..<70: return .orange
default: return .red
}
}
}
#Preview {
MenuBarView()
.environmentObject(SystemMonitor.shared)
}