1
1
Эх сурвалжийг харах

Refactor `PrintPreviewViewModel` and improve print preview initialization.

- Replaced `_disposables` with `Disposables` in `PrintPreviewViewModel` for consistency.
- Simplified array initializations for `ScaleModes`, `Orientations`, and `ColorModes`.
- Updated `PrintPreviewWindow` to use `DebugHelper.LogDebug` for exception handling.
- Refactored print preview creation logic in `WindowInitializer` with TODO markers for further improvements.
- Enhanced theme updates in `PrintPreviewWindow.axaml.cs` with extracted method for maintainability.
Ruben 1 өдөр өмнө
parent
commit
a715e6e7dd

+ 12 - 5
src/PicView.Avalonia.Win32/Views/PrintPreviewWindow.axaml.cs

@@ -11,6 +11,7 @@ using PicView.Avalonia.Printing;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.Win32.Printing;
+using PicView.Core.DebugTools;
 using PicView.Core.Localization;
 using PicView.Core.ViewModels;
 using R3;
@@ -26,7 +27,11 @@ public partial class PrintPreviewWindow : Window
         InitializeComponent();
 
         GenericWindowHelper.GenericWindowInitialize(this, TranslationManager.Translation.Print + " - PicView");
+        ThemeUpdates();
+    }
 
+    private void ThemeUpdates()
+    {
         if (!Settings.Theme.Dark)
         {
             PrintPreviewView.Background = UIHelper.GetMenuBackgroundColor();
@@ -85,7 +90,7 @@ public partial class PrintPreviewWindow : Window
             .AsObservable()
             .DistinctUntilChanged()
             .Subscribe(_ => UpdatePrinter(vm.PrintPreview))
-            .AddTo(vm.PrintPreview._disposables);
+            .AddTo(vm.PrintPreview.Disposables);
 
         // Any setting change triggers preview update
         // ReSharper disable once InvokeAsExtensionMethod
@@ -102,7 +107,7 @@ public partial class PrintPreviewWindow : Window
                     => (orientation, top, bottom, left, right, scale, color, paper))
             .ThrottleLast(TimeSpan.FromMilliseconds(100))
             .Subscribe(_ => UpdatePreview(vm.PrintPreview))
-            .AddTo(vm.PrintPreview._disposables);
+            .AddTo(vm.PrintPreview.Disposables);
 
         // Initial render
         UpdatePrinter(vm.PrintPreview);
@@ -148,7 +153,9 @@ public partial class PrintPreviewWindow : Window
         }
         catch (Exception ex)
         {
-            Console.WriteLine($"[PrintPreview] Failed to reload paper sizes: {ex}");
+            DebugHelper.LogDebug(nameof(PrintPreviewView), nameof(UpdatePrinter),
+                "[PrintPreview] Failed to reload paper sizes");
+            DebugHelper.LogDebug(nameof(PrintPreviewView), nameof(UpdatePrinter), ex);
         }
     }
 
@@ -237,7 +244,7 @@ public partial class PrintPreviewWindow : Window
         }
         catch (Exception ex)
         {
-            Console.WriteLine($"[PrintPreview] UpdatePreview failed: {ex}");
+            DebugHelper.LogDebug(nameof(PrintPreviewView), nameof(UpdatePreview), ex);
         }
     }
 
