瀏覽代碼

Refactor, use `DebugHelper.LogDebug`, misc.

Ruben 5 月之前
父節點
當前提交
de07bb4bc6

+ 5 - 18
src/PicView.Avalonia/Clipboard/ClipboardFileOperations.cs

@@ -1,10 +1,10 @@
 using System.Diagnostics;
-using System.Runtime.InteropServices;
 using Avalonia.Platform.Storage;
 using PicView.Avalonia.Animations;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
+using PicView.Core.DebugTools;
 using PicView.Core.FileHandling;
 using PicView.Core.Localization;
 using PicView.Core.ProcessHandling;
@@ -143,17 +143,14 @@ public static class ClipboardFileOperations
                     break;
                 case IStorageItem singleFile:
                 {
-                    var path = GetLocalPath(singleFile.Path);
-                    await NavigationManager.LoadPicFromStringAsync(path, vm);
+                    await NavigationManager.LoadPicFromStringAsync(singleFile.Path.AbsolutePath, vm);
                     break;
                 }
             }
         }
         catch (Exception ex)
         {
-#if DEBUG
-            Debug.WriteLine($"{nameof(ClipboardFileOperations)} {nameof(PasteFiles)} {ex.StackTrace}");
-#endif
+            DebugHelper.LogDebug(nameof(ClipboardFileOperations), nameof(PasteFiles), ex);
         }
     }
     
@@ -165,22 +162,12 @@ public static class ClipboardFileOperations
         }
 
         // Load the first file
-        var firstFile = storageItems[0];
-        var firstPath = GetLocalPath(firstFile.Path);
-        await NavigationManager.LoadPicFromStringAsync(firstPath, vm);
+        await NavigationManager.LoadPicFromStringAsync(storageItems[0].Path.AbsolutePath, vm);
 
         // Open consecutive files in a new process
         foreach (var file in storageItems.Skip(1))
         {
-            var path = GetLocalPath(file.Path);
-            ProcessHelper.StartNewProcess(path);
+            ProcessHelper.StartNewProcess(file.Path.AbsolutePath);
         }
     }
-    
-    private static string GetLocalPath(Uri uri)
-    {
-        return RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
-            ? uri.AbsolutePath
-            : uri.LocalPath;
-    }
 }

+ 4 - 15
src/PicView.Avalonia/Clipboard/ClipboardImageOperations.cs

@@ -1,4 +1,3 @@
-using System.Diagnostics;
 using System.Runtime.InteropServices;
 using Avalonia.Input;
 using Avalonia.Input.Platform;
@@ -6,6 +5,7 @@ using Avalonia.Media.Imaging;
 using PicView.Avalonia.ImageHandling;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.ViewModels;
+using PicView.Core.DebugTools;
 using PicView.Core.Localization;
 
 namespace PicView.Avalonia.Clipboard;
@@ -80,11 +80,7 @@ public static class ClipboardImageOperations
             }
             catch (Exception ex)
             {
-#if DEBUG
-                Debug.WriteLine(
-                    $"{nameof(ClipboardImageOperations)} {nameof(CopyBase64ToClipboard)} error: {ex.Message}");
-                Debug.WriteLine(ex.StackTrace);
-#endif
+                DebugHelper.LogDebug(nameof(ClipboardImageOperations), nameof(CopyBase64ToClipboard), ex);
                 return false;
             }
         });
@@ -155,10 +151,7 @@ public static class ClipboardImageOperations
         }
         catch (Exception ex)
         {
-#if DEBUG
-            Debug.WriteLine($"{nameof(ClipboardImageOperations)} {nameof(PasteClipboardImage)} error: {ex.Message}");
-            Debug.WriteLine(ex.StackTrace);
-#endif
+            DebugHelper.LogDebug(nameof(ClipboardImageOperations), nameof(PasteClipboardImage), ex);
         }
     }
 
