Migrate mail printing to WinUI print preview

This commit is contained in:
Burak Kaan Köse
2026-04-11 15:07:22 +02:00
parent 24626d1c31
commit e206368801
12 changed files with 565 additions and 828 deletions
-66
View File
@@ -1,66 +0,0 @@
<ContentDialog
x:Class="Wino.Mail.WinUI.Dialogs.PrintDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:printing="using:Wino.Core.Domain.Models.Printing"
Title="Print Settings"
MinWidth="400"
MinHeight="300"
DefaultButton="Primary"
Loaded="PrintDialog_Loaded"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
PrimaryButtonText="Print"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick"
SecondaryButtonText="Cancel"
Style="{StaticResource WinoDialogStyle}"
mc:Ignorable="d">
<StackPanel Spacing="20">
<!-- Printer Selection -->
<ComboBox
x:Name="PrinterComboBox"
HorizontalAlignment="Stretch"
Header="Printer"
SelectedItem="{x:Bind PrintSettings.PrinterName, Mode=TwoWay}"
SelectionChanged="PrinterComboBox_SelectionChanged" />
<!-- Copies -->
<NumberBox
Header="Copies"
Maximum="999"
Minimum="1"
SpinButtonPlacementMode="Inline"
Value="{x:Bind PrintSettings.Copies, Mode=TwoWay}" />
<!-- Orientation -->
<RadioButtons
x:Name="OrientationRadioButtons"
Header="Orientation"
SelectionChanged="OrientationRadio_SelectionChanged">
<RadioButton Content="Portrait" />
<RadioButton Content="Landscape" />
</RadioButtons>
<!-- Print Options -->
<StackPanel Spacing="8">
<TextBlock
Margin="0,0,0,8"
Style="{ThemeResource BodyStrongTextBlockStyle}"
Text="Options" />
<Border
Padding="16,12"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="1"
CornerRadius="6">
<StackPanel Spacing="8">
<CheckBox Content="Print backgrounds" IsChecked="{x:Bind PrintSettings.ShouldPrintBackgrounds, Mode=TwoWay}" />
<CheckBox Content="Print headers and footers" IsChecked="{x:Bind PrintSettings.ShouldPrintHeaderAndFooter, Mode=TwoWay}" />
</StackPanel>
</Border>
</StackPanel>
</StackPanel>
</ContentDialog>
-170
View File
@@ -1,170 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Controls;
using Serilog;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Models.Printing;
using Wino.Mail.WinUI.Helpers;
namespace Wino.Mail.WinUI.Dialogs;
/// <summary>
/// Custom print dialog for configuring WebView2 print settings.
/// </summary>
public sealed partial class PrintDialog : ContentDialog
{
public WebView2PrintSettingsModel PrintSettings { get; set; } = new WebView2PrintSettingsModel();
public PrintDialog()
{
this.InitializeComponent();
}
/// <summary>
/// Initializes the dialog with existing print settings.
/// </summary>
/// <param name="printSettings">The initial print settings to load.</param>
public PrintDialog(WebView2PrintSettingsModel printSettings = default!)
{
if (printSettings != null) PrintSettings = printSettings;
this.InitializeComponent();
}
private void PrintDialog_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) => LoadSettingsToUI(PrintSettings);
private void OrientationRadio_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is RadioButtons radioButtons)
{
PrintSettings.Orientation = (PrintOrientation)radioButtons.SelectedIndex;
}
}
private void PrinterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ComboBox comboBox && comboBox.SelectedItem != null)
{
PrintSettings.PrinterName = comboBox.SelectedItem.ToString();
}
}
/// <summary>
/// Sets the list of available printers for the dialog.
/// </summary>
/// <param name="printers">List of available printer names.</param>
public void SetAvailablePrinters(IEnumerable<string> printers)
{
var printerList = printers?.ToList() ?? new List<string>();
if (this.FindName("PrinterComboBox") is ComboBox printerComboBox)
{
printerComboBox.ItemsSource = printerList;
if (printerList.Any())
{
// Set to first printer or to the one in settings
var targetPrinter = !string.IsNullOrEmpty(PrintSettings.PrinterName)
? PrintSettings.PrinterName
: printerList.First();
var index = printerList.IndexOf(targetPrinter);
printerComboBox.SelectedIndex = index >= 0 ? index : 0;
// Update the settings model with the selected printer
PrintSettings.PrinterName = printerComboBox.SelectedItem?.ToString() ?? string.Empty;
}
}
}
/// <summary>
/// Loads available printers asynchronously and sets them in the dialog.
/// </summary>
public async Task LoadAvailablePrintersAsync()
{
try
{
var printers = await Task.Run(() =>
{
return InstalledPrinterHelper.GetInstalledPrinters().AsEnumerable();
});
SetAvailablePrinters(printers);
}
catch (System.Exception ex)
{
// Log the exception if logging is available
Log.Error(ex, "Error getting available printers");
// Set empty list if printer discovery fails
SetAvailablePrinters(Enumerable.Empty<string>());
}
}
private void LoadSettingsToUI(WebView2PrintSettingsModel settings)
{
if (settings == null) return;
// Only handle orientation manually since other properties are bound via x:Bind
if (this.FindName("OrientationRadioButtons") is RadioButtons orientationRadio)
{
orientationRadio.SelectedIndex = (int)settings.Orientation;
}
}
private void UpdateSettingsFromUI()
{
// Most properties are bound via x:Bind, only handle orientation manually
if (this.FindName("OrientationRadioButtons") is RadioButtons orientationRadio)
{
PrintSettings.Orientation = (PrintOrientation)orientationRadio.SelectedIndex;
}
// Also update printer name from ComboBox since it uses ItemsSource binding
if (this.FindName("PrinterComboBox") is ComboBox printerComboBox &&
printerComboBox.SelectedItem != null)
{
PrintSettings.PrinterName = printerComboBox.SelectedItem.ToString();
}
}
/// <summary>
/// Validates the current print settings before closing the dialog.
/// </summary>
/// <returns>True if settings are valid, false otherwise.</returns>
private bool ValidateSettings()
{
// Check if a printer is selected
if (this.FindName("PrinterComboBox") is ComboBox printerComboBox &&
printerComboBox.SelectedItem == null)
{
return false;
}
// Copies validation is handled by the bound property with validation in the model
if (PrintSettings.Copies <= 0)
{
return false;
}
return true;
}
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
// Update settings from UI before validation
UpdateSettingsFromUI();
// Validate settings before closing
if (!ValidateSettings())
{
args.Cancel = true;
}
}
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
// Cancel was clicked, no validation needed
}
}