Question

I've seen the post on how to move a window to a different NSScreen but that's not exactly what I'm looking for (and I'm not sure the answers there are actually the right way to do it in any case).

When I call makeKeyAndOrderFront, I want the window to show on the screen with the menu bar - because it's an About window which means the user had to select the About menu item (the window isn't user movable) and the pointer will be right there. The default behavior of course is to have the window show up on the screen that has key focus.

Now, I could calculate it's frame rect based on [[NSScreen screens] objectAtIndex:0] which is apparently the window with the menu bar. But 1) that seems like a hack which should be something very simple and 2) not sure that's going to work on Mavericks where the menu bar will move around.

Am I missing something fundamental? Because it sure seems like it.

Was it helpful?

Solution 2

I've decided to go another way, but I did find that if you make your window an NSPanel instead of an NSWindow it seems to go to the main screen (with the menu bar). Not sure what it would do on Mavericks however.

OTHER TIPS

Since you're sure the window will be created by mouse aciton, you could find the screen which contains the mouse pointer rather than looking for the menu bar:

NSScreen * mouseScreen = nil;
NSPoint mousePoint = [NSEvent mouseLocation];
for( NSScreen * screen in [NSScreen screens] ){
    if( NSMouseInRect(mousePoint, [screen frame], NO) ){
        mouseScreen = screen;
        break;
    }
}

You can subclass NSWindow and constrain its frame to the main screen.

- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen {
    return [super constrainFrameRect:frameRect toScreen:[NSScreen mainScreen]];
}

[NSScreen mainScreen] does not refer to the computers main screen, but the screen which contains the window currently receiving keyboard events.

Note the the window does not move to a different screen if opened and then reopened.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top