I'm trying to create a toolbar button whose state is linked to whether the QLPreviewPanel is visible. I've tried adding adding a binding for the visible property of [QLPreviewPanel sharedPreviewPanel] but I'm running into some really odd behaviour where by observeValueForKeyPath:ofObject:change:context: is called twice once with the value of YES and a second time with the value of NO each time [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront: nil] is invoked. Even more odd is that even though the second call has a value of NO the panel is visible. And the icing on the cake is that my code that displays the panel is only called once. Following the stack trace I can see that makeKeyAndOrderFront: is only called once from my code.
At the same time, when I call [[QLPreviewPanel sharedPreviewPanel] orderOut: nil] I don't receive any calls to observeValueForKeyPath:ofObject:change:context:. This seems really odd to me since the code that determines which of makeKeyAndOrderFront: and orderOut: to call specifically checks the isVisible method.
Below is the pertinent pieces of code around this problem.
- (void) awakeFromNib {
[[QLPreviewPanel sharedPreviewPanel] addObserver: self
forKeyPath: @"open"
options: NSKeyValueObservingOptionNew
context: nil];
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == [QLPreviewPanel sharedPreviewPanel]) {
BOOL visible = [[QLPreviewPanel sharedPreviewPanel] isVisible];
if (visible) {
[quickLookButton setState: NSOnState];
} else {
[quickLookButton setState: NSOffState];
}
}
}
- (IBAction) toggleQuickLook: (id) sender {
if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel sharedPreviewPanel] isVisible]) {
[[QLPreviewPanel sharedPreviewPanel] orderOut:nil];
} else {
[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];
}
}