sharingType property for NSWindow works but property value is not updated correctly

55 Views Asked by At

I am trying to cut off my window from the screenshot on macOS. It is working perfectly fine. But when I am trying to get its value, which I was setting for sharingType (NSWindowSharingNone or NSWindowSharingReadOnly), I am getting 0 (NSWindowSharingNone) all the time after the second iteration. Here is the code that I have:

@interface ViewController () {
    BOOL sharedState;
}

@property (strong) NSWindow* childWindow;
@property (strong) NSTimer* timer;
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createWindowAction: nil];
    
    sharedState = YES;
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(triggerAction:) name: @"NotificationMessageEvent" object: nil];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval: 4.0f repeats: YES block:^(NSTimer * _Nonnull timer)
    {
        self->sharedState = !self->sharedState;
        
        NSLog( @"Before sharing type was: %lu", (unsigned long)[self.childWindow sharingType] );
        NSWindowSharingType updatedType = self->sharedState ? NSWindowSharingNone : NSWindowSharingReadOnly;
        NSLog( @"Updating sharing type to: %lu", (unsigned long)updatedType );
        [self.childWindow setSharingType: updatedType];
        NSLog( @"After sharing type was: %lu", (unsigned long)[self.childWindow sharingType] );
        NSLog(@"");
    }];
}

-(void) dealloc
{
    [self.timer invalidate];
    self.timer = nil;
}

- (IBAction)createWindowAction:(NSButton *)sender {
    NSLog(@"createWindowAction");
    if (!self.childWindow) {
        self.childWindow = [[NSWindow alloc] initWithContentRect: CGRectMake( 100.0f, 150.0f, 500.0f, 400.0f ) styleMask: NSWindowStyleMaskBorderless | NSWindowStyleMaskNonactivatingPanel backing: NSBackingStoreBuffered defer: NO];
        self.childWindow.collectionBehavior = NSWindowCollectionBehaviorMoveToActiveSpace | NSWindowCollectionBehaviorTransient | NSWindowCollectionBehaviorAuxiliary | NSWindowCollectionBehaviorParticipatesInCycle;
        self.childWindow.hasShadow = NO;
        self.childWindow.accessibilityElement = YES;
        [self.childWindow setCanHide: NO];
    }
    
    [self.childWindow orderFrontRegardless];
}

The behaviour is very weird because I could not read the actual current property. I tried to modify some properties of the window but nothing helps. The property is working as expected all the time but it is not updated.

0

There are 0 best solutions below