I have an NSWindow that is updated every second to display the current time.
This drawing is quite processor intensive and I want to avoid doing it while the window is completely obscured by other windows.
Unfortunately, [NSWindow isVisible] does not show whether a window is actually visible on the screen, but only whether it is on screen at the moment. Meaning if the window is in the window list but completely obscured by other windows isVisible == YES, my custom drawRect gets called and I end up drawing everything into a buffer that is never used.
Is there any way of detecting whether a Window and its content is actually visible on the screen?
Any help would be much appreciated.
On 10.9 you can use NSWindow's -occlusionState and the associated delegate method.
From the release notes:
Windows are considered occluded if their entire content, including title bar and tool bar, is 100% covered by another opaque window. Windows are also occluded if they are ordered off screen, minimized to the dock, or on another space. Partial occlusion counts as “visible.”
Example:
- (void)windowDidChangeOcclusionState:(NSNotification *)notification
{
if ([[notification object] occlusionState] & NSWindowOcclusionStateVisible) {
// visible
} else {
// occluded
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With