ソースを参照

Implement macOS move file to trash

Ruben 5 ヶ月 前
コミット
b2af213fa0

+ 7 - 2
src/PicView.Avalonia.MacOS/App.axaml.cs

@@ -220,9 +220,14 @@ public class App : Application, IPlatformSpecificService, IPlatformWindowService
         FileAssociationManager.Initialize(iIFileAssociationService);
     }
 
-    public Task<bool> DeleteFile(string path, bool recycle)
+    public async Task<bool> DeleteFile(string path, bool recycle)
     {
-         throw new NotImplementedException(); 
+        if (recycle)
+        {
+            return await Task.Run(() => OsxFileHelper.MoveFileToRecycleBinAsync(path));
+        }
+        await Task.Run(() => File.Delete(path));
+        return !File.Exists(path); 
     }
     
     #endregion

+ 13 - 0
src/PicView.Core.MacOS/FileFunctions/OsxFileHelper.cs

@@ -0,0 +1,13 @@
+using PicView.Core.MacOS.AppleScripts;
+
+namespace PicView.Core.MacOS.FileFunctions;
+
+public static class OsxFileHelper
+{
+    public static async Task<bool> MoveFileToRecycleBinAsync(string filePath)
+    {
+        var appleScript = "tell application \"Finder\" to delete POSIX file \"" + filePath + "\"";
+    
+        return await AppleScriptManager.ExecuteAppleScriptAsync(appleScript);
+    }
+}