2009-02-28 23:54:36 +00:00
|
|
|
//
|
|
|
|
// DockIconController.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 2/28/09.
|
|
|
|
// Copyright 2009 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DockIconController.h"
|
2016-05-05 20:05:39 +00:00
|
|
|
#import "PlaybackController.h"
|
2022-02-07 05:49:27 +00:00
|
|
|
#import <CogAudio/Status.h>
|
2009-02-28 23:54:36 +00:00
|
|
|
|
|
|
|
@implementation DockIconController
|
|
|
|
|
|
|
|
static NSString *DockIconPlaybackStatusObservationContext = @"DockIconPlaybackStatusObservationContext";
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)startObserving {
|
|
|
|
[playbackController addObserver:self forKeyPath:@"playbackStatus" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
[Job Queue] Overhauled long action handling
Long actions, such as file opening, playlist loading, metadata loading
and refreshing, etc, are now handled through NSProgress. Additionally,
a new status bar change displays the progress of the task instead of
the total duration of the playlist. Finally, app quit is blocked by a
running task, and if the app is quit while a task is running, it will
be delayed until the task completes, at which time the app will
terminate cleanly.
Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2022-06-15 08:01:45 +00:00
|
|
|
[playbackController addObserver:self forKeyPath:@"progressOverall" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionOld) context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
2022-02-07 05:49:27 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.colorfulDockIcons" options:0 context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
2009-02-28 23:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)stopObserving {
|
[Job Queue] Overhauled long action handling
Long actions, such as file opening, playlist loading, metadata loading
and refreshing, etc, are now handled through NSProgress. Additionally,
a new status bar change displays the progress of the task instead of
the total duration of the playlist. Finally, app quit is blocked by a
running task, and if the app is quit while a task is running, it will
be delayed until the task completes, at which time the app will
terminate cleanly.
Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2022-06-15 08:01:45 +00:00
|
|
|
[playbackController removeObserver:self forKeyPath:@"playbackStatus" context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
|
|
|
[playbackController removeObserver:self forKeyPath:@"progressOverall" context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.colorfulDockIcons" context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startObservingProgress:(NSProgress *)progress {
|
|
|
|
[progress addObserver:self forKeyPath:@"fractionCompleted" options:0 context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopObservingProgress:(NSProgress *)progress {
|
|
|
|
[progress removeObserver:self forKeyPath:@"fractionCompleted" context:(__bridge void *_Nullable)(DockIconPlaybackStatusObservationContext)];
|
2013-10-11 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static NSString *getBadgeName(NSString *baseName, BOOL colorfulIcons) {
|
|
|
|
if(colorfulIcons) {
|
|
|
|
return [baseName stringByAppendingString:@"Colorful"];
|
|
|
|
} else {
|
2022-05-02 21:35:09 +00:00
|
|
|
return [baseName stringByAppendingString:@"Normal"];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
2013-10-11 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)refreshDockIcon:(NSInteger)playbackStatus withProgress:(double)progressStatus {
|
|
|
|
BOOL displayChanged = NO;
|
|
|
|
BOOL drawIcon = NO;
|
|
|
|
BOOL removeProgress = NO;
|
|
|
|
|
|
|
|
if(playbackStatus < 0)
|
|
|
|
playbackStatus = lastPlaybackStatus;
|
|
|
|
else {
|
|
|
|
lastPlaybackStatus = playbackStatus;
|
|
|
|
drawIcon = YES;
|
|
|
|
}
|
2009-02-28 23:54:36 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(progressStatus < -2)
|
|
|
|
progressStatus = [lastProgressStatus doubleValue];
|
|
|
|
else {
|
|
|
|
if(progressStatus < 0 && [lastProgressStatus doubleValue] >= 0)
|
|
|
|
removeProgress = YES;
|
2022-06-17 13:39:02 +00:00
|
|
|
lastProgressStatus = @(progressStatus);
|
2009-02-28 23:54:36 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
BOOL displayProgress = (progressStatus >= 0.0);
|
|
|
|
|
|
|
|
NSImage *badgeImage = nil;
|
|
|
|
|
|
|
|
BOOL colorfulIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"colorfulDockIcons"];
|
|
|
|
|
|
|
|
if((colorfulIcons && lastColorfulStatus < 1) ||
|
|
|
|
(!colorfulIcons && lastColorfulStatus != 0)) {
|
|
|
|
lastColorfulStatus = colorfulIcons ? 1 : 0;
|
|
|
|
drawIcon = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDockTile *dockTile = [NSApp dockTile];
|
|
|
|
|
|
|
|
if(drawIcon) {
|
|
|
|
switch(playbackStatus) {
|
|
|
|
case CogStatusPlaying:
|
2022-05-02 21:35:09 +00:00
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"Play", colorfulIcons)];
|
2022-02-07 05:49:27 +00:00
|
|
|
break;
|
|
|
|
case CogStatusPaused:
|
2022-05-02 21:35:09 +00:00
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"Pause", colorfulIcons)];
|
2022-02-07 05:49:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-05-02 21:35:09 +00:00
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"Stop", colorfulIcons)];
|
2022-02-07 05:49:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSSize badgeSize = [badgeImage size];
|
|
|
|
|
|
|
|
NSImage *newDockImage = [dockImage copy];
|
|
|
|
[newDockImage lockFocus];
|
|
|
|
|
2022-05-02 21:35:09 +00:00
|
|
|
[badgeImage drawInRect:NSMakeRect(0, 0, 1024, 1024)
|
2022-02-07 05:49:27 +00:00
|
|
|
fromRect:NSMakeRect(0, 0, badgeSize.width, badgeSize.height)
|
|
|
|
operation:NSCompositingOperationSourceOver
|
|
|
|
fraction:1.0];
|
|
|
|
|
|
|
|
[newDockImage unlockFocus];
|
|
|
|
|
|
|
|
imageView = [[NSImageView alloc] init];
|
|
|
|
[imageView setImage:newDockImage];
|
|
|
|
[dockTile setContentView:imageView];
|
|
|
|
|
|
|
|
progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0.0, 0.0, dockTile.size.width, 10.0)];
|
|
|
|
[progressIndicator setStyle:NSProgressIndicatorBarStyle];
|
|
|
|
[progressIndicator setIndeterminate:NO];
|
|
|
|
[progressIndicator setBezeled:YES];
|
|
|
|
[progressIndicator setMinValue:0];
|
|
|
|
[progressIndicator setMaxValue:100];
|
|
|
|
[progressIndicator setHidden:YES];
|
|
|
|
|
|
|
|
[imageView addSubview:progressIndicator];
|
|
|
|
|
|
|
|
displayChanged = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(displayProgress) {
|
|
|
|
if(!imageView) {
|
|
|
|
imageView = [[NSImageView alloc] init];
|
|
|
|
[imageView setImage:[NSApp applicationIconImage]];
|
|
|
|
[dockTile setContentView:imageView];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!progressIndicator) {
|
|
|
|
progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0.0, 0.0, dockTile.size.width, 10.0)];
|
|
|
|
[progressIndicator setIndeterminate:NO];
|
|
|
|
[progressIndicator setBezeled:YES];
|
|
|
|
[progressIndicator setMinValue:0];
|
|
|
|
[progressIndicator setMaxValue:100];
|
|
|
|
|
|
|
|
[imageView addSubview:progressIndicator];
|
|
|
|
}
|
|
|
|
|
|
|
|
[progressIndicator setDoubleValue:progressStatus];
|
|
|
|
[progressIndicator setHidden:NO];
|
|
|
|
|
|
|
|
displayChanged = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(removeProgress) {
|
|
|
|
if(progressIndicator)
|
|
|
|
[progressIndicator setHidden:YES];
|
|
|
|
|
|
|
|
displayChanged = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(displayChanged)
|
|
|
|
[dockTile display];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
|
|
if([DockIconPlaybackStatusObservationContext isEqual:(__bridge id)(context)]) {
|
|
|
|
if([keyPath isEqualToString:@"playbackStatus"]) {
|
|
|
|
NSInteger playbackStatus = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
|
|
|
|
|
|
|
|
[self refreshDockIcon:playbackStatus withProgress:-10];
|
[Job Queue] Overhauled long action handling
Long actions, such as file opening, playlist loading, metadata loading
and refreshing, etc, are now handled through NSProgress. Additionally,
a new status bar change displays the progress of the task instead of
the total duration of the playlist. Finally, app quit is blocked by a
running task, and if the app is quit while a task is running, it will
be delayed until the task completes, at which time the app will
terminate cleanly.
Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2022-06-15 08:01:45 +00:00
|
|
|
} else if([keyPath isEqualToString:@"progressOverall"]) {
|
|
|
|
double progressStatus = [lastProgressStatus doubleValue];
|
|
|
|
|
|
|
|
id objNew = [change objectForKey:NSKeyValueChangeNewKey];
|
|
|
|
id objOld = [change objectForKey:NSKeyValueChangeOldKey];
|
|
|
|
|
|
|
|
NSProgress *progressNew = nil, *progressOld = nil;
|
|
|
|
|
|
|
|
if(objNew && [objNew isKindOfClass:[NSProgress class]])
|
|
|
|
progressNew = (NSProgress *)objNew;
|
|
|
|
if(objOld && [objOld isKindOfClass:[NSProgress class]])
|
|
|
|
progressOld = (NSProgress *)objOld;
|
|
|
|
|
|
|
|
if(progressOld) {
|
|
|
|
[self stopObservingProgress:progressOld];
|
|
|
|
progressStatus = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(progressNew) {
|
|
|
|
[self startObservingProgress:progressNew];
|
|
|
|
progressStatus = progressNew.fractionCompleted * 100.0;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
[self refreshDockIcon:-1 withProgress:progressStatus];
|
|
|
|
} else if([keyPath isEqualToString:@"values.colorfulDockIcons"]) {
|
|
|
|
[self refreshDockIcon:-1 withProgress:-10];
|
[Job Queue] Overhauled long action handling
Long actions, such as file opening, playlist loading, metadata loading
and refreshing, etc, are now handled through NSProgress. Additionally,
a new status bar change displays the progress of the task instead of
the total duration of the playlist. Finally, app quit is blocked by a
running task, and if the app is quit while a task is running, it will
be delayed until the task completes, at which time the app will
terminate cleanly.
Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2022-06-15 08:01:45 +00:00
|
|
|
} else if([keyPath isEqualToString:@"fractionCompleted"]) {
|
|
|
|
double progressStatus = [(NSProgress *)object fractionCompleted];
|
|
|
|
[self refreshDockIcon:-1 withProgress:progressStatus * 100.0];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-02-28 23:54:36 +00:00
|
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)awakeFromNib {
|
2022-05-02 21:35:09 +00:00
|
|
|
dockImage = [[NSImage imageNamed:@"Plaque"] copy];
|
2022-02-07 05:49:27 +00:00
|
|
|
lastColorfulStatus = -1;
|
2022-06-17 13:39:02 +00:00
|
|
|
lastProgressStatus = @(-1.0);
|
2022-02-07 05:49:27 +00:00
|
|
|
imageView = nil;
|
|
|
|
progressIndicator = nil;
|
2009-02-28 23:54:36 +00:00
|
|
|
[self startObserving];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)dealloc {
|
2009-02-28 23:54:36 +00:00
|
|
|
[self stopObserving];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|