@@ -187,11 +180,7 @@ public static class ClipboardImageOperations
             catch (Exception ex)
             {
                 // Ignore format errors and try next format
-#if DEBUG
-                Debug.WriteLine(
-                    $"{nameof(ClipboardImageOperations)} {nameof(TryGetBitmapFromClipboard)} error: {ex.Message}");
-                Debug.WriteLine(ex.StackTrace);
-#endif
+                DebugHelper.LogDebug(nameof(ClipboardImageOperations), nameof(TryGetBitmapFromClipboard), ex);
             }
         }
 

+ 3 - 5
src/PicView.Avalonia/Clipboard/ClipboardPasteOperations.cs

@@ -1,8 +1,8 @@
-using System.Diagnostics;
-using Avalonia.Input;
+using Avalonia.Input;
 using Avalonia.Threading;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.ViewModels;
+using PicView.Core.DebugTools;
 
 namespace PicView.Avalonia.Clipboard;
 
@@ -43,9 +43,7 @@ public static class ClipboardPasteOperations
         }
         catch (Exception ex)
         {
-#if DEBUG
-            Debug.WriteLine($"Paste operation failed: {ex.Message}");
-#endif
+            DebugHelper.LogDebug(nameof(ClipboardPasteOperations), nameof(Paste), ex);
         }
     }
 }

+ 2 - 4
src/PicView.Avalonia/Clipboard/ClipboardService.cs

@@ -1,8 +1,8 @@
-using System.Diagnostics;
 using Avalonia;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Input.Platform;
 using PicView.Avalonia.Animations;
+using PicView.Core.DebugTools;
 
 namespace PicView.Avalonia.Clipboard;
 
@@ -45,9 +45,7 @@ public static class ClipboardService
         }
         catch (Exception ex)
         {
-#if DEBUG
-            Debug.WriteLine($"{nameof(ClipboardService)} operation failed: {ex.Message}\n{ex.StackTrace}");
-#endif
+            DebugHelper.LogDebug(nameof(ClipboardService), nameof(ExecuteClipboardOperation), ex);
             return false;
         }
     }

+ 5 - 3
src/PicView.Avalonia/CustomControls/PicBox.cs

@@ -144,11 +144,13 @@ public class PicBox : Control, IDisposable
 
     private void UpdateSvgSource()
     {
-        if (Source is string svg)
+        if (Source is not string svg)
         {
-            var svgSource = SvgSource.Load(svg);
-            Source = new SvgImage { Source = svgSource };
+            return;
         }
+
+        var svgSource = SvgSource.Load(svg);
+        Source = new SvgImage { Source = svgSource };
     }
 
     private void UpdateAnimatedSource()

+ 3 - 6
src/PicView.Avalonia/FileSystem/FileManager.cs

@@ -1,10 +1,10 @@
-using System.Diagnostics;
-using Avalonia.Threading;
+using Avalonia.Threading;
 using PicView.Avalonia.ImageHandling;
 using PicView.Avalonia.Interfaces;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.Views.UC.PopUps;
+using PicView.Core.DebugTools;
 using PicView.Core.Localization;
 
 namespace PicView.Avalonia.FileSystem;
@@ -172,10 +172,7 @@ public static class FileManager
     /// </summary>
     private static async Task LogAndShowError(Exception ex, string methodName)
     {
-#if DEBUG
-        Debug.WriteLine($"{nameof(FileManager)}.{methodName}: {ex.Message}");
-        Debug.WriteLine($"Stack trace: {ex.StackTrace}");
-#endif
+        DebugHelper.LogDebug(nameof(FileManager), methodName, ex);
         await TooltipHelper.ShowTooltipMessageAsync(ex.Message);
     }
 

+ 3 - 5
src/PicView.Avalonia/ImageHandling/ImageOptimizer.cs

@@ -1,7 +1,7 @@
-using System.Diagnostics;
-using PicView.Avalonia.Navigation;
+using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
+using PicView.Core.DebugTools;
 
 namespace PicView.Avalonia.ImageHandling;
 
