0
votes

I added a menu bar icon that opens a popover. It works ok but when I close the app via the red cross and try to open the app again with a click on the dock icon it opens the application window (as it should) but also the menu bar popover (which it should not). How can I only open the application window and not the popover when I click on the dock icon? My code that handles the opening of when clicking the dock icon looks like this:

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
    if !flag {
        for window: AnyObject in sender.windows {
            window.makeKeyAndOrderFront(self)
        }
    }
    return true
}
1

1 Answers

0
votes

I met this problem today too. I had the same code so I tried to detect somehow if window is popover but failed. Also I found that if you close main window after start and then click dock icon, if you print sender.windows, it shows 3 items (in my case): <NSStatusBarWindow: 0x101300110>, <NSStatusBarWindow: 0x101108800>, <NSWindow: 0x6080001e0400>and opens only main window, without popover, but if popover was opened once, then clicking on dock icon (when the main window is closed) causes show of both the main window and the popover. And print(window) in the for cycle now shows 4 items - the last is <_NSPopoverWindow: 0x1011284b0>.
And also I printed popover.isShown, and it says false even after the popover is opened by clicking the dock icon. I couldn't find a way to detect and ignore this particular window.

So the only way I found is to replace

    for window: AnyObject in sender.windows {
        window.makeKeyAndOrderFront(self)
    }

with

sender.windows[2].makeKeyAndOrderFront(self)

because every time the main window appears on the third place
Hope there's a better way and someone will teach us.