Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions office_plugin/release/OfficePluginSetup-2.4.0.exe
Git LFS file not shown
2 changes: 1 addition & 1 deletion office_plugin/release/OfficePluginSetup-2.4.0.exe.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0e741fd63882c3faa02ed2ec1be2f2298f55ec68b49c35de38822a3e98dfb14f OfficePluginSetup-2.4.0.exe
df536a2a52010019cb3c13f0f038efe7c7acbac71c2f1c8097af62ae3b267100 OfficePluginSetup-2.4.0.exe
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));
}
}
Expand All @@ -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)
Expand All @@ -160,6 +154,11 @@ private async Task<T> RunOnUiThreadAsync<T>(Func<WebView2, Task<T>> 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<T>();
host.BeginInvoke(new Action(async () =>
{
Expand All @@ -181,6 +180,37 @@ private async Task<T> RunOnUiThreadAsync<T>(Func<WebView2, Task<T>> 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<object?>();
Expand Down