Selaa lähdekoodia

[Avalonia] Refactoring, cut files, use LibraryImports

Ruben 1 vuosi sitten
vanhempi
sitoutus
a57b7db3ae

+ 8 - 1
src/PicView.Avalonia.MacOS/App.axaml.cs

@@ -277,9 +277,16 @@ public void ShowAboutWindow()
         // TODO: Implement SetAsLockScreen
     }
     
-    public void CopyFile(string path)
+    public bool CopyFile(string path)
     {
         // TODO: Implement CopyFile
+        return false;
+    }
+    
+    public bool CutFile(string path)
+    {
+        // TODO: Implement CutFile
+        return false;
     }
     
     public Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory)

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

@@ -312,9 +312,14 @@ public class App : Application, IPlatformSpecificService
         LockscreenHelper.SetLockScreenImage(path);
     }
 
-    public void CopyFile(string path)
+    public bool CopyFile(string path)
     {
-        ClipboardHelper.CopyFileToClipboard(path);
+        return ClipboardHelper.CopyFileToClipboard(false, path);
+    }
+    
+    public bool CutFile(string path)
+    {
+        return ClipboardHelper.CopyFileToClipboard(true, path);
     }
     
     public async Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory)

+ 11 - 4
src/PicView.Avalonia/Clipboard/ClipboardHelper.cs

@@ -56,8 +56,11 @@ public static class ClipboardHelper
 
     public static async Task CopyFileToClipboard(string? file, MainViewModel vm)
     {
-        await Task.Run(() => vm.PlatformService.CopyFile(file));
-        await CopyAnimation();
+        var success = await Task.Run(() => vm.PlatformService.CopyFile(file));
+        if (success)
+        {
+            await CopyAnimation();
+        }
     }
 
     public static async Task CopyImageToClipboard()
@@ -109,9 +112,13 @@ public static class ClipboardHelper
         await CopyAnimation();
     }   
 
-    public static async Task CutFile(string path)
+    public static async Task CutFile(string path, MainViewModel vm)
     {
-        // TODO: Implement CutFile
+        var success = await Task.Run(() => vm.PlatformService.CutFile(path));
+        if (success)
+        {
+            await CopyAnimation();
+        }
     }
     
     public static async Task Paste(MainViewModel vm)

+ 3 - 1
src/PicView.Avalonia/Interfaces/IPlatformSpecificService.cs

@@ -34,7 +34,9 @@ public interface IPlatformSpecificService
     
     void SetAsLockScreen(string path);
     
-    void CopyFile(string path);
+    bool CopyFile(string path);
+    
+    bool CutFile(string path);
     
     Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory);
 }

+ 1 - 1
src/PicView.Avalonia/UI/FunctionsHelper.cs

@@ -689,7 +689,7 @@ public static class FunctionsHelper
         {
             return;
         }
-        await ClipboardHelper.CutFile(Vm.FileInfo.FullName);
+        await ClipboardHelper.CutFile(Vm.FileInfo.FullName, Vm);
     }
 
     public static async Task Paste()

+ 1 - 1
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -1469,7 +1469,7 @@ public class MainViewModel : ViewModelBase
         {
             return;
         }
-        await ClipboardHelper.CutFile(path);
+        await ClipboardHelper.CutFile(path, this);
     }
     
     private async Task DeleteFileTask(string path)

+ 32 - 20
src/PicView.Windows/FileHandling/ClipboardHelper.cs

@@ -2,42 +2,43 @@
 
 namespace PicView.Windows.FileHandling;
 
-public static class ClipboardHelper
+public static partial class ClipboardHelper
 {
-    [DllImport("user32.dll", SetLastError = true)]
+    [LibraryImport("user32.dll", SetLastError = true)]
     [return: MarshalAs(UnmanagedType.Bool)]
-    public static extern bool OpenClipboard(IntPtr hWndNewOwner);
+    public static partial bool OpenClipboard(IntPtr hWndNewOwner);
 
-    [DllImport("user32.dll", SetLastError = true)]
+    [LibraryImport("user32.dll", SetLastError = true)]
     [return: MarshalAs(UnmanagedType.Bool)]
-    public static extern bool CloseClipboard();
+    public static partial bool CloseClipboard();
 
-    [DllImport("user32.dll", SetLastError = true)]
+    [LibraryImport("user32.dll", SetLastError = true)]
     [return: MarshalAs(UnmanagedType.Bool)]
-    public static extern bool EmptyClipboard();
+    public static partial bool EmptyClipboard();
 
-    [DllImport("user32.dll", SetLastError = true)]
-    public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
+    [LibraryImport("user32.dll", SetLastError = true)]
+    public static partial IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
 
-    [DllImport("kernel32.dll", SetLastError = true)]
-    public static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
+    [LibraryImport("kernel32.dll", SetLastError = true)]
+    public static partial IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
 
-    [DllImport("kernel32.dll", SetLastError = true)]
-    public static extern IntPtr GlobalLock(IntPtr hMem);
+    [LibraryImport("kernel32.dll", SetLastError = true)]
+    public static partial IntPtr GlobalLock(IntPtr hMem);
 
-    [DllImport("kernel32.dll", SetLastError = true)]
+    [LibraryImport("kernel32.dll", SetLastError = true)]
     [return: MarshalAs(UnmanagedType.Bool)]
-    public static extern bool GlobalUnlock(IntPtr hMem);
+    public static partial bool GlobalUnlock(IntPtr hMem);
 
-    [DllImport("kernel32.dll", SetLastError = true)]
-    public static extern IntPtr GlobalFree(IntPtr hMem);
+    [LibraryImport("kernel32.dll", SetLastError = true)]
+    public static partial IntPtr GlobalFree(IntPtr hMem);
 
     public const uint CF_HDROP = 15;
     public const uint CF_PREFERREDDROPEFFECT = 0x000D; // CFSTR_PREFERREDDROPEFFECT
     public const uint GMEM_MOVEABLE = 0x0002;
     public const uint DROPEFFECT_MOVE = 2;
+    public const uint DROPEFFECT_COPY = 1;
 
-    public static bool CopyFileToClipboard(string filePath)
+    public static bool CopyFileToClipboard(bool cut, string filePath)
     {
         if (!OpenClipboard(IntPtr.Zero))
         {
@@ -89,14 +90,23 @@ public static class ClipboardHelper
                 return false;
             }
 
-            // Set the preferred drop effect to move
+            
             var dropEffect = Marshal.AllocHGlobal(sizeof(uint));
-            Marshal.WriteInt32(dropEffect, (int)DROPEFFECT_MOVE);
+            if (cut)
+            {
+                // Setting the drop effect to move doesn't work.
+                Marshal.WriteInt32(dropEffect, (int)DROPEFFECT_MOVE);
+            }
+            else
+            {
+                Marshal.WriteInt32(dropEffect, (int)DROPEFFECT_COPY);
+            }
             if (SetClipboardData(CF_PREFERREDDROPEFFECT, dropEffect) == IntPtr.Zero)
             {
                 Marshal.FreeHGlobal(dropEffect);
                 return false;
             }
+
         }
         finally
         {
@@ -107,6 +117,8 @@ public static class ClipboardHelper
 
     }
 
+
+
     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
     private struct Dropfiles
     {