UPD: load app bundle icon by Cocoa Api on macOS

This commit is contained in:
rich2014 2025-06-18 22:47:09 +08:00
commit 4e993732ef
3 changed files with 10 additions and 42 deletions

View file

@ -949,10 +949,17 @@ begin
Result:= (pFinderInfo^.finderFlags and kHasCustomIcon) <> 0;
end;
function isExtUniqueIcon( const path: String ): Boolean;
begin
Result:= False;
if path.EndsWith('.app') or path.EndsWith('.fcpbundle') or path.EndsWith('.fcpxmld') then
Result:= True;
end;
function getMacOSFileUniqueIcon( const path: String ): NSImage;
begin
Result:= nil;
if hasUniqueIcon(path) then
if hasUniqueIcon(path) or isExtUniqueIcon(path) then
Result:= NSWorkspace.sharedWorkspace.iconForFile( StringToNSString(path) );
end;

View file

@ -525,7 +525,7 @@ begin
mi:= TMenuItem.Create( miOpenWith );
mi.Caption:= NSFileManager.defaultManager.displayNameAtPath(appUrl.path).UTF8String;
mi.Hint := appUrl.path.UTF8String;
ImageIndex:= PixMapManager.GetApplicationBundleIcon(appUrl.path.UTF8String, -1);
ImageIndex:= PixMapManager.CheckAddFileUniqueIcon(appUrl.path.UTF8String);
if ImageIndex >= 0 then begin
bmpTemp:= PixMapManager.GetBitmap(ImageIndex);
if Assigned(bmpTemp) then begin

View file

@ -248,7 +248,6 @@ type
function GetSystemFolderIcon: PtrInt;
function GetMimeIcon(AFileExt: String; AIconSize: Integer): PtrInt;
function LoadImageFileBitmap( const filename:String; const size:Integer ): TBitmap;
function CheckAddFileUniqueIcon(AFullPath: String; AIconSize : Integer = 0): PtrInt;
{$ENDIF}
function GetBuiltInDriveIcon(Drive : PDrive; IconSize : Integer; clBackColor : TColor) : Graphics.TBitmap;
@ -342,7 +341,7 @@ type
}
function GetIconOverlayByFile(AFile: TFile; DirectAccess: Boolean): PtrInt;
{$ELSEIF DEFINED(DARWIN)}
function GetApplicationBundleIcon(sFileName: String; iDefaultIcon: PtrInt): PtrInt;
function CheckAddFileUniqueIcon(AFullPath: String; AIconSize : Integer = 0): PtrInt;
{$ENDIF}
function GetIconByName(const AIconName: String): PtrInt;
function GetThemeIcon(const AIconName: String; AIconSize: Integer) : Graphics.TBitmap;
@ -1101,21 +1100,6 @@ end;
{$ELSEIF DEFINED(DARWIN)}
function getAppIconFilename( appName: String ) : String;
var
appBundle : NSBundle;
infoDict : NSDictionary;
iconTag : NSString;
begin
Result := '';
appBundle := NSBundle.bundleWithPath( StringToNSString(appName) );
if appBundle=nil then exit;
infoDict := appBundle.infoDictionary;
if infoDict=nil then exit;
iconTag := NSString( infoDict.valueForKey( StringToNSString('CFBundleIconFile')) );
Result := NSStringToString( appBundle.pathForImageResource( iconTag ) );
end;
function getBestNSImageWithSize( const srcImage:NSImage; const size:Integer ): NSImage;
var
bestRect: NSRect;
@ -1190,18 +1174,6 @@ begin
end;
end;
function TPixMapManager.GetApplicationBundleIcon(sFileName: String;
iDefaultIcon: PtrInt): PtrInt;
var
I: PtrInt;
sIconName: String;
begin
Result:= iDefaultIcon;
sIconName:= getAppIconFilename(sFileName);
I:= GetIconByName(sIconName);
if I >= 0 then Result:= I;
end;
function TPixMapManager.CheckAddFileUniqueIcon(AFullPath: String;
AIconSize: Integer): PtrInt;
var
@ -2276,17 +2248,6 @@ begin
else Exit(FiDirIconID);
end
else
{$ELSEIF DEFINED(DARWIN)}
if (IconsMode = sim_all_and_exe) and
(DirectAccess and (ExtractFileExt(FullPath) = '.app')) then
begin
if LoadIcon then
Result := GetApplicationBundleIcon(FullPath, FiDirIconID)
else
Result := -1;
Exit;
end
else
{$ENDIF}
begin
Exit(FiDirIconID);