Menu Bar Popover Opens on Dock Icon Click OSX - swift

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
}

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.

Related

Prevent macOS app from staying in 'Recent' section of Dock

My application is an Agent (doesn't show up in dock) until one specific window is shown, before showing that window I set the following to let the dock display the icon:
NSApp.setActivationPolicy(.regular)
When the window gets closed I set the following to hide the icon from the dock:
NSApp.setActivationPolicy(.accessory)
It works on most of the machines, however on others the app icon stays in dock in 'Recent' section with no 'active' indicator. I can manually remove it via right click and selecting 'remove from dock' but it appears again the next time the window is open. Is it even a 'Recent' section? What's the API to either remove the app from there or prevent it from being added to dock when it's open?
Thank you!

how to keep the dockMenu open when clicking items in cocoa app

i have items in my dock menu that needs the dock menu to stay open
when the items are clicked
im refering to when u right click on an app and you get the dock menu
automaticly, it closes it
anyway to override this?

close sidr menu when user clicks link

I'm developing a site that uses the sidr menu. This site also uses modal windows to display content. when a user presses a menu item and it opens a modal window, the window opens below the sidr menu.
should I:
A. stack the modal window above the slidr menu so the entire contents can be seen? This might work as the toggle menu button is covered.
B. Force the menu to close so the modal window is not closed.
I thought changing the z-index of the modal window would work but I can't get it to load above the menu. the z-index of the menu is 999999. I tried 1,000,000 for the modal window, but it still loads below the menu.
what else can I try?

Cocoa Swift: Connect Main Menu to Preferences Window Controller & Disabling Items like Minimise in Attributes Inspector doesn't work in Xcode?

I have a simple Menubar app in which it has 2 options - namely Preferences & Quit
After clicking on Preferences it opens a new window but doesn't show the menu bar
I tried connecting from Main Menu to the Window Controller but it doesn't work.
I drag it from the Application Name to the Window Controller or View Controller using Ctrl+Drag but still it doesn't work.
Another issue is my Preferences Window is resizing even though I unchecked the resize button in the Attributes Inspector.
Even Maximise & Minimise checkboxes are disabled but they are still showing.
Any solutions?
See NSApplication.activationPolicy.
If you want the app's main menu bar to appear, you'll need to change the activationPolicy to NSApplicationActivationPolicyRegular. Once you do that, the app's main menu will appear when your app is the active one. (It will also appear in the dock.)
If you want this to happen only when your preferences window is open, then you'll need to change the activationPolicy when the window opens, then set it back again when it closes.
You don't need to connect any outlets for this; the Main Menu should already be a resource for your app and should be loaded automatically when your app launches.
I think I had given Storyboard Id to my View Controller rather than my Window Controller so it wasn't working.
I also checked some apps like Dropbox which didn't have Main Menu Bar because it was a background or accessory app so I ditched it.
Some apps like Focus have it though. But I went with the popular choice.
As for no resizing, no minimising & no maximising goes, I pasted below code & it worked
var myWindow: NSWindow? = nil
myWindow?.styleMask.remove(NSWindow.StyleMask.resizable)
myWindow?.standardWindowButton(.miniaturizeButton)?.isEnabled=false
myWindow?.standardWindowButton(.zoomButton)?.isEnabled=false

NSWindow makeKey() in macOS not working

In my menu bar app, I have a menu item which opens a window. The window opens in the front with the window behind it remaining the key window.
I can't get window.makeKey() to work unless I run it after a breakpoint. I tried putting the line of code in different places in the window's life cycle and even put it in DispatchQueue.main.asyncAfter(deadline: execute:) and it didn't work.
I have also tried using window.makeKeyAndOrderFront(sender:). The property window.canBecomeKey is true.
Turns out I needed to put this line in the IBAction for the menu item that opens the window:
NSApplication.shared.activate(ignoringOtherApps: true)
That makes it the key window when it opens, so I don't have to call window.makeKey()