macOS: Opening statusbar menu and tapping menu item generate warnings

250 Views Asked by At

This is the same issue as Open a window from menu bar (SwiftUI, MacOS), except the solution doesn't work (anymore?).

First, when I press the icon:

Warning: Window NSMenuWindowManagerWindow 0x7fa7fa823570 ordered front from a non-active application and may order beneath the active application's windows. // This one is gone now

When I tap the menu item, I receive the following warnings:

CGSWindowShmemCreateWithPort failed on port 0

CGSWindowShmemCreateWithPort failed on port 0

CGSWindowShmemCreateWithPort failed on port 0

CGSWindowShmemCreateWithPort failed on port 0

CGSWindowShmemCreateWithPort failed on port 0 // This one is gone now

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

After running the Window logic on main, async after activating the app, the Window warning went away:

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    //...
    @IBAction
    func showPreferences(_ sender: Any) {
        guard let windowController = storyboard.instantiateController(withIdentifier: "DataWindowController") as? NSWindowController else {
            return
        }

        guard let window = windowController.window else {
            return
        }

        if #available(macOS 14.0, *) {
            NSApp.activate()
        } else {
            NSApp.activate(ignoringOtherApps: true)
        }

        DispatchQueue.main.async {
            windowController.showWindow(self)
            window.orderFrontRegardless()
        }
    }
}

So apparently you cannot activate the app in the same runloop "tick" as you show the window but it's OK to do it right after.

But how do I get rid of the NSMenuWindowManagerWindow warning? This is all handled by the app itself through the Main.storyboard.


I managed to get rid of one CGSWindowShmemCreateWithPort failed on port 0 by changing the following line:

-        window!.addTabbedWindow(newWindow, ordered: .above) // old way of adding a tab
+        window!.tabGroup?.addWindow(newWindow) // new way

That was found through using b libsystem_trace.dylib`_os_log_error_impl in the debug console

I was getting error: Driver threw unknown argument: '-Wdeprecated-declarations' without emitting errors. when I added that flag so I didn't find anything new after that.

1

There are 1 best solutions below

1
soundflix On

activate(ignoringOtherApps:) is deprecated starting macOS 14. If are targeting macOS 14, try to use the newer activate().

This thread on Apple developer gives some clues how to get rid of similar warnings or decide ignore with a warning, i.e. considering it "log noise".