@@ -274,7 +281,7 @@ public partial class PrintPreviewWindow : Window
         }
         catch (Exception ex)
         {
-            Console.WriteLine($"[PrintPreview] RunPrintAsync error: {ex}");
+            DebugHelper.LogDebug(nameof(PrintPreviewView), nameof(RunPrintAsync), ex);
         }
         finally
         {

+ 15 - 16
src/PicView.Avalonia.Win32/WindowImpl/WindowInitializer.cs

@@ -1,4 +1,5 @@
-using Avalonia;
+using System.Drawing.Printing;
+using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Threading;
@@ -456,19 +457,22 @@ public class WindowInitializer : IPlatformSpecificUpdate
 
             if (_printPreviewWindow is null)
             {
-                vm.PrintPreview = new();
+                vm.PrintPreview = new PrintPreviewViewModel();
 
-                var printerSettings = new System.Drawing.Printing.PrinterSettings();
+                // TODO: Move this initialization to its own dedicated class
+
+                var printerSettings = new PrinterSettings();
 
                 // Load installed printers
-                vm.PrintPreview.Printers.Value = new List<string>(System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast<string>());
+                vm.PrintPreview.Printers.Value = new List<string>(PrinterSettings.InstalledPrinters);
                 vm.PrintPreview.PaperSizes.Value = new List<string>(PrintEngine.GetPaperSizes(printerSettings.PrinterName));
 
 
                 // Pre-select default printer settings
                 var pageSettings = printerSettings.DefaultPageSettings;
 
-                var currentPrintSettings = new PrintSettings
+                var currentPrintSettings =
+                    new PrintSettings // TODO: Add print settings to its own config class to remember user preference
                 {
                     ImagePath = { Value = vm.PicViewer.FileInfo?.Value?.FullName },
                     PrinterName = { Value = printerSettings.PrinterName },
@@ -482,12 +486,7 @@ public class WindowInitializer : IPlatformSpecificUpdate
                 };
 
                 vm.PrintPreview.PrintSettings.Value = currentPrintSettings;
-
-                if (vm.PicViewer.FileInfo.Value != null && File.Exists(vm.PicViewer.FileInfo.Value.FullName))
-                {
-                    using var fs = File.OpenRead(vm.PicViewer.FileInfo.Value.FullName);
-                    vm.PrintPreview.PreviewImage.Value = new System.Drawing.Bitmap(fs);
-                }
+                vm.PrintPreview.PreviewImage.Value = vm.PicViewer.ImageSource.CurrentValue;
 
                 _printPreviewWindow = new PrintPreviewWindow
                 {
@@ -495,19 +494,19 @@ public class WindowInitializer : IPlatformSpecificUpdate
                     WindowStartupLocation = WindowStartupLocation.CenterOwner
                 };
 
-                vm.PrintPreview.PrintCommand.SubscribeAwait(async (_, ct) =>
+                vm.PrintPreview.PrintCommand.SubscribeAwait(async (_, _) =>
                 {
                     await _printPreviewWindow?.RunPrintAsync(vm);
                 })
-                .AddTo(vm.PrintPreview._disposables);
+                .AddTo(vm.PrintPreview.Disposables);
 
-                vm.PrintPreview.CancelCommand.SubscribeAwait(async (_, ct) =>
+                vm.PrintPreview.CancelCommand.SubscribeAwait(async (_, _) =>
                 {
                     await Dispatcher.UIThread.InvokeAsync(() => _printPreviewWindow?.Close());
-                }).AddTo(vm.PrintPreview._disposables);
+                }).AddTo(vm.PrintPreview.Disposables);
 
                 _printPreviewWindow.Show(desktop.MainWindow);
-                _printPreviewWindow.Closing += (s, e) => _printPreviewWindow = null;
+                _printPreviewWindow.Closing += (_, _) => _printPreviewWindow = null;
             }
             else
             {

+ 20 - 24
src/PicView.Core/ViewModels/PrintPreviewViewModel.cs

@@ -1,39 +1,35 @@
-using System.Collections;
-using System.Collections.ObjectModel;
-using PicView.Avalonia.Printing;
+using PicView.Avalonia.Printing;
 using PicView.Core.Localization;
 using R3;
 
-// ReSharper disable CompareOfFloatsByEqualityOperator
-
 namespace PicView.Core.ViewModels;
 
 public class PrintPreviewViewModel : IDisposable
 {
-    public readonly CompositeDisposable _disposables = new();
+    public readonly CompositeDisposable Disposables = new();
 
     public PrintPreviewViewModel()
     {
-        ScaleModes.Value = new [] 
-        { 
+        ScaleModes.Value =
+        [
             TranslationManager.Translation.Fit, 
             TranslationManager.Translation.Fill, 
-            TranslationManager.Translation.Stretch, 
-            TranslationManager.Translation.Center 
-        };
-
-        Orientations.Value = new [] 
-        { 
-            TranslationManager.Translation.Portrait, 
-            TranslationManager.Translation.Landscape 
-        };
-
-        ColorModes.Value = new [] 
-        { 
+            TranslationManager.Translation.Stretch,
+            TranslationManager.Translation.Center
+        ];
+
+        Orientations.Value =
+        [
+            TranslationManager.Translation.Portrait,
+            TranslationManager.Translation.Landscape
+        ];
+
+        ColorModes.Value =
+        [
             TranslationManager.Translation.Auto, 
-            TranslationManager.Translation.Color, 
-            TranslationManager.Translation.BlackAndWhite 
-        };
+            TranslationManager.Translation.Color,
+            TranslationManager.Translation.BlackAndWhite
+        ];
 
     }
 
@@ -72,7 +68,7 @@ public class PrintPreviewViewModel : IDisposable
 
     public void Dispose()
     {
-        Disposable.Dispose(_disposables,
+        Disposable.Dispose(Disposables,
             Printers,
             PaperSizes,
             ScaleModes,