macoscocoamission-control

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.


Solution

  • 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];
    }