Scrollr is a small open-source menu bar app that reverses mouse wheel scrolling but leaves the trackpad alone. It has a "Show in menu bar" toggle, and opening the app again from Finder should bring a hidden icon back. That simple feature hit two SwiftUI surprises in a row.
The setup everyone uses
// App
@AppStorage("showIcon") private var showIcon = true
MenuBarExtra(isInserted: $showIcon) { ... }
// AppDelegate
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows: Bool) -> Bool {
UserDefaults.standard.set(true, forKey: "showIcon")
return true
}
It passed my tests, because I always started with the icon visible and hid it from Settings. Then I tried the case I'd skipped: launch with the icon already hidden, then reopen. The handler ran (I logged it), the value became true, and it was false again within 100 ms. Deferring the write by 0.3 s didn't help. Oddly, a defaults write from Terminal while the app was running did stick.
Bug 1: who is writing false?
I added a KVO observer on the key and logged Thread.callStackSymbols on every change. The false came from SwiftUI itself:
-[NSUserDefaults setObject:forKey:] // showIcon = false
UserDefaultLocation.set(_:transaction:)
Binding.wrappedValue.setter
MenuBarExtraController.observeValue(forKeyPath:of:change:context:)
NSKeyValueDidChange
_NSSetBoolValueAndNotify
MenuBarExtraController.updateConfiguration(_:environment:)
AppMenuBarExtrasController.updateMenuBarExtras(namespace:sceneList:)
AppDelegate.appGraphDidChange() // SwiftUI's, not mine
GraphHost.flushTransactions()
During a normal graph update, MenuBarExtraController sets a Bool on its status item, its own KVO observer fires, and it writes the item's state back through the isInserted binding. The scene still had the old value at that moment, so what went back was false. (That last part is my reading of the stack; I can't see SwiftUI's source.)
The fix: stop having two writers. One @Published property owns the flag, and reopen, the Settings toggle and Restore Defaults all go through it.
Bug 2: the fix pinned the CPU
My first version bound the property directly:
MenuBarExtra(isInserted: $app.showIcon) { ... }
A code review caught that Scrollr now sat at about 97% CPU while idle. A CPU sample showed the cycle:
-
MenuBarExtrawritesisInsertedback on every update (bug 1, same behaviour). -
@Publishedannounces a change even when the new value equals the old one. - The scene updates again, so go to 1.
The final version skips equal writes:
final class Controller: ObservableObject {
static let shared = Controller()
@Published var showIcon: Bool {
didSet { UserDefaults.standard.set(showIcon, forKey: "showIcon") }
}
// ...
}
@main
struct ScrollrApp: App {
@NSApplicationDelegateAdaptor private var delegate: AppDelegate
@ObservedObject private var app = Controller.shared
var body: some Scene {
// MenuBarExtra writes isInserted back on every update, so skip equal writes
MenuBarExtra(isInserted: Binding(
get: { app.showIcon },
set: { if $0 != app.showIcon { app.showIcon = $0 } }
)) { ... } label: { ... }
Settings { ... }
}
}
// AppDelegate
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows: Bool) -> Bool {
app.showIcon = true
DispatchQueue.main.async { [self] in // after the label has handed over openSettings
NSApp.activate()
openSettings?()
}
return true
}
Back to 0% at idle, and a hidden icon comes back on reopen.
Two smaller things from the same code
-
OpenSettingsActiononly exists in the SwiftUI environment, so an app delegate can't create one. The menu bar label captures it in.onAppearand hands it to the delegate, which calls it on the next run-loop turn after showing the icon. - AppKit also stores the status item's visibility in your app's defaults (
NSStatusItem VisibleCC Item-0on macOS 27). Deleting it didn't affect this bug, but it's worth knowing when you're poking at menu bar state.
Takeaways
- If a binding keeps "un-setting" itself, log the call stack from a KVO observer on the backing key. It shows who did it in one run.
- Any SwiftUI control that writes its binding back during updates plus a
@Publishedproperty means a feedback loop. Guard the setter with an equality check.
Tested on macOS 27.2 with Xcode 27.2. The fix is in commits 000fd62 and fd29bb8 of https://github.com/atuuuuuuu/Scrollr (ScrollrApp.swift, Controller.swift). Scrollr is MIT licensed:
brew install --cask atuuuuuuu/tap/scrollr
Written with help from an AI assistant; the stack trace, measurements and code are from the real app.
Top comments (0)