ADD: support base function of searching by finder tag name

This commit is contained in:
rich2014 2024-10-18 00:18:35 +08:00
commit 10393c3bfe
3 changed files with 126 additions and 0 deletions

View file

@ -2010,6 +2010,11 @@ end;"/>
<IsPartOfProject Value="True"/>
<UnitName Value="uDarwinFinderModel"/>
</Unit275>
<Unit276>
<Filename Value="platform\unix\darwin\usearchresultutil.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="uSearchResultUtil"/>
</Unit276>
</Units>
</ProjectOptions>
<CompilerOptions>

View file

@ -49,6 +49,8 @@ type
TFinderTagNSColors = Array of NSColor;
TMacOSSearchResultHandler = procedure ( const files: TStringArray ) of object;
{ uDarwinFinderModelUtil }
uDarwinFinderModelUtil = class
@ -72,6 +74,7 @@ type
class procedure setTagNamesOfFile( const url: NSURL; const tagNames: NSArray );
class procedure addTagForFile( const url: NSURL; const tagName: NSString );
class procedure removeTagForFile( const url: NSURL; const tagName: NSString );
class procedure searchFilesForTagName( const tagName: NSString; const handler: TMacOSSearchResultHandler );
public
class property rectFinderTagNSColors: TFinderTagNSColors read _rectFinderTagNSColors;
class property dotFinderTagNSColors: TFinderTagNSColors read _dotFinderTagNSColors;
@ -160,6 +163,44 @@ begin
Result:= _tags.objectForKey( tagName );
end;
{ TMacOSQueryHandler }
type
TMacOSQueryHandler = objcclass( NSObject )
private
_query: NSMetadataQuery;
_handler: TMacOSSearchResultHandler;
procedure initalGatherComplete( sender: id ); message 'initalGatherComplete:';
end;
procedure TMacOSQueryHandler.initalGatherComplete(sender: id);
var
item: NSMetadataItem;
path: NSString;
files: TStringArray;
i: Integer;
count: Integer;
begin
_query.stopQuery;
NSNotificationCenter.defaultCenter.removeObserver_name_object(
self,
NSMetadataQueryDidFinishGatheringNotification,
_query );
files:= nil;
count:= _query.resultCount;
if count > 0 then begin
SetLength( files, count );
for i:=0 to count-1 do begin
item:= NSMetadataItem( _query.results.objectAtIndex(i) );
path:= item.valueForAttribute( NSString(kMDItemPath) );
files[i]:= path.UTF8String;
end;
end;
_handler( files );
end;
{ uDarwinFinderModelUtil }
class function uDarwinFinderModelUtil.getTagNamesOfFile(const url: NSURL
@ -204,6 +245,30 @@ begin
uDarwinFinderModelUtil.setTagNamesOfFile( url, newTagNames );
end;
class procedure uDarwinFinderModelUtil.searchFilesForTagName(
const tagName: NSString; const handler: TMacOSSearchResultHandler);
var
queryHandler: TMacOSQueryHandler;
query: NSMetadataQuery;
predicate: NSPredicate;
format: NSString;
begin
query:= NSMetadataQuery.new;
queryHandler:= TMacOSQueryHandler.new;
queryHandler._query:= query;
queryHandler._handler:= handler;
NSNotificationCenter.defaultCenter.addObserver_selector_name_object(
QueryHandler,
objcselector('initalGatherComplete:'),
NSMetadataQueryDidFinishGatheringNotification,
query );
format:= NSString.stringWithFormat( NSSTR('kMDItemUserTags == ''%@'''), tagName );
predicate:= NSPredicate.predicateWithFormat( format );
query.setPredicate( predicate );
query.startQuery;
end;
class function uDarwinFinderModelUtil.getAllTags: NSDictionary;
var
tagDictionary: NSDictionary;

View file

@ -0,0 +1,56 @@
unit uSearchResultUtil;
{$mode ObjFPC}{$H+}
interface
uses
SysUtils, Classes,
uSearchResultFileSource, uFile, uFileSystemFileSource, uFileSource,
fMain, uFileViewNotebook;
type
TSearchResultUtil = class
class procedure addResultPage( const files: TStringArray);
end;
implementation
class procedure TSearchResultUtil.addResultPage( const files: TStringArray);
var
i: integer;
count: Integer;
sFileName: string;
SearchResultFS: ISearchResultFileSource;
FileList: TFileTree;
aFile: TFile;
Notebook: TFileViewNotebook;
NewPage: TFileViewPage;
begin
if files = nil then
Exit;
count:= Length(files);
FileList := TFileTree.Create;
for i:=0 to count-1 do begin
sFileName := files[i];
aFile := TFileSystemFileSource.CreateFileFromFile(sFileName);
FileList.AddSubNode(aFile);
end;
// Add new tab for search results.
Notebook := frmMain.ActiveNotebook;
NewPage := Notebook.NewPage(Notebook.ActiveView);
// Create search result file source.
// Currently only searching FileSystem is supported.
SearchResultFS := TSearchResultFileSource.Create;
SearchResultFS.AddList(FileList, Notebook.ActiveView.FileSource);
NewPage.FileView.AddFileSource(SearchResultFS, SearchResultFS.GetRootDir);
NewPage.FileView.FlatView := True;
NewPage.MakeActive;
end;
end.