Skip to content

Commit

Permalink
Merge pull request #2074 from cwensley/curtis/webview2-initialize-ove…
Browse files Browse the repository at this point in the history
…rride

Allow overriding the default WebView2 initialization
  • Loading branch information
cwensley committed Nov 25, 2021
2 parents 2695f77 + 9cef7cb commit 583b51e
Showing 1 changed file with 17 additions and 6 deletions.
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

0 comments on commit 583b51e

Please sign in to comment.