Skip to content

Commit

Permalink
DYN-7055 Library view initialization crash (#15307)
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongTang authored Jun 11, 2024
1 parent 836d522 commit 19295df
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
47 changes: 30 additions & 17 deletions src/LibraryViewExtensionWebView2/LibraryViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 Down Expand Up @@ -354,6 +355,16 @@ private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessa

private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (!e.IsSuccess)
{
if (e.InitializationException != null)
{
LogToDynamoConsole(e.InitializationException.Message);
}
LogToDynamoConsole("LibraryViewExtension CoreWebView2 initialization failed.");
return;
}

LibraryViewModel model = new LibraryViewModel();
LibraryView view = new LibraryView(model);

Expand Down Expand Up @@ -384,26 +395,21 @@ private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebV

try
{
this.browser.NavigateToString(libraryHTMLPage);
this.browser.NavigateToString(libraryHTMLPage);
SetLibraryFontSize();
SetTooltipText();
browser.ZoomFactor = (double)dynamoViewModel.Model.PreferenceSettings.LibraryZoomScale / 100d;
browser.ZoomFactorChanged += Browser_ZoomFactorChanged;
browser.KeyDown += Browser_KeyDown;

// Hosts an object that will expose the properties and methods to be called from the javascript side
browser.CoreWebView2.AddHostObjectToScript("scriptObject",
new ScriptObject(OnCopyToClipboard, OnPasteFromClipboard));
}
catch (Exception ex)
{
string msg = ex.Message;
LogToDynamoConsole("LibraryViewExtension CoreWebView2 initialization failed: " + ex.Message);
}

SetLibraryFontSize();
SetTooltipText();
//The default value of the zoom factor is 1.0. The value that comes from the slider is in percentage, so we divide by 100 to be equivalent
double zoomFactor = ((double)dynamoViewModel.Model.PreferenceSettings.LibraryZoomScale / 100d);

//The default value of the zoom factor is 1.0. The value that comes from the slider is in percentage, so we divide by 100 to be equivalent
browser.ZoomFactor = (double)dynamoViewModel.Model.PreferenceSettings.LibraryZoomScale / 100;
browser.ZoomFactorChanged += Browser_ZoomFactorChanged;
browser.KeyDown += Browser_KeyDown;

// Hosts an object that will expose the properties and methods to be called from the javascript side
browser.CoreWebView2.AddHostObjectToScript("scriptObject",
new ScriptObject(OnCopyToClipboard, OnPasteFromClipboard));
}

private void Browser_Loaded(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -699,7 +705,14 @@ internal void UpdatePopupLocation()
/// <param name="meessage"></param>
internal void LogToDynamoConsole(string message)
{
this.dynamoViewModel.Model.Logger.Log(message);
if (DynamoModel.IsTestMode)
{
System.Console.WriteLine(message);
}
else
{
this.dynamoViewModel?.Model?.Logger?.Log(message);
}
}

public void Dispose()
Expand Down
29 changes: 23 additions & 6 deletions src/Notifications/NotificationCenterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ private void WebView_NewWindowRequested(object sender, Microsoft.Web.WebView2.Co

private void WebView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
if (!e.IsSuccess)
{
if (e.InitializationException != null)
{
Log(e.InitializationException.Message);
}
Log("NotificationCenter CoreWebView2 initialization failed.");
return;
}

var assembly = Assembly.GetExecutingAssembly();
string htmlString = string.Empty;

Expand All @@ -241,15 +251,22 @@ private void WebView_CoreWebView2InitializationCompleted(object sender, Microsof
// More initialization options
// Context menu disabled
notificationUIPopup.webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
notificationUIPopup.webView.CoreWebView2.Settings.IsZoomControlEnabled = false;
notificationUIPopup.webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
// Opening hyper-links using default system browser instead of WebView2 tab window
notificationUIPopup.webView.CoreWebView2.NewWindowRequested += WebView_NewWindowRequested;
notificationUIPopup.webView.CoreWebView2.NavigateToString(htmlString);
// Hosts an object that will expose the properties and methods to be called from the javascript side
notificationUIPopup.webView.CoreWebView2.AddHostObjectToScript("scriptObject",
new ScriptObject(OnMarkAllAsRead, OnNotificationPopupUpdated));

notificationUIPopup.webView.CoreWebView2.Settings.IsZoomControlEnabled = false;
notificationUIPopup.webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
try
{
notificationUIPopup.webView.CoreWebView2.NavigateToString(htmlString);
// Hosts an object that will expose the properties and methods to be called from the javascript side
notificationUIPopup.webView.CoreWebView2.AddHostObjectToScript("scriptObject",
new ScriptObject(OnMarkAllAsRead, OnNotificationPopupUpdated));
}
catch (Exception ex)
{
Log("NotificationCenter CoreWebView2 initialization failed: " + ex.Message);
}
}
}

Expand Down

0 comments on commit 19295df

Please sign in to comment.