Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5ea1c3

Browse files
committed
Progressbar update taken out of FilesQueue
1 parent bfc2d20 commit f5ea1c3

3 files changed

Lines changed: 42 additions & 33 deletions

File tree

imageoptim/FilesQueue.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@
1212
@interface FilesQueue : NSArrayController <NSTableViewDelegate,NSTableViewDataSource> {
1313
NSTableView *tableView;
1414
NSArrayController *filesController;
15-
BOOL isEnabled;
15+
BOOL isEnabled, isBusy;
1616
NSInteger nextInsertRow;
1717
NSOperationQueue *cpuQueue;
1818
NSOperationQueue *fileIOQueue;
1919
NSOperationQueue *dirWorkerQueue;
2020

2121
NSHashTable *seenPathHashes;
2222

23-
NSProgressIndicator *progressBar;
24-
2523
NSLock *queueWaitingLock;
2624
}
2725

28-
-(id)configureWithTableView:(NSTableView*)a progressBar:(NSProgressIndicator *)p;
26+
-(id)configureWithTableView:(NSTableView*)a;
2927

3028
- (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)aTableColumn row:(int)row mouseLocation:(NSPoint)mouseLocation;
3129
-(void)addPaths:(NSArray *)paths;
@@ -51,5 +49,6 @@
5149
-(NSArray *)fileTypes;
5250

5351
@property (readonly, nonatomic) NSNumber *queueCount;
52+
@property (readonly) BOOL isBusy;
5453

5554
@end

imageoptim/FilesQueue.m

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ @interface FilesQueue ()
1212

1313
-(NSArray*)extensions;
1414
-(BOOL)isAnyQueueBusy;
15-
-(void)updateProgressbar;
15+
-(void)updateBusyState;
1616
-(void)deleteObjects:(NSArray*)objects;
1717
@end
1818

1919
static NSString *kIMDraggedRowIndexesPboardType = @"com.imageoptim.rows";
2020

2121
@implementation FilesQueue
2222

23-
-(id)configureWithTableView:(NSTableView*)inTableView progressBar:(NSProgressIndicator *)inBar
23+
@synthesize isBusy;
24+
25+
-(id)configureWithTableView:(NSTableView*)inTableView
2426
{
2527
if (self = [super init]) {
26-
progressBar = inBar;
2728
tableView = inTableView;
2829
seenPathHashes = [[NSHashTable alloc] initWithOptions:NSHashTableZeroingWeakMemory capacity:1000];
2930

@@ -71,21 +72,15 @@ -(void)waitForQueuesToFinish {
7172
[cpuQueue waitUntilAllOperationsAreFinished];
7273

7374
} while ([self isAnyQueueBusy]);
75+
NSLog(@"wait thread loop end");
7476
}
7577
@finally {
7678
[queueWaitingLock unlock];
7779
}
7880
}
79-
[self performSelectorOnMainThread:@selector(updateProgressbar) withObject:nil waitUntilDone:NO];
81+
[self performSelectorOnMainThread:@selector(updateBusyState) withObject:nil waitUntilDone:NO];
8082
}
8183

82-
-(void)waitInBackgroundForQueuesToFinish {
83-
if ([queueWaitingLock tryLock]) // if it's locked, there's thread waiting for finish
84-
{
85-
[queueWaitingLock unlock]; // can't lock/unlock across threads, so new lock will have to be made
86-
[self performSelectorInBackground:@selector(waitForQueuesToFinish) withObject:nil];
87-
}
88-
}
8984

9085
-(void)setRow:(NSInteger)row
9186
{
@@ -322,7 +317,7 @@ -(void)addFileObjects:(NSArray *)files
322317
}
323318
}
324319

325-
[self updateProgressbar];
320+
[self updateBusyState];
326321
}
327322

328323
-(void)addPaths:(NSArray*)paths
@@ -434,23 +429,26 @@ -(void)startAgainOptimized:(BOOL)optimized
434429

435430
if (!anyStarted) NSBeep();
436431

437-
[self updateProgressbar];
432+
[self updateBusyState];
438433
}
439434

440-
-(void)updateProgressbar
435+
-(void)updateBusyState
441436
{
442-
if (![self isAnyQueueBusy])
443-
{
444-
[progressBar stopAnimation:nil];
445-
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BounceDock"]) {
446-
[NSApp requestUserAttention:NSInformationalRequest];
437+
BOOL currentlyBusy = [self isAnyQueueBusy];
438+
439+
if (isBusy != currentlyBusy) {
440+
[self willChangeValueForKey:@"isBusy"];
441+
isBusy = currentlyBusy;
442+
[self didChangeValueForKey:@"isBusy"];
443+
444+
if (isBusy) {
445+
if ([queueWaitingLock tryLock]) // if it's locked, there's thread waiting for finish
446+
{
447+
[queueWaitingLock unlock]; // can't lock/unlock across threads, so new lock will have to be made
448+
[self performSelectorInBackground:@selector(waitForQueuesToFinish) withObject:nil];
449+
}
447450
}
448-
}
449-
else
450-
{
451-
[progressBar startAnimation:nil];
452-
[self waitInBackgroundForQueuesToFinish];
453-
}
451+
}
454452
}
455453

456454
#define PNG_ENABLED 1

imageoptim/ImageOptim.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ @implementation ImageOptim
1313
NSDictionary *statusImages;
1414

1515
static NSString *kIMPreviewPanelContext = @"preview";
16+
static NSString *kIMQueueBusyContext = @"isBusy";
1617

1718
@synthesize filesQueue=filesController;
1819

@@ -28,7 +29,8 @@ - (void)applicationWillFinishLaunching:(NSNotification *)unused {
2829

2930
[[NSUserDefaults standardUserDefaults] registerDefaults:defs];
3031

31-
[filesController configureWithTableView:tableView progressBar:progressBar];
32+
[filesController configureWithTableView:tableView];
33+
[filesController addObserver:self forKeyPath:@"isBusy" options:nil context:kIMQueueBusyContext];
3234

3335
[NSApp setServicesProvider:self];
3436
NSUpdateDynamicServices();
@@ -192,9 +194,19 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
192194
if (context == kIMPreviewPanelContext) {
193195
[previewPanel reloadData];
194196
} else {
195-
// Defer and coalesce statusbar updates
196-
dispatch_source_merge_data(statusBarUpdateQueue, 1);
197-
}
197+
if (context == kIMQueueBusyContext) {
198+
if (filesController.isBusy) {
199+
[progressBar startAnimation:self];
200+
} else {
201+
[progressBar stopAnimation:self];
202+
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"BounceDock"]) {
203+
[NSApp requestUserAttention:NSInformationalRequest];
204+
}
205+
}
206+
}
207+
// Defer and coalesce statusbar updates
208+
dispatch_source_merge_data(statusBarUpdateQueue, 1);
209+
}
198210
}
199211

200212
-(int)numberOfCPUs

0 commit comments

Comments
 (0)