Add PreviewerApp and provide extensions for displaying previewer.

This commit is contained in:
Bebo-Maker
2022-03-16 22:48:07 +01:00
parent b5645bfbd5
commit 1311b6db32
3 changed files with 82 additions and 0 deletions
@@ -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
View File
@@ -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
View File
@@ -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();
}
}
}