-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTunesCantComplainView.m
182 lines (153 loc) · 5.36 KB
/
iTunesCantComplainView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// iTunesCantComplainView.m
// iTunesCantComplain
//
// Created by Uli Kusterer on 24.04.08.
// Copyright (c) 2008, The Void Software. All rights reserved.
//
#import "iTunesCantComplainView.h"
#import "iTunes.h"
@implementation iTunesCantComplainView
@synthesize currTrackArt;
@synthesize currTrackAlbum;
@synthesize currTrackName;
@synthesize currTrackArtist;
@synthesize currTrackLyrics;
@synthesize currTrackPercentage;
@synthesize imagePos;
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
self = [super initWithFrame:frame isPreview:isPreview];
if (self)
{
[self setAnimationTimeInterval: 4];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector: @selector(iTunesTrackChanged:)
name: @"com.apple.iTunes.playerInfo"
object: nil];
srand( time(NULL) );
}
return self;
}
-(void) dealloc
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver: self
name: @"com.apple.iTunes.playerInfo"
object: nil];
[currTrackArt release];
[currTrackArtist release];
[currTrackAlbum release];
[currTrackName release];
[currTrackLyrics release];
[super dealloc];
}
- (void)startAnimation
{
[super startAnimation];
[self iTunesTrackChanged: nil];
}
- (void)stopAnimation
{
[super stopAnimation];
}
- (void)drawRect: (NSRect)rect
{
[super drawRect: rect];
NS_DURING
NSDictionary* bigAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont boldSystemFontOfSize: 24], NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
nil];
NSDictionary* smAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont boldSystemFontOfSize: 18], NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
nil];
NSDictionary* tinyAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize: 18], NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
nil];
NSDictionary* paleAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize: 18], NSFontAttributeName,
[[NSColor lightGrayColor] colorWithAlphaComponent: 0.7], NSForegroundColorAttributeName,
nil];
// Draw cover image with track info:
NSRect desiredBox = NSMakeRect(imagePos.x,imagePos.y,0,0);
if( [self currTrackArt] )
{
desiredBox.size = [[self currTrackArt] size];
desiredBox.size.width *= (330.0f / desiredBox.size.height);
desiredBox.size.height = 330.0f;
[[self currTrackArt] drawInRect: desiredBox fromRect: NSZeroRect operation: NSCompositeSourceAtop fraction: 1.0];
}
[currTrackName drawAtPoint: NSMakePoint(desiredBox.origin.x,desiredBox.origin.y + 390.0f) withAttributes: bigAttrs];
[currTrackArtist drawAtPoint: NSMakePoint(desiredBox.origin.x,desiredBox.origin.y + 370.0f) withAttributes: smAttrs];
[currTrackAlbum drawAtPoint: NSMakePoint(desiredBox.origin.x,desiredBox.origin.y + 340.0f) withAttributes: tinyAttrs];
// Indicate playback progress:
NSRect progressBox = NSMakeRect(NSMinX(desiredBox), NSMinY(desiredBox) -24, desiredBox.size.width, 12);
[[NSColor whiteColor] set];
[NSBezierPath strokeRect: progressBox];
progressBox.size.width *= [self currTrackPercentage];
[NSBezierPath fillRect: progressBox];
// Draw lyrics, pale and transparent on top of any track image:
if( currTrackLyrics )
{
NSPoint lyricsPos = NSMakePoint(10,10);
lyricsPos.y -= ([currTrackLyrics sizeWithAttributes: paleAttrs].height -[self bounds].size.height +20) * (1.0 -[self currTrackPercentage]);
[currTrackLyrics drawAtPoint: lyricsPos withAttributes: paleAttrs];
}
NS_HANDLER
NSLog( @"iTunesCantComplain: Error %@", [localException reason] );
NS_ENDHANDLER
}
- (void)animateOneFrame
{
[self iTunesTrackChanged: nil];
}
- (BOOL)hasConfigureSheet
{
return NO;
}
- (NSWindow*)configureSheet
{
return nil;
}
-(void) iTunesTrackChanged: (NSNotification*)notif
{
NS_DURING
iTunesApplication* itunes = [SBApplication applicationWithBundleIdentifier: @"com.apple.iTunes"];
iTunesTrack* currTrack = [itunes currentTrack];
NSArray* theImages = [[currTrack artworks] get];
NS_DURING
[self setCurrTrackArt: [(iTunesArtwork*)[theImages objectAtIndex: 0] data]];
NS_HANDLER
[self setCurrTrackArt: nil];
NSLog( @"iTunesCantComplain: Error %@", [localException reason] );
NS_ENDHANDLER
[self setCurrTrackArtist: [currTrack artist]];
[self setCurrTrackAlbum: [currTrack album]];
[self setCurrTrackName: [currTrack name]];
float duration = [currTrack finish] -[currTrack start];
float percentage = [itunes playerPosition] -[currTrack start];
percentage /= duration;
[self setCurrTrackPercentage: percentage];
NS_DURING
[self setCurrTrackLyrics: [currTrack lyrics]];
NS_HANDLER
[self setCurrTrackLyrics: nil];
NSLog( @"iTunesCantComplain: Error %@", [localException reason] );
NS_ENDHANDLER
[self setNeedsDisplay: YES];
int availWidth = [self bounds].size.width -420;
float leftMin = 10;
if( [self currTrackLyrics] && [[self currTrackLyrics] length] > 0 )
{
availWidth /= 2;
leftMin += availWidth;
}
imagePos = NSMakePoint( leftMin +(rand() % availWidth),
10 +(rand() % ((int)[self bounds].size.height -420)) );
NS_HANDLER
NSLog( @"iTunesCantComplain: Error %@", [localException reason] );
NS_ENDHANDLER
}
@end