在 Swift 中重新激活 NSApplication 并取消隐藏 NSWindow 作为菜单栏应用程序?

Reactivate NSApplication and Unhide NSWindow As a Menubar App in Swift?

我有一个菜单栏应用程序,我可以停用该应用程序并关闭 window。它仍然停留在 WiFi 图标旁边的菜单栏上。但是,当我 select 从菜单栏显示菜单时,我在重新激活应用程序并再次显示 window 时遇到问题。

这是相关代码。这应该很简单,但我确实遗漏了一些东西...

    var windows:[NSWindow] = []

    func applicationDidFinishLaunching(_ notification: Notification) {
        windows = NSApplication.shared.windows
    }

    func hide() {
        for window in windows {
            window.close()
        }
        NSApplication.shared.hide(self)
        NSApplication.shared.deactivate()
    }

    func show() {
        NSApplication.shared.activate(ignoringOtherApps: true)
        NSApplication.shared.unhide(self)
        for window in windows {
            window.display()
        }
    }

这使 window 回来了。

window.makeKeyAndOrderFront(nil)
NSApp.shared.activate(ignoringOtherApps: true)