From 040c47e01760515fb320b9eded87208ff02091bc Mon Sep 17 00:00:00 2001 From: Bebo-Maker Date: Wed, 23 Mar 2022 08:35:28 +0100 Subject: [PATCH 1/4] Fix issue when showing previewer multiple times. Previously when calling ShowPreviewer in the same process multiple times lead to an Exception inside Avalonia. --- QuestPDF.Previewer/DocumentPreviewerExtensions.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/QuestPDF.Previewer/DocumentPreviewerExtensions.cs b/QuestPDF.Previewer/DocumentPreviewerExtensions.cs index 1a335ac..29f3f70 100644 --- a/QuestPDF.Previewer/DocumentPreviewerExtensions.cs +++ b/QuestPDF.Previewer/DocumentPreviewerExtensions.cs @@ -1,4 +1,5 @@ using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; using QuestPDF.Infrastructure; namespace QuestPDF.Previewer @@ -17,6 +18,19 @@ namespace QuestPDF.Previewer { ArgumentNullException.ThrowIfNull(document); + //Currently there is no way to unitialize a previously run avalonia app. + //So we need to check if the previewer was already run and show the window again. + if(Application.Current?.ApplicationLifetime is ClassicDesktopStyleApplicationLifetime desktop) + { + desktop.MainWindow = new PreviewerWindow() + { + Document = document, + }; + desktop.MainWindow.Show(); + desktop.Start(Array.Empty()); + return; + } + AppBuilder .Configure(() => new PreviewerApp() { From 828bb867370715e974022c15e681db2782a09bfa Mon Sep 17 00:00:00 2001 From: Bebo-Maker Date: Wed, 23 Mar 2022 08:35:53 +0100 Subject: [PATCH 2/4] Fix MaxWidth calculation when pages are empty. --- QuestPDF.Previewer/InteractiveCanvas.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QuestPDF.Previewer/InteractiveCanvas.cs b/QuestPDF.Previewer/InteractiveCanvas.cs index 63ca72b..0494878 100644 --- a/QuestPDF.Previewer/InteractiveCanvas.cs +++ b/QuestPDF.Previewer/InteractiveCanvas.cs @@ -26,7 +26,7 @@ class InteractiveCanvas : ICustomDrawOperation private const float SafeZone = 50f; public float TotalHeight => Pages.Sum(x => x.Size.Height) + (Pages.Count - 1) * PageSpacing; - public float MaxWidth => Pages.Max(x => x.Size.Width); + public float MaxWidth => Pages.Any() ? Pages.Max(x => x.Size.Width) : 0; public float MaxTranslateY => -(Height / 2 - SafeZone) / Scale; public float MinTranslateY => (Height / 2 - SafeZone) / Scale - TotalHeight; From 0abb67b7483ffaab2d461017fbd78a5bbad25c91 Mon Sep 17 00:00:00 2001 From: Bebo-Maker Date: Wed, 23 Mar 2022 09:22:46 +0100 Subject: [PATCH 3/4] Unregister HotReload handler when window is closed --- QuestPDF.Previewer.Examples/Program.cs | 6 +++--- QuestPDF.Previewer/PreviewerWindow.axaml.cs | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/QuestPDF.Previewer.Examples/Program.cs b/QuestPDF.Previewer.Examples/Program.cs index fb2d971..7e218a8 100644 --- a/QuestPDF.Previewer.Examples/Program.cs +++ b/QuestPDF.Previewer.Examples/Program.cs @@ -7,9 +7,9 @@ using QuestPDF.ReportSample; using QuestPDF.ReportSample.Layouts; using Colors = QuestPDF.Helpers.Colors; -// var model = DataSource.GetReport(); -// var report = new StandardReport(model); -// report.ShowInPreviewer(); +var model = DataSource.GetReport(); +var report = new StandardReport(model); +report.ShowInPreviewer(); Document .Create(container => diff --git a/QuestPDF.Previewer/PreviewerWindow.axaml.cs b/QuestPDF.Previewer/PreviewerWindow.axaml.cs index 27de11e..fa8a176 100644 --- a/QuestPDF.Previewer/PreviewerWindow.axaml.cs +++ b/QuestPDF.Previewer/PreviewerWindow.axaml.cs @@ -65,14 +65,16 @@ namespace QuestPDF.Previewer // .Click += (_, _) => _ = PreviewerUtils.SavePdfWithDialog(Document, this); DocumentProperty.Changed.Subscribe(v => Task.Run(() => DocumentRenderer.UpdateDocument(v.NewValue.Value))); - HotReloadManager.UpdateApplicationRequested += (_, _) => InvalidatePreview(); + HotReloadManager.UpdateApplicationRequested += InvalidatePreview; } - private void InitializeComponent() + protected override void OnClosed(EventArgs e) { - AvaloniaXamlLoader.Load(this); + HotReloadManager.UpdateApplicationRequested -= InvalidatePreview; + base.OnClosed(e); } + private void InvalidatePreview(object? sender, EventArgs e) => InvalidatePreview(); private void InvalidatePreview() { Dispatcher.UIThread.Post(() => @@ -81,5 +83,10 @@ namespace QuestPDF.Previewer _ = Task.Run(() => DocumentRenderer.UpdateDocument(document)); }); } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } } } From 734d116bfe1e638b27134556b9517738c678968e Mon Sep 17 00:00:00 2001 From: Bebo-Maker Date: Wed, 23 Mar 2022 12:58:58 +0100 Subject: [PATCH 4/4] Fix scroll viewer visibility. --- QuestPDF.Previewer/PreviewerWindow.axaml.cs | 39 +++++++++++---------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/QuestPDF.Previewer/PreviewerWindow.axaml.cs b/QuestPDF.Previewer/PreviewerWindow.axaml.cs index fa8a176..5651a2e 100644 --- a/QuestPDF.Previewer/PreviewerWindow.axaml.cs +++ b/QuestPDF.Previewer/PreviewerWindow.axaml.cs @@ -23,44 +23,45 @@ namespace QuestPDF.Previewer get => GetValue(DocumentProperty); set => SetValue(DocumentProperty, value); } - - - - public static readonly StyledProperty CurrentScrollProperty = AvaloniaProperty.Register(nameof(CurrentScroll)); - + + public static readonly StyledProperty CurrentScrollProperty = + AvaloniaProperty.Register(nameof(CurrentScroll)); + public float CurrentScroll { get => GetValue(CurrentScrollProperty); set => SetValue(CurrentScrollProperty, value); } - - public static readonly StyledProperty ScrollViewportSizeProperty = AvaloniaProperty.Register(nameof(ScrollViewportSize)); - + + public static readonly StyledProperty ScrollViewportSizeProperty = + AvaloniaProperty.Register(nameof(ScrollViewportSize)); + public float ScrollViewportSize { get => GetValue(ScrollViewportSizeProperty); set => SetValue(ScrollViewportSizeProperty, value); } - - public static readonly StyledProperty VerticalScrollbarVisibleProperty = AvaloniaProperty.Register(nameof(VerticalScrollbarVisible)); - + + public static readonly StyledProperty VerticalScrollbarVisibleProperty = + AvaloniaProperty.Register(nameof(VerticalScrollbarVisible)); + public bool VerticalScrollbarVisible { get => GetValue(VerticalScrollbarVisibleProperty); set => SetValue(VerticalScrollbarVisibleProperty, value); } - - + public PreviewerWindow() { InitializeComponent(); - ScrollViewportSizeProperty.Changed.Subscribe(_ => - { - VerticalScrollbarVisible = ScrollViewportSize < 1; - Debug.WriteLine($"Scrollbar visible: {VerticalScrollbarVisible}"); - }); - + ScrollViewportSizeProperty.Changed + .Subscribe(e => Dispatcher.UIThread.Post(() => + { + VerticalScrollbarVisible = e.NewValue.Value < 1; + Debug.WriteLine($"Scrollbar visible: {VerticalScrollbarVisible}"); + })); + // this.FindControl