diff --git a/IoListTestingWindow.PrintPreview.cs b/IoListTestingWindow.PrintPreview.cs new file mode 100644 index 0000000..1db01c0 --- /dev/null +++ b/IoListTestingWindow.PrintPreview.cs @@ -0,0 +1,317 @@ +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Threading; +using ArIED61850Tester.Services.IoTesting; +using Microsoft.Win32; + +namespace ArIED61850Tester; + +public partial class IoListTestingWindow +{ + private bool _printPreviewInstalled; + private bool _printPreviewActive; + private DataGrid? _signalWorkspaceGrid; + private Grid? _printPreviewHost; + private WebBrowser? _printPreviewBrowser; + private Button? _printPreviewToggle; + private TextBlock? _printPreviewTitle; + private TextBlock? _printPreviewSubtitle; + private DispatcherTimer? _preparationStateGuard; + + protected override void OnContentRendered(EventArgs e) + { + base.OnContentRendered(e); + if (_printPreviewInstalled) + return; + + InstallPerIedPrintPreview(); + InstallPreparationStateGuard(); + PropertyChanged += PrintPreviewWindow_PropertyChanged; + Session.PropertyChanged += PrintPreviewSession_PropertyChanged; + Closed += PrintPreviewWindow_Closed; + _printPreviewInstalled = true; + } + + private void InstallPerIedPrintPreview() + { + if (Content is not Grid root) + return; + + var middle = root.Children + .OfType() + .FirstOrDefault(child => Grid.GetRow(child) == 2); + var workspaceBorder = middle?.Children + .OfType() + .FirstOrDefault(child => Grid.GetColumn(child) == 2); + if (workspaceBorder?.Child is not Grid workspaceGrid) + return; + + _signalWorkspaceGrid = workspaceGrid.Children.OfType().FirstOrDefault(); + if (_signalWorkspaceGrid == null) + return; + + RemoveMainPreparationSurface(workspaceGrid); + InstallPreviewToggle(root); + _printPreviewHost = BuildPrintPreviewHost(); + Grid.SetRow(_printPreviewHost, Grid.GetRow(_signalWorkspaceGrid)); + workspaceGrid.Children.Add(_printPreviewHost); + } + + private static void RemoveMainPreparationSurface(Grid workspaceGrid) + { + var preparationSurface = workspaceGrid.Children + .OfType() + .FirstOrDefault(border => Grid.GetRow(border) == 1 && + border.Descendants().Any(progress => progress.IsIndeterminate)); + if (preparationSurface != null) + workspaceGrid.Children.Remove(preparationSurface); + + if (workspaceGrid.RowDefinitions.Count > 1) + workspaceGrid.RowDefinitions[1].Height = new GridLength(0); + if (workspaceGrid.RowDefinitions.Count > 2) + workspaceGrid.RowDefinitions[2].Height = new GridLength(10); + } + + private void InstallPreviewToggle(Grid root) + { + var headerBorder = root.Children + .OfType() + .FirstOrDefault(child => Grid.GetRow(child) == 0); + var headerGrid = headerBorder?.Child as Grid; + var actions = headerGrid?.Children.OfType().FirstOrDefault(); + if (actions == null) + return; + + _printPreviewToggle = BuildButton("Print Preview", TogglePrintPreview_Click, primary: false); + _printPreviewToggle.ToolTip = "Preview the selected IED report in the workspace"; + var pdfButtonIndex = actions.Children + .OfType