1

I have created a menu bar app for Mac. I want to open the app when clicking on menu bar icon. After updating the OS to Ventura (13.4 (22F66)) I am observing an issue where the NSWindowController is getting displayed on launching the app.

enter image description here

I was able to dismiss the window by adding this flag but now it's not working anymore.

Now I am seeing an error in console on launching the app.

[Window] Warning: Window NSWindow 0x119e17510 ordered front from a non-active application and may order beneath the active application's windows.

class AppDelegate: NSObject, NSApplicationDelegate {

    let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        statusItem.button?.image = NSImage(systemSymbolName: "book", accessibilityDescription: "Open")
        statusItem.button?.toolTip = "Demo"
        statusItem.button?.target = self
        statusItem.button?.action = #selector(showSettings)
        
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
        return true
    }
    
    @objc func showSettings() {
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        guard let vc = storyboard.instantiateController(withIdentifier: "ViewController") as? ViewController else {
            fatalError("Unable to find ViewController in the storyboard.")
        }

        guard let button = statusItem.button else {
            fatalError("Couldn't find status item button.")
        }

        let popoverView = NSPopover()
        popoverView.contentViewController = vc
        popoverView.behavior = .transient
        popoverView.show(relativeTo: button.bounds, of: button, preferredEdge: .maxY)
    }

}
subin272
  • 733
  • 6
  • 24
  • Do you want to close the window or do you want to not show the window at launch? What do you mean by "open the app"? Isn't the app running? Post a [mre] please. How is the window created and dismissed? – Willeke Jun 20 '23 at 07:27
  • @Willeke I do not want to show the window at launch. Instead I would like to open the app when I tap on the app icon in Status bar. Otherwise the app is working fine. I didn't see this behavior earlier so not sure if something got changed. Will share the code snippet of how I am launching it. – subin272 Jun 20 '23 at 16:33
  • Please do not confuse app and window. Do you load a `NSWindowController` or a `NSViewController`? Is there a Window Controller in the storyboard with "Is Initial Controller" switched on? – Willeke Jun 20 '23 at 18:56
  • @Willeke yes there is an NSWindowController which is set as "Is Initial Controller" – subin272 Jun 20 '23 at 19:53
  • 1
    Is this the window you don't want? Do you use this window controller? What happens if you remove it or switch "Is Initial Controller" off? – Willeke Jun 21 '23 at 07:00
  • @Willeke unchecked the "Is Initial Controller" for NSWindowController and now it works. The window doesn't show up on launch. Thanks. – subin272 Jun 21 '23 at 12:56

0 Answers0