fix(converters): fix HtmlProvider WebView2 thread crash and add MarkdownProvider for pure .NET conversions
This commit is contained in:
parent
63d50c165d
commit
4e5285e27c
7 changed files with 447 additions and 28 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Threading;
|
||||
using Everything2Everything.Core.Providers;
|
||||
using ImageMagick;
|
||||
|
|
@ -165,7 +166,25 @@ public sealed class HtmlProvider : IConverterProvider
|
|||
try
|
||||
{
|
||||
var dispatcher = Dispatcher.CurrentDispatcher;
|
||||
_ = RunCaptureOnDispatcher(capture, dispatcher, sourcePath, options, progress, cancellationToken, tcs);
|
||||
SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(dispatcher));
|
||||
|
||||
dispatcher.InvokeAsync(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var bytes = await RunCaptureOnDispatcher(capture, sourcePath, options, progress, cancellationToken);
|
||||
tcs.TrySetResult(bytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
dispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
|
||||
}
|
||||
});
|
||||
|
||||
Dispatcher.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -181,16 +200,15 @@ public sealed class HtmlProvider : IConverterProvider
|
|||
return tcs.Task;
|
||||
}
|
||||
|
||||
private static async Task RunCaptureOnDispatcher(
|
||||
private static async Task<byte[]> RunCaptureOnDispatcher(
|
||||
Func<CoreWebView2, IProgress<double>?, Task<byte[]>> capture,
|
||||
Dispatcher dispatcher,
|
||||
string sourcePath,
|
||||
ConvertOptions options,
|
||||
IProgress<double>? progress,
|
||||
CancellationToken cancellationToken,
|
||||
TaskCompletionSource<byte[]> tcs)
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
CoreWebView2Controller? controller = null;
|
||||
HwndSource? hwndSource = null;
|
||||
try
|
||||
{
|
||||
var userDataFolder = Path.Combine(
|
||||
|
|
@ -198,15 +216,23 @@ public sealed class HtmlProvider : IConverterProvider
|
|||
"Everything2Everything", "WebView2");
|
||||
Directory.CreateDirectory(userDataFolder);
|
||||
|
||||
var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder).ConfigureAwait(true);
|
||||
var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
|
||||
progress?.Report(0.15);
|
||||
|
||||
controller = await env.CreateCoreWebView2ControllerAsync(new IntPtr(-3)).ConfigureAwait(true);
|
||||
|
||||
int width = options.HtmlRender.ViewportWidth > 0 ? options.HtmlRender.ViewportWidth : 1280;
|
||||
int height = options.HtmlRender.ViewportHeight ?? 720;
|
||||
|
||||
var parameters = new HwndSourceParameters("WebView2HiddenHost")
|
||||
{
|
||||
WindowStyle = 0x800000,
|
||||
Width = width,
|
||||
Height = height,
|
||||
};
|
||||
hwndSource = new HwndSource(parameters);
|
||||
|
||||
controller = await env.CreateCoreWebView2ControllerAsync(hwndSource.Handle);
|
||||
controller.Bounds = new System.Drawing.Rectangle(0, 0, width, height);
|
||||
controller.IsVisible = false;
|
||||
controller.IsVisible = true;
|
||||
|
||||
var web = controller.CoreWebView2;
|
||||
progress?.Report(0.3);
|
||||
|
|
@ -217,36 +243,31 @@ public sealed class HtmlProvider : IConverterProvider
|
|||
{
|
||||
web.NavigationCompleted -= navHandler!;
|
||||
if (e.IsSuccess) navTcs.TrySetResult(true);
|
||||
else navTcs.TrySetException(new InvalidOperationException(
|
||||
$"내비게이션 실패: {e.WebErrorStatus}"));
|
||||
else navTcs.TrySetException(new InvalidOperationException($"내비게이션 실패: {e.WebErrorStatus}"));
|
||||
};
|
||||
web.NavigationCompleted += navHandler;
|
||||
|
||||
var fileUri = new Uri(sourcePath).AbsoluteUri;
|
||||
var fileUri = new Uri(Path.GetFullPath(sourcePath)).AbsoluteUri;
|
||||
web.Navigate(fileUri);
|
||||
|
||||
using (cancellationToken.Register(() => navTcs.TrySetCanceled()))
|
||||
{
|
||||
await navTcs.Task.ConfigureAwait(true);
|
||||
await navTcs.Task;
|
||||
}
|
||||
progress?.Report(0.5);
|
||||
|
||||
if (options.HtmlRender.WaitMilliseconds > 0)
|
||||
await Task.Delay(options.HtmlRender.WaitMilliseconds, cancellationToken).ConfigureAwait(true);
|
||||
await Task.Delay(options.HtmlRender.WaitMilliseconds, cancellationToken);
|
||||
|
||||
progress?.Report(0.65);
|
||||
|
||||
var bytes = await capture(web, progress).ConfigureAwait(true);
|
||||
tcs.TrySetResult(bytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
var bytes = await capture(web, progress);
|
||||
return bytes;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { controller?.Close(); } catch { }
|
||||
dispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
|
||||
try { hwndSource?.Dispose(); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue