Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an NSWindow that floats over all other windows in the app but not over windows from other apps

Tags:

cocoa

nswindow

I am trying to make my NSWindow always show up on top in my application but I don't want it to float over other apps that have become active. I have tired the following code but this makes the window float over other applications:

NSRect frame = NSMakeRect(100, 100, 800, 800);
myWindow  = [[NSWindow alloc] initWithContentRect:frame
                                                styleMask:NSBorderlessWindowMask
                                                  backing:NSBackingStoreBuffered
                                                    defer:NO];
[myWindow setLevel:NSFloatingWindowLevel  ];
[myWindow setBackgroundColor:[NSColor blueColor]];
[myWindow makeKeyAndOrderFront:NSApp];

I have also tried all of the constants listed in the NSWindow documentation and did not find one that would make the NSWindow float over other windows in my but not other windows of other active apps. Is this not possible?

like image 761
Mike2012 Avatar asked May 29 '14 22:05

Mike2012


1 Answers

There's no built-in support for that. You might consider setting your window to hide on deactivate.

Alternatively, you can have the window controller observe the NSApplicationWillResignActiveNotification and NSApplicationDidBecomeActiveNotification notifications and adjust the window level. When your app is about to resign active status, you set the window level back to normal. When it becomes active again, you set it to float. (If the window is controlled by the app delegate, then you can do this in the -applicationWillResignActive: and -applicationDidBecomeActive: delegate methods.)

like image 70
Ken Thomases Avatar answered Sep 30 '22 04:09

Ken Thomases