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)];
|
|
|
|
[[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"];
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshDockIcon:(NSInteger)playbackStatus
|
|
|
|
{
|
|
|
|
if ( playbackStatus < 0 )
|
|
|
|
playbackStatus = lastPlaybackStatus;
|
|
|
|
else
|
|
|
|
lastPlaybackStatus = playbackStatus;
|
|
|
|
|
|
|
|
NSImage *badgeImage = nil;
|
|
|
|
|
|
|
|
BOOL colorfulIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"colorfulDockIcons"];
|
|
|
|
|
|
|
|
if (playbackStatus == kCogStatusPlaying) {
|
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"playDockBadge", colorfulIcons)];
|
|
|
|
}
|
|
|
|
else if (playbackStatus == kCogStatusPaused) {
|
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"pauseDockBadge", colorfulIcons)];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
badgeImage = [NSImage imageNamed:getBadgeName(@"stopDockBadge", colorfulIcons)];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSSize badgeSize = [badgeImage size];
|
|
|
|
|
|
|
|
NSImage *newDockImage = [dockImage copy];
|
|
|
|
[newDockImage lockFocus];
|
|
|
|
|
|
|
|
[badgeImage drawInRect:NSMakeRect(0, 0, 128, 128)
|
|
|
|
fromRect:NSMakeRect(0, 0, badgeSize.width, badgeSize.height)
|
2021-01-06 10:17:30 +00:00
|
|
|
operation:NSCompositingOperationSourceOver fraction:1.0];
|
2013-10-11 15:35:57 +00:00
|
|
|
|
|
|
|
[newDockImage unlockFocus];
|
|
|
|
[NSApp setApplicationIconImage:newDockImage];
|
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];
|
|
|
|
|
|
|
|
[self refreshDockIcon:playbackStatus];
|
|
|
|
}
|
|
|
|
else if ([keyPath isEqualToString:@"values.colorfulDockIcons"])
|
|
|
|
{
|
|
|
|
[self refreshDockIcon:-1];
|
|
|
|
}
|
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];
|
2009-02-28 23:54:36 +00:00
|
|
|
[self startObserving];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self stopObserving];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|