deminiaturize NSWindow without making it key

2019-06-09 07:21发布

问题:

I have main window and several child windows, i would like to show main window when user click on dock icon, but without making it a key window, if there just was one (it will become automatically key if there is no key window).

Here is current code:

if ( fMainWinDelegate ) {
    if (not [NSApp keyWindow]) {
        NSLog(@"AppDelegate::applicationShouldHandleReopen [fMainWinDelegate showWindow]");
        [fMainWinDelegate showWindow];
    }
    else {
        if ([fMainWinDelegate.window isMiniaturized]) {
            NSLog(@"AppDelegate::applicationShouldHandleReopen [fMainWinDelegate.window deminiaturize:self]");
            [fMainWinDelegate.window deminiaturize:self];
            //this one is not working, it make window also the key window
        }
        else if (not [fMainWinDelegate.window isVisible] && not [fMainWinDelegate.window isMiniaturized]) {
            NSLog(@"AppDelegate::applicationShouldHandleReopen [fMainWinDelegate.window orderFront:self]");
            [fMainWinDelegate.window orderFront:self];
        }
    }
}

回答1:

I've ended with pretty simple solution:

if ( fMainWinDelegate ) {
    if (not [NSApp keyWindow]) {
        NSLog(@"AppDelegate::applicationShouldHandleReopen [fMainWinDelegate showWindow]");
        [fMainWinDelegate showWindow];
    }
    else {
        if ([fMainWinDelegate.window isMiniaturized]) {
            NSLog(@"AppDelegate::applicationShouldHandleReopen [fMainWinDelegate.window deminiaturize:self]");
            // save current key window
            NSWindow *currKey = [NSApp keyWindow];
            [fMainWinDelegate.window deminiaturize:self];
            // restore saved key window
            [currKey makeKeyWindow];
        }
        else if (not [fMainWinDelegate.window isVisible] && not [fMainWinDelegate.window isMiniaturized]) {
            NSLog(@"AppDelegate::applicationShouldHandleReopen [fMainWinDelegate.window orderFront:self]");
            [fMainWinDelegate.window orderFront:self];
        }
    }
}