How track a NSWindow's onActiveSpace property

33 Views Asked by At

I would like find a way to track changes to the onActiveSpace property of an NSWindow.

I used the KVO method to monitor this property, but did not receive the property change notification. Does this property not support KVO? If so, is there any way to confirm whether a certain attribute supports KVO? Here is my code:

  • first: addobserver
- (instancetype)initWithWidgetSpaceTracker:(WidgetSpaceTracker*)owner {
  if ((self = [super init])) {
    DCHECK(owner);
    _owner = owner;
    NSWindow* window = _owner->GetNSWindow();

    if (window) {
      [window addObserver:self
               forKeyPath:@"onActiveSpace"
                  options:NSKeyValueObservingOptionNew
                  context:nil];
    }
  }

  return self;
}
  • second:receive the change notification
- (void)observeValueForKeyPath:(NSString*)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey, id>*)change
                       context:(void*)context {
  DCHECK(_owner);
  if ([keyPath isEqual:@"onActiveSpace"]) {
    _owner->OnSpaceActiveChanged([change[NSKeyValueChangeNewKey] boolValue]);
  } else {
    [super observeValueForKeyPath:keyPath
                         ofObject:object
                           change:change
                          context:context];
  }
}
0

There are 0 best solutions below