diff --git a/office_plugin/release/OfficePluginSetup-2.4.0.exe b/office_plugin/release/OfficePluginSetup-2.4.0.exe index a5817354..d763db16 100644 --- a/office_plugin/release/OfficePluginSetup-2.4.0.exe +++ b/office_plugin/release/OfficePluginSetup-2.4.0.exe @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0e741fd63882c3faa02ed2ec1be2f2298f55ec68b49c35de38822a3e98dfb14f -size 7317741 +oid sha256:df536a2a52010019cb3c13f0f038efe7c7acbac71c2f1c8097af62ae3b267100 +size 7317889 diff --git a/office_plugin/release/OfficePluginSetup-2.4.0.exe.sha256 b/office_plugin/release/OfficePluginSetup-2.4.0.exe.sha256 index 719afbcf..b45a4689 100644 --- a/office_plugin/release/OfficePluginSetup-2.4.0.exe.sha256 +++ b/office_plugin/release/OfficePluginSetup-2.4.0.exe.sha256 @@ -1 +1 @@ -0e741fd63882c3faa02ed2ec1be2f2298f55ec68b49c35de38822a3e98dfb14f OfficePluginSetup-2.4.0.exe +df536a2a52010019cb3c13f0f038efe7c7acbac71c2f1c8097af62ae3b267100 OfficePluginSetup-2.4.0.exe diff --git a/office_plugin/src/LaTeXSnipper.OfficePlugin.Rendering/WebView2MathJaxJavaScriptRuntime.cs b/office_plugin/src/LaTeXSnipper.OfficePlugin.Rendering/WebView2MathJaxJavaScriptRuntime.cs index 753739b9..577d03c6 100644 --- a/office_plugin/src/LaTeXSnipper.OfficePlugin.Rendering/WebView2MathJaxJavaScriptRuntime.cs +++ b/office_plugin/src/LaTeXSnipper.OfficePlugin.Rendering/WebView2MathJaxJavaScriptRuntime.cs @@ -102,12 +102,12 @@ public void Dispose() if (_hostReady.Task.Status == TaskStatus.RanToCompletion) { Form host = _hostReady.Task.Result; - if (!host.IsDisposed) + if (!host.IsDisposed && host.IsHandleCreated) { host.BeginInvoke(new Action(() => { - host.Close(); _applicationContext?.ExitThread(); + host.Dispose(); })); } } @@ -117,26 +117,20 @@ private void RunUiThread() { try { - var hostForm = new Form - { - ShowInTaskbar = false, - StartPosition = FormStartPosition.Manual, - Width = 1, - Height = 1, - Opacity = 0 - }; + var hostForm = new HiddenWebViewHostForm(); var webView = new WebView2 { Dock = DockStyle.Fill }; hostForm.Controls.Add(webView); - _applicationContext = new ApplicationContext(hostForm); - hostForm.Load += (sender, args) => - { - hostForm.Hide(); - _hostReady.TrySetResult(hostForm); - _webViewReady.TrySetResult(webView); - }; + + // WebView2 needs real Win32 handles and an STA message loop, but the + // renderer host must never become a user-visible top-level window. + _ = hostForm.Handle; + _ = webView.Handle; + _applicationContext = new ApplicationContext(); + _hostReady.TrySetResult(hostForm); + _webViewReady.TrySetResult(webView); Application.Run(_applicationContext); } catch (Exception ex) @@ -160,6 +154,11 @@ private async Task RunOnUiThreadAsync(Func> action, Canc cancellationToken.ThrowIfCancellationRequested(); Form host = await _hostReady.Task.ConfigureAwait(false); WebView2 webView = await _webViewReady.Task.ConfigureAwait(false); + if (host.IsDisposed || !host.IsHandleCreated) + { + throw new InvalidOperationException("MathJax rendering host is unavailable."); + } + var completion = new TaskCompletionSource(); host.BeginInvoke(new Action(async () => { @@ -181,6 +180,37 @@ private async Task RunOnUiThreadAsync(Func> action, Canc } } + private sealed class HiddenWebViewHostForm : Form + { + private const int WsExToolWindow = 0x00000080; + private const int WsExAppWindow = 0x00040000; + + public HiddenWebViewHostForm() + { + ShowInTaskbar = false; + FormBorderStyle = FormBorderStyle.None; + ControlBox = false; + Width = 1; + Height = 1; + } + + protected override CreateParams CreateParams + { + get + { + CreateParams parameters = base.CreateParams; + parameters.ExStyle |= WsExToolWindow; + parameters.ExStyle &= ~WsExAppWindow; + return parameters; + } + } + + protected override void SetVisibleCore(bool value) + { + base.SetVisibleCore(false); + } + } + private static Task NavigateToStringAsync(WebView2 webView, string html) { var completion = new TaskCompletionSource();