Revision: 23912 http://sourceforge.net/p/bibdesk/svn/23912 Author: hofman Date: 2019-07-01 17:48:45 +0000 (Mon, 01 Jul 2019) Log Message: ----------- Manage progress indicator cells separately from downloads, and have them updated from download delegate methods. Allows them to be added externally.
Modified Paths: -------------- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.h trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.m trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.h trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.m Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.h =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.h 2019-07-01 16:35:31 UTC (rev 23911) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.h 2019-07-01 17:48:45 UTC (rev 23912) @@ -38,7 +38,6 @@ #import <Cocoa/Cocoa.h> -@class FVProgressIndicatorCell; @interface FVDownload : NSObject <NSURLDownloadDelegate> { @@ -48,7 +47,6 @@ NSUInteger _indexInView; long long _expectedLength; long long _receivedLength; - FVProgressIndicatorCell *_progressIndicator; id _delegate; } - (id)initWithDownloadURL:(NSURL *)aURL indexInView:(NSUInteger)indexInView; @@ -66,8 +64,9 @@ - (long long)expectedLength; - (void)incrementReceivedLengthBy:(NSUInteger)length; -- (FVProgressIndicatorCell *)progressIndicator; +- (CGFloat)currentProgress; + - (void)start; - (void)cancel; Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.m =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.m 2019-07-01 16:35:31 UTC (rev 23911) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVDownload.m 2019-07-01 17:48:45 UTC (rev 23912) @@ -37,7 +37,6 @@ */ #import "FVDownload.h" -#import "FVProgressIndicatorCell.h" /* FVDownload associates a URL/index combination, and stores the download destination (temp directory) for a given download, along with some other data (download progress, progress indicator rotation angle for indeterminate indicators). @@ -58,7 +57,6 @@ _fileURL = nil; _expectedLength = 0; _receivedLength = 0; - _progressIndicator = [[FVProgressIndicatorCell alloc] init]; } return self; } @@ -82,7 +80,6 @@ { [_downloadURL release]; [_fileURL release]; - [_progressIndicator release]; [super dealloc]; } @@ -104,10 +101,6 @@ - (void)setExpectedLength:(long long)expectedLength { _expectedLength = expectedLength; - if (NSURLResponseUnknownLength == expectedLength) - [_progressIndicator setStyle:FVProgressIndicatorIndeterminate]; - else - [_progressIndicator setStyle:FVProgressIndicatorDeterminate]; } - (long long)expectedLength { return _expectedLength; } @@ -115,20 +108,16 @@ - (void)setReceivedLength:(long long)receivedLength { _receivedLength = receivedLength; - [_progressIndicator setCurrentProgress:[self currentProgress]]; } - (void)incrementReceivedLengthBy:(NSUInteger)length; { _receivedLength += length; - [_progressIndicator setCurrentProgress:[self currentProgress]]; } - (NSURL *)downloadURL { return _downloadURL; } - (NSUInteger)indexInView { return _indexInView; } -- (FVProgressIndicatorCell *)progressIndicator { return _progressIndicator; } - - (id)delegate { return _delegate; } - (void)setDelegate:(id)obj { _delegate = obj; } Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.h =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.h 2019-07-01 16:35:31 UTC (rev 23911) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.h 2019-07-01 17:48:45 UTC (rev 23912) @@ -248,6 +248,7 @@ FVOperationQueue *_operationQueue; NSDictionary *_contentBinding; NSMutableArray *_downloads; + NSMutableDictionary *_progressIndicators; CFRunLoopTimerRef _progressTimer; NSMutableSet *_modificationSet; NSLock *_modificationLock; @@ -470,6 +471,39 @@ @param anOperation The new drop operation. */ - (void)setDropIndex:(NSUInteger)anIndex dropOperation:(FVDropOperation)anOperation; +/** Add a progress indicator for a URL at the given index. + + This can be used when the delegate handles downloads. + + @param aURL The URL for the icon. + @param anIndex The index for the icon. */ +- (void)addProgressIndicatorForURL:(NSURL *)aURL atIndex:(NSUInteger)anIndex; + +/** Remove a progress indicator for a URL at the given index. + + This can be used when the delegate handles downloads. + + @param aURL The URL for the icon. + @param anIndex The index for the icon. */ +- (void)removeProgressIndicatorForURL:(NSURL *)aURL atIndex:(NSUInteger)anIndex; + +/** Update a progress indicator for a URL at the given index. + + This can be used when the delegate handles downloads. + + @param progress The progress value, pass -1 to get an indeterminate progress indicator, otherwise should be between 0 and 1. + @param aURL The URL for the icon. + @param anIndex The index for the icon. */ +- (void)updateProgressIndicator:(double)progress forURL:(NSURL *)aURL atIndex:(NSUInteger)anIndex; + +/** Move a progress indicator for a URL to another index. + + This can be used when the delegate handles downloads. + + @param aURL The URL for the icon. + @param anIndex The new index for the icon. */ +- (void)moveProgressIndicatorForURL:(NSURL *)aURL toIndex:(NSUInteger)anIndex; + @end /** @var FVZoomInMenuItemTag Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.m =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.m 2019-07-01 16:35:31 UTC (rev 23911) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVFileView.m 2019-07-01 17:48:45 UTC (rev 23912) @@ -50,6 +50,7 @@ #import "FVDownload.h" #import "FVDownloadSession.h" #import "FVDownloadDeprecated.h" +#import "FVProgressIndicatorCell.h" #import "FVSlider.h" #import "FVColorMenuView.h" #import "FVAccessibilityIconElement.h" @@ -206,8 +207,8 @@ - (void)_hideArrows; - (BOOL)_hasArrows; - (void)_cancelDownloads; +- (void)_invalidateProgressTimer; - (void)_downloadURLAtIndex:(NSUInteger)anIndex; -- (void)_invalidateProgressTimer; - (void)_handleFinderLabelChanged:(NSNotification *)note; - (void)_updateBinding:(NSString *)binding; - (void)_setSelectionIndexes:(NSIndexSet *)indexSet; @@ -382,6 +383,9 @@ // array of FVDownload instances _downloads = nil; + // a dictionary of progress indicators + _progressIndicators = nil; + // timer to update the view when a download's length is indeterminate _progressTimer = NULL; @@ -415,6 +419,7 @@ - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; + [self _invalidateProgressTimer]; [_titleCell release]; [_subtitleCell release]; [_arrowAnimation stopAnimation]; @@ -438,6 +443,7 @@ // takes care of the timer as well [self _cancelDownloads]; [_downloads release]; + [_progressIndicators release]; [_modificationSet release]; [_modificationLock release]; [_operationQueue terminate]; @@ -2582,14 +2588,15 @@ [self _drawRubberbandRect]; } - if ([self allowsDownloading] && [_downloads count]) { - NSEnumerator *dlEnum = [_downloads objectEnumerator]; - FVDownload *download; - while ((download = [dlEnum nextObject])) { - NSUInteger anIndex = [download indexInView]; + if ([_progressIndicators count]) { + NSEnumerator *urlEnum = [_progressIndicators objectEnumerator]; + NSURL *url; + while ((url = [urlEnum nextObject])) { + FVProgressIndicatorCell *progressIndicator = [_progressIndicators objectForKey:url]; + NSUInteger anIndex = [progressIndicator tag]; // we only draw a if there's an active download for this URL/index pair - if (anIndex < [self numberOfIcons] && [[self URLAtIndex:anIndex] isEqual:[download downloadURL]]) - [[download progressIndicator] drawWithFrame:[self _rectOfProgressIndicatorForIconAtIndex:anIndex] inView:self]; + if (anIndex < [self numberOfIcons] && [[self URLAtIndex:anIndex] isEqual:url]) + [progressIndicator drawWithFrame:[self _rectOfProgressIndicatorForIconAtIndex:anIndex] inView:self]; } } } @@ -3849,7 +3856,7 @@ } else if (action == @selector(downloadSelectedLink:) || action == @selector(downloadCopyOfSelectedLink:)) { if ([self allowsDownloading]) { - BOOL alreadyDownloading = [[_downloads valueForKey:@"downloadURL"] containsObject:aURL]; + BOOL alreadyDownloading = nil != [_progressIndicators objectForKey:aURL]; // don't check reachability; just handle the error if it fails return isMissing == NO && isEditable && selectionCount == 1 && [aURL isFileURL] == NO && FALSE == alreadyDownloading; } else return NO; @@ -4050,18 +4057,13 @@ - (void)_progressTimerFired:(CFRunLoopTimerRef)timer { - [[_downloads valueForKey:@"progressIndicator"] makeObjectsPerformSelector:@selector(animate)]; + [[_progressIndicators allValues] makeObjectsPerformSelector:@selector(animate)]; [self setNeedsDisplay:YES]; } - (void)downloadUpdated:(FVDownload *)download { - if (NSURLResponseUnknownLength == [download expectedLength] && NULL == _progressTimer) { - // runloop will retain this timer, but we'll retain it too and release in -dealloc - _progressTimer = FVCreateWeakTimerWithTimeInterval(PROGRESS_TIMER_INTERVAL, CFAbsoluteTimeGetCurrent() + PROGRESS_TIMER_INTERVAL, self, @selector(_progressTimerFired:)); - CFRunLoopAddTimer(CFRunLoopGetCurrent(), _progressTimer, kCFRunLoopDefaultMode); - } - [self setNeedsDisplay:YES]; + [self updateProgressIndicator:[download currentProgress] forURL:[download downloadURL] atIndex:[download indexInView]]; } - (void)download:(FVDownload *)download setDestinationWithSuggestedFilename:(NSString *)filename; @@ -4098,17 +4100,14 @@ [self _setNeedsDisplayForIconInRow:r column:c]; } } + [self removeProgressIndicatorForURL:[download downloadURL] atIndex:[download indexInView]]; [_downloads removeObject:download]; - if ([_downloads count] == 0) - [self _invalidateProgressTimer]; } - (void)downloadFailed:(FVDownload *)download { + [self removeProgressIndicatorForURL:[download downloadURL] atIndex:[download indexInView]]; [_downloads removeObject:download]; - if ([_downloads count] == 0) - [self _invalidateProgressTimer]; - [self setNeedsDisplay:YES]; } - (NSWindow *)downloadWindowForSheet:(FVDownload *)download @@ -4119,9 +4118,11 @@ - (void)_cancelDownloads; { [_downloads makeObjectsPerformSelector:@selector(cancel)]; + NSEnumerator *downloadEnum = [_downloads objectEnumerator]; + FVDownload *download; + while ((download = [downloadEnum nextObject])) + [self removeProgressIndicatorForURL:[download downloadURL] atIndex:[download indexInView]]; [_downloads removeAllObjects]; - [self _invalidateProgressTimer]; - [self setNeedsDisplay:YES]; } - (void)_downloadURLAtIndex:(NSUInteger)anIndex; @@ -4131,9 +4132,10 @@ NSURL *theURL = [self URLAtIndex:anIndex]; FVDownload *download = [[downloadClass alloc] initWithDownloadURL:theURL indexInView:anIndex]; [_downloads addObject:download]; - [download release]; + [self addProgressIndicatorForURL:theURL atIndex:anIndex]; [download setDelegate:self]; [download start]; + [download release]; } } @@ -4162,6 +4164,45 @@ } } +- (void)addProgressIndicatorForURL:(NSURL *)aURL atIndex:(NSUInteger)anIndex { + FVProgressIndicatorCell *progressIndicator = [[FVProgressIndicatorCell alloc] init]; + [progressIndicator setTag:anIndex]; + [_progressIndicators setObject:progressIndicator forKey:aURL]; + [progressIndicator release]; + [self setNeedsDisplay:YES]; +} + +- (void)removeProgressIndicatorForURL:(NSURL *)aURL atIndex:(NSUInteger)anIndex { + FVProgressIndicatorCell *progressIndicator = [_progressIndicators objectForKey:aURL]; + if (progressIndicator) + [_progressIndicators setObject:progressIndicator forKey:aURL]; + [self setNeedsDisplay:YES]; + if ([_progressIndicators count] == 0 || [[[_progressIndicators allValues] valueForKey:@"style"] containsObject:[NSNumber numberWithInteger:FVProgressIndicatorIndeterminate]] == NO) + [self _invalidateProgressTimer]; +} + +- (void)updateProgressIndicator:(double)progress forURL:(NSURL *)aURL atIndex:(NSUInteger)anIndex { + FVProgressIndicatorCell *progressIndicator = [_progressIndicators objectForKey:aURL]; + if (progress < 0.0) { + [progressIndicator setStyle:FVProgressIndicatorIndeterminate]; + if (_progressTimer == NULL) { + // runloop will retain this timer, but we'll retain it too and release in -dealloc + _progressTimer = FVCreateWeakTimerWithTimeInterval(PROGRESS_TIMER_INTERVAL, CFAbsoluteTimeGetCurrent() + PROGRESS_TIMER_INTERVAL, self, @selector(_progressTimerFired:)); + CFRunLoopAddTimer(CFRunLoopGetCurrent(), _progressTimer, kCFRunLoopDefaultMode); + } + } else { + [progressIndicator setStyle:FVProgressIndicatorDeterminate]; + [progressIndicator setCurrentProgress:progress]; + [self setNeedsDisplay:YES]; + } +} + +- (void)moveProgressIndicatorForURL:(NSURL *)aURL toIndex:(NSUInteger)anIndex { + FVProgressIndicatorCell *progressIndicator = [_progressIndicators objectForKey:aURL]; + [progressIndicator setTag:anIndex]; + [self setNeedsDisplay:YES]; +} + #pragma mark Quick Look support - (void)handlePreviewerWillClose:(NSNotification *)aNote This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit