feat: load LUT libraries and selections in background with cancellation and visible-only thumbnails
这个提交包含在:
1 父节点
57830ca744
当前提交
87baa785fd
9 个文件已更改
+710
-100
没有与搜索条件相匹配的文件
+77
-39
@@ -121,7 +121,12 @@ static NSString *const DYBrowserFolderPreference=@"com.dayu.color.lookLUTPreview
|
||||
NSCache *_thumbnailCache;
|
||||
dispatch_queue_t _previewQueue;
|
||||
DYBrowserWork *_work;
|
||||
BOOL _closed,_sizing,_visible,_remoteMode,_importing;
|
||||
BOOL _closed,_sizing,_visible,_remoteMode,_importing,_layingOut;
|
||||
NSProgress *_libraryTask,*_selectionTask;
|
||||
NSTimer *_progressTimer;
|
||||
NSUInteger _libraryGeneration,_selectionGeneration;
|
||||
NSInteger _columns;
|
||||
CGFloat _tileWidth,_tileHeight;
|
||||
}
|
||||
@end
|
||||
|
||||
@@ -168,6 +173,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
}
|
||||
- (void)dealloc{
|
||||
[NSObject cancelPreviousPerformRequestsWithTarget:self];[[NSNotificationCenter defaultCenter]removeObserver:self];
|
||||
[_libraryTask cancel];[_libraryTask release];[_selectionTask cancel];[_selectionTask release];[_progressTimer invalidate];[_progressTimer release];
|
||||
_work.cancelled=YES;_work.owner=nil;[_work release];self.window.delegate=nil;
|
||||
_popover.delegate=nil;[_popover close];[_popover release];[_root removeFromSuperview];
|
||||
[_library release];[_preview release];[_selectedPath release];[_folder release];[_palette release];[_selection release];[_closeAction release];
|
||||
@@ -214,7 +220,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
// The API owner's completion may release this controller synchronously.
|
||||
[self retain];
|
||||
[NSObject cancelPreviousPerformRequestsWithTarget:self];
|
||||
_closed=YES;_visible=NO;_work.cancelled=YES;_work.owner=nil;
|
||||
_closed=YES;_visible=NO;_work.cancelled=YES;_work.owner=nil;[_libraryTask cancel];[_selectionTask cancel];[_progressTimer invalidate];[_progressTimer release];_progressTimer=nil;
|
||||
_popover.delegate=nil;if(_popover.shown)[_popover close];
|
||||
[_root removeFromSuperview];[_queuedPaths removeAllObjects];[_thumbnailCache removeAllObjects];
|
||||
[_selection release];_selection=nil;
|
||||
@@ -223,6 +229,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
[self release];
|
||||
}
|
||||
- (void)layoutBrowser{
|
||||
if(_layingOut)return;_layingOut=YES;
|
||||
CGFloat w=_root.bounds.size.width,h=_root.bounds.size.height,left=162;
|
||||
_titleLabel.frame=NSMakeRect(12,15,145,20);_countLabel.frame=NSMakeRect(12,39,145,18);
|
||||
_search.frame=NSMakeRect(left+14,14,MAX(125,w-left-351),24);
|
||||
@@ -237,12 +244,30 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
_folderDocument.frame=NSMakeRect(0,0,fw,MAX(fy,_folderScroll.contentSize.height));
|
||||
CGFloat available=MAX(80,_gridScroll.contentSize.width),tileWidth=_sizeSlider.doubleValue,gap=10;
|
||||
NSInteger columns=MAX(1,(NSInteger)floor((available+gap)/(tileWidth+gap)));tileWidth=MIN(tileWidth,(available-gap*(columns-1)-14)/columns);tileWidth=MAX(70,tileWidth);
|
||||
CGFloat tileHeight=ceil(tileWidth*9/16)+24,x=7,y=8;NSInteger index=0;
|
||||
for(NSDictionary *entry in _visibleEntries){DYBrowserTile *tile=_tiles[entry[@"path"]];NSInteger col=index%columns,row=index/columns;tile.frame=NSMakeRect(x+col*(tileWidth+gap),y+row*(tileHeight+gap),tileWidth,tileHeight);index++;}
|
||||
NSInteger rows=(_visibleEntries.count+columns-1)/columns;CGFloat contentHeight=MAX(_gridScroll.contentSize.height,rows*(tileHeight+gap)+12);
|
||||
_columns=columns;_tileWidth=tileWidth;_tileHeight=ceil(tileWidth*9/16)+24;
|
||||
NSInteger rows=(_visibleEntries.count+columns-1)/columns;CGFloat contentHeight=MAX(_gridScroll.contentSize.height,rows*(_tileHeight+gap)+12);
|
||||
_gridDocument.frame=NSMakeRect(0,0,available,contentHeight);_emptyLabel.frame=NSMakeRect(20,MAX(36,_gridScroll.contentSize.height/2-45),MAX(50,available-40),90);
|
||||
_layingOut=NO;[self updateVisibleTiles];
|
||||
}
|
||||
- (void)resetThumbnailWork {
|
||||
_work.cancelled=YES;_work.owner=nil;[_work release];_work=[DYBrowserWork new];_work.owner=self;[_queuedPaths removeAllObjects];
|
||||
}
|
||||
- (void)updateVisibleTiles {
|
||||
if(_closed||_layingOut||_columns<1)return;
|
||||
NSRect visible=NSInsetRect(_gridScroll.contentView.bounds,-30,-180);CGFloat step=_tileHeight+10;
|
||||
NSUInteger first=(NSUInteger)MAX(0,floor((NSMinY(visible)-8)/step))*_columns;
|
||||
NSUInteger end=MIN(_visibleEntries.count,(NSUInteger)MAX(0,ceil((NSMaxY(visible)-8)/step)+1)*_columns);
|
||||
NSMutableSet *wanted=[NSMutableSet set];BOOL changed=NO;
|
||||
for(NSUInteger i=first;i<end;i++){
|
||||
NSDictionary *entry=_visibleEntries[i];NSString *path=entry[@"path"];[wanted addObject:path];DYBrowserTile *tile=_tiles[path];
|
||||
if(!tile){tile=[[[DYBrowserTile alloc]initWithFrame:NSZeroRect]autorelease];tile.identifier=path;tile.displayName=entry[@"name"];tile.toolTip=entry[@"relativePath"]?:path;tile.palette=_palette;tile.chosen=[path isEqualToString:_selectedPath];tile.target=self;tile.action=@selector(thumbnailClicked:);tile.enabled=YES;[_gridDocument addSubview:tile];_tiles[path]=tile;changed=YES;}
|
||||
tile.frame=NSMakeRect(7+(i%_columns)*(_tileWidth+10),8+(i/_columns)*step,_tileWidth,_tileHeight);
|
||||
}
|
||||
for(NSString *path in _tiles.allKeys)if(![wanted containsObject:path]){[_tiles[path]removeFromSuperview];[_tiles removeObjectForKey:path];changed=YES;}
|
||||
if(changed||!_work)[self resetThumbnailWork];
|
||||
if(!_sizing)[self generateVisibleThumbnails];
|
||||
}
|
||||
|
||||
- (void)updatePalette:(NSDictionary *)palette{
|
||||
NSMutableDictionary *browserPalette=[NSMutableDictionary dictionaryWithDictionary:palette?:@{}];
|
||||
browserPalette[@"background"]=[NSColor colorWithCalibratedRed:.035 green:.08 blue:.12 alpha:1];
|
||||
@@ -275,12 +300,26 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
[_folderDocument addSubview:button];[_folderButtons addObject:button];
|
||||
}
|
||||
}
|
||||
- (void)pollLibraryProgress:(NSTimer *)timer {
|
||||
if(_libraryTask)[self setStatus:_libraryTask.localizedDescription];
|
||||
_statusLabel.toolTip=_libraryTask.localizedAdditionalDescription?:_statusLabel.stringValue;
|
||||
}
|
||||
- (void)beginLibraryTask:(NSProgress *)task {
|
||||
[_libraryTask release];_libraryTask=[task retain];_importFiles.enabled=NO;_importFolder.enabled=NO;_refresh.title=@"取消";
|
||||
[_progressTimer invalidate];[_progressTimer release];_progressTimer=[[NSTimer timerWithTimeInterval:.1 target:self selector:@selector(pollLibraryProgress:) userInfo:nil repeats:YES]retain];
|
||||
[[NSRunLoop mainRunLoop]addTimer:_progressTimer forMode:NSRunLoopCommonModes];[self pollLibraryProgress:nil];
|
||||
}
|
||||
- (void)endLibraryTask {
|
||||
[_libraryTask release];_libraryTask=nil;[_progressTimer invalidate];[_progressTimer release];_progressTimer=nil;
|
||||
_importFiles.enabled=YES;_importFolder.enabled=YES;_refresh.title=@"刷新";
|
||||
}
|
||||
- (void)reloadLibrary:(id)sender{
|
||||
[_thumbnailCache removeAllObjects];
|
||||
NSError *error=nil;
|
||||
if(![_library reload:&error]&&error)[self setStatus:error.localizedDescription];
|
||||
else [self setStatus:@"单击缩略图应用 LUT"];
|
||||
[self rebuildFolders];[self rebuildGrid];
|
||||
if(_libraryTask){[_libraryTask cancel];[self setStatus:@"正在取消…"];return;}
|
||||
[_thumbnailCache removeAllObjects];NSUInteger generation=++_libraryGeneration;
|
||||
NSProgress *task=[_library reloadAsync:^(NSError *error){
|
||||
if(_closed||generation!=_libraryGeneration)return;[self endLibraryTask];
|
||||
[self rebuildFolders];[self rebuildGrid];[self setStatus:error?error.localizedDescription:@"单击缩略图应用 LUT"];
|
||||
}];[self beginLibraryTask:task];
|
||||
}
|
||||
- (void)folderClicked:(NSButton *)sender{
|
||||
[_folder release];_folder=[sender.identifier copy];
|
||||
@@ -299,20 +338,19 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
- (void)refreshThumbnailSize{
|
||||
if(_closed)return;_sizing=NO;
|
||||
_work.cancelled=YES;_work.owner=nil;[_work release];_work=[DYBrowserWork new];_work.owner=self;[_queuedPaths removeAllObjects];
|
||||
for(DYBrowserTile *tile in _tiles.allValues){tile.thumbnail=nil;tile.failure=nil;tile.enabled=NO;[tile setNeedsDisplay:YES];}
|
||||
for(DYBrowserTile *tile in _tiles.allValues){tile.thumbnail=nil;tile.failure=nil;tile.enabled=YES;[tile setNeedsDisplay:YES];}
|
||||
[self generateVisibleThumbnails];
|
||||
}
|
||||
- (void)gridScrolled:(NSNotification *)notification{if(!_sizing)[self generateVisibleThumbnails];}
|
||||
- (void)gridScrolled:(NSNotification *)notification{[self updateVisibleTiles];}
|
||||
- (void)generateVisibleThumbnails{
|
||||
if(_closed||!_work)return;
|
||||
NSRect visible=NSInsetRect(_gridScroll.contentView.bounds,-30,-180);
|
||||
NSUInteger width=MAX((NSUInteger)64,(NSUInteger)llround(_sizeSlider.doubleValue)),height=MAX((NSUInteger)40,(NSUInteger)llround(width*9./16));
|
||||
for(NSDictionary *entry in _visibleEntries){
|
||||
NSString *path=entry[@"path"];DYBrowserTile *tile=_tiles[path];
|
||||
for(NSString *path in _tiles.allKeys){
|
||||
DYBrowserTile *tile=_tiles[path];
|
||||
if(!NSIntersectsRect(tile.frame,visible)){if(tile.thumbnail){tile.thumbnail=nil;tile.enabled=NO;}continue;}
|
||||
if(tile.thumbnail||tile.failure.length)continue;
|
||||
NSDictionary *attributes=[[NSFileManager defaultManager]attributesOfItemAtPath:path error:nil];
|
||||
NSString *cacheKey=[NSString stringWithFormat:@"%lu|%@|%.9f|%@|%@",(unsigned long)width,path,[attributes[NSFileModificationDate]timeIntervalSinceReferenceDate],attributes[NSFileSize],attributes[NSFileSystemFileNumber]];
|
||||
NSString *cacheKey=[NSString stringWithFormat:@"%lu|%@",(unsigned long)width,path];
|
||||
NSDictionary *cached=[_thumbnailCache objectForKey:cacheKey];
|
||||
if(cached){tile.thumbnail=cached[@"image"];tile.failure=cached[@"error"];tile.enabled=tile.thumbnail!=nil;if(tile.failure.length)tile.toolTip=tile.failure;[tile setNeedsDisplay:YES];continue;}
|
||||
if([_queuedPaths containsObject:cacheKey])continue;[_queuedPaths addObject:cacheKey];
|
||||
@@ -332,8 +370,7 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
}
|
||||
}
|
||||
- (void)rebuildGrid{
|
||||
_work.cancelled=YES;_work.owner=nil;[_work release];_work=[DYBrowserWork new];_work.owner=self;
|
||||
[_queuedPaths removeAllObjects];
|
||||
[self resetThumbnailWork];
|
||||
for(NSView *tile in _tiles.allValues)[tile removeFromSuperview];[_tiles removeAllObjects];
|
||||
NSString *query=[_search.stringValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];NSMutableArray *entries=[NSMutableArray array];
|
||||
for(NSDictionary *entry in _library.entries){
|
||||
@@ -342,41 +379,42 @@ static NSButton *BrowserButton(NSString *title,id target,SEL action){NSButton *b
|
||||
if(folderMatch&&(!query.length||[entry[@"name"]localizedCaseInsensitiveContainsString:query]||[entry[@"relativePath"]localizedCaseInsensitiveContainsString:query]))[entries addObject:entry];
|
||||
}
|
||||
[_visibleEntries release];_visibleEntries=[entries copy];_countLabel.stringValue=[NSString stringWithFormat:@"%lu 个 LUT",(unsigned long)entries.count];_emptyLabel.hidden=entries.count>0;
|
||||
for(NSDictionary *entry in entries){
|
||||
NSString *path=entry[@"path"];DYBrowserTile *tile=[[[DYBrowserTile alloc]initWithFrame:NSZeroRect]autorelease];tile.identifier=path;tile.displayName=entry[@"name"];tile.toolTip=entry[@"relativePath"]?:path;tile.palette=_palette;tile.chosen=[path isEqualToString:_selectedPath];tile.target=self;tile.action=@selector(thumbnailClicked:);tile.enabled=NO;[_gridDocument addSubview:tile];_tiles[path]=tile;
|
||||
}
|
||||
[self layoutBrowser];[_gridScroll.contentView scrollToPoint:NSZeroPoint];[_gridScroll reflectScrolledClipView:_gridScroll.contentView];
|
||||
[_gridScroll.contentView scrollToPoint:NSZeroPoint];[self layoutBrowser];[_gridScroll reflectScrolledClipView:_gridScroll.contentView];
|
||||
NSAccessibilityPostNotification(_gridDocument,NSAccessibilityLayoutChangedNotification);
|
||||
}
|
||||
- (void)thumbnailClicked:(DYBrowserTile *)sender{
|
||||
if(!sender.enabled||!sender.thumbnail||sender.failure.length)return;
|
||||
std::vector<float> values;NSUInteger dimension=0;NSError *error=nil;
|
||||
if(!DYLUTReadCube(sender.identifier,values,dimension,&error)){
|
||||
sender.failure=error.localizedDescription?:@"LUT 文件已失效,请刷新浏览库";sender.enabled=NO;[sender setNeedsDisplay:YES];[self setStatus:sender.failure];return;
|
||||
}
|
||||
[_selectedPath release];_selectedPath=[sender.identifier copy];
|
||||
for(DYBrowserTile *tile in _tiles.allValues){tile.chosen=[tile.identifier isEqualToString:_selectedPath];[tile setNeedsDisplay:YES];}
|
||||
[self setStatus:[NSString stringWithFormat:@"已应用 %@",sender.displayName]];
|
||||
if(_selection)_selection(_selectedPath);
|
||||
if(sender.doubleClicked)[self close];
|
||||
if(!sender.enabled||sender.failure.length)return;
|
||||
[_selectionTask cancel];[_selectionTask release];NSUInteger generation=++_selectionGeneration;
|
||||
NSString *path=sender.identifier,*name=sender.displayName;BOOL closeAfter=sender.doubleClicked;[self setStatus:[@"正在载入 " stringByAppendingString:name]];
|
||||
_selectionTask=[DYLUTPrepareAsync(path,^(NSError *error){
|
||||
if(_closed||generation!=_selectionGeneration)return;[_selectionTask release];_selectionTask=nil;
|
||||
if(error){[self setStatus:error.localizedDescription];return;}
|
||||
[_selectedPath release];_selectedPath=[path copy];
|
||||
for(DYBrowserTile *tile in _tiles.allValues){tile.chosen=[tile.identifier isEqualToString:path];[tile setNeedsDisplay:YES];}
|
||||
if(_selection)_selection(path);[self setStatus:[@"已选择 " stringByAppendingString:name]];if(closeAfter)[self close];
|
||||
})retain];
|
||||
}
|
||||
- (void)clearSelection:(id)sender{
|
||||
++_selectionGeneration;[_selectionTask cancel];[_selectionTask release];_selectionTask=nil;
|
||||
[_selectedPath release];_selectedPath=nil;
|
||||
for(DYBrowserTile *tile in _tiles.allValues){tile.chosen=NO;[tile setNeedsDisplay:YES];}
|
||||
if(_selection)_selection(nil);[self setStatus:@"已移除 LUT"];
|
||||
}
|
||||
- (void)done:(id)sender{[self close];}
|
||||
- (void)showImportError:(NSError *)error{
|
||||
if(!error||_closed)return;NSAlert *alert=[[[NSAlert alloc]init]autorelease];alert.messageText=@"LUT 导入未完成";alert.informativeText=error.localizedDescription?:@"请选择有效的 .cube LUT 文件。";[alert addButtonWithTitle:@"好"];
|
||||
_importing=YES;[alert runModal];_importing=NO;
|
||||
- (void)showImportError:(NSError *)error{if(error&&!_closed)[self setStatus:error.localizedDescription];}
|
||||
- (void)importSelectedURLs:(NSArray<NSURL *> *)urls {
|
||||
if(_closed||_libraryTask)return;NSUInteger generation=++_libraryGeneration;
|
||||
NSProgress *task=[_library importURLsAsync:urls completion:^(NSArray *imported,NSError *error){
|
||||
if(_closed||generation!=_libraryGeneration)return;[self endLibraryTask];
|
||||
if(!imported){[self showImportError:error];return;}
|
||||
[_thumbnailCache removeAllObjects];[self rebuildFolders];[self rebuildGrid];[self setStatus:[NSString stringWithFormat:@"已导入 %lu 个 LUT",(unsigned long)imported.count]];
|
||||
}];[self beginLibraryTask:task];
|
||||
}
|
||||
- (void)importPanelForFolders:(BOOL)folders{
|
||||
if(_libraryTask||_importing||_closed)return;
|
||||
NSOpenPanel *panel=[NSOpenPanel openPanel];panel.canChooseFiles=!folders;panel.canChooseDirectories=folders;panel.allowsMultipleSelection=YES;panel.title=folders?@"导入 LUT 文件夹":@"导入 LUT 文件";panel.message=folders?@"所选文件夹中的 .cube 文件会按目录复制到大鱼调色 LUT 库。":@"选择要复制到大鱼调色 LUT 库的 .cube 文件。";
|
||||
if(!folders)panel.allowedFileTypes=@[@"cube"];
|
||||
_importing=YES;NSModalResponse response=[panel runModal];_importing=NO;
|
||||
if(response!=NSModalResponseOK||_closed)return;NSError *error=nil;NSArray *imported=[_library importURLs:panel.URLs error:&error];
|
||||
if(!imported){[self showImportError:error];return;}
|
||||
[self rebuildFolders];[self rebuildGrid];[self setStatus:[NSString stringWithFormat:@"已导入 %lu 个 LUT",(unsigned long)imported.count]];
|
||||
_importing=YES;[panel beginWithCompletionHandler:^(NSModalResponse response){_importing=NO;if(response==NSModalResponseOK&&!_closed)[self importSelectedURLs:panel.URLs];}];
|
||||
}
|
||||
- (void)importFiles:(id)sender{[self importPanelForFolders:NO];}
|
||||
- (void)importFolder:(id)sender{[self importPanelForFolders:YES];}
|
||||
|
||||
在新工单中引用
屏蔽一个用户