Only process visualizations when visible
Stop visualization processing when the host window is completely occluded, thus reducing background CPU usage levels significantly Signed-off-by: Christopher Snowhill <kode54@gmail.com>main
parent
58a3c3aeeb
commit
ee5231f567
115
SpectrumViewCG.m
115
SpectrumViewCG.m
|
@ -29,6 +29,7 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
BOOL isListening;
|
BOOL isListening;
|
||||||
BOOL observersAdded;
|
BOOL observersAdded;
|
||||||
BOOL isFullView;
|
BOOL isFullView;
|
||||||
|
BOOL isOccluded;
|
||||||
|
|
||||||
NSRect initFrame;
|
NSRect initFrame;
|
||||||
|
|
||||||
|
@ -58,15 +59,32 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)updateVisListening {
|
- (void)updateVisListening {
|
||||||
if(self.isListening && (![self visibleInWindow] || paused || stopped)) {
|
if(self.isListening && (![self visibleInWindow] || isOccluded || paused || stopped)) {
|
||||||
[self stopTimer];
|
[self stopTimer];
|
||||||
self.isListening = NO;
|
self.isListening = NO;
|
||||||
} else if(!self.isListening && ([self visibleInWindow] && !stopped && !paused)) {
|
} else if(!self.isListening && ([self visibleInWindow] && !isOccluded && !stopped && !paused)) {
|
||||||
[self startTimer];
|
[self startTimer];
|
||||||
self.isListening = YES;
|
self.isListening = YES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)setOccluded:(BOOL)occluded {
|
||||||
|
isOccluded = occluded;
|
||||||
|
[self updateVisListening];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)windowChangedOcclusionState:(NSNotification *)notification {
|
||||||
|
if([notification object] == self.window) {
|
||||||
|
BOOL curOccluded = !self.window;
|
||||||
|
if(!curOccluded) {
|
||||||
|
curOccluded = !(self.window.occlusionState & NSWindowOcclusionStateVisible);
|
||||||
|
}
|
||||||
|
if(curOccluded != isOccluded) {
|
||||||
|
[self setOccluded:curOccluded];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (void)setup {
|
- (void)setup {
|
||||||
visController = [NSClassFromString(@"VisualizationController") sharedController];
|
visController = [NSClassFromString(@"VisualizationController") sharedController];
|
||||||
timer = nil;
|
timer = nil;
|
||||||
|
@ -118,31 +136,39 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
|
|
||||||
- (void)addObservers {
|
- (void)addObservers {
|
||||||
if(!observersAdded) {
|
if(!observersAdded) {
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewCGContext];
|
NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController];
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewCGContext];
|
[sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewCGContext];
|
||||||
|
[sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewCGContext];
|
||||||
|
|
||||||
[self addObserver:self forKeyPath:@"self.window.visible" options:0 context:kSpectrumViewCGContext];
|
[self addObserver:self forKeyPath:@"self.window.visible" options:0 context:kSpectrumViewCGContext];
|
||||||
|
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
|
||||||
selector:@selector(colorsDidChange:)
|
[defaultCenter addObserver:self
|
||||||
name:NSSystemColorsDidChangeNotification
|
selector:@selector(colorsDidChange:)
|
||||||
object:nil];
|
name:NSSystemColorsDidChangeNotification
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
object:nil];
|
||||||
selector:@selector(playbackDidBegin:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidBeginNotficiation
|
selector:@selector(playbackDidBegin:)
|
||||||
object:nil];
|
name:CogPlaybackDidBeginNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
object:nil];
|
||||||
selector:@selector(playbackDidPause:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidPauseNotficiation
|
selector:@selector(playbackDidPause:)
|
||||||
object:nil];
|
name:CogPlaybackDidPauseNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
object:nil];
|
||||||
selector:@selector(playbackDidResume:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidResumeNotficiation
|
selector:@selector(playbackDidResume:)
|
||||||
object:nil];
|
name:CogPlaybackDidResumeNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
object:nil];
|
||||||
selector:@selector(playbackDidStop:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidStopNotficiation
|
selector:@selector(playbackDidStop:)
|
||||||
object:nil];
|
name:CogPlaybackDidStopNotficiation
|
||||||
|
object:nil];
|
||||||
|
|
||||||
|
[defaultCenter addObserver:self
|
||||||
|
selector:@selector(windowChangedOcclusionState:)
|
||||||
|
name:NSWindowDidChangeOcclusionStateNotification
|
||||||
|
object:nil];
|
||||||
|
|
||||||
observersAdded = YES;
|
observersAdded = YES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,26 +182,33 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
|
|
||||||
- (void)removeObservers {
|
- (void)removeObservers {
|
||||||
if(observersAdded) {
|
if(observersAdded) {
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewCGContext];
|
NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController];
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewCGContext];
|
[sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewCGContext];
|
||||||
|
[sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewCGContext];
|
||||||
|
|
||||||
[self removeObserver:self forKeyPath:@"self.window.visible" context:kSpectrumViewCGContext];
|
[self removeObserver:self forKeyPath:@"self.window.visible" context:kSpectrumViewCGContext];
|
||||||
|
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
|
||||||
name:NSSystemColorsDidChangeNotification
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:NSSystemColorsDidChangeNotification
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
object:nil];
|
||||||
name:CogPlaybackDidBeginNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidBeginNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
object:nil];
|
||||||
name:CogPlaybackDidPauseNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidPauseNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
object:nil];
|
||||||
name:CogPlaybackDidResumeNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidResumeNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
object:nil];
|
||||||
name:CogPlaybackDidStopNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidStopNotficiation
|
||||||
|
object:nil];
|
||||||
|
|
||||||
|
[defaultCenter removeObserver:self
|
||||||
|
name:NSWindowDidChangeOcclusionStateNotification
|
||||||
|
object:nil];
|
||||||
|
|
||||||
observersAdded = NO;
|
observersAdded = NO;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
BOOL bandsReset;
|
BOOL bandsReset;
|
||||||
BOOL cameraControlEnabled;
|
BOOL cameraControlEnabled;
|
||||||
BOOL observersAdded;
|
BOOL observersAdded;
|
||||||
|
BOOL isOccluded;
|
||||||
|
|
||||||
NSColor *backgroundColor;
|
NSColor *backgroundColor;
|
||||||
ddb_analyzer_t _analyzer;
|
ddb_analyzer_t _analyzer;
|
||||||
|
@ -90,15 +91,32 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)updateVisListening {
|
- (void)updateVisListening {
|
||||||
if(self.isListening && (![self visibleInWindow] || paused || stopped)) {
|
if(self.isListening && (![self visibleInWindow] || isOccluded || paused || stopped)) {
|
||||||
[self stopTimer];
|
[self stopTimer];
|
||||||
self.isListening = NO;
|
self.isListening = NO;
|
||||||
} else if(!self.isListening && ([self visibleInWindow] && !stopped && !paused)) {
|
} else if(!self.isListening && ([self visibleInWindow] && !isOccluded && !stopped && !paused)) {
|
||||||
[self startTimer];
|
[self startTimer];
|
||||||
self.isListening = YES;
|
self.isListening = YES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)setOccluded:(BOOL)occluded {
|
||||||
|
isOccluded = occluded;
|
||||||
|
[self updateVisListening];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)windowChangedOcclusionState:(NSNotification *)notification {
|
||||||
|
if([notification object] == self.window) {
|
||||||
|
BOOL curOccluded = !self.window;
|
||||||
|
if(!curOccluded) {
|
||||||
|
curOccluded = !(self.window.occlusionState & NSWindowOcclusionStateVisible);
|
||||||
|
}
|
||||||
|
if(curOccluded != isOccluded) {
|
||||||
|
[self setOccluded:curOccluded];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (void)observeValueForKeyPath:(NSString *)keyPath
|
- (void)observeValueForKeyPath:(NSString *)keyPath
|
||||||
ofObject:(id)object
|
ofObject:(id)object
|
||||||
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
|
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
|
||||||
|
@ -209,28 +227,35 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
|
|
||||||
- (void)addObservers {
|
- (void)addObservers {
|
||||||
if(!observersAdded) {
|
if(!observersAdded) {
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumProjectionMode" options:0 context:kSpectrumViewSKContext];
|
NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController];
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewSKContext];
|
[sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumProjectionMode" options:0 context:kSpectrumViewSKContext];
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewSKContext];
|
[sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewSKContext];
|
||||||
|
[sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewSKContext];
|
||||||
|
|
||||||
[self addObserver:self forKeyPath:@"self.window.visible" options:0 context:kSpectrumViewSKContext];
|
[self addObserver:self forKeyPath:@"self.window.visible" options:0 context:kSpectrumViewSKContext];
|
||||||
|
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
|
||||||
selector:@selector(playbackDidBegin:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidBeginNotficiation
|
selector:@selector(playbackDidBegin:)
|
||||||
object:nil];
|
name:CogPlaybackDidBeginNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
object:nil];
|
||||||
selector:@selector(playbackDidPause:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidPauseNotficiation
|
selector:@selector(playbackDidPause:)
|
||||||
object:nil];
|
name:CogPlaybackDidPauseNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
object:nil];
|
||||||
selector:@selector(playbackDidResume:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidResumeNotficiation
|
selector:@selector(playbackDidResume:)
|
||||||
object:nil];
|
name:CogPlaybackDidResumeNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
object:nil];
|
||||||
selector:@selector(playbackDidStop:)
|
[defaultCenter addObserver:self
|
||||||
name:CogPlaybackDidStopNotficiation
|
selector:@selector(playbackDidStop:)
|
||||||
object:nil];
|
name:CogPlaybackDidStopNotficiation
|
||||||
|
object:nil];
|
||||||
|
|
||||||
|
[defaultCenter addObserver:self
|
||||||
|
selector:@selector(windowChangedOcclusionState:)
|
||||||
|
name:NSWindowDidChangeOcclusionStateNotification
|
||||||
|
object:nil];
|
||||||
|
|
||||||
observersAdded = YES;
|
observersAdded = YES;
|
||||||
}
|
}
|
||||||
|
@ -245,24 +270,30 @@ extern NSString *CogPlaybackDidStopNotficiation;
|
||||||
|
|
||||||
- (void)removeObservers {
|
- (void)removeObservers {
|
||||||
if(observersAdded) {
|
if(observersAdded) {
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumProjectionMode" context:kSpectrumViewSKContext];
|
NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController];
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewSKContext];
|
[sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumProjectionMode" context:kSpectrumViewSKContext];
|
||||||
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewSKContext];
|
[sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewSKContext];
|
||||||
|
[sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewSKContext];
|
||||||
|
|
||||||
[self removeObserver:self forKeyPath:@"self.window.visible" context:kSpectrumViewSKContext];
|
[self removeObserver:self forKeyPath:@"self.window.visible" context:kSpectrumViewSKContext];
|
||||||
|
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
|
||||||
name:CogPlaybackDidBeginNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidBeginNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
object:nil];
|
||||||
name:CogPlaybackDidPauseNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidPauseNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
object:nil];
|
||||||
name:CogPlaybackDidResumeNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidResumeNotficiation
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
object:nil];
|
||||||
name:CogPlaybackDidStopNotficiation
|
[defaultCenter removeObserver:self
|
||||||
object:nil];
|
name:CogPlaybackDidStopNotficiation
|
||||||
|
object:nil];
|
||||||
|
|
||||||
|
[defaultCenter removeObserver:self
|
||||||
|
name:NSWindowDidChangeOcclusionStateNotification
|
||||||
|
object:nil];
|
||||||
|
|
||||||
observersAdded = NO;
|
observersAdded = NO;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue