Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you open an NSWindow on the screen with the menu bar

Tags:

cocoa

nswindow

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.

like image 294
machomeautoguy Avatar asked Dec 06 '25 05:12

machomeautoguy


1 Answers

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;
    }
}
like image 200
jscs Avatar answered Dec 08 '25 14:12

jscs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!