Browse Source

Fix effects not applying when printing on macOS and fix window resizing for the print window

Refactor window resizing logic and macOS print initialization

- Extract `KeepWindowSize` logic from `HandleWindowResize` for better modularity.
- Update `MacPrintInitialization` to streamline format handling and eliminate redundant dispatcher calls.
- Replace `ValueTask` with `Task` for improved consistency in async print workflow.
Ruben 1 week ago
parent
commit
ebebcf6683

+ 3 - 3
src/PicView.Avalonia.MacOS/Printing/MacPrintInitialization.cs

@@ -1,4 +1,3 @@
-using Avalonia.Media.Imaging;
 using Avalonia.Threading;
 using PicView.Avalonia.ImageHandling;
 using PicView.Avalonia.MacOS.Views;
@@ -10,7 +9,7 @@ namespace PicView.Avalonia.MacOS.Printing;
 
 public static class MacPrintInitialization
 {
-    public static async ValueTask Initialize(MainViewModel vm, string path, PrintPreviewWindow printPreviewWindow)
+    public static async Task Initialize(MainViewModel vm, string path, PrintPreviewWindow printPreviewWindow)
     {
         // 1. Printers via CUPS
         var printers = MacOSPrint.GetAvailablePrinters().ToList(); // includes "Save as PDF" first
@@ -22,6 +21,7 @@ public static class MacPrintInitialization
         vm.PrintPreview.PaperSizes.Value =
             CupsPaperQuery.GetPaperSizes(defaultPrinter).ToList();
         
+        // Allow every format that is viewable to also be printed, or just make sure the image effect stays applied on print
         var commonSupportedFormat = await ImageFormatConverter.ConvertToCommonSupportedFormatAsync(path, vm)
             .ConfigureAwait(false);
 
@@ -41,6 +41,6 @@ public static class MacPrintInitialization
 
         vm.PrintPreview.PrintSettings.Value = currentPrintSettings;
 
-        await Dispatcher.UIThread.InvokeAsync(() => printPreviewWindow.Initialize(path));
+        await Dispatcher.UIThread.InvokeAsync(() => printPreviewWindow.Initialize(commonSupportedFormat));
     }
 }

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

@@ -36,7 +36,7 @@ public partial class PrintPreviewWindow : Window
             ClientSizeProperty.Changed.ToObservable()
                 .Subscribe(size =>
                 {
-                    WindowResizing.HandleWindowResize(this, size);
+                    WindowResizing.KeepWindowSize(this, size);
                 });
         };
     }

+ 1 - 4
src/PicView.Avalonia.MacOS/WindowImpl/WindowInitializer.cs

@@ -453,10 +453,7 @@ public class WindowInitializer : IPlatformSpecificUpdate
                 _printPreviewWindow.Show(desktop.MainWindow);
                 _printPreviewWindow.Closing += (_, _) => _printPreviewWindow = null;
 
-                Task.Run(() =>
-                {
-                     MacPrintInitialization.Initialize(vm, path, _printPreviewWindow);
-                });
+                Task.Run(() => MacPrintInitialization.Initialize(vm, path, _printPreviewWindow));
             }
             else
             {

+ 21 - 7
src/PicView.Avalonia/WindowBehavior/WindowResizing.cs

@@ -18,17 +18,15 @@ public static class WindowResizing
 {
     #region Window Resize Handling
 
-    public static void HandleWindowResize(Window window, AvaloniaPropertyChangedEventArgs<Size> size)
+    public static bool KeepWindowSize(Window window, AvaloniaPropertyChangedEventArgs<Size> size)
     {
-        if (!Settings.WindowProperties.AutoFit ||
-            !size.OldValue.HasValue || !size.NewValue.HasValue ||
+        if (!size.OldValue.HasValue || !size.NewValue.HasValue ||
             size.Sender != window || size.OldValue.Value.Width == 0 || size.OldValue.Value.Height == 0 ||
-            size.NewValue.Value.Width == 0 || size.NewValue.Value.Height == 0 ||
-            window.DataContext is not MainViewModel vm)
+            size.NewValue.Value.Width == 0 || size.NewValue.Value.Height == 0)
         {
-            return;
+            return false;
         }
-
+        
         var oldSize = size.OldValue.Value;
         var newSize = size.NewValue.Value;
 
@@ -36,6 +34,22 @@ public static class WindowResizing
         var y = (oldSize.Height - newSize.Height) / 2;
 
         window.Position = new PixelPoint(window.Position.X + (int)x, window.Position.Y + (int)y);
+        
+        return true;
+    }
+
+    public static void HandleWindowResize(Window window, AvaloniaPropertyChangedEventArgs<Size> size)
+    {
+        if (!Settings.WindowProperties.AutoFit || window.DataContext is not MainViewModel vm)
+        {
+            return;
+        }
+
+        var isWindowResized = KeepWindowSize(window, size);
+        if (!isWindowResized)
+        {
+            return;
+        }
 
         RepositionCursorIfTriggered(vm, vm.MainWindow.IsNavigationButtonLeftClicked,
             clicked => vm.MainWindow.IsNavigationButtonLeftClicked = clicked,