Previewer improvements

This commit is contained in:
MarcinZiabek
2022-03-31 23:11:34 +02:00
parent c137bcfcde
commit 9e7a1ed49d
7 changed files with 65 additions and 77 deletions
+3 -34
View File
@@ -1,14 +1,10 @@
using System.Net.Http.Headers;
using Avalonia.Media;
using QuestPDF.Fluent;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using QuestPDF.Previewer;
using QuestPDF.ReportSample;
using QuestPDF.ReportSample.Layouts;
using Colors = QuestPDF.Helpers.Colors;
ImagePlaceholder.Solid = true;
//ImagePlaceholder.Solid = true;
// var model = DataSource.GetReport();
// var report = new StandardReport(model);
@@ -51,16 +47,6 @@ Document
});
x.Item().Text("Modify this line and the preview should show your changes instantly.");
// for testing exception handling
// try
// {
// throw new ArgumentException("This file does not exists... peace.png");
// }
// catch (Exception e)
// {
// throw new FileNotFoundException("This is the top exception!", e);
// }
});
page.Footer()
@@ -71,22 +57,5 @@ Document
x.CurrentPageNumber();
});
});
container.Page(page =>
{
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.DefaultTextStyle(x => x.FontSize(20));
page.Content()
.PaddingVertical(1, Unit.Centimetre)
.Column(x =>
{
x.Spacing(20);
foreach (var i in Enumerable.Range(0, 10))
x.Item().Background(Colors.Grey.Lighten2).Height(80);
});
});
})
.ShowInPreviewer().Wait();
.ShowInPreviewer();
@@ -8,7 +8,6 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\QuestPDF.Previewer\QuestPDF.Previewer.csproj" />
<ProjectReference Include="..\QuestPDF.ReportSample\QuestPDF.ReportSample.csproj" />
<ProjectReference Include="..\QuestPDF\QuestPDF.csproj" />
</ItemGroup>
+3 -3
View File
@@ -32,7 +32,7 @@ class CommunicationService
Application = builder.Build();
Application.MapGet("ping", () => Results.Ok());
Application.MapGet("version", () => Results.Ok(GetType().Assembly.GetName().Version));
Application.MapGet("version", () => Results.Json(GetType().Assembly.GetName().Version));
Application.MapPost("update/preview", HandleUpdatePreview);
return Application.RunAsync($"http://localhost:{port}/");
@@ -58,8 +58,8 @@ class CommunicationService
return new PreviewPage(picture, page.Width, page.Height);
})
.ToList();
OnDocumentRefreshed(pages);
Task.Run(() => OnDocumentRefreshed(pages));
return Results.Ok();
}
}
+32 -20
View File
@@ -1,17 +1,32 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:previewer="clr-namespace:QuestPDF.Previewer"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="QuestPDF.Previewer.PreviewerWindow"
WindowStartupLocation="CenterScreen"
ExtendClientAreaToDecorationsHint="true"
ExtendClientAreaTitleBarHeightHint="-1"
Background="#666"
Icon="/Resources/Logo.png"
UseLayoutRounding="True"
Title="QuestPDF Document Preview">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:previewer="clr-namespace:QuestPDF.Previewer"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="QuestPDF.Previewer.PreviewerWindow"
WindowStartupLocation="CenterScreen"
ExtendClientAreaToDecorationsHint="true"
ExtendClientAreaTitleBarHeightHint="-1"
Background="#666"
Icon="/Resources/Logo.png"
UseLayoutRounding="True"
Title="QuestPDF Document Preview">
<Window.Styles>
<Style Selector="Button.actions">
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Padding" Value="10"/>
<Setter Property="CornerRadius" Value="100"/>
<Setter Property="Background" Value="#888"/>
</Style>
<Style Selector="Button:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="#999"/>
</Style>
</Window.Styles>
<Panel>
<Grid>
<Grid.RowDefinitions>
@@ -35,9 +50,8 @@
ScrollViewportSize="{Binding ScrollViewportSize, Mode=OneWayToSource}"
Pages="{Binding Pages, Mode=OneWay}" />
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" VerticalAlignment="Bottom" Spacing="16" Margin="32" ZIndex="100">
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left"
Padding="10" CornerRadius="100"
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" VerticalAlignment="Bottom" Spacing="16" Margin="32">
<Button Classes="actions"
Command="{Binding ShowPdfCommand, Mode=OneTime}"
ToolTip.Tip="Generates PDF file and shows it in the default browser. Useful for testing compatibility and interactive links.">
<Viewbox Width="24" Height="24">
@@ -47,8 +61,7 @@
</Viewbox>
</Button>
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left"
Padding="10" CornerRadius="100"
<Button Classes="actions"
Command="{Binding ShowDocumentationCommand, Mode=OneTime}"
ToolTip.Tip="Opens official QuestPDF documentation">
<Viewbox Width="24" Height="24">
@@ -58,8 +71,7 @@
</Viewbox>
</Button>
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left"
Padding="10" CornerRadius="100"
<Button Classes="actions"
Command="{Binding SponsorProjectCommand, Mode=OneTime}"
ToolTip.Tip="Do you like QuestPDF? Please consider sponsoring the project. It really helps!">
<Viewbox Width="24" Height="24">
+6 -1
View File
@@ -10,7 +10,12 @@ namespace QuestPDF.Previewer
{
public static class Extensions
{
public static async Task ShowInPreviewer(this IDocument document, int port = 12500)
public static void ShowInPreviewer(this IDocument document, int port = 12500)
{
document.ShowInPreviewerAsync(port).ConfigureAwait(true).GetAwaiter().GetResult();
}
public static async Task ShowInPreviewerAsync(this IDocument document, int port = 12500)
{
var previewerService = new PreviewerService(port);
await previewerService.Connect();
@@ -12,15 +12,10 @@ namespace QuestPDF.Previewer
public class Page
{
public string Id { get; }
public string Id { get; } = Guid.NewGuid().ToString("N");
public float Width { get; init; }
public float Height { get; init; }
public Page()
{
Id = Guid.NewGuid().ToString("N");
}
}
}
}
+20 -12
View File
@@ -19,22 +19,25 @@ namespace QuestPDF.Previewer
HttpClient = new()
{
BaseAddress = new Uri($"http://localhost:{port}/"),
Timeout = TimeSpan.FromMilliseconds(100)
Timeout = TimeSpan.FromMilliseconds(250)
};
}
public async Task Connect()
{
var isConnected = await TestConnection();
if (isConnected)
return;
var isAvailable = await IsPreviewerAvailable();
StartPreviewer();
await WaitForConnection();
if (!isAvailable)
{
StartPreviewer();
await WaitForConnection();
}
var previewerVersion = await GetPreviewerVersion();
CheckVersionCompatibility(previewerVersion);
}
private async Task<bool> TestConnection()
private async Task<bool> IsPreviewerAvailable()
{
try
{
@@ -50,8 +53,7 @@ namespace QuestPDF.Previewer
private async Task<Version> GetPreviewerVersion()
{
var result = await HttpClient.GetAsync("/version");
var value = await result.Content.ReadAsStringAsync();
return Version.Parse(value);
return await result.Content.ReadFromJsonAsync<Version>();
}
private static void StartPreviewer()
@@ -72,13 +74,19 @@ namespace QuestPDF.Previewer
}
catch
{
throw new Exception("Cannot start the QuestPDF Previewer tool. Please install it by executing in terminal the following command: 'dotnet tool install --global QuestPDF.Previewer'.");
throw new Exception("Cannot start the QuestPDF Previewer tool. " +
"Please install it by executing in terminal the following command: 'dotnet tool install --global QuestPDF.Previewer'.");
}
}
private void CheckVersionCompatibility(Version version)
{
if (version.Major == 2022 && version.Minor == 4)
return;
throw new Exception($"Previewer version is not compatible. Possible solutions: " +
$"1) Update the QuestPDF library to newer version. " +
$"2) Update the QuestPDF previewer tool using the following command: 'dotnet tool update --global QuestPDF.Previewer --version 2022.4'");
}
private async Task WaitForConnection()
@@ -87,7 +95,7 @@ namespace QuestPDF.Previewer
{
await Task.Delay(TimeSpan.FromSeconds(1));
var isConnected = await TestConnection();
var isConnected = await IsPreviewerAvailable();
if (isConnected)
break;