From 6022526ad4f2e65320aba67a28fd949d60d02a9e Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Tue, 7 Jun 2022 19:01:47 -0700 Subject: [PATCH] [Visualization] Improve hidden window detection The NSView should detect if it's a visible control in a window Signed-off-by: Christopher Snowhill --- Visualization/SpectrumView.m | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Visualization/SpectrumView.m b/Visualization/SpectrumView.m index bab638011..932dba465 100644 --- a/Visualization/SpectrumView.m +++ b/Visualization/SpectrumView.m @@ -20,6 +20,29 @@ extern NSString *CogPlaybackDidPauseNotficiation; extern NSString *CogPlaybackDidResumeNotficiation; extern NSString *CogPlaybackDidStopNotficiation; +@interface NSView (Visibility) +- (BOOL)visibleInWindow; +@end + +@implementation NSView (Visibility) + +- (BOOL)visibleInWindow +{ + if(self.window == nil) { + return NO; + } + + // Might have zero opacity. + if(self.alphaValue == 0 || self.hiddenOrHasHiddenAncestor) { + return NO; + } + + // Might be clipped by an ancestor. + return !NSIsEmptyRect(self.visibleRect); +} + +@end + @interface SpectrumView () { VisualizationController *visController; NSTimer *timer; @@ -64,10 +87,10 @@ extern NSString *CogPlaybackDidStopNotficiation; } - (void)updateVisListening { - if(self.isListening && (!self.window.isVisible || paused || stopped)) { + if(self.isListening && (![self visibleInWindow] || paused || stopped)) { [self stopTimer]; self.isListening = NO; - } else if(!self.isListening && (self.window.isVisible && !stopped && !paused)) { + } else if(!self.isListening && ([self visibleInWindow] && !stopped && !paused)) { [self startTimer]; self.isListening = YES; }