Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzart90 committed Jan 10, 2024
1 parent d25719f commit 984cc8d
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<UserControl x:Class="Dynamo.DocumentationBrowser.DocumentationBrowserView"
<UserControl x:Class="Dynamo.DocumentationBrowser.DocumentationBrowserView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:Dynamo.UI;assembly=DynamoCoreWpf"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:wv2="clr-namespace:Dynamo.Wpf.Utilities;assembly=DynamoCoreWpf"
mc:Ignorable="d"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Expand All @@ -22,7 +22,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<wv2:WebView2 Name="documentationBrowser"
<wv2:DynamoWebView2 Name="documentationBrowser"
Grid.Row="0"
HorizontalAlignment="Stretch"
Visibility="{Binding Path=ShowBrowser,Converter={StaticResource BooleanToVisibilityConverter}}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public DocumentationBrowserView(DocumentationBrowserViewModel viewModel)
{
InitializeComponent();

if (TestUtilities.RunningFromNUnit)
{
TestUtilities.IncrementWebView2(nameof(DocumentationBrowserView));
}

this.DataContext = viewModel;
this.viewModel = viewModel;

Expand Down Expand Up @@ -121,11 +116,6 @@ protected virtual void Dispose(bool disposing)
this.viewModel.LinkChanged -= NavigateToPage;
if (this.documentationBrowser != null)
{
if (TestUtilities.RunningFromNUnit)
{
TestUtilities.DecrementWebView2(nameof(DocumentationBrowserView));
}

this.documentationBrowser.NavigationStarting -= ShouldAllowNavigation;
this.documentationBrowser.DpiChanged -= DocumentationBrowser_DpiChanged;
if (this.documentationBrowser.CoreWebView2 != null)
Expand Down
40 changes: 37 additions & 3 deletions src/DynamoCoreWpf/Utilities/WebView2Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
using System.Windows;
using Dynamo.Wpf.Properties;
using Dynamo.Wpf.Utilities;
using DynamoUtilities;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;

namespace Dynamo.Utilities
namespace Dynamo.Wpf.Utilities
{
public class DynamoWebView2 : WebView2
{
#region API/Data used for debugging/testing
private string stamp;
#endregion

public DynamoWebView2() : base()
{
stamp = TestUtilities.WebView2Stamp;
}

protected override void Dispose(bool disposing)
{
if (System.Environment.CurrentManagedThreadId != Dispatcher.Thread.ManagedThreadId)
{
System.Console.WriteLine($"WebView2 instance with stamp {stamp} is being disposed of on non-UI thread");
}
if (Dispatcher != null)
{
Dispatcher.Invoke(() =>
{
base.Dispose(disposing);
});
}
else
{
System.Console.WriteLine($"WebView2 instance with stamp {stamp} is being disposed of but has no valid Dispatcher");
// Should we still try to dispose ?
base.Dispose(disposing);
}
}
}

/// <summary>
/// This class will contain several utility functions that will be used for the WebView2 component
/// </summary>
Expand All @@ -25,7 +59,7 @@ public static bool ValidateWebView2RuntimeInstalled()
catch (WebView2RuntimeNotFoundException)
{
var messageStr = Resources.ResourceManager.GetString("WebView2RequiredMessage");
if(messageStr.IndexOf("\\n") >= 0)
if (messageStr.IndexOf("\\n") >= 0)
messageStr = messageStr.Replace("\\n", "\n");

MessageBoxService.Show(messageStr,
Expand Down
13 changes: 3 additions & 10 deletions src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels.GuidedTour;
using DynamoUtilities;
using Microsoft.Web.WebView2.Wpf;

namespace Dynamo.Wpf.Views.GuidedTour
{
Expand All @@ -29,7 +27,7 @@ public partial class PopupWindow : Popup
//Field that indicates wheter popups are left-aligned or right-aligned
private const string menuDropAligment = "_menuDropAlignment";

internal WebView2 webBrowserComponent;
internal DynamoWebView2 webBrowserComponent;
//Assembly path to the Font file
private const string mainFontStylePath = "Dynamo.Wpf.Views.GuidedTour.HtmlPages.Resources.ArtifaktElement-Regular.woff";
//Assembly path to the Resources folder
Expand Down Expand Up @@ -115,12 +113,7 @@ private void PopupWindow_Opened(object sender, EventArgs e)

private async void InitWebView2Component()
{
webBrowserComponent = new WebView2();

if (TestUtilities.RunningFromNUnit)
{
TestUtilities.IncrementWebView2(nameof(PopupWindow));
}
webBrowserComponent = new DynamoWebView2();

webBrowserComponent.Margin = new System.Windows.Thickness(popupBordersOffSet, 0, 0, 0);
webBrowserComponent.Width = popupViewModel.Width;
Expand Down
15 changes: 3 additions & 12 deletions src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.Utilities;
using DynamoUtilities;
using Greg.AuthProviders;
using Microsoft.Web.WebView2.Core;
Expand Down Expand Up @@ -88,7 +89,7 @@ public DynamoView DynamoView
/// <summary>
/// The WebView2 Browser instance used to display splash screen
/// </summary>
internal WebView2 webView;
internal DynamoWebView2 webView;

/// <summary>
/// This delegate is used in StaticSplashScreenReady events
Expand Down Expand Up @@ -142,14 +143,9 @@ public SplashScreen(bool enableSignInButton = true)
loadingTimer = new Stopwatch();
loadingTimer.Start();

webView = new WebView2();
webView = new DynamoWebView2();
ShadowGrid.Children.Add(webView);

if (TestUtilities.RunningFromNUnit)
{
TestUtilities.IncrementWebView2(nameof(SplashScreen));
}

// Bind event handlers
webView.NavigationCompleted += WebView_NavigationCompleted;
DynamoModel.RequestUpdateLoadBarStatus += DynamoModel_RequestUpdateLoadBarStatus;
Expand Down Expand Up @@ -558,11 +554,6 @@ protected override void OnClosed(EventArgs e)
authManager.LoginStateChanged -= OnLoginStateChanged;
}

if (TestUtilities.RunningFromNUnit)
{
TestUtilities.DecrementWebView2(nameof(SplashScreen));
}

webView.Dispose();
webView = null;

Expand Down
2 changes: 1 addition & 1 deletion src/DynamoSandbox/DynamoCoreSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using Dynamo.DynamoSandbox.Properties;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.UI;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels.Watch3D;

Expand Down
50 changes: 2 additions & 48 deletions src/DynamoUtilities/TestUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,13 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DynamoUtilities
{
internal class TestUtilities
internal static class TestUtilities
{
internal static bool RunningFromNUnit = false;
static TestUtilities()
{
foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies())
{
// Can't do something like this as it will load the nUnit assembly
// if (assem == typeof(NUnit.Framework.Assert))

if (assem.FullName.ToLowerInvariant().StartsWith("nunit.framework"))
{
RunningFromNUnit = true;
break;
}
}
}

private static ConcurrentDictionary<string, int> WebView2Counter = new ConcurrentDictionary<string, int>();
internal static void IncrementWebView2(string containingType)
{
if (!WebView2Counter.TryGetValue(containingType, out _))
{
WebView2Counter.TryAdd(containingType, 0);
}
WebView2Counter[containingType]++;
}

internal static void DecrementWebView2(string containingType)
{
WebView2Counter[containingType]--;
}

internal static void AssertCounters()
{
var exceptions = new List<Exception>();
foreach (var counter in WebView2Counter)
{
if (counter.Value != 0)
{
exceptions.Add(new Exception($"Unexpected number of webview2 allocations/deallocations: {counter.Value} webview2 instances for containing class {counter.Key}"));
}
}
WebView2Counter.Clear();
if ( exceptions.Count > 0 ) { throw new AggregateException(exceptions.ToArray()); }
}
internal static string WebView2Stamp;
}
}
15 changes: 3 additions & 12 deletions src/LibraryViewExtensionWebView2/LibraryViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Dynamo.ViewModels;
using Dynamo.Wpf.Interfaces;
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels;
using DynamoUtilities;
using ICSharpCode.AvalonEdit.Document;
Expand Down Expand Up @@ -76,7 +77,7 @@ public class LibraryViewController : IDisposable
private FloatingLibraryTooltipPopup libraryViewTooltip;
// private ResourceHandlerFactory resourceFactory;
private IDisposable observer;
internal WebView2 browser;
internal DynamoWebView2 browser;
ScriptingObject twoWayScriptingObject;
private const string CreateNodeInstrumentationString = "Search-NodeAdded";
// TODO remove this when we can control the library state from Dynamo more precisely.
Expand Down Expand Up @@ -131,12 +132,7 @@ internal LibraryViewController(Window dynamoView, ICommandExecutive commandExecu
var sidebarGrid = dynamoWindow.FindName("sidebarGrid") as Grid;
sidebarGrid.Children.Add(view);

browser = view.mainGrid.Children.OfType<WebView2>().FirstOrDefault();

if (TestUtilities.RunningFromNUnit)
{
TestUtilities.IncrementWebView2(nameof(LibraryView));
}
browser = view.mainGrid.Children.OfType<DynamoWebView2>().FirstOrDefault();

browser.Loaded += Browser_Loaded;
browser.SizeChanged += Browser_SizeChanged;
Expand Down Expand Up @@ -723,11 +719,6 @@ protected void Dispose(bool disposing)
}
if (this.browser != null)
{
if (TestUtilities.RunningFromNUnit)
{
TestUtilities.DecrementWebView2(nameof(LibraryView));
}

browser.CoreWebView2.RemoveHostObjectFromScript("bridgeTwoWay");
browser.SizeChanged -= Browser_SizeChanged;
browser.Loaded -= Browser_Loaded;
Expand Down
6 changes: 3 additions & 3 deletions src/LibraryViewExtensionWebView2/Views/LibraryView.xaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<UserControl x:Class="Dynamo.LibraryViewExtensionWebView2.Views.LibraryView"
<UserControl x:Class="Dynamo.LibraryViewExtensionWebView2.Views.LibraryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:wv2="clr-namespace:Dynamo.Wpf.Utilities;assembly=DynamoCoreWpf"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Dynamo.LibraryViewExtensionWebView2.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="mainGrid">
<wv2:WebView2 Visibility="Hidden"/>
<wv2:DynamoWebView2 Visibility="Hidden"/>
</Grid>
</UserControl>
10 changes: 0 additions & 10 deletions src/Notifications/NotificationCenterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ internal NotificationCenterController(DynamoView view, DynamoLogger dynLogger)
};
logger = dynLogger;

if (TestUtilities.RunningFromNUnit)
{
TestUtilities.IncrementWebView2(nameof(Notifications));
}

// If user turns on the feature, they will need to restart Dynamo to see the count
// This ensures no network traffic when Notification center feature is turned off
if (dynamoViewModel.PreferenceSettings.EnableNotificationCenter && !dynamoViewModel.Model.NoNetworkMode )
Expand Down Expand Up @@ -336,11 +331,6 @@ protected virtual void Dispose(bool disposing)

if (notificationUIPopup.webView != null)
{
if (TestUtilities.RunningFromNUnit)
{
TestUtilities.DecrementWebView2(nameof(Notifications));
}

notificationUIPopup.webView.Visibility = Visibility.Hidden;
notificationUIPopup.webView.Loaded -= InitializeBrowserAsync;
notificationUIPopup.webView.NavigationCompleted -= WebView_NavigationCompleted;
Expand Down
6 changes: 3 additions & 3 deletions src/Notifications/View/NotificationUI.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Popup x:Class="Dynamo.Notifications.View.NotificationUI"
<Popup x:Class="Dynamo.Notifications.View.NotificationUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -7,7 +7,7 @@
xmlns:controls="clr-namespace:Dynamo.Controls;assembly=DynamoCoreWpf"
xmlns:p="clr-namespace:Dynamo.Wpf.Properties;assembly=DynamoCoreWpf"
xmlns:fa="http://schemas.fontawesome.io/icons/"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:wv2="clr-namespace:Dynamo.Wpf.Utilities;assembly=DynamoCoreWpf"
mc:Ignorable="d"
AllowsTransparency="True"
StaysOpen="False"
Expand Down Expand Up @@ -73,7 +73,7 @@

<Grid x:Name="mainPopupGrid" Background="White"
Width="{Binding PopupRectangleWidth}">
<wv2:WebView2 Name="webView" ></wv2:WebView2>
<wv2:DynamoWebView2 Name="webView" ></wv2:DynamoWebView2>
</Grid>
</Canvas>
</Popup>
5 changes: 2 additions & 3 deletions test/DynamoCoreTests/UnitTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public virtual void Setup()
{

System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Start test: {TestContext.CurrentContext.Test.Name}");
TestUtilities.AssertCounters();
TestUtilities.WebView2Stamp = TestContext.CurrentContext.Test.Name;

SetupDirectories();

Expand Down Expand Up @@ -104,8 +104,7 @@ public virtual void Cleanup()
{
AppDomain.CurrentDomain.AssemblyResolve -= assemblyHelper.ResolveAssembly;
}

TestUtilities.AssertCounters();
TestUtilities.WebView2Stamp = string.Empty;
System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Finished test: {TestContext.CurrentContext.Test.Name}");
}

Expand Down
Loading

0 comments on commit 984cc8d

Please sign in to comment.