Browse Source

Add PreviewerApp and provide extensions for displaying previewer.

Bebo-Maker 3 years ago
parent
commit
1311b6db32

+ 31 - 0
QuestPDF.Previewer/DocumentPreviewerExtensions.cs

@@ -0,0 +1,31 @@
+using Avalonia;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Previewer
+{
+    /// <summary>
+    /// Extensions for <see cref="IDocument"/> for previewer
+    /// </summary>
+    public static class DocumentPreviewerExtensions
+    {
+        /// <summary>
+        /// Displays the document in a previewer which supports hot reloading.
+        /// </summary>
+        /// <remarks>
+        /// Intended for development only. Not intended for shipping.
+        /// </remarks>
+        /// <param name="document"></param>
+        public static void ShowInPreviewer(this IDocument document)
+        {
+            ArgumentNullException.ThrowIfNull(document);
+
+            AppBuilder
+                .Configure(() => new PreviewerApp()
+                {
+                    Document = document,
+                })
+                .UsePlatformDetect()
+                .StartWithClassicDesktopLifetime(Array.Empty<string>());
+        }
+    }
+}

+ 7 - 0
QuestPDF.Previewer/PreviewerApp.axaml

@@ -0,0 +1,7 @@
+<Application x:Class="QuestPDF.Previewer.PreviewerApp"
+             xmlns="https://github.com/avaloniaui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
+	<Application.Styles>
+		<FluentTheme />
+	</Application.Styles>
+</Application>

+ 44 - 0
QuestPDF.Previewer/PreviewerApp.axaml.cs

@@ -0,0 +1,44 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Markup.Xaml;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Previewer
+{
+    internal class PreviewerApp : Application
+    {
+        private PreviewerView? _preview;
+
+        public IDocument? Document { get; init; }
+
+        public override void Initialize()
+        {
+            AvaloniaXamlLoader.Load(this);
+        }
+
+        public override void OnFrameworkInitializationCompleted()
+        {
+            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
+            {
+                HotReloadManager.Register(HandleDocumentHotReload);
+
+                _preview = new PreviewerView()
+                {
+                    Document = Document,
+                };
+                desktop.MainWindow = new Window()
+                {
+                    Title = "QuestPDF Document Preview",
+                    Content = _preview,
+                };
+            }
+            base.OnFrameworkInitializationCompleted();
+        }
+
+        private void HandleDocumentHotReload()
+        {
+            _preview?.InvalidatePreview();
+        }
+    }
+}