Browse Source

Add comments for clarity and improve exception handling in print workflow

- Document `ImagePath` usage in `PrintSettings`.
- Add cleanup for temporary files in `MacPrintEngine`.
- Replace manual null checks with `ArgumentNullException.ThrowIfNull`.
- Fix minor casing inconsistency in `GrayCache` property type.
Ruben 6 days ago
parent
commit
38d12abcba

+ 1 - 0
src/PicView.Avalonia.MacOS/Printing/MacPrintEngine.cs

@@ -86,6 +86,7 @@ public static class MacPrintEngine
         }
         finally
         {
+            // 7. Cleanup, delete temporary file
             try { File.Delete(settings.ImagePath.Value); } catch { /* ignore */ }
         }
     }

+ 2 - 1
src/PicView.Avalonia.MacOS/Views/PrintPreviewWindow.axaml.cs

@@ -63,6 +63,7 @@ public partial class PrintPreviewWindow : Window
             .AddTo(vm.PrintPreview.Disposables);
 
         // Any setting change triggers preview update
+        // ReSharper disable once InvokeAsExtensionMethod
         Observable.CombineLatest(
                 ps.Orientation.AsObservable(),
                 ps.MarginTop.AsObservable(),
@@ -182,7 +183,7 @@ public partial class PrintPreviewWindow : Window
 
         try
         {
-            await Task.Run(() => MacPrintEngine.RunPrintJob(settings, _outputImage as Bitmap));
+            await MacPrintEngine.RunPrintJob(settings, _outputImage as Bitmap);
             await Dispatcher.UIThread.InvokeAsync(Close);
         }
         catch (Exception ex)

+ 1 - 4
src/PicView.Avalonia/Printing/PrintCore.cs

@@ -64,10 +64,7 @@ public static class PrintCore
 
     public static Bitmap ToGrayScale(Bitmap src, float dpi)
     {
-        if (src is null)
-        {
-
-        }
+        ArgumentNullException.ThrowIfNull(src);
 
         int width, height;
         try

+ 4 - 0
src/PicView.Core/Printing/PrintSettings.cs

@@ -4,6 +4,10 @@ namespace PicView.Core.Printing
 {
     public class PrintSettings
     {
+        /// <summary>
+        /// Path to the image to print.
+        /// <remarks>Should point to a temporary file of a common format</remarks>
+        /// </summary>
         public BindableReactiveProperty<string?> ImagePath { get; } = new();
         public BindableReactiveProperty<string?> PrinterName { get; } = new();
         public BindableReactiveProperty<string?> PaperSize { get; } = new();

+ 1 - 2
src/PicView.Core/ViewModels/PrintPreviewViewModel.cs

@@ -51,8 +51,7 @@ public class PrintPreviewViewModel : IDisposable
     public BindableReactiveProperty<bool> IsProcessing { get; } = new(false);
     public BindableReactiveProperty<double> Opacity { get; } = new(1.0);
 
-    public Object? GrayCache { get; set; }
-
+    public object? GrayCache { get; set; }
 
     #endregion