forked from bps/scrobblepod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BGRoundedInfoView.m
655 lines (528 loc) · 21.6 KB
/
BGRoundedInfoView.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#import "BGRoundedInfoView.h"
#import "NSBezierPath+RoundedRect.h"
#import "BGScrobbleDecisionManager.h"
#import "Defines.h"
#pragma mark Fixed Values
#define LeftPadding 38//18+30
#define RightPadding 29 //Changed from 30 because blue backround's right side was 1px away from rounded rect's border
#define LineWidth 0.5
#define BlueAmount 2
#define BlueSpeed 0.01
#define ScrollAmount 1
#define ScrollSpeed 0.05
#define blueLimit_On 90.0
#define blueLimit_Off 22.0
#define FadeSpeed 0.05
#define BLUE_STILL 0
#define BLUE_SHRINKING 1
#define BLUE_GROWING 2
#define drawHeight 18.0
#define shineHeight 10.0
@implementation BGRoundedInfoView
@synthesize isResizingBlue;
@synthesize currentBlueAction;
@synthesize currentLoveHateIconOpacity;
@synthesize blueIsClosed;
@synthesize scrobblingEnabled;
@synthesize scrobblingAuto;
#pragma mark Initialisation Methods
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame:frameRect]) != nil) {
// Assign initial values
[self calculateDrawingBounds];
self.isResizingBlue = NO;
self.active = NO;
self.currentBlueAction = BLUE_GROWING;
self.currentLoveHateIconOpacity = 10;
self.blueIsClosed = YES;
self.scrobblingEnabled = NO;
self.scrobblingAuto = YES;
self.hoveredIcon = -1;
self.pressedIcon = -1;
currentScrollOffset = 0.0;
// Create Objects Needed Later On
[self createTextAttributesDictionary];
[self setStringValue:@"BGRoundedView: Please set string"];
[self createIconSet];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(decisionChangedNotificationReceived:) name:BGScrobbleDecisionChangedNotification object:nil];
}
return self;
}
- (BOOL)acceptsFirstResponder { return YES; }
-(void)dealloc {
[self setStringValue:nil];
//[gradientFill release];
[attributesDictionary release];
[backgroundImage release];
[blueTimer release];
[fadeIconTimer release];
[statusChangeTimer release];
[iconSet release];
[scrollTimer invalidate];
[super dealloc];
}
-(void)decisionChangedNotificationReceived:(NSNotification *)notification {
[self generateStatusImage];
if ([self window]) {
[self setNeedsDisplay:YES];
}
}
-(void)createIconSet {
iconSet = [[NSMutableArray alloc] initWithCapacity:4];
NSImage *heartIcon;
NSImage *banIcon;
NSImage *recIcon;
NSImage *tagIcon;
heartIcon = [[NSImage imageNamed:@"Love"] copy];
[self addIconFromImage:heartIcon withSelector:@"loveSong:" andDescription:@"Love this song"];
[heartIcon release];
banIcon = [[NSImage imageNamed:@"Ban"] copy];
[self addIconFromImage:banIcon withSelector:@"banSong:" andDescription:@"Ban this song"];
[banIcon release];
recIcon = [[NSImage imageNamed:@"Rec"] copy];
[self addIconFromImage:recIcon withSelector:@"recommendSong:" andDescription:@"Share this song"];
[recIcon release];
tagIcon = [[NSImage imageNamed:@"Tag"] copy];
[self addIconFromImage:tagIcon withSelector:@"tagSong:" andDescription:@"Tag this song"];
[tagIcon release];
}
#pragma mark Last.fm Icons
-(void)addIconFromImage:(NSImage *)theImage withSelector:(NSString *)action andDescription:(NSString *)aDescription {
NSDictionary *newDictionary = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:theImage,action,aDescription,nil]
forKeys: [NSArray arrayWithObjects:@"image",@"action",@"description",nil] ];
[iconSet addObject:newDictionary];
}
-(void)createTextAttributesDictionary {
NSShadow *myShadow;
myShadow = [[NSShadow alloc] init];
[myShadow setShadowColor:[NSColor colorWithCalibratedHue:0.000 saturation:0.000 brightness:1.000 alpha:0.6]];
[myShadow setShadowBlurRadius:0.0];
[myShadow setShadowOffset:NSMakeSize(0,-1)];
currentBlueOffset = drawingBounds.origin.x + drawingBounds.size.width - blueLimit_Off;
attributesDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSFont fontWithName:@"Lucida Grande" size:11],myShadow,[NSColor blackColor],nil]
forKeys:[NSArray arrayWithObjects:NSFontAttributeName,NSShadowAttributeName,NSForegroundColorAttributeName,nil]];
[myShadow release];
[attributesDictionary retain];
}
-(NSString *)selectorNameForClickOffset:(NSPoint)clickPoint {
if (clickPoint.y >= 0 && clickPoint.y <= 20) {
float lastDrawPoint = drawingBounds.origin.x + drawingBounds.size.width - blueLimit_On+7.0;
int i;
for (i=0; i<iconSet.count; i++) {
NSImage *currentIcon = [[iconSet objectAtIndex:i] objectForKey:@"image"];
float imageWidth = currentIcon.size.width;
if (clickPoint.x >= lastDrawPoint && clickPoint.x <= lastDrawPoint + imageWidth) return [[iconSet objectAtIndex:i] objectForKey:@"action"];
lastDrawPoint += imageWidth + 4.0;
}
}
return nil;
}
-(NSString *)descriptionForClickOffset:(NSPoint)clickPoint {
if (clickPoint.y >= 0 && clickPoint.y <= 20) {
float lastDrawPoint = drawingBounds.origin.x + drawingBounds.size.width - blueLimit_On+7.0;
int i;
for (i=0; i<iconSet.count; i++) {
NSImage *currentIcon = [[iconSet objectAtIndex:i] objectForKey:@"image"];
float imageWidth = currentIcon.size.width;
if (clickPoint.x >= lastDrawPoint && clickPoint.x <= lastDrawPoint + imageWidth) {
return [[iconSet objectAtIndex:i] objectForKey:@"description"];
}
lastDrawPoint += imageWidth + 4.0;
}
}
return nil;
}
-(int)indexForClickOffset:(NSPoint)clickPoint {
if (clickPoint.y >= 0 && clickPoint.y <= 20) {
float lastDrawPoint = drawingBounds.origin.x + drawingBounds.size.width - blueLimit_On+7.0;
int i;
for (i=0; i<iconSet.count; i++) {
NSImage *currentIcon = [[iconSet objectAtIndex:i] objectForKey:@"image"];
float imageWidth = currentIcon.size.width;
if (clickPoint.x >= lastDrawPoint && clickPoint.x <= lastDrawPoint + imageWidth) return i;
lastDrawPoint += imageWidth + 4.0;
}
}
return -1;
}
@synthesize hoveredIcon;
#pragma mark Event Tracking
@synthesize pressedIcon;
-(void)mouseDown:(NSEvent *)theEvent {
NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
if (loc.x > 15 && loc.x < 40) {
[self closeBlueMenu];
BGScrobbleDecisionManager *decisionMaker = [BGScrobbleDecisionManager sharedManager];
BOOL oldAutomaticScrobblingDecision = [decisionMaker shouldScrobbleAuto];
if (decisionMaker.isDecisionMadeAutomatically) { //Changing from auto to manual
decisionMaker.isDecisionMadeAutomatically = NO;
decisionMaker.usersManualChoice = !oldAutomaticScrobblingDecision;
[self setTemporaryHoverStringValue:(decisionMaker.usersManualChoice ? @"Scrobbling is now ON" : @"Scrobbling is now OFF")];
} else {
// If you want to see the logic that thse 2 lines replace, email me. Basically, they replace
// an inefficient series of "if" selectors, saving 10-15 lines of code.
decisionMaker.usersManualChoice = !decisionMaker.usersManualChoice;
decisionMaker.isDecisionMadeAutomatically = oldAutomaticScrobblingDecision ^ decisionMaker.usersManualChoice; //XOR
[self setTemporaryHoverStringValue:(decisionMaker.isDecisionMadeAutomatically ? @"Scrobbling is set automatically" : (decisionMaker.usersManualChoice ? @"Scrobbling is now ON" : @"Scrobbling is now OFF"))];
}
[self generateStatusImage];
[self generateBackgroundImage];
[self setNeedsDisplay:YES];
} else if (self.active) {
float startAreaRight = drawingBounds.origin.x+drawingBounds.size.width;
float startAreaLeft = startAreaRight - blueLimit_Off;
if (loc.x < startAreaRight && loc.x > startAreaLeft) {
if (self.currentBlueAction==BLUE_STILL) self.currentLoveHateIconOpacity = (self.blueIsClosed ? 0 : 10);
(self.blueIsClosed ? [self openBlueMenu] : [self closeBlueMenu]);
} else {
NSString *selectorName = [self selectorNameForClickOffset:loc];
if (selectorName) {
self.pressedIcon = [self indexForClickOffset:loc];
self.hoveredIcon = self.pressedIcon;
[self setNeedsDisplay:YES];
SEL methodSelector = NSSelectorFromString(selectorName);
if ([appController respondsToSelector:methodSelector]) {
[appController performSelector:methodSelector withObject:self];
[[NSRunLoop currentRunLoop] addTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(resetPressedIcon:) userInfo:nil repeats:NO] forMode:NSEventTrackingRunLoopMode];
}
}
}
}
}
-(void)resetPressedIcon:(NSTimer *)theTimer {
self.pressedIcon = -1;
[self setNeedsDisplay:YES];
}
-(BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
return YES;
}
-(BOOL)acceptsFirstReponder {
return YES;
}
#pragma mark Blue Timer
-(void)resetBlueToOffState {
self.currentBlueAction = BLUE_STILL;
self.blueIsClosed = YES;
self.hoveredIcon = -1;
[self stopBlueTimer];
[self stopFadeTimer];
self.currentLoveHateIconOpacity = 0;
currentBlueOffset = (drawingBounds.origin.x + drawingBounds.size.width) - blueLimit_Off;
if (self.window) [self generateBackgroundImage];
[self setNeedsDisplay:YES];
}
-(void)startBlueTimer {
if (!isResizingBlue && self.currentBlueAction > BLUE_STILL) {
[self stopBlueTimer];
self.isResizingBlue = YES;
blueTimer = [[NSTimer scheduledTimerWithTimeInterval:BlueSpeed target:self selector:@selector(incrementBlue:) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:blueTimer forMode:NSEventTrackingRunLoopMode];
}
}
-(void)stopBlueTimer {
if (blueTimer!=nil) {
[blueTimer invalidate];
self.isResizingBlue = NO;
}
}
-(void)incrementBlue:(NSTimer *)timer {
if ( (self.currentBlueAction==BLUE_SHRINKING && [self currentBlueWidth]<=blueLimit_Off) || (self.currentBlueAction==BLUE_GROWING && [self currentBlueWidth]>=blueLimit_On) ) {
[self stopBlueTimer];
// Once drawing finishes, make sure it is on the exact (correct) value
if (self.currentBlueAction==BLUE_SHRINKING) {
[self resetBlueToOffState];
} else if (self.currentBlueAction==BLUE_GROWING) {
currentBlueOffset = drawingBounds.origin.x + drawingBounds.size.width - blueLimit_On;
self.blueIsClosed = NO;
[[self window] setAcceptsMouseMovedEvents:YES];
}
self.currentBlueAction = BLUE_STILL;
} else {
currentBlueOffset += BlueAmount * ( (self.currentBlueAction==BLUE_SHRINKING) ? 1 : -1);
}
[self generateBackgroundImage];
[self setNeedsDisplay:YES];
}
-(void)mouseMoved:(NSEvent *)theEvent {
NSPoint thePoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
NSString *theString = [self descriptionForClickOffset:thePoint];
self.hoveredIcon = [self indexForClickOffset:thePoint];
if (theString) [self setTemporaryHoverStringValue:theString];
[self setNeedsDisplay:YES];
}
#pragma mark Fade Timer
-(void)startFadeTimer {
[self stopFadeTimer];
fadeIconTimer = [[NSTimer scheduledTimerWithTimeInterval:FadeSpeed target:self selector:@selector(stepIconOpacity:) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:fadeIconTimer forMode:NSEventTrackingRunLoopMode];
}
-(void)stopFadeTimer {
if (fadeIconTimer!=nil) {
[fadeIconTimer invalidate];
}
}
#pragma mark Status Change Timer
-(void)startStatusChangeTimer {
[self stopStatusChangeTimer];
statusChangeTimer = [[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(revertFromHoverToStringValue:) userInfo:nil repeats:NO] retain];
[[NSRunLoop currentRunLoop] addTimer:statusChangeTimer forMode:NSEventTrackingRunLoopMode];
}
-(void)stopStatusChangeTimer {
if (statusChangeTimer!=nil) {
[statusChangeTimer invalidate];
}
}
-(void)stepIconOpacity:(NSTimer *)timer {
if (self.currentBlueAction==BLUE_SHRINKING) {
self.currentLoveHateIconOpacity -= 1;
if (self.currentLoveHateIconOpacity <= 0) {
self.currentLoveHateIconOpacity = 0;
[self stopFadeTimer];
}
} else {
self.currentLoveHateIconOpacity += 1;
if (self.currentLoveHateIconOpacity >= 10) {
self.currentLoveHateIconOpacity = 10;
[self stopFadeTimer];
}
}
if (!self.isResizingBlue) [self setNeedsDisplay:YES];
}
#pragma mark Convenience Blue Methods
-(float)currentBlueWidth {
return (drawingBounds.origin.x + drawingBounds.size.width) - currentBlueOffset;
}
-(void)openBlueMenu {
self.blueIsClosed = NO;
[self startBlueTimerWithDirection:BLUE_GROWING];
}
-(void)closeBlueMenu {
self.blueIsClosed = YES;
[[self window] setAcceptsMouseMovedEvents:NO];
[self startBlueTimerWithDirection:BLUE_SHRINKING];
}
-(void)startBlueTimerWithDirection:(int)direction {
// if (!self.isResizingBlue) {
self.currentBlueAction = direction;
[self startBlueTimer];
[self startFadeTimer];
// }
}
#pragma mark Calculating Regions
-(void)calculateDrawingBounds {
NSRect tempBounds = [self bounds];
tempBounds.origin.x += LeftPadding;
tempBounds.size.width -= RightPadding+LeftPadding;
tempBounds.size.height = drawHeight;
tempBounds.size.width -= LineWidth*2;
tempBounds.size.height -= 2;//2px padding
tempBounds.origin.y = 5;
tempBounds.origin.x += LineWidth / 2;
tempBounds.origin.x -= 2;
drawingBounds = tempBounds;
}
#pragma mark Drawing
- (void)drawRect:(NSRect)rect {
float textWidth = (self.active ? currentBlueOffset-drawingBounds.origin.x-2 : drawingBounds.size.width-2);
NSImage *primaryStringImage = [self stringImageWithWidth:textWidth andOffset:currentScrollOffset-2];//autoreleased
float currentPrimaryDrawPoint = drawingBounds.origin.x + 2.0;
[self lockFocus];
[[self backgroundImage] compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
float yDrawPoint = (drawingBounds.size.height/2)-(primaryStringImage.size.height/2)+1;
[primaryStringImage compositeToPoint:NSMakePoint(currentPrimaryDrawPoint, yDrawPoint) operation:NSCompositeSourceAtop];
if ([self shouldScroll]) {
NSImage *secondaryStringImage = [self stringImageWithWidth:textWidth andOffset:currentScrollOffset - stringImage.size.width - 20];//autoreleased
[secondaryStringImage compositeToPoint:NSMakePoint(currentPrimaryDrawPoint, yDrawPoint) operation:NSCompositeSourceOver];
}
if (self.active) {
NSImage *arrowImage = (self.blueIsClosed ? [NSImage imageNamed:@"BlueArrow_Left"] : [NSImage imageNamed:@"BlueArrow_Right"]);
[arrowImage drawAtPoint:NSMakePoint(drawingBounds.origin.x+drawingBounds.size.width-(blueLimit_Off/2)-(arrowImage.size.width/2),(drawHeight/2)-(arrowImage.size.height/2)+1) fromRect:NSMakeRect(0, 0, arrowImage.size.width, arrowImage.size.height) operation:NSCompositeSourceAtop fraction:1];
float lastDrawPoint = drawingBounds.origin.x + drawingBounds.size.width - blueLimit_On+7.0;
float opacityValue = self.currentLoveHateIconOpacity/10.0;
if (opacityValue>0.0) { // Fixes strange (but possibly useful) functionality where fraction:0.0 = fraction:1.0
int i;
for (i=0; i<iconSet.count; i++) {
NSImage *currentIcon;
BOOL wasCopied = NO;
if (self.hoveredIcon==i) {
wasCopied = YES;
currentIcon = [[[iconSet objectAtIndex:i] objectForKey:@"image"] copy];
NSImage *overlayColor = [[NSImage alloc] initWithSize:currentIcon.size];
[overlayColor lockFocus];
[(self.pressedIcon == i ? [NSColor blackColor] : [NSColor whiteColor]) set];
NSRectFill(NSMakeRect(0,0,currentIcon.size.width,currentIcon.size.height));
[overlayColor unlockFocus];
[currentIcon lockFocus];
[overlayColor compositeToPoint:NSZeroPoint operation:NSCompositeSourceAtop fraction:0.3];
[currentIcon unlockFocus];
[overlayColor release];
} else {
currentIcon = [[iconSet objectAtIndex:i] objectForKey:@"image"];
}
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationNone];
[currentIcon drawAtPoint:NSMakePoint(lastDrawPoint,3) fromRect:NSMakeRect(0, 0, currentIcon.size.width, currentIcon.size.height) operation:NSCompositeSourceOver fraction:opacityValue];
lastDrawPoint += currentIcon.size.width + 4;
if (wasCopied) [currentIcon release];
}
}
}
[self unlockFocus];
}
- (BOOL)isFlipped {
return NO;
}
#pragma mark Generating Cached Images
-(NSImage *)backgroundImage {
if (!backgroundImage) [self generateBackgroundImage];
return backgroundImage;
}
-(void)generateBackgroundImage {
if (backgroundImage!=nil) [backgroundImage release];
float drawWidth = self.bounds.size.width;
backgroundImage = [[NSImage alloc] initWithSize:NSMakeSize(drawWidth, drawHeight)];
NSRect roundedFrame;
roundedFrame.origin.x = 37;
roundedFrame.origin.y = 0;
roundedFrame.origin.y += LineWidth;
roundedFrame.size.height = drawHeight;
roundedFrame.size.width = drawWidth-roundedFrame.origin.x-30;
roundedFrame.size.height -= LineWidth*2;
roundedFrame.size.width -= LineWidth*2;
NSBezierPath *roundedPath = [NSBezierPath bezierPathWithRoundRectInRect:roundedFrame radius:drawHeight-0.5];
NSBezierPath *shinePath = [NSBezierPath bezierPathWithTopHalfOfRoundRectInRect:roundedFrame radius:drawHeight-0.5];
NSImage *shineImage = [[NSImage alloc] initWithSize:NSMakeSize(drawWidth-30,roundedFrame.size.height)];
[shineImage lockFocus];
[[NSColor whiteColor] set];
[shinePath fill];
[shineImage unlockFocus];
float blueHeight = drawHeight-1;
float currentBlueWidth;
currentBlueWidth = (drawingBounds.origin.x + drawingBounds.size.width) - currentBlueOffset;
NSImage *blueImage;
blueImage = [[NSImage alloc] initWithSize:NSMakeSize(currentBlueWidth,blueHeight)];
[blueImage lockFocus];
[[NSColor colorWithCalibratedRed:(131.0/255.0) green:(175.0/255.0) blue:(234.0/255.0) alpha:1.0] set];
[NSBezierPath fillRect:NSMakeRect(1,1,currentBlueWidth,blueHeight)];
[blueImage unlockFocus];
[backgroundImage lockFocus];
NSGradient *backgroundGradient = [[[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:0.80 green:0.80 blue:0.80 alpha:1.00] endingColor:[NSColor colorWithCalibratedRed:0.90 green:0.90 blue:0.90 alpha:1.00]] autorelease];
[backgroundGradient drawInBezierPath:roundedPath angle:90];
if (self.active) [blueImage drawAtPoint:NSMakePoint(currentBlueOffset,0) fromRect:NSZeroRect operation:NSCompositeSourceAtop fraction:0.9];
[self.statusImage drawAtPoint:NSMakePoint(19,(drawHeight/2) - (self.statusImage.size.height/2)) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[shineImage drawAtPoint:NSMakePoint(0,0) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.4];
// Stroke Entire Capsule
[[NSColor colorWithCalibratedWhite:0.0 alpha:1.0] set];
[roundedPath setLineWidth:LineWidth];
[roundedPath stroke];
// Draw 1px vertical blue divider
if (self.active) {
[[NSColor colorWithCalibratedWhite:0.5 alpha:1.0] set];
NSBezierPath *onePixelVerticalLine = [NSBezierPath bezierPathWithRect:NSMakeRect(currentBlueOffset, 1,1,drawHeight-2)];
[onePixelVerticalLine fill];
}
[backgroundImage unlockFocus];
[shineImage release];
[blueImage release];
}
@synthesize stringImage;
@synthesize statusImage;
-(NSImage *)stringImage {
if (!stringImage) [self generateStringImage];
return stringImage;
}
-(NSImage *)stringImageWithWidth:(float)theWidth andOffset:(float)theOffset {
if (!stringImage) [self generateStringImage];
NSImage *returnImage = [[NSImage alloc] initWithSize:NSMakeSize(theWidth, stringImage.size.height)];
[returnImage lockFocus];
[stringImage compositeToPoint:NSMakePoint(0-theOffset+6, 0) operation:NSCompositeSourceOver];
[returnImage unlockFocus];
return [returnImage autorelease];
}
-(void)generateStringImage {
NSAttributedString *drawString = [[NSAttributedString alloc] initWithString:self.stringValue attributes:attributesDictionary];
NSImage *tempImage = [[NSImage alloc] initWithSize:[drawString size]];
[tempImage lockFocus];
[drawString drawAtPoint:NSZeroPoint];
[tempImage unlockFocus];
self.stringImage = tempImage;
[tempImage release];
[drawString release];
}
-(NSImage *)statusImage {
if (!statusImage) [self generateStatusImage];
return statusImage;
}
-(void)generateStatusImage {
NSImage *tempImage;
BGScrobbleDecisionManager *decisionMaker = [BGScrobbleDecisionManager sharedManager];
if ([decisionMaker isDecisionMadeAutomatically]) {
tempImage = [decisionMaker shouldScrobble] ? [NSImage imageNamed:@"auto1"] : [NSImage imageNamed:@"auto0"];
} else {
tempImage = [decisionMaker shouldScrobble] ? [NSImage imageNamed:@"1"] : [NSImage imageNamed:@"0"];
}
self.statusImage = tempImage;
}
#pragma mark View Being Shown
-(void)viewDidMoveToWindow {
[self resetBlueToOffState];
[self startScrolling];
}
#pragma mark Scrolling
-(void)startScrolling {
currentScrollOffset = 0.0;
[self stopScrolling];
if ([self shouldScroll]) {
scrollTimer = [[NSTimer scheduledTimerWithTimeInterval:ScrollSpeed target:self selector:@selector(incrementScroll:) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:scrollTimer forMode:NSEventTrackingRunLoopMode];
}
[self setNeedsDisplay:YES];
}
-(void)stopScrolling {
if (scrollTimer!=nil) {
[scrollTimer invalidate];
[self setNeedsDisplay:YES];
}
}
-(void)incrementScroll:(NSTimer *)timer {
currentScrollOffset += ScrollAmount;
if (drawingBounds.origin.x - currentScrollOffset + stringImage.size.width < 18) currentScrollOffset = 0.0;
[self setNeedsDisplay:YES];
}
-(BOOL)shouldScroll {
return (drawingBounds.size.width-44 < self.stringImage.size.width);
}
#pragma mark String Value
-(NSString *)stringValue {
return stringValue;
}
-(void)setStringValue:(NSString *)aString {
if (aString) {
if (stringValue) {
[stringValue release];
stringValue = nil;
}
stringValue = [aString copy];
[self generateStringImage];
[self startScrolling];
[self setNeedsDisplay:YES];
}
}
-(void)setStringValue:(NSString *)aString isActive:(BOOL)aBool {
[self setActive:aBool];
self.stringValue = aString;
self.properStringValue = aString;
}
@synthesize active;
-(void)setActive:(BOOL)aBool {
active = aBool;
// If not active, hide blue section
}
@synthesize properStringValue;
-(void)setTemporaryHoverStringValue:(NSString *)aString {
[self startStatusChangeTimer];
self.stringValue = aString;
}
-(void)revertFromHoverToStringValue:(NSTimer*)theTimer {
self.stringValue = self.properStringValue;
}
@end