r/swift • u/Affectionate_Pea3516 • 5h ago
Sysbar: a menu bar system monitor built with SwiftPM only (no .xcodeproj), AppKit + SwiftUI, IOKit/Mach APIs. Open source, plus a few lessons learned
I've been building **Sysbar**, a small macOS menu bar system monitor (CPU with E/P-core split, GPU, memory, disk, network, top processes). It's open source under Apache-2.0, and I thought the implementation might interest people here.
**Repo:** https://github.com/rodrigotrombeta/Sysbar
**Project setup: no Xcode project at all**
- It's a plain SwiftPM `executableTarget` (about 4.3k lines), linking `IOKit` and `ServiceManagement`.
- A ~30-line `build.sh` runs `swift build -c release`, assembles the `.app` bundle by hand (binary, `Info.plist`, icon), ad-hoc signs it with `codesign --sign -` and installs it. You only need the Command Line Tools.
- Release builds are universal: `swift build --arch arm64 --arch x86_64`, then packaged into a DMG.
**Where the data comes from (public APIs only, no sudo, no helper)**
- CPU: `host_processor_info` for per-core ticks, split into E-cores and P-cores on Apple Silicon.
- GPU: `IOServiceMatching("IOAccelerator")`, reading the `PerformanceStatistics` dictionary ("Device Utilization %" and in-use memory).
- Memory: `host_statistics64(HOST_VM_INFO64)`.
- Disk I/O: the `IOBlockStorageDriver` statistics.
- Top processes: `proc_listallpids` + `proc_pid_rusage(RUSAGE_INFO_V4)`.
- Launch at login: `SMAppService.mainApp`.
**UI**
- An `NSStatusItem` with an icon drawn into an `NSImage(size:flipped:drawingHandler:)` on every tick.
- The panels are SwiftUI hosted in an `NSPanel` subclass. A small `NSHostingView` subclass overrides `acceptsFirstMouse` so buttons respond to the first click even when the panel isn't key.
**One lesson that surprised me**
My sparklines started out as SwiftUI `Canvas`. In a panel that stays pinned open and redraws every second, `Canvas` re-rendered through GPU-backed buffers and added **tens of MB** of memory. Switching to plain `Shape` paths (`SparklineShape` filled with a gradient) brought the app back to about **25 MB of RAM**. If you're building something that redraws constantly, measure `Canvas` before committing to it.
**Credits:** the monitoring logic started from the System Stats panel in [oMLX](https://github.com/jundot/omlx), and the menu bar icon is inspired by [Stats](https://github.com/exelban/stats).
I'd appreciate a code review from people who know AppKit better than I do, especially on the `NSPanel`/SwiftUI hosting and hover-tracking parts. Issues and PRs are welcome.
