PrintUI thing.

This commit is contained in:
Burak Kaan Köse
2025-10-20 21:10:14 +02:00
parent 7a7281f2fa
commit baf4141773
2 changed files with 28 additions and 30 deletions
@@ -55,6 +55,8 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
// Used in 'Save as' and 'Print' functionality.
public Func<string, Task<bool>> SaveHTMLasPDFFunc { get; set; }
public Action ShowPrintUIAction { get; set; }
#region Properties
public bool ShouldDisplayDownloadProgress => IsIndetermineProgress || (CurrentDownloadPercentage > 0 && CurrentDownloadPercentage <= 100);
@@ -255,7 +257,7 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
}
else if (operation == MailOperation.Print)
{
await PrintAsync();
ShowPrintUI();
}
else if (operation == MailOperation.ViewMessageSource)
{
@@ -680,37 +682,16 @@ public partial class MailRenderingPageViewModel : MailBaseViewModel,
}
}
private async Task PrintAsync()
private void ShowPrintUI()
{
// Printing:
// 1. Let WebView2 save the current HTML as PDF to temporary location.
// 2. Saving as PDF will divide pages correctly for Win2D CanvasBitmap.
// 3. Use Win2D CanvasBitmap as IPrintDocumentSource and WinRT APIs to print the PDF.
try
{
var printFilePath = Path.Combine(_applicationConfiguration.ApplicationTempFolderPath, "print.pdf");
if (File.Exists(printFilePath)) File.Delete(printFilePath);
await SaveHTMLasPDFFunc(printFilePath);
var result = await PrintService.PrintPdfFileAsync(printFilePath, Subject);
if (result == PrintingResult.Submitted)
{
_dialogService.InfoBarMessage(Translator.DialogMessage_PrintingSuccessTitle, Translator.DialogMessage_PrintingSuccessMessage, InfoBarMessageType.Success);
}
else if (result != PrintingResult.Canceled)
{
var message = string.Format(Translator.DialogMessage_PrintingFailedMessage, result);
_dialogService.InfoBarMessage(Translator.DialogMessage_PrintingFailedTitle, message, InfoBarMessageType.Warning);
}
ShowPrintUIAction?.Invoke();
}
catch (Exception ex)
catch (Exception exception)
{
Log.Error(ex, "Failed to print mail.");
_dialogService.InfoBarMessage(string.Empty, ex.Message, InfoBarMessageType.Error);
Log.Error(exception, "Failed to print mail.");
_dialogService.InfoBarMessage(string.Empty, exception.Message, InfoBarMessageType.Error);
}
}
@@ -43,12 +43,29 @@ public sealed partial class MailRenderingPage : MailRenderingPageAbstract,
Environment.SetEnvironmentVariable("WEBVIEW2_DEFAULT_BACKGROUND_COLOR", "00FFFFFF");
Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--enable-features=OverlayScrollbar,msOverlayScrollbarWinStyle,msOverlayScrollbarWinStyleAnimation,msWebView2CodeCache");
ViewModel.ShowPrintUIAction = ShowPrintUI;
ViewModel.SaveHTMLasPDFFunc = new Func<string, Task<bool>>((path) =>
{
return Chromium.CoreWebView2.PrintToPdfAsync(path, null).AsTask();
});
}
private async void ShowPrintUI()
{
if (Chromium.CoreWebView2 == null) return;
// TODO: Footer still shows wino.mail/html, there is no way to change it currently.
// TODO: ShowPrintUI - System doesn't open.
// TODO: ShowPrintUI - System doesn't open.
// Set the document title before printing. This title will be used in the print dialog and header.
await Chromium.CoreWebView2.ExecuteScriptAsync($"document.title = '{ViewModel.Subject}';");
var settings = Chromium.CoreWebView2.Environment.CreatePrintSettings();
Chromium.CoreWebView2?.ShowPrintUI(CoreWebView2PrintDialogKind.System);
}
public override async void OnEditorThemeChanged()
{
base.OnEditorThemeChanged();
@@ -181,7 +198,7 @@ public sealed partial class MailRenderingPage : MailRenderingPageAbstract,
var editorBundlePath = (await ViewModel.NativeAppService.GetEditorBundlePathAsync()).Replace("editor.html", string.Empty);
Chromium.CoreWebView2.SetVirtualHostNameToFolderMapping("app.reader", editorBundlePath, CoreWebView2HostResourceAccessKind.Allow);
Chromium.CoreWebView2.SetVirtualHostNameToFolderMapping("wino.mail", editorBundlePath, CoreWebView2HostResourceAccessKind.Allow);
Chromium.CoreWebView2.DOMContentLoaded -= DOMContentLoaded;
Chromium.CoreWebView2.DOMContentLoaded += DOMContentLoaded;
@@ -189,7 +206,7 @@ public sealed partial class MailRenderingPage : MailRenderingPageAbstract,
Chromium.CoreWebView2.NewWindowRequested -= WindowRequested;
Chromium.CoreWebView2.NewWindowRequested += WindowRequested;
Chromium.Source = new Uri("https://app.reader/reader.html");
Chromium.Source = new Uri("https://wino.mail/reader.html");
}
@@ -206,7 +223,7 @@ public sealed partial class MailRenderingPage : MailRenderingPageAbstract,
private async void WebViewNavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
{
// This is our reader.
if (args.Uri == "https://app.reader/reader.html")
if (args.Uri == "https://wino.mail/reader.html")
return;
// Cancel all external navigations since it's navigating to different address inside the WebView2.