From 91a57ea84f5b64a3e7bdbba791d72d2312e90f28 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Thu, 7 Dec 2023 21:12:49 -0800 Subject: [PATCH] Don't run print in a task on Mac/Linux #238 --- .../ImportExport/GtkScannedImagePrinter.cs | 111 +++++++++--------- .../ImportExport/MacScannedImagePrinter.cs | 54 ++++----- 2 files changed, 78 insertions(+), 87 deletions(-) diff --git a/NAPS2.Lib.Gtk/ImportExport/GtkScannedImagePrinter.cs b/NAPS2.Lib.Gtk/ImportExport/GtkScannedImagePrinter.cs index 7dece68534..c74cbae1ca 100644 --- a/NAPS2.Lib.Gtk/ImportExport/GtkScannedImagePrinter.cs +++ b/NAPS2.Lib.Gtk/ImportExport/GtkScannedImagePrinter.cs @@ -5,76 +5,73 @@ namespace NAPS2.ImportExport; public class GtkScannedImagePrinter : IScannedImagePrinter { - public async Task PromptToPrint( + public Task PromptToPrint( Eto.Forms.Window parentWindow, IList images, IList selectedImages) { if (!images.Any()) { - return false; + return Task.FromResult(false); } - return await Task.Run(() => + var printOp = new PrintOperation { - var printOp = new PrintOperation - { - NPages = images.Count, - UseFullPage = true, - HasSelection = selectedImages.Count > 1, - SupportSelection = selectedImages.Count > 1 - }; - if (selectedImages.Count == 1) + NPages = images.Count, + UseFullPage = true, + HasSelection = selectedImages.Count > 1, + SupportSelection = selectedImages.Count > 1 + }; + if (selectedImages.Count == 1) + { + printOp.CurrentPage = images.IndexOf(selectedImages[0]); + } + var printTarget = images; + printOp.BeginPrint += (_, args) => + { + if (printOp.PrintSettings.PrintPages == PrintPages.Selection) { - printOp.CurrentPage = images.IndexOf(selectedImages[0]); + printTarget = selectedImages; + printOp.NPages = printTarget.Count; } - var printTarget = images; - printOp.BeginPrint += (_, args) => + }; + printOp.DrawPage += (_, args) => + { + var image = printTarget[args.PageNr].Render(); + try { - if (printOp.PrintSettings.PrintPages == PrintPages.Selection) + var ctx = args.Context; + var cairoCtx = ctx.CairoContext; + + if (Math.Sign(image.Width - image.Height) != Math.Sign(ctx.Width - ctx.Height)) { - printTarget = selectedImages; - printOp.NPages = printTarget.Count; + // Flip portrait/landscape to match output + image = image.PerformTransform(new RotationTransform(90)); } - }; - printOp.DrawPage += (_, args) => - { - var image = printTarget[args.PageNr].Render(); - try - { - var ctx = args.Context; - var cairoCtx = ctx.CairoContext; - - if (Math.Sign(image.Width - image.Height) != Math.Sign(ctx.Width - ctx.Height)) - { - // Flip portrait/landscape to match output - image = image.PerformTransform(new RotationTransform(90)); - } - // Fit the image into the output rect (centered) while maintaining its aspect ratio - var heightBound = image.Width / ctx.Width < image.Height / ctx.Height; - var targetWidth = heightBound ? image.Width * ctx.Height / image.Height : ctx.Width; - var targetHeight = heightBound ? ctx.Height : image.Height * ctx.Width / image.Width; - var targetX = (ctx.Width - targetWidth) / 2; - var targetY = (ctx.Height - targetHeight) / 2; - cairoCtx.Translate(targetX, targetY); - cairoCtx.Scale(targetWidth / image.Width, targetHeight / image.Height); + // Fit the image into the output rect (centered) while maintaining its aspect ratio + var heightBound = image.Width / ctx.Width < image.Height / ctx.Height; + var targetWidth = heightBound ? image.Width * ctx.Height / image.Height : ctx.Width; + var targetHeight = heightBound ? ctx.Height : image.Height * ctx.Width / image.Width; + var targetX = (ctx.Width - targetWidth) / 2; + var targetY = (ctx.Height - targetHeight) / 2; + cairoCtx.Translate(targetX, targetY); + cairoCtx.Scale(targetWidth / image.Width, targetHeight / image.Height); - Gdk.CairoHelper.SetSourcePixbuf(cairoCtx, image.AsPixbuf(), 0, 0); - cairoCtx.Paint(); - } - finally - { - image.Dispose(); - } - }; - printOp.EndPrint += (_, args) => + Gdk.CairoHelper.SetSourcePixbuf(cairoCtx, image.AsPixbuf(), 0, 0); + cairoCtx.Paint(); + } + finally { - Log.Event(EventType.Print, new EventParams - { - Name = MiscResources.Print, - Pages = printOp.NPagesToPrint - }); - }; - var result = printOp.Run(PrintOperationAction.PrintDialog, (Window) parentWindow.ControlObject); - return result == PrintOperationResult.Apply; - }); + image.Dispose(); + } + }; + printOp.EndPrint += (_, args) => + { + Log.Event(EventType.Print, new EventParams + { + Name = MiscResources.Print, + Pages = printOp.NPagesToPrint + }); + }; + var result = printOp.Run(PrintOperationAction.PrintDialog, (Window) parentWindow.ControlObject); + return Task.FromResult(result == PrintOperationResult.Apply); } } \ No newline at end of file diff --git a/NAPS2.Lib.Mac/ImportExport/MacScannedImagePrinter.cs b/NAPS2.Lib.Mac/ImportExport/MacScannedImagePrinter.cs index 92aed024a8..9a5e61caea 100644 --- a/NAPS2.Lib.Mac/ImportExport/MacScannedImagePrinter.cs +++ b/NAPS2.Lib.Mac/ImportExport/MacScannedImagePrinter.cs @@ -4,44 +4,38 @@ namespace NAPS2.ImportExport; public class MacScannedImagePrinter : IScannedImagePrinter { - public async Task PromptToPrint( + public Task PromptToPrint( Eto.Forms.Window parentWindow, IList images, IList selectedImages) { if (!images.Any()) { - return false; + return Task.FromResult(false); } - return await Task.Run(() => + using var view = new PaginatedImageView(images); + var printOp = NSPrintOperation.FromView(view, new NSPrintInfo { - return Invoker.Current.InvokeGet(() => + LeftMargin = 0, + BottomMargin = 0, + RightMargin = 0, + TopMargin = 0, + HorizontalPagination = NSPrintingPaginationMode.Fit, + VerticalPagination = NSPrintingPaginationMode.Fit, + HorizontallyCentered = true, + VerticallyCentered = true, + Orientation = view.CurrentImage!.Width > view.CurrentImage.Height + ? NSPrintingOrientation.Landscape + : NSPrintingOrientation.Portrait + }); + if (printOp.RunOperation()) + { + Log.Event(EventType.Print, new EventParams { - using var view = new PaginatedImageView(images); - var printOp = NSPrintOperation.FromView(view, new NSPrintInfo - { - LeftMargin = 0, - BottomMargin = 0, - RightMargin = 0, - TopMargin = 0, - HorizontalPagination = NSPrintingPaginationMode.Fit, - VerticalPagination = NSPrintingPaginationMode.Fit, - HorizontallyCentered = true, - VerticallyCentered = true, - Orientation = view.CurrentImage!.Width > view.CurrentImage.Height - ? NSPrintingOrientation.Landscape - : NSPrintingOrientation.Portrait - }); - if (printOp.RunOperation()) - { - Log.Event(EventType.Print, new EventParams - { - Name = MiscResources.Print, - Pages = (int) printOp.PageRange.Length - }); - return true; - } - return false; + Name = MiscResources.Print, + Pages = (int) printOp.PageRange.Length }); - }); + return Task.FromResult(true); + } + return Task.FromResult(false); } private class PaginatedImageView : NSBox