Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically closing an NSWindow when it loses focus

I am making an image picker that will display an n by n grid of selectable button when the picker is popped up. This grid of buttons will be contained within an NSWindow but I would like for the window to be close automatically if the user clicks off the screen. Is there a flag that can be set so that when the window looses focus it will be closed automatically?

like image 491
Mike2012 Avatar asked Dec 23 '09 00:12

Mike2012


2 Answers

There are two notifications that you may be interested in: NSWindowDidResignKeyNotification and NSWindowDidResignMainNotification. You can simply register for the one you're interested in in awakeFromNib (or windowDidLoad if you have a custom controller) and then close or hide the window as appropriate when you receive the notifications.

I won't delve too much into whether or not this is a good idea from UI standpoint. But, it might be a better idea to have either an overlay view or a panel for the functionality you describe.

like image 117
sbooth Avatar answered Sep 22 '22 01:09

sbooth


You might check out NSPanel. It's an NSWindow subclass that will hide itself when the app is in the background, and that behavior sounds very similar to what you are looking for.

like image 41
Vincent Gable Avatar answered Sep 20 '22 01:09

Vincent Gable