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

DYN-7156: initialize libview only once #15335

Merged
merged 1 commit into from
Jul 1, 2024
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
70 changes: 39 additions & 31 deletions src/LibraryViewExtensionWebView2/LibraryViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using CoreNodeModels.Properties;
using Dynamo.Extensions;
using Dynamo.LibraryViewExtensionWebView2.Handlers;
Expand All @@ -23,6 +22,7 @@
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels;
using DynamoUtilities;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using Newtonsoft.Json;
Expand Down Expand Up @@ -81,6 +81,7 @@ public class LibraryViewController : IDisposable
private const string CreateNodeInstrumentationString = "Search-NodeAdded";
// TODO remove this when we can control the library state from Dynamo more precisely.
private bool disableObserver = false;
internal AsyncMethodState initState = AsyncMethodState.NotStarted;

private LayoutSpecProvider layoutProvider;
private NodeItemDataProvider nodeProvider;
Expand Down Expand Up @@ -309,41 +310,48 @@ private string ReplaceUrlWithBase64Image(string html, string minifiedURL, bool m

async void InitializeAsync()
{
try
{
var absolutePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
@"runtimes\win-x64\native");
CoreWebView2Environment.SetLoaderDllFolderPath(absolutePath);
}
catch (InvalidOperationException e)
if (initState == AsyncMethodState.NotStarted)
{
LogToDynamoConsole("WebView2Loader.dll is already loaded successfully.");
}

browser.CoreWebView2InitializationCompleted += Browser_CoreWebView2InitializationCompleted;
initState = AsyncMethodState.Started;

if (!string.IsNullOrEmpty(WebBrowserUserDataFolder))
{
//This indicates in which location will be created the WebView2 cache folder
this.browser.CreationProperties = new CoreWebView2CreationProperties()
try
{
UserDataFolder = WebBrowserUserDataFolder
};
}
var absolutePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
@"runtimes\win-x64\native");
CoreWebView2Environment.SetLoaderDllFolderPath(absolutePath);
}
catch (InvalidOperationException e)
{
LogToDynamoConsole("WebView2Loader.dll is already loaded successfully.");
}

try
{
await browser.Initialize(LogToDynamoConsole);
browser.CoreWebView2InitializationCompleted += Browser_CoreWebView2InitializationCompleted;

this.browser.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
twoWayScriptingObject = new ScriptingObject(this);
//register the interop object into the browser.
this.browser.CoreWebView2.AddHostObjectToScript("bridgeTwoWay", twoWayScriptingObject);
browser.CoreWebView2.Settings.IsZoomControlEnabled = true;
}
catch (ObjectDisposedException ex)
{
LogToDynamoConsole(ex.Message);
if (!string.IsNullOrEmpty(WebBrowserUserDataFolder))
{
//This indicates in which location will be created the WebView2 cache folder
this.browser.CreationProperties = new CoreWebView2CreationProperties()
{
UserDataFolder = WebBrowserUserDataFolder
};
}

try
{
await browser.Initialize(LogToDynamoConsole);

this.browser.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
twoWayScriptingObject = new ScriptingObject(this);
//register the interop object into the browser.
this.browser.CoreWebView2.AddHostObjectToScript("bridgeTwoWay", twoWayScriptingObject);
browser.CoreWebView2.Settings.IsZoomControlEnabled = true;

initState = AsyncMethodState.Done;
}
catch (ObjectDisposedException ex)
{
LogToDynamoConsole(ex.Message);
}
}
}

Expand Down
Loading