How to display NSPopover in the center of the screen?

I have a MacOS menu bar application and I'm trying to display an NSPopover in the center of the screen. This is the code I've come up with so far:

class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {

    private var statusItem: NSStatusItem!
    private var popover: NSPopover!


    @MainActor func applicationDidFinishLaunching(_ notification: Notification) {
        // ... init of statusItem hidden
        self.popover = NSPopover()
        self.popover.contentSize = NSSize(width: 300, height: 300)
        self.popover.behavior = .transient
        self.popover.contentViewController = NSHostingController(rootView: ContentView())
    }

    @objc func togglePopover() {
        if let button = statusItem.button {
            if popover.isShown {
                self.popover.performClose(nil)
            } else {
                if let screen = NSScreen.main {
                    let screenFrame = screen.visibleFrame
                    let x = screenFrame.origin.x + (screenFrame.width - self.popover.contentSize.width) / 2
                    let y = screenFrame.origin.y + (screenFrame.height - self.popover.contentSize.height) / 2
                    let centerPoint = NSPoint(x: x, y: y)
                    popover.show(relativeTo: NSRect(origin: centerPoint, size: CGSize.zero), of: screenFrame, preferredEdge: NSRectEdge.minX)
                }
            }
        }
    }
}

Unfortunately the code above does not compile because the of parameter of popover.show expects an NSView, whereas screenFrame is an NSRect. I've tried creating a view out of the rect but this leads to a run-time error that the view is not contained within a window.

Any thoughts?

Why not create a view that would be positionned at the center of screen. And use it in of: parameter ?

I'm interested in the transient behaviour of a popup.

How to display NSPopover in the center of the screen?
 
 
Q