关闭电缆断开连接NSWindow

时间:2018-02-20 14:13:38

标签: objective-c macos cocoa nsnotificationcenter nswindow

当第二个屏幕与我的Mac断开连接时,我在关闭NSWindow时出现问题。

我正在侦听NSApplicationDidChangeScreenParametersNotification并检查连接的屏幕数量。

如果只有1个屏幕,我会拨打[secondScreenWindow close]。这是NSWindow的一个实例。

但所有发生的事情都是第二个屏幕窗口移动到主屏幕,因为它设置为NSScreenSaverWindowLevel,它只是覆盖整个桌面并挂起Xcode。

我必须重新启动Mac才能让它消失。

通知触发时调用:

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

}

正如您所看到的,我正在检查屏幕数量,并根据需要创建或销毁第二个NSWindow。

创作很好。但是破坏并没有。

正确方向的任何指针最受欢迎。

提前致谢

0 个答案:

没有答案