0

I am having an issue closing an NSWindow when the second screen is disconnected from my Mac.

I am listening for NSApplicationDidChangeScreenParametersNotification and checking the number of screens connected.

If only 1 screen exists I call [secondScreenWindow close]. This is an instance of NSWindow.

But all that happens is the second screen window moves to the main screen and because it is set to NSScreenSaverWindowLevel it just covers up the entire desktop and hangs Xcode.

I have to hard reboot the Mac to get it to go away.

Called when Notification fires:

-(void)configureExternalDisplays:(NSNotification*)notification
{
    NSLog(@"Notification: %@",[notification description]);

    NSUInteger screenCount = [[NSScreen screens] count];

    if (secondScreenWindow)
    {
        NSLog(@"Remove second window");
        [secondScreenWindow close];
        secondScreenWindow = nil;
    }

    if (screenCount >1)
    {
        NSScreen *screen = [[NSScreen screens] objectAtIndex:1];
        NSRect mainDisplayRect = [screen frame];
        secondScreenWindow = [[NSWindow alloc] initWithContentRect: mainDisplayRect styleMask:NSWindowStyleMaskBorderless
                                                           backing:NSBackingStoreBuffered defer:YES];
        [[secondScreenWindow contentView] setWantsLayer:TRUE];
        [secondScreenWindow setBackgroundColor:[NSColor blackColor]];
        [secondScreenWindow setOpaque:YES];
        [secondScreenWindow setHidesOnDeactivate:NO];
        [secondScreenWindow makeKeyAndOrderFront:nil];
        [secondScreenWindow setLevel:NSScreenSaverWindowLevel];

        NSLog(@"Second screen resolution. Width: %f. Height: %f",[[secondScreenWindow contentView] frame].size.width,[[secondScreenWindow contentView] frame].size.height);

        [secondScreenWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces|NSWindowCollectionBehaviorFullScreenAuxiliary|NSWindowCollectionBehaviorStationary];

        [secondScreenWindow makeKeyAndOrderFront:self];

        txCountdown = [[NSTextView alloc] initWithFrame:NSMakeRect(0,([[secondScreenWindow contentView] frame].size.height-([[secondScreenWindow contentView] frame].size.width*0.42))/2,[[secondScreenWindow contentView] frame].size.width,[[secondScreenWindow contentView] frame].size.width*0.42)];
        [txCountdown setBackgroundColor:[NSColor clearColor]];
        [txCountdown setTextColor:[NSColor whiteColor]];
        [txCountdown setString:@"00:00"];
        int fontSize = [[secondScreenWindow contentView] frame].size.width/3;
        [txCountdown setFont:[NSFont fontWithName:@"Helvetica Neue" size:fontSize]];
        [txCountdown sizeToFit];
        [txCountdown setAlignment:NSTextAlignmentCenter];
        [[secondScreenWindow contentView] addSubview:txCountdown];
    }

}

As you can see I am checking the screen count and either creating or destroying the second NSWindow as needed.

Creation works fine. But destruction doesn't.

Any pointers in the right direction most appreciated.

Thanks in advance

Ben

Ben O'Dwyer
  • 69
  • 1
  • 7
  • Have you tried the Core Graphics function mentioned in the accepted answer of [this question](https://stackoverflow.com/questions/18042641/notification-when-display-gets-connected-or-disconnected)? – mschmidt Feb 20 '18 at 22:29
  • How is `secondScreenWindow` declared? Tip: use a small window for debugging. – Willeke Feb 22 '18 at 15:48
  • secondScreenWindow is declared in the .h Using NSWindow *secondScreenWindow; – Ben O'Dwyer Feb 22 '18 at 19:17

0 Answers0