I've been building **Sysbar**, an open-source (Apache-2.0) macOS menu bar system monitor, with the UI entirely in SwiftUI hosted in AppKit panels. A few patterns from it might be useful to others doing SwiftUI outside a normal window.
**Repo:** https://github.com/rodrigotrombeta/Sysbar
**1. Hover a section and a detail panel opens beside it**
The main panel is a SwiftUI stack of sections (CPU, GPU, memory, disk, network). Hovering a section opens a detail window aligned with it, and clicking pins that window open.
SwiftUI's `onHover` wasn't reliable in a non-key `NSPanel`, so the tracking happens in AppKit. Each section reports its frame through a `PreferenceKey`:
struct SectionFramesKey: PreferenceKey {
static var defaultValue: [SystemStatKind: CGRect] = [:]
static func reduce(value: inout [SystemStatKind: CGRect],
nextValue: () -> [SystemStatKind: CGRect]) {
value.merge(nextValue()) { $1 }
}
}
// on each section:
.background(GeometryReader { geo in
Color.clear.preference(key: SectionFramesKey.self,
value: [kind: geo.frame(in: .global)])
})
The root view forwards the result with `.onPreferenceChange` to the window controller. There, an `NSView` container rebuilds its `NSTrackingArea`s, and the frames also tell it where to place the detail window.
**2. A scroll view that sizes to its content up to a limit**
The panel should be exactly as tall as its content, but scroll once it hits the screen's height. To do that, a second `PreferenceKey` measures the content height inside the `ScrollView`, and the scroll view gets `.frame(height: min(contentHeight, maxHeight))`. Scroll indicators only show when the content actually overflows.
**3. First click on a non-key panel**
Buttons in an `NSHostingView` inside an `NSPanel` ignored the first click. Fixing it took a tiny subclass that overrides `acceptsFirstMouse(for:)` to return `true`.
**4. Sparklines as `Shape` instead of `Canvas`**
With redraws every second in a pinned panel, `Shape` paths used far less memory than `Canvas`. The whole app runs at about 25 MB.
**Where I'd like feedback**
- State lives in a single `ObservableObject` with `@Published` snapshots updated every tick. Is it worth migrating to `@Observable` for a view tree that updates this often?
- When the language or appearance changes, I force a full rebuild with `.id(model.revision)`. It works, but it feels like a hack. How do you handle a runtime language switch without restarting the app?
Credits: the stats panels started from [oMLX](https://github.com/jundot/omlx), and the menu bar icon is inspired by [Stats](https://github.com/exelban/stats).