@@ -16,8 +16,6 @@ public static class ImageOptimizer
     /// <param name="vm">The main view model</param>
     public static async Task OptimizeImageAsync(MainViewModel vm)
     {
-        ArgumentNullException.ThrowIfNull(vm);
-
         if (!NavigationManager.CanNavigate(vm) || vm.PicViewer.FileInfo == null)
         {
             return;
@@ -35,7 +33,7 @@ public static class ImageOptimizer
             }
             catch (Exception ex)
             {
-                Debug.WriteLine($"Error optimizing image: {ex.Message}");
+                DebugHelper.LogDebug(nameof(ImageOptimizer), nameof(OptimizeImageAsync), ex);
             }
         });
         

+ 3 - 3
src/PicView.Avalonia/UI/HideInterfaceLogic.cs

@@ -1,5 +1,4 @@
-using System.Runtime.InteropServices;
-using Avalonia.Controls;
+using Avalonia.Controls;
 using Avalonia.Threading;
 using PicView.Avalonia.Animations;
 using PicView.Avalonia.Gallery;
@@ -86,8 +85,9 @@ public static class HideInterfaceLogic
             }
         }
         
-        WindowResizing.SetSize(vm);
         MenuManager.CloseMenus(vm);
+        await WindowResizing.SetSizeAsync(vm);
+
         await SaveSettingsAsync();
     }
     

+ 6 - 6
src/PicView.Avalonia/Update/UpdateInfo.cs

@@ -7,15 +7,15 @@ public class UpdateInfo
     ////////////\\\\\\\\\\\\
     ///  Windows versions \\\
     ////////////\\\\\\\\\\\\\
-    public required string X64Portable { get; set; }
-    public required string X64Install { get; set; }
-    public required string Arm64Portable { get; set; }
-    public required string Arm64Install { get; set; }
+    public string? X64Portable { get; set; }
+    public string? X64Install { get; set; }
+    public string? Arm64Portable { get; set; }
+    public string? Arm64Install { get; set; }
     
     
     ////////////\\\\\\\\\\\
     ///  macOS versions \\\
     ////////////\\\\\\\\\\\\
-    public required string MacIntel { get; set; }
-    public required string MacArm64 { get; set; }
+    public string? MacIntel { get; set; }
+    public string? MacArm64 { get; set; }
 }

+ 33 - 3
src/PicView.Core/DebugTools/DebugHelper.cs

@@ -1,12 +1,42 @@
 using System.Diagnostics;
+using System.Globalization;
 
 namespace PicView.Core.DebugTools;
 
+/// <summary>
+///     Provides helper methods for logging debug information during development.
+/// </summary>
 public static class DebugHelper
 {
+    /// <summary>
+    ///     Logs detailed debug information for an exception, including the class name, method name, message, and stack trace.
+    ///     This method is intended to be used for development and debugging purposes only.
+    /// </summary>
+    /// <param name="className">
+    ///     The name of the class where the exception occurred. Typically passed using
+    ///     <c>nameof(ClassName)</c>.
+    /// </param>
+    /// <param name="methodName">
+    ///     The name of the method where the exception occurred. Typically passed using
+    ///     <c>nameof(MethodName)</c>.
+    /// </param>
+    /// <param name="exception">The exception that was thrown.</param>
+    /// <example>
+    ///     <code>
+    /// try
+    /// {
+    ///     // Code that might throw
+    /// }
+    /// catch (Exception ex)
+    /// {
+    ///     DebugHelper.LogDebug(nameof(MyClass), nameof(MyMethod), ex);
+    /// }
+    /// </code>
+    /// </example>
     public static void LogDebug(string className, string methodName, Exception exception)
     {
-        Debug.WriteLine($"{className}:{methodName} exception {exception.Message}");
-        Debug.WriteLine(Environment.NewLine + exception.StackTrace);
+        Debug.WriteLine(
+            $"\n[{DateTime.Now.ToString("T", CultureInfo.CurrentCulture)}] {className}.{methodName} exception: {exception.Message}");
+        Debug.WriteLine(exception.StackTrace + Environment.NewLine);
     }
-}
+}