Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding the default WebView2 initialization #2074

Merged
Merged
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
23 changes: 17 additions & 6 deletions src/Eto.Wpf/Forms/Controls/WebView2Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.IO;
using System.Runtime.Serialization;


#if WINFORMS
using WebView2Control = Microsoft.Web.WebView2.WinForms.WebView2;
using BaseHandler = Eto.WinForms.Forms.WindowsControl<Microsoft.Web.WebView2.WinForms.WebView2, Eto.Forms.WebView, Eto.Forms.WebView.ICallback>;
Expand Down Expand Up @@ -385,10 +386,7 @@ public WebView2InitializationException(string message, Exception inner) : base(m
public class WebView2Handler : BaseHandler, WebView.IHandler
{
bool webView2Ready;
protected bool WebView2Ready
{
get { return webView2Ready; }
}
protected bool WebView2Ready => webView2Ready;

List<Action> delayedActions;

Expand All @@ -397,20 +395,26 @@ public WebView2Handler()
WebView2Loader.EnsureWebView2Runtime();
Control = new WebView2Control();
Control.CoreWebView2InitializationCompleted += Control_CoreWebView2Ready;
InitializeAsync();
}

public static CoreWebView2Environment CoreWebView2Environment;

async void InitializeAsync()
/// <summary>
/// Override to use your own WebView2 initialization, if necessary
/// </summary>
/// <returns>Task</returns>
protected async virtual Task OnInitializeWebView2Async()
{
await Control.EnsureCoreWebView2Async(CoreWebView2Environment);
}

async void InitializeAsync() => await OnInitializeWebView2Async();

protected override void Initialize()
{
base.Initialize();
Size = new Size(100, 100);
InitializeAsync();
}

void Control_CoreWebView2Ready(object sender, CoreWebView2InitializationCompletedEventArgs e)
Expand Down Expand Up @@ -454,6 +458,13 @@ private void CoreWebView2_DocumentTitleChanged(object sender, object e)

protected void RunWhenReady(Action action)
{
if (webView2Ready)
{
// already ready, run now!
action();
return;
}

if (delayedActions == null)
{
delayedActions = new List<Action>();
Expand Down