Keep Cocoa Window Minimized When App Displays NSAlert

179 Views Asked by At

I'm having difficulty determining how to keep an application minimized when it's running and it displays an NSAlert.

The short of it is that I have an application that is transferring data, and it displays an NSAlert when it finishes the transfer. If the app is minimized, I want it to remain that way even though it displays the NSAlert.

I call the NSAlert with (in Swift):

alert.beginSheetModalForWindow(self, completionHandler: handler)

But the window unminimizes itself when this code fires. If the window is already not minimized, it doesn't grab focus or do anything unexpected (which is the way I want it).

Is there a way to accomplish this? Thanks!

1

There are 1 best solutions below

0
On

You can try to delay displaying NSAlert.

Put your alert code in method. When there is a trigger to call this method check if your window is visible. If yes -> call this method. If not -> schedule an NSTimer that will fire after a second and call custom method to check if window is visible (YES -> display alert, NO -> schedule timer again).

FYI: NSAlert when displayed as sheet creates instance of NSPanel which is a subclass of NSWindow.