fix(menubar): rebuild the popover content on every open and drop it on close

A popover content view that outlived its window kept re-laying itself out
at display cadence once the popover had been shown during the first load
(loading overlay 520pt tall, then the real height): 6 to 7 percent idle
CPU until relaunch, reproduced on demand. Limiting the hosting controller
to preferredContentSize was not enough on its own. Building the 360pt view
fresh per open and releasing it on close removes the survivor entirely;
the dock's hidden bubble panel gets the same treatment.

Reproduction on the same machine (minute logs, popover opened during the
first load and closed): before 6.1 to 7.1 percent sustained; after 0.1
percent sustained through the data load.
This commit is contained in:
iamtoruk 2026-09-01 02:49:01 -07:00
parent 80330ca03e
commit fdd18e7bb4
2 changed files with 33 additions and 5 deletions

View file

@ -122,7 +122,7 @@ final class CapacityDockController {
pointerInsideDetail = false
hoveredRowProvider = nil
detailPanel?.orderOut(nil)
releaseDetailPanel()
railPanel?.orderOut(nil)
detailPanel = nil
railPanel = nil
@ -240,6 +240,14 @@ final class CapacityDockController {
railPanel = panel
}
/// The bubble is cheap to rebuild and expensive to keep: a hidden, ordered-out
/// bubble panel left the app re-laying it out at display cadence for as long
/// as it lived (0.1 percent idle CPU before the first hover, 6 percent after).
private func releaseDetailPanel() {
detailPanel?.orderOut(nil)
detailPanel = nil
}
private func ensureDetailPanel() {
guard detailPanel == nil else { return }
@ -565,7 +573,7 @@ final class CapacityDockController {
guard let provider, detailPanel?.isVisible == true else {
model.hoveredProvider = nil
detailIsDismissing = false
detailPanel?.orderOut(nil)
releaseDetailPanel()
return
}
if animated {
@ -574,7 +582,7 @@ final class CapacityDockController {
stopDetailMotion()
model.hoveredProvider = nil
detailIsDismissing = false
detailPanel?.orderOut(nil)
releaseDetailPanel()
detailPanel?.alphaValue = 1
}
}
@ -1132,7 +1140,7 @@ final class CapacityDockController {
guard let self, generation == self.detailMotionGeneration else { return }
self.detailMotion = nil
if fadeOut {
self.detailPanel?.orderOut(nil)
self.releaseDetailPanel()
self.detailPanel?.alphaValue = 1
self.model.hoveredProvider = nil
self.detailIsDismissing = false

View file

@ -1398,12 +1398,28 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate, NSM
popover.animates = true
popover.delegate = self
popover.contentViewController = makePopoverContent()
}
/// Built fresh for every show and dropped on close: a popover content view
/// that outlives its window kept re-laying itself out at display cadence
/// after being shown once during the first load (6 percent idle CPU until
/// relaunch). Rebuilding a 360pt view on open is far cheaper than that.
private func makePopoverContent() -> NSViewController {
let content = MenuBarContent()
.environment(store)
.environment(updateChecker)
.frame(width: popoverWidth)
popover.contentViewController = NSHostingController(rootView: content)
let hosting = NSHostingController(rootView: content)
// Let the popover size itself from preferredContentSize only. With the
// default options the hosting view also pushes min/max content sizes at
// its window on every pass, and once the popover has been shown while
// loading (content 520pt tall, then the real height), the closed
// popover keeps re-laying itself out at display cadence: 6 percent idle
// CPU until relaunch.
hosting.sizingOptions = [.preferredContentSize]
return hosting
}
@objc private func handleButtonClick(_ sender: AnyObject?) {
@ -1427,6 +1443,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate, NSM
// closes. The popover's window takes keyboard focus on its own
// via makeKeyAndOrderFront, which is enough for keystrokes to
// reach the SwiftUI content.
if popover.contentViewController == nil {
popover.contentViewController = makePopoverContent()
}
store.menuPopoverVisible = true
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
if let window = popover.contentViewController?.view.window {
@ -1642,6 +1661,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate, NSM
func popoverDidClose(_ notification: Notification) {
store.menuPopoverVisible = false
popover.contentViewController = nil
// Catch up on any menubar title updates that were skipped while the
// popover was anchored.
refreshStatusButton()