Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out whether any part of an NSWindow is currently visible (not just ordered in)

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.

like image 858
Frank R. Avatar asked Jan 08 '13 15:01

Frank R.


1 Answers

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
    }
}
like image 62
Nick Avatar answered Oct 21 '22 04:10

Nick