Ver Fonte

ADD #2342: support Custom File Icon on macOS

rich2014 há 2 meses atrás
pai
commit
d184c1af5a
1 ficheiros alterados com 42 adições e 1 exclusões
  1. 42 1
      src/platform/upixmapmanager.pas

+ 42 - 1
src/platform/upixmapmanager.pas

@@ -55,7 +55,7 @@ uses
   {$ELSEIF DEFINED(UNIX)}
   , DCFileAttributes
     {$IF DEFINED(DARWIN)}
-    , CocoaAll, MacOSAll, CocoaUtils, uDarwinUtil, uClassesEx
+    , CocoaAll, MacOSAll, CocoaUtils, uDarwinUtil, uMyDarwin, uClassesEx
     {$ELSEIF NOT DEFINED(HAIKU)}
     , Math, Contnrs, uGio, uXdg
       {$IFDEF GTK2_FIX}
@@ -248,6 +248,7 @@ 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;
 
@@ -1201,6 +1202,37 @@ begin
   if I >= 0 then Result:= I;
 end;
 
+function TPixMapManager.CheckAddFileUniqueIcon(AFullPath: String;
+  AIconSize: Integer): PtrInt;
+var
+  fileIndex: PtrInt;
+  image: NSImage;
+  bmpBitmap: Graphics.TBitmap;
+begin
+  Result:= -1;
+  if AIconSize = 0 then AIconSize := gIconsSize;
+
+  FPixmapsLock.Acquire;
+  try
+    fileIndex := FPixmapsFileNames.Find(AFullPath);
+    if fileIndex >= 0 then begin
+      Result:= PtrInt(FPixmapsFileNames.List[fileIndex]^.Data);
+      Exit;
+    end;
+
+    image:= getMacOSFileUniqueIcon(AFullPath);
+    if image = nil then
+      Exit;
+
+    image:= getBestNSImageWithSize(image, AIconSize);
+    bmpBitmap:= NSImageToTBitmap(image);
+    Result := FPixmapList.Add(bmpBitmap);
+    FPixmapsFileNames.Add(AFullPath, Pointer(Result));
+  finally
+    FPixmapsLock.Release;
+  end;
+end;
+
 {$ENDIF} // Unix
 
 function TPixMapManager.CheckAddThemePixmapLocked(AIconName: String; AIconSize: Integer): PtrInt;
@@ -2202,6 +2234,15 @@ begin
       {$ENDIF}
     end;
 
+    {$IF DEFINED(DARWIN)}
+    if DirectAccess and (IconsMode = sim_all_and_exe) then
+    begin
+      Result:= checkAddFileUniqueIcon(FullPath);
+      if Result >= 0 then
+        Exit;
+    end;
+    {$ENDIF}
+
     if IsDirectory or IsLinkToDirectory then
     begin
       {$IF DEFINED(MSWINDOWS)}