-
Notifications
You must be signed in to change notification settings - Fork 29
/
SSProgressPanel.m
586 lines (472 loc) · 12.8 KB
/
SSProgressPanel.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
//
// SSProgressPanel.m
// Stick Software subsystem
//
// Created by Ben Haller on Thu Jul 31 2003.
// Copyright (c) 2003 Stick Software. All rights reserved.
//
#import "SSProgressPanel.h"
#import "CocoaExtra.h"
@interface SSProgressPanel (Private)
- (void)makeProgressWindow;
@end
@implementation SSProgressPanel
+ (SSProgressPanel *)progressPanelModalForWindow:(NSWindow *)window title:(NSString *)title subtitle:(NSString *)subtitle determinate:(BOOL)determinate
{
return [[[SSProgressPanel alloc] initModalForWindow:window title:title subtitle:subtitle determinate:determinate] autorelease];
}
- (id)initModalForWindow:(NSWindow *)window title:(NSString *)title subtitle:(NSString *)subtitle determinate:(BOOL)determinate
{
if (self = [super init])
{
modalWindow = window;
titleString = [title retain];
subtitleString = [subtitle retain];
isDeterminate = determinate;
minValue = 0.0;
maxValue = 1.0;
progress = 0.0;
thresholdTime = 0.20;
}
return self;
}
- (BOOL)isDeterminate
{
return isDeterminate;
}
- (void)setDeterminate:(BOOL)flag
{
if (flag != isDeterminate)
{
isDeterminate = flag;
[progressIndicator setIndeterminate:!isDeterminate];
if (isDeterminate)
{
[progressIndicator setMinValue:minValue];
[progressIndicator setMaxValue:maxValue];
[progressIndicator setDoubleValue:progress];
}
}
}
- (void)dealloc
{
if (progressWindow)
[self finish];
[titleString release];
titleString = nil;
[subtitleString release];
subtitleString = nil;
[elapsedTimeBase release];
elapsedTimeBase = nil;
[lastUpdateTime release];
lastUpdateTime = nil;
[super dealloc];
}
- (void)start
{
if (!elapsedTimeBase)
{
// Remember the time we started at, so we can figure how long we've got without showing our panel
elapsedTimeBase = [[NSDate alloc] init];
// Uncomment this to make our panel be visible as soon as a task is started,
// bypassing the time to completion calculations.
//[self forceVisible];
}
}
- (void)startNewTask
{
[elapsedTimeBase release];
elapsedTimeBase = nil;
[lastUpdateTime release];
lastUpdateTime = nil;
if (isDeterminate)
[self setProgress:0.0];
[self start];
}
- (void)startNewTaskAndSetMaxValue:(double)value
{
[self setMaxValue:value];
[self startNewTask];
}
- (void)finish
{
if (progressWindow)
{
// Stop our modal loop or sheet
if (modalWindow)
[NSApp endSheet:progressWindow];
else
[NSApp endModalSession:modalSession];
// Close our window, which releases itself
[progressWindow close];
progressWindow = nil;
// Nil out the views inside our window, which have now been released
progressIndicator = nil;
progressTitle = nil;
progressSubtitle = nil;
}
// Forget our elapsed time
[elapsedTimeBase release];
elapsedTimeBase = nil;
}
- (void)finishAndRelease
{
[self finish];
[self release];
}
- (double)minValue
{
if (!isDeterminate)
NSLog(@"minValue called on an indeterminate SSProgressPanel");
return minValue;
}
- (void)setMinValue:(double)value
{
if (isDeterminate)
{
minValue = value;
if (progressIndicator)
[progressIndicator setMinValue:value];
if (progress < minValue)
[self setProgress:minValue];
}
else
NSLog(@"setMinValue: called on an indeterminate SSProgressPanel");
}
- (double)maxValue
{
if (!isDeterminate)
NSLog(@"maxValue called on an indeterminate SSProgressPanel");
return maxValue;
}
- (void)setMaxValue:(double)value
{
if (isDeterminate)
{
maxValue = value;
if (progressIndicator)
[progressIndicator setMaxValue:value];
if (progress > maxValue)
[self setProgress:maxValue];
}
else
NSLog(@"setMaxValue: called on an indeterminate SSProgressPanel");
}
- (double)progress
{
if (!isDeterminate)
NSLog(@"progress called on an indeterminate SSProgressPanel");
return progress;
}
- (void)_setProgress:(double)value
{
if (isDeterminate)
{
if (value < minValue) value = minValue;
if (value > maxValue) value = maxValue;
progress = value;
if (!elapsedTimeBase)
[self start];
}
else
NSLog(@"setProgress: called on an indeterminate SSProgressPanel");
}
- (void)makeVisibleIfNecessary
{
// If we haven't yet shown our panel, decide whether we should
if (!progressWindow)
{
double elapsedTime = [self elapsedTime];
// We never bring up our panel before a certain threshold, since our time-to-completion estimates
// may be very inaccurate at the start, and we don't want to flash for very brief tasks.
if (elapsedTime > thresholdTime)
{
if (isDeterminate)
{
double estimatedTimeToCompletion = [self estimatedTimeToCompletion];
if (estimatedTimeToCompletion > 1.0)
{
//NSLog(@"progress == %f, max == %f, elapsedTime == %f, estimatedTimeToCompletion == %f; forcing panel visible", progress, maxValue, elapsedTime, estimatedTimeToCompletion);
[self forceVisible];
}
}
else
{
[self forceVisible];
}
}
}
}
- (void)setProgress:(double)value
{
if (isDeterminate)
{
if (value < minValue) value = minValue;
if (value > maxValue) value = maxValue;
progress = value;
if (!elapsedTimeBase)
[self start];
[progressIndicator setDoubleValue:progress];
}
else
NSLog(@"setProgress: called on an indeterminate SSProgressPanel");
[self makeVisibleIfNecessary];
}
- (void)_giveTime
{
if (!elapsedTimeBase)
NSLog(@"-_giveTime called without having started a task");
// We only give time if our panel has been put up. Otherwise, the enabling of menu items and such will be wrong.
if (progressWindow)
{
if (modalWindow)
{
// If we're running as a sheet, we give time to the main event loop in the default mode
NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate date] inMode:NSDefaultRunLoopMode dequeue:YES];
if (event)
[NSApp sendEvent:event];
}
else
{
// If we're running as a modal panel, we give time to the modal run loop
[NSApp runModalSession:modalSession];
}
}
else if (giveTimeToRunLoop)
{
// We spin the run loop, which allows URL fetches to proceed even though we're not processing events right now
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate date]];
}
}
- (BOOL)giveTime
{
BOOL refreshTimeElapsed = NO;
// -setProgress: does this for determinate tasks; we do it for indeterminate ones
if (!elapsedTimeBase)
[self start];
if (!lastUpdateTime)
{
lastUpdateTime = [[NSDate alloc] init];
refreshTimeElapsed = YES;
}
else
{
if (-[lastUpdateTime timeIntervalSinceNow] > 0.05)
{
[lastUpdateTime release];
lastUpdateTime = [[NSDate alloc] init];
refreshTimeElapsed = YES;
}
}
if (refreshTimeElapsed)
{
// -setProgress: does this for determinate tasks; we do it for indeterminate ones
[self makeVisibleIfNecessary];
[self _giveTime];
}
return !progressStopped;
}
- (BOOL)giveTimeAndSetProgress:(double)value
{
BOOL refreshTimeElapsed = NO;
if (!lastUpdateTime)
{
lastUpdateTime = [[NSDate alloc] init];
refreshTimeElapsed = YES;
}
else
{
if (-[lastUpdateTime timeIntervalSinceNow] > 0.05)
{
[lastUpdateTime release];
lastUpdateTime = [[NSDate alloc] init];
refreshTimeElapsed = YES;
}
}
if (isDeterminate)
{
if (refreshTimeElapsed)
[self setProgress:value];
else
[self _setProgress:value];
}
else
NSLog(@"giveTimeAndSetProgress: called on an indeterminate SSProgressPanel");
if (refreshTimeElapsed)
[self _giveTime];
return !progressStopped;
}
- (BOOL)giveTimeToRunLoop
{
return giveTimeToRunLoop;
}
- (void)setGiveTimeToRunLoop:(BOOL)flag
{
giveTimeToRunLoop = flag;
}
- (NSString *)title
{
return titleString;
}
- (void)setTitle:(NSString *)title
{
if (![titleString isEqualToString:title])
{
[title retain];
[titleString release];
titleString = title;
if (progressTitle)
[progressTitle setStringValue:(titleString ? titleString : @"")];
}
}
- (NSString *)subtitle
{
return subtitleString;
}
- (void)setSubtitle:(NSString *)subtitle
{
if (![subtitleString isEqualToString:subtitle])
{
[subtitle retain];
[subtitleString release];
subtitleString = subtitle;
if (progressSubtitle)
[progressSubtitle setStringValue:(subtitleString ? subtitleString : @"")];
}
}
- (BOOL)isVisible
{
return (progressWindow ? YES : NO);
}
- (void)forceVisible
{
if (!progressWindow)
{
[self makeProgressWindow];
if (!isDeterminate)
[progressIndicator startAnimation:nil];
if (modalWindow)
[modalWindow beginSheet:progressWindow completionHandler:NULL];
else
modalSession = [NSApp beginModalSessionForWindow:progressWindow];
}
}
- (BOOL)isStoppedByUser
{
return progressStopped;
}
- (IBAction)performStop:(id)sender
{
#pragma unused (sender)
progressStopped = YES;
}
- (NSTimeInterval)elapsedTime
{
NSTimeInterval elapsedTime = 0.0;
if (elapsedTimeBase)
elapsedTime = -[elapsedTimeBase timeIntervalSinceNow];
else
NSLog(@"elapsedTime called on an SSProgressPanel that has not been started");
return elapsedTime;
}
- (NSTimeInterval)estimatedTotalTime
{
NSTimeInterval estimatedTotalTime = 0.0;
if (isDeterminate)
{
if (elapsedTimeBase)
{
NSTimeInterval elapsedTime = [self elapsedTime];
double fractionCompleted = ((progress - minValue) / (maxValue - minValue));
estimatedTotalTime = elapsedTime / fractionCompleted;
}
else
NSLog(@"estimatedTotalTime called on an unstarted SSProgressPanel");
}
else
NSLog(@"estimatedTotalTime called on an indeterminate SSProgressPanel");
return estimatedTotalTime;
}
- (NSTimeInterval)estimatedTimeToCompletion
{
NSTimeInterval estimatedTimeToCompletion = 0.0;
if (isDeterminate)
{
if (elapsedTimeBase)
{
NSTimeInterval elapsedTime = [self elapsedTime];
double fractionCompleted = ((progress - minValue) / (maxValue - minValue));
estimatedTimeToCompletion = (elapsedTime / fractionCompleted) - elapsedTime;
}
else
NSLog(@"estimatedTimeToCompletion called on an unstarted SSProgressPanel");
}
else
NSLog(@"estimatedTimeToCompletion called on an indeterminate SSProgressPanel");
return estimatedTimeToCompletion;
}
- (double)thresholdTime
{
return thresholdTime;
}
- (void)setThresholdTime:(double)newThresholdTime
{
thresholdTime = newThresholdTime;
}
@end
@implementation SSProgressPanel (Private)
- (void)makeProgressWindow
{
NSSize panelSize = NSMakeSize(331, 128);
NSImageView *iconView = [[NSImageView alloc] initWithFrame:NSMakeRect(24, panelSize.height - (16 + 64), 64, 64)];
NSButton *cancelButton = [[NSButton alloc] initWithFrame:NSMakeRect(panelSize.width - 110, 11, 90, 34)];
NSString *buttonString = SSLocalizedStringFromTable(@"Stop button", @"Base", @"Stop button");
NSView *contentView;
NSFont *lucida13bold = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
NSFont *lucida13 = [NSFont systemFontOfSize:[NSFont systemFontSize]];
NSFont *lucida11 = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
progressWindow = [[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, panelSize.width, panelSize.height) styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:YES];
[progressWindow setReleasedWhenClosed:YES];
[progressWindow setDelegate:self];
[progressWindow setHidesOnDeactivate:NO];
progressTitle = [[NSTextField alloc] initWithFrame:NSMakeRect(104, panelSize.height - (15 + 17), panelSize.width - 136, 17)];
progressSubtitle = [[NSTextField alloc] initWithFrame:NSMakeRect(104, panelSize.height - (41 + 28), panelSize.width - 136, 28)];
progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(105, 18, panelSize.width - 227, 20)];
contentView = [progressWindow contentView];
[iconView setImage:[NSImage imageNamed:@"NSApplicationIcon"]];
[contentView addSubview:iconView];
[iconView release];
[progressTitle setStringValue:(titleString ? titleString : @"")];
[progressTitle setFont:lucida13bold];
[progressTitle setEditable:NO];
[progressTitle setBordered:NO];
[progressTitle setDrawsBackground:NO];
[contentView addSubview:progressTitle];
[progressTitle release];
[progressSubtitle setStringValue:(subtitleString ? subtitleString : @"")];
[progressSubtitle setFont:lucida11];
[progressSubtitle setEditable:NO];
[progressSubtitle setBordered:NO];
[progressSubtitle setDrawsBackground:NO];
[contentView addSubview:progressSubtitle];
[progressSubtitle release];
[cancelButton setTitle:buttonString];
[cancelButton setFont:lucida13];
[cancelButton setBezelStyle:NSBezelStyleRounded];
[cancelButton setTarget:self];
[cancelButton setAction:@selector(performStop:)];
[contentView addSubview:cancelButton];
[cancelButton release];
[progressIndicator setIndeterminate:!isDeterminate];
[progressIndicator setUsesThreadedAnimation:YES];
if (isDeterminate)
{
[progressIndicator setMinValue:minValue];
[progressIndicator setMaxValue:maxValue];
[progressIndicator setDoubleValue:progress];
}
[contentView addSubview:progressIndicator];
[progressIndicator release];
progressStopped = NO;
}
@end