Repository navigation
feat: add PrintAsync that sends the PDF export to the platform print service (#386) - #522
Open
michaelstonis wants to merge 1 commit into
Open
michaelstonis wants to merge 1 commit into
michaelstonis wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #386.
Stacked on #502: until that PR merges, this one also shows its commits.
Changes
PrintAsyncon the MAUI and UnoKumikoDataGridexports the grid as the #340 PDF to a temporary file, shows the platform's print dialog for it, and deletes the file. It follows theIKumikoClipboardpattern: a small Core interface, with a default implementation per platform supplied by the host.Core (
KumikoUI.Core.Export)IKumikoPrintService:IsSupportedandPrintPdfAsync(Stream pdf, string jobName, CancellationToken). The stream is readable and seekable, positioned at 0, and valid until the task completes.KumikoPrintResult:Printed,Canceled,NotSupported,Failed.KumikoPrintExtensions.PrintAsync(this KumikoSource, IKumikoPrintService?, IKumikoExporter, string jobName, options, progress, cancellationToken). This is the platform-neutral flow, and it is usable without a grid:NotSupportedwithout exporting.Path.GetTempPath()/kumiko-print-{guid}.pdf, opened withFileShare.Read, rewinds the file, and calls the service on the caller's context. It uses noConfigureAwait(false)because dialogs belong to the UI thread.finally. Export and service exceptions propagate after cleanup, and a canceled token throwsOperationCanceledException.KumikoStrings.PrintJobName("Data grid"). The job name is thejobNameargument, else the PDF'sMetadata.Title, else this string (internalResolveJobName).Hosts (both
KumikoDataGrids,KumikoDataGrid.Printing.cs)PrintService { get; set; }defaults to the platform service and is created lazily. Settingnullturns printing off.CanPrintisPrintService?.IsSupported == true.PrintAsync(KumikoExportOptions? options = null, KumikoPdfOptions? pdf = null, string? jobName = null, IProgress<KumikoExportProgress>? progress = null, CancellationToken cancellationToken = default).PrintCommand(ICommand, parameter ignored, can execute whileCanPrint;CanExecuteChangedfires whenPrintServiceis replaced). Uno gets it too, for parity withShowColumnChooserCommand.Platform services. The code is shared by both hosts:
src/Shared/Printing/*.cs, linked into each project with<Compile Include LinkBase="Printing">. Each file compiles only for its own platform (#if).ApplePrintService):UIPrintInteractionControllerwith the temp file'sNSUrlas the printing item, orNSDatafor a non-file stream. It usesPresentFromRectInViewfrom the grid's bounds on iPad and Mac, andPresenton iPhone. The completion handler maps toPrinted,CanceledorFailed.IsSupportedisPrintingAvailable.AndroidPrintService):PrintManager.Printwith aPrintDocumentAdapterthat copies the PDF to the write descriptor. The page count is reported as unknown, so the framework paginates and previews the document itself. The task completes inOnFinish, and the result comes from thePrintJobstate. No permission or manifest entry is needed.Platform.CurrentActivityon MAUI andContextHelper.Currenton Uno.WindowsPrintService). WinUI printing can only print XAML elements, so:Windows.Data.Pdfand renders each page to aBitmapImageat 150 dpi when the preview (GetPreviewPage) or the printer (AddPages) asks for it, sized to the printer's page.PrintManagerInterop.GetForWindowandShowPrintUIForWindowAsync, and mapsPrintTask.CompletedtoPrinted,CanceledorFailed.WindowNative.GetWindowHandleon MAUI and fromXamlRoot.ContentIslandEnvironment.AppWindowId→Win32Interop.GetWindowFromWindowIdon Uno.src/KumikoUI.Uno/Printing/BrowserPrintService.cs):JSHost.ImportAsyncfrom adata:URL, so the package ships no web asset andcheck-uno-packageis unaffected.print().Printedonce the dialog opened (documented). The frame and the blob URL are removed when the page gets focus back, or after 10 minutes.data:scripts makes the call returnFailed.PrintServiceis null,CanPrintis false andPrintAsyncreturnsNotSupported, as documented. Apps can assign their own service, for example one that opens the OS viewer.Other changes
docs/EXPORT.mdgets a new "Printing" section: per-platform table, threading, cancellation, preview, the Core flow.README.mdgets a print snippet under Exporting (compiled by the docs snippet projects), andPrintJobNameis added to the strings list.CHANGELOG.mdgets an entry under Unreleased.No renderer, input controller, hit tester or PDF page writer code is touched. The per-cell styling code that #385 changes is left alone: printing goes through the public
KumikoPdfExporter, so #385's styling carries over.Public API added
IKumikoPrintService(IsSupported,PrintPdfAsync),KumikoPrintResult(4 members),KumikoPrintExtensions.PrintAsync,KumikoStrings.PrintJobName.PublicAPI.Unshipped.txt:KumikoDataGrid.PrintService(get/set),CanPrint,PrintAsync(...),PrintCommand.Acceptance criteria
PrintAsyncprints a multi-page grouped report on iOS, Mac Catalyst, Android and Windows (MAUI), and on Uno WebAssembly, iOS, Android and WinAppSDK; Uno desktop behavior decided and documented.NotSupported). No device print dialog could be run here; per platform:print(). The test browser blocks print dialogs it did not see a user open, so no dialog appeared.net10.0-ios,net10.0-maccatalystandnet10.0-android.net10.0-iosandnet10.0-android.net10.0-windows10.0.19041.0, Unonet10.0-windows10.0.26100).WindowsPrintService.csand both hosts' window-handle lookups were compiled without errors or warnings in a scratchnet10.0-windows10.0.19041.0WinUI project (Microsoft.WindowsAppSDK 1.8, MAUI 10.0.110 global usings,IsAotCompatible,latest-recommended). CI's Windows jobs are the first real build.CanPrint/IsSupportedreport correctly per platform;NotSupportedreturned rather than thrown. Covered by tests for the null and unsupported service, the desktop factory returning null, and the MAUI grid on net10.0.tests/KumikoUI.Core.Testswith a fakeIKumikoPrintService(flow, cancellation, cleanup); host tests intests/KumikoUI.Maui.Testsandtests/KumikoUI.Uno.Testsfor wiring.%PDF-…%%EOF), the job name resolves, andCanPrintandPrintCommandfollow the service.KumikoDataGridcannot be created in the desktop test project (missingUno.WinUI.Graphics2DSKwithout a running app). The Uno tests therefore cover the pieces the grid's members delegate to: the platform factory (null on desktop) andPrintGridCommand.PublicAPI.Unshipped.txtupdated. No member is head-specific. As in Export: add a paginated PDF exporter in KumikoUI.SkiaSharp #340,eng/update-public-api.shdoes not write members back in this worktree, so the RS0016 lines from the build were added by hand, sorted like the script (LC_ALL=C sort).IsAotCompatibleand the trim and AOT analyzers on every head built here, with no warnings. The new code uses no reflection, and the JS interop is a[JSImport]. No AOT app publish was run locally.Verification
dotnet build src/KumikoUI.Core(Debug and Release) anddotnet build src/KumikoUI.SkiaSharp -c Release: succeeded, no warnings.dotnet build src/KumikoUI.Maui -f net10.0-maccatalyst,-f net10.0-ios(Debug and Release) and-f net10.0-android: succeeded, no warnings from this change. Android prints the machine's existing "Java SDK installation" environment warning.dotnet build src/KumikoUI.Uno -f net10.0-desktop,-f net10.0,-f net10.0-ios,-f net10.0-androidand-f net10.0-browserwasm(also-c Release): succeeded. The only warnings are the existing Uno0001 ones; none come from the printing files.net10.0-windows10.0.19041.0WinUI project compilingsrc/Shared/Printing/WindowsPrintService.csplus probes of both hosts' window-handle code, built with-p:AppxGeneratePriEnabled=false: built, 0 warnings.dotnet test tests/KumikoUI.Core.Tests: 4385 passed, 5 skipped. The 20 newPrintFlowTestscover flow, cleanup, cancellation,NotSupported, progress and job-name resolution;LocalizationTestscovers GermanPrintJobName.dotnet test tests/KumikoUI.Maui.Tests: 454 passed (5 newKumikoDataGridPrintingTests).dotnet test tests/KumikoUI.Uno.Tests: 324 passed (3 newKumikoDataGridPrintingTests).dotnet test tests/KumikoUI.SkiaSharp.Tests: 79 passed.dotnet build samples/SampleApp.Uno -f net10.0-desktopand-f net10.0-browserwasm, anddotnet build samples/SampleApp.Maui -f net10.0-maccatalyst -p:KumikoDevFlow=false: succeeded.dotnet run --project eng/DocsSnippets -c Release, thendotnet build tests/KumikoUI.Docs.Snippets.Uno -f net10.0-desktop -c Releaseanddotnet build tests/KumikoUI.Docs.Snippets.Maui -f net10.0-maccatalyst -c Release: succeeded.dotnet run --project eng/DocsIdentifiers -c Release: 0 problems.print()was called; the browser blocked the dialog itself.