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"
|
|
|
|
#import <CogAudio/Status.h>
|
2016-05-05 20:05:39 +00:00
|
|
|
#import "PlaybackController.h"
|
2009-02-28 23:54:36 +00:00
|
|
|
|
|
|
|
@implementation DockIconController
|
|
|
|
|
|
|
|
static NSString *DockIconPlaybackStatusObservationContext = @"DockIconPlaybackStatusObservationContext";
|
|
|
|
|
|
|
|
- (void)startObserving
|
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
[playbackController addObserver:self forKeyPath:@"playbackStatus" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:(__bridge void * _Nullable)(DockIconPlaybackStatusObservationContext)];
|
2022-01-09 10:10:08 +00:00
|
|
|
[playbackController addObserver:self forKeyPath:@"progressBarStatus" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:(__bridge void * _Nullable)(DockIconPlaybackStatusObservationContext)];
|
2016-05-05 20:05:39 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.colorfulDockIcons" options:0 context:(__bridge void * _Nullable)(DockIconPlaybackStatusObservationContext)];
|
2009-02-28 23:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopObserving
|
|
|
|
{
|
|
|
|
[playbackController removeObserver:self forKeyPath:@"playbackStatus"];
|
2022-01-09 10:10:08 +00:00
|
|
|
[playbackController removeObserver:self forKeyPath:@"progressBarStatus"];
|
2013-10-11 15:35:57 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.colorfulDockIcons"];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *getBadgeName(NSString *baseName, BOOL colorfulIcons)
|
|
|
|
{
|
|
|
|
if (colorfulIcons)
|
|
|
|
{
|
|
|
|
return [baseName stringByAppendingString:@"Colorful"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return baseName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:10:08 +00:00
|
|
|
- (void)refreshDockIcon:(NSInteger)playbackStatus withProgress:(double)progressStatus
|
2013-10-11 15:35:57 +00:00
|
|
|
{
|
2022-01-09 10:10:08 +00:00
|
|
|
BOOL displayChanged = NO;
|
|
|
|
BOOL drawIcon = NO;
|
|
|
|
BOOL removeProgress = NO;
|
|
|
|
|
2013-10-11 15:35:57 +00:00
|
|
|
if ( playbackStatus < 0 )
|
|
|
|
playbackStatus = lastPlaybackStatus;
|
|
|
|
else
|
2022-01-09 10:10:08 +00:00
|
|
|
{
|
2013-10-11 15:35:57 +00:00
|
|
|
lastPlaybackStatus = playbackStatus;
|
2022-01-09 10:10:08 +00:00
|
|
|
drawIcon = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( progressStatus < -2 )
|
|
|
|
progressStatus = [lastProgressStatus doubleValue];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (progressStatus < 0 && [lastProgressStatus doubleValue] >= 0)
|
|
|
|
removeProgress = YES;
|
|
|
|
lastProgressStatus = [NSNumber numberWithDouble:progressStatus];
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL displayProgress = (progressStatus >= 0.0);
|
2013-10-11 15:35:57 +00:00
|
|
|
|
|
|
|
NSImage *badgeImage = nil;
|
|
|
|
|
|
|
|
BOOL colorfulIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"colorfulDockIcons"];
|
|
|
|
|
2022-01-09 10:10:08 +00:00
|
|
|
if ((colorfulIcons && lastColorfulStatus < 1) ||
|
|
|
|
(!colorfulIcons && lastColorfulStatus != 0))
|
|
|
|
{
|
|
|
|
lastColorfulStatus = colorfulIcons ? 1 : 0;
|
|
|
|
drawIcon = YES;
|
2013-10-11 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 10:10:08 +00:00
|
|
|
NSDockTile *dockTile = [NSApp dockTile];
|
|
|
|
|
|
|
|
if (drawIcon)
|
|
|
|
{
|
|
|
|
switch (playbackStatus) {
|
|
|
|
case CogStatusPlaying:
|
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"playDockBadge", colorfulIcons)];
|
|
|
|
break;
|
|
|
|
case CogStatusPaused:
|
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"pauseDockBadge", colorfulIcons)];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"stopDockBadge", colorfulIcons)];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSSize badgeSize = [badgeImage size];
|
|
|
|
|
|
|
|
NSImage *newDockImage = [dockImage copy];
|
|
|
|
[newDockImage lockFocus];
|
2013-10-11 15:35:57 +00:00
|
|
|
|
2022-01-09 10:10:08 +00:00
|
|
|
[badgeImage drawInRect:NSMakeRect(0, 0, 128, 128)
|
|
|
|
fromRect:NSMakeRect(0, 0, badgeSize.width, badgeSize.height)
|
|
|
|
operation:NSCompositingOperationSourceOver fraction:1.0];
|
2013-10-11 15:35:57 +00:00
|
|
|
|
2022-01-09 10:10:08 +00:00
|
|
|
[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;
|
|
|
|
}
|
2013-10-11 15:35:57 +00:00
|
|
|
|
2022-01-09 10:10:08 +00:00
|
|
|
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];
|
2009-02-28 23:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
if ([DockIconPlaybackStatusObservationContext isEqual:(__bridge id)(context)])
|
2009-02-28 23:54:36 +00:00
|
|
|
{
|
2013-10-11 15:35:57 +00:00
|
|
|
if ([keyPath isEqualToString:@"playbackStatus"])
|
|
|
|
{
|
|
|
|
NSInteger playbackStatus = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
|
|
|
|
|
2022-01-09 10:10:08 +00:00
|
|
|
[self refreshDockIcon:playbackStatus withProgress:-10];
|
|
|
|
}
|
|
|
|
else if ([keyPath isEqualToString:@"progressBarStatus"])
|
|
|
|
{
|
|
|
|
double progressStatus = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
|
|
|
|
|
|
|
|
[self refreshDockIcon:-1 withProgress:progressStatus];
|
2013-10-11 15:35:57 +00:00
|
|
|
}
|
|
|
|
else if ([keyPath isEqualToString:@"values.colorfulDockIcons"])
|
|
|
|
{
|
2022-01-09 10:10:08 +00:00
|
|
|
[self refreshDockIcon:-1 withProgress:-10];
|
2013-10-11 15:35:57 +00:00
|
|
|
}
|
2009-02-28 23:54:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
2013-10-11 14:16:47 +00:00
|
|
|
dockImage = [[NSImage imageNamed:@"icon_blank"] copy];
|
2022-01-09 10:10:08 +00:00
|
|
|
lastColorfulStatus = -1;
|
|
|
|
lastProgressStatus = [NSNumber numberWithDouble:-1];
|
|
|
|
imageView = nil;
|
|
|
|
progressIndicator = nil;
|
2009-02-28 23:54:36 +00:00
|
|
|
[self startObserving];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self stopObserving];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|