Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping window on top when switching spaces

I have created a window using -[NSWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces].
It only does half of what I want, though: when I switch spaces, the window also switches spaces (as expected), but my window moves to the back behind all other windows in that space. This is especially bad because my app is active but its window is below all other apps' windows. I tried changing the level to NSFloatingWindowLevel, and that does keep it on top, but then it loses key status (focus) when switching spaces.

I tried NSWindowCollectionBehaviorMoveToActiveSpace for the collection behaviour but it's definitely not what I'm looking for.

Is there hope? I know there are almost no other APIs that relate to Spaces.

like image 824
George Avatar asked Feb 20 '11 21:02

George


1 Answers

Spaces is a pain. My solution was to register for a change notification like so:

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self 
                             selector:@selector(activeSpaceDidChange:)
                                 name:NSWorkspaceActiveSpaceDidChangeNotification 
                               object:nil];

Then in my WindowController class:

- (void) activeSpaceDidChange:(NSNotification *)aNotification {
    if ([NSApp isActive]) [[self window] orderFront:self];
}
like image 70
Francis McGrew Avatar answered Oct 23 '22 04:10

Francis McGrew