Forráskód Böngészése

Merge pull request #155 from Bebo-Maker/previewer

Previewer - Fix multiple small issues
Marcin Ziąbek 3 éve
szülő
commit
545b76fd40

+ 3 - 3
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 =>

+ 14 - 0
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<string>());
+                return;
+            }
+
             AppBuilder
                 .Configure(() => new PreviewerApp()
                 {

+ 2 - 1
QuestPDF.Previewer/InteractiveCanvas.cs

@@ -27,7 +27,8 @@ class InteractiveCanvas : ICustomDrawOperation
 
     public float TotalPagesHeight => Pages.Sum(x => x.Size.Height) + (Pages.Count - 1) * PageSpacing;
     public float TotalHeight => TotalPagesHeight + SafeZone * 2 / Scale;
-    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;
 

+ 23 - 18
QuestPDF.Previewer/PreviewerWindow.axaml.cs

@@ -23,55 +23,55 @@ namespace QuestPDF.Previewer
             get => GetValue(DocumentProperty);
             set => SetValue(DocumentProperty, value);
         }
-        
-        
-        
+
         public static readonly StyledProperty<float> CurrentScrollProperty = AvaloniaProperty.Register<PreviewerWindow, float>(nameof(CurrentScroll));
-        
+
         public float CurrentScroll
         {
             get => GetValue(CurrentScrollProperty);
             set => SetValue(CurrentScrollProperty, value);
         }
-        
+
         public static readonly StyledProperty<float> ScrollViewportSizeProperty = AvaloniaProperty.Register<PreviewerWindow, float>(nameof(ScrollViewportSize));
-        
+
         public float ScrollViewportSize
         {
             get => GetValue(ScrollViewportSizeProperty);
             set => SetValue(ScrollViewportSizeProperty, value);
         }
-        
+
         public static readonly StyledProperty<bool> VerticalScrollbarVisibleProperty = AvaloniaProperty.Register<PreviewerWindow, bool>(nameof(VerticalScrollbarVisible));
-        
+
         public bool VerticalScrollbarVisible
         {
             get => GetValue(VerticalScrollbarVisibleProperty);
             set => SetValue(VerticalScrollbarVisibleProperty, value);
         }
-        
-        
+
         public PreviewerWindow()
         {
             InitializeComponent();
 
-            ScrollViewportSizeProperty.Changed.Subscribe(_ =>
-            {
-                VerticalScrollbarVisible = ScrollViewportSize < 1;
-            });
-            
+            ScrollViewportSizeProperty.Changed
+                .Subscribe(e => Dispatcher.UIThread.Post(() =>
+                {
+                    VerticalScrollbarVisible = e.NewValue.Value < 1;
+                }));
+                
             // this.FindControl<Button>("GeneratePdf")
             //     .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(() =>
@@ -80,5 +80,10 @@ namespace QuestPDF.Previewer
                 _ = Task.Run(() => DocumentRenderer.UpdateDocument(document));
             });
         }
+
+        private void InitializeComponent()
+        {
+            AvaloniaXamlLoader.Load(this);
+        }
     }
 }