Display modal NSWindow, without activating other application windows

I have an NSStatusItem that displays correctly in the MenuBar. One of the elements (when pressed) displays the modal NSWindow from my application, which is designed to perform a one-time task, then disappears. (For example, the user enters a small bit of text, clicks Save, and the modal NSWindow disappears.)

The problem occurs when the application is running in the background. The modal window is correctly displayed so that the application is running in the foreground, but when the user clicks the "Save" button, the remaining application windows also become active. This is undesirable since the user must click back into any application that they used. (Destroying the convenience of NSStatusItem.) I show a modal window using:

 [myWindow setFrame:finalRect display:YES animate:NO]; [myWindow setLevel:NSPopUpMenuWindowLevel]; [NSApp runModalForWindow:myWindow]; 

Is there a way to prevent clicks / events in my popup to make other applications become active? Or a way to let NSApp know that this particular panel should not automatically activate the rest of the application? Thanks!

+6
xcode cocoa modal-dialog macos nswindow
source share
4 answers

Instead of creating an NSWindow create an NSPanel with the NSNonactivatingPanelMask style. Then you can do the usual makeKeyAndOrderFront: and orderOut: to show / hide the panel as needed.

+16
source share

Ken Thomases' solution on the cocoa -dev list a couple of years ago also looks here:

 [[NSApplication sharedApplication] hide:self]; [[NSApplication sharedApplication] performSelector:@selector (unhideWithoutActivation) withObject:nil afterDelay:0.05]; 

Which theoretically tells the application to hide itself and show at the bottom of the window stack.

You can also capture the mouse click event and use [NSApp preventWindowOrdering]

+1
source share

NSApp beginModalSessionForWindow, runModalSession, endModalSession are the methods you need.

See here, for example, how to use it: Creating a fully customized NSAlert

+1
source share

You can try something like:

 ... if ([NSApp isHidden]) [myWindow makeKeyAndOrderFront:self]; else [NSApp runModalForWindow:myWindow]; ... 

and upon completion:

 ... if ([NSApp isHidden]) [myWindow orderOut:self]; else [NSApp stopModal]; ... 
0
source share

All Articles