ADD: support NSImage cache in TDarwinImageCacheManager on macOS

This commit is contained in:
rich2014 2026-01-28 12:01:29 +08:00
commit 2596a2582d
5 changed files with 67 additions and 11 deletions

View file

@ -146,7 +146,7 @@ var
iconPath: String;
begin
iconPath:= mbExpandFileName( ICON_PATH );
Result:= darwinImageCacheForPath.copyImageForFileContent( iconPath, iconSize, True );
Result:= darwinImageCacheForPath.copyBitmapForFileContent( iconPath, iconSize, True );
end;
{$ELSE}
begin

View file

@ -97,7 +97,7 @@ begin
{$IFDEF DARWIN}
if path = PathDelim then
Result:= darwinImageCacheForExt.copyIconForFileExt( FCurrentAddress, iconSize );
Result:= darwinImageCacheForExt.copyBitmapForFileExt( FCurrentAddress, iconSize );
{$ENDIF}
end;

View file

@ -152,7 +152,7 @@ function TSmartFolderSearchResultFileSource.GetCustomIcon(
const path: String;
const iconSize: Integer ): TBitmap;
begin
Result:= darwinImageCacheForExt.copyIconForFileExt( 'savedSearch', iconSize );
Result:= darwinImageCacheForExt.copyBitmapForFileExt( 'savedSearch', iconSize );
end;
{ TDarwinSearchResultHandler }

View file

@ -53,18 +53,23 @@ type
procedure onThemeChanged;
function copyIconForFileExt(
function copyBitmapForFileExt(
const path: String;
const size: Integer ): TBitmap;
function copyImageForFileContent(
function copyBitmapForFileContent(
const path: String;
const size: Integer;
const autoDark: Boolean = False ): TBitmap;
function copyImageForNSImage(
function copyBitmapForNSImage(
const key: String;
const image: NSImage ): TBitmap;
function getNSImageForFileContent(
const path: String;
const size: Integer;
const autoDark: Boolean = False ): NSImage;
end;
var
@ -73,6 +78,31 @@ var
implementation
type
{ TNSImageCacheItem }
TNSImageCacheItem = class
private
_image: NSImage;
public
constructor Create( const image: NSImage );
destructor Destroy; override;
end;
{ TNSImageCacheItem }
constructor TNSImageCacheItem.Create( const image: NSImage );
begin
_image:= image;
_image.retain;
end;
destructor TNSImageCacheItem.Destroy;
begin
_image.release;
end;
class function TDarwinImageUtil.filt(
const filterName: NSString;
const sourceImage: NSImage ): NSImage;
@ -242,7 +272,7 @@ begin
end;
end;
function TDarwinImageCacheManager.copyIconForFileExt(
function TDarwinImageCacheManager.copyBitmapForFileExt(
const path: String;
const size: Integer ): TBitmap;
var
@ -274,7 +304,7 @@ begin
end;
end;
function TDarwinImageCacheManager.copyImageForFileContent(
function TDarwinImageCacheManager.copyBitmapForFileContent(
const path: String;
const size: Integer;
const autoDark: Boolean = False ): TBitmap;
@ -302,7 +332,7 @@ begin
end;
end;
function TDarwinImageCacheManager.copyImageForNSImage(
function TDarwinImageCacheManager.copyBitmapForNSImage(
const key: String;
const image: NSImage ): TBitmap;
var
@ -327,6 +357,32 @@ begin
end;
end;
function TDarwinImageCacheManager.getNSImageForFileContent(
const path: String;
const size: Integer;
const autoDark: Boolean = False ): NSImage;
var
item: TNSImageCacheItem;
image: NSImage;
begin
Result:= nil;
_lockObject.Acquire;
try
item:= TNSImageCacheItem(_images[path]);
if _images[path] = nil then begin
image:= TDarwinImageUtil.getBestFromFileContentWithSize( path, size, autoDark );
item:= TNSImageCacheItem.Create( image );
_images[path]:= item;
end;
finally
_lockObject.Release;
end;
if Assigned( item ) then
Result:= item._image;
end;
initialization
darwinImageCacheForPath:= TDarwinImageCacheManager.Create;
TCocoaThemeServices.addObserver( darwinImageCacheForPath );

View file

@ -719,12 +719,12 @@ begin
Result:= nil;
if path = GetRootDir(path) then begin
TiCloudDriveFileSource.GetMainIcon( iconPath );
Result:= darwinImageCacheForPath.copyImageForFileContent( iconPath, iconSize );
Result:= darwinImageCacheForPath.copyBitmapForFileContent( iconPath, iconSize );
end else begin
realPath:= self.GetRealPath( path );
image:= getAppIconByPath( realPath );
if image <> nil then
Result:= darwinImageCacheForPath.copyImageForNSImage( realPath, image );
Result:= darwinImageCacheForPath.copyBitmapForNSImage( realPath, image );
end;
end;