forked from jgallen23/MilkMaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YRKSpinningProgressIndicator.m
350 lines (292 loc) · 8.96 KB
/
YRKSpinningProgressIndicator.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
//
// YRKSpinningProgressIndicator.m
//
// Copyright 2009 Kelan Champagne. All rights reserved.
//
#import "YRKSpinningProgressIndicator.h"
@interface YRKSpinningProgressIndicator (YRKSpinningProgressIndicatorPrivate)
- (void)updateFrame:(NSTimer *)timer;
- (void)animateInBackgroundThread;
- (void)actuallyStartAnimation;
- (void)actuallyStopAnimation;
@end
@implementation YRKSpinningProgressIndicator
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_position = 0;
_numFins = 12;
_isAnimating = NO;
_isFadingOut = NO;
_isIndeterminate = YES;
_currentValue = 0.0;
_maxValue = 100.0;
}
return self;
}
- (void) dealloc {
if (_foreColor) [_foreColor release];
if (_backColor) [_backColor release];
if (_isAnimating) [self stopAnimation:self];
[super dealloc];
}
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
if ([self window] == nil) {
// No window? View hierarchy may be going away. Dispose timer to clear circular retain of timer to self to timer.
[self actuallyStopAnimation];
}
else if (_isAnimating) {
[self actuallyStartAnimation];
}
}
- (void)drawRect:(NSRect)rect
{
int i;
float alpha = 1.0;
// Determine size based on current bounds
NSSize size = [self bounds].size;
float maxSize;
if(size.width >= size.height)
maxSize = size.height;
else
maxSize = size.width;
// fill the background, if set
if(_drawBackground) {
[_backColor set];
[NSBezierPath fillRect:[self bounds]];
}
CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
[NSGraphicsContext saveGraphicsState];
// Move the CTM so 0,0 is at the center of our bounds
CGContextTranslateCTM(currentContext,[self bounds].size.width/2,[self bounds].size.height/2);
if (_isIndeterminate) {
// do initial rotation to start place
CGContextRotateCTM(currentContext, 3.14159*2/_numFins * _position);
NSBezierPath *path = [[NSBezierPath alloc] init];
float lineWidth = 0.0859375 * maxSize; // should be 2.75 for 32x32
float lineStart = 0.234375 * maxSize; // should be 7.5 for 32x32
float lineEnd = 0.421875 * maxSize; // should be 13.5 for 32x32
[path setLineWidth:lineWidth];
[path setLineCapStyle:NSRoundLineCapStyle];
[path moveToPoint:NSMakePoint(0,lineStart)];
[path lineToPoint:NSMakePoint(0,lineEnd)];
for(i=0; i<_numFins; i++) {
if(_isAnimating) {
[[_foreColor colorWithAlphaComponent:alpha] set];
}
else {
[[_foreColor colorWithAlphaComponent:0.2] set];
}
[path stroke];
// we draw all the fins by rotating the CTM, then just redraw the same segment again
CGContextRotateCTM(currentContext, 6.282185/_numFins);
alpha -= 1.0/_numFins;
}
[path release];
}
else {
float lineWidth = 1 + (0.01 * maxSize);
float circleRadius = (maxSize - lineWidth) / 2.1;
NSPoint circleCenter = NSMakePoint(0, 0);
[[_foreColor colorWithAlphaComponent:alpha] set];
NSBezierPath *path = [[NSBezierPath alloc] init];
[path setLineWidth:lineWidth];
[path appendBezierPathWithOvalInRect:NSMakeRect(-circleRadius, -circleRadius, circleRadius*2, circleRadius*2)];
[path stroke];
[path release];
path = [[NSBezierPath alloc] init];
[path appendBezierPathWithArcWithCenter:circleCenter radius:circleRadius startAngle:90 endAngle:90-(360*(_currentValue/_maxValue)) clockwise:YES];
[path lineToPoint:circleCenter] ;
[path fill];
[path release];
}
[NSGraphicsContext restoreGraphicsState];
}
# pragma mark -
# pragma mark Subclass
- (void)updateFrame:(NSTimer *)timer;
{
if(_position > 0) {
_position--;
}
else {
_position = _numFins - 1;
}
if (_usesThreadedAnimation) {
// draw now instead of waiting for setNeedsDisplay (that's the whole reason
// we're animating from background thread)
[self display];
}
else {
[self setNeedsDisplay:YES];
}
}
- (void)animateInBackgroundThread
{
NSAutoreleasePool *animationPool = [[NSAutoreleasePool alloc] init];
// Set up the animation speed to subtly change with size > 32.
// int animationDelay = 38000 + (2000 * ([self bounds].size.height / 32));
// Set the rev per minute here
int omega = 100; // RPM
int animationDelay = 60*1000000/omega/_numFins;
int poolFlushCounter = 0;
do {
[self updateFrame:nil];
usleep(animationDelay);
poolFlushCounter++;
if (poolFlushCounter > 256) {
[animationPool drain];
animationPool = [[NSAutoreleasePool alloc] init];
poolFlushCounter = 0;
}
} while (![[NSThread currentThread] isCancelled]);
[animationPool release];
}
- (void)startAnimation:(id)sender
{
if (!_isIndeterminate) return;
if (_isAnimating) return;
[self actuallyStartAnimation];
_isAnimating = YES;
}
- (void)stopAnimation:(id)sender
{
[self actuallyStopAnimation];
_isAnimating = NO;
}
- (void)actuallyStartAnimation
{
// Just to be safe kill any existing timer.
[self actuallyStopAnimation];
if ([self window]) {
// Why animate if not visible? viewDidMoveToWindow will re-call this method when needed.
if (_usesThreadedAnimation) {
_animationThread = [[NSThread alloc] initWithTarget:self selector:@selector(animateInBackgroundThread) object:nil];
[_animationThread start];
}
else {
_animationTimer = [[NSTimer timerWithTimeInterval:(NSTimeInterval)0.05
target:self
selector:@selector(updateFrame:)
userInfo:nil
repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:NSEventTrackingRunLoopMode];
}
}
}
- (void)actuallyStopAnimation
{
if (_animationThread) {
// we were using threaded animation
[_animationThread cancel];
if (![_animationThread isFinished]) {
[[NSRunLoop currentRunLoop] runMode:NSModalPanelRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
}
[_animationThread release];
_animationThread = nil;
}
else if (_animationTimer) {
// we were using timer-based animation
[_animationTimer invalidate];
[_animationTimer release];
_animationTimer = nil;
}
[self setNeedsDisplay:YES];
}
# pragma mark Not Implemented
- (void)setStyle:(NSProgressIndicatorStyle)style
{
if (NSProgressIndicatorSpinningStyle != style) {
NSAssert(NO, @"Non-spinning styles not available.");
}
}
# pragma mark -
# pragma mark Accessors
- (NSColor *)foreColor
{
return [[_foreColor retain] autorelease];
}
- (void)setForeColor:(NSColor *)value
{
if (_foreColor != value) {
[_foreColor release];
_foreColor = [value copy];
[self setNeedsDisplay:YES];
}
}
- (NSColor *)backColor
{
return [[_backColor retain] autorelease];
}
- (void)setBackColor:(NSColor *)value
{
if (_backColor != value) {
[_backColor release];
_backColor = [value copy];
[self setNeedsDisplay:YES];
}
}
- (BOOL)drawBackground
{
return _drawBackground;
}
- (void)setDrawBackground:(BOOL)value
{
if (_drawBackground != value) {
_drawBackground = value;
}
[self setNeedsDisplay:YES];
}
- (BOOL)isIndeterminate
{
return _isIndeterminate;
}
- (void)setIndeterminate:(BOOL)isIndeterminate
{
_isIndeterminate = isIndeterminate;
if (!_isIndeterminate && _isAnimating) [self stopAnimation:self];
[self setNeedsDisplay:YES];
}
- (double)doubleValue
{
return _currentValue;
}
- (void)setDoubleValue:(double)doubleValue
{
// Automatically put it into determinate mode if it's not already.
if (_isIndeterminate) {
[self setIndeterminate:NO];
}
_currentValue = doubleValue;
[self setNeedsDisplay:YES];
}
- (double)maxValue
{
return _maxValue;
}
- (void)setMaxValue:(double)maxValue
{
_maxValue = maxValue;
[self setNeedsDisplay:YES];
}
- (void)setUsesThreadedAnimation:(BOOL)useThreaded
{
if (_usesThreadedAnimation != useThreaded) {
_usesThreadedAnimation = useThreaded;
if (_isAnimating) {
// restart the timer to use the new mode
[self stopAnimation:self];
[self startAnimation:self];
}
}
}
- (BOOL)usesThreadedAnimation
{
return _usesThreadedAnimation;
}
@end