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 5107 webview2 update in guided tours #13142

Merged
merged 3 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 1 addition & 7 deletions src/DynamoCoreWpf/DynamoCoreWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<ItemGroup>
<PackageReference Include="DynamoVisualProgramming.LibG_228_0_0" Version="2.13.0.9046" />
<PackageReference Include="Greg" Version="2.3.0.1646" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1264.42" />
<PackageReference Include="Newtonsoft.Json" Version="8.0.3" />
<PackageReference Include="RestSharp" Version="106.12.0" />
<PackageReference Include="FontAwesome.WPF" Version="4.7.0.9" CopyXML="true" />
Expand Down Expand Up @@ -342,9 +343,6 @@
<Compile Include="Views\GuidedTour\ExitGuideWindow.xaml.cs">
<DependentUpon>ExitGuideWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\GuidedTour\WebBrowserWindow.xaml.cs">
<DependentUpon>WebBrowserWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\GuidedTour\RealTimeInfoWindow.xaml.cs">
<DependentUpon>RealTimeInfoWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -501,10 +499,6 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\GuidedTour\WebBrowserWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Core\ConnectorContextMenuView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
18 changes: 0 additions & 18 deletions src/DynamoCoreWpf/UI/GuidedTour/GuidesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,6 @@ internal void CreateExitModal(ExitGuide exitGuide)
exitGuideWindow.IsOpen = true;
}

/// <summary>
/// Shows/Hides the Popup based in if the DynamoView is Active or not
/// </summary>
/// <param name="isActive"></param>
internal void ManagePopupActivation(bool isActive)
{
if (GuideFlowEvents.IsAnyGuideActive)
{
if (currentGuide.CurrentStep.stepUIPopup.IsOpen == !isActive)
currentGuide.CurrentStep.stepUIPopup.IsOpen = isActive;
}
else if (GuideFlowEvents.IsAnyGuideActive == false && GuideFlowEvents.IsGuideExited == false)
{
if(exitGuideWindow != null && currentGuide.Name.Equals(GuidesManager.PackagesGuideName))
exitGuideWindow.IsOpen = isActive;
}
}

private void ContinueTourButton_Click(object sender, RoutedEventArgs e)
{
exitGuideWindow.IsOpen = false;
Expand Down
12 changes: 10 additions & 2 deletions src/DynamoCoreWpf/UI/GuidedTour/Step.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ internal void Hide(Guide.GuideFlow currentFlow)
{
stepUIPopup.IsOpen = false;

if (HostPopupInfo.HtmlPage != null && !string.IsNullOrEmpty(HostPopupInfo.HtmlPage.FileName))
{
var webBrowser = (stepUIPopup as PopupWindow).webBrowserComponent;
if(webBrowser != null)
{
webBrowser.Dispose();
}
}

//Disable the HightlightArea functionality
SetHighlightSection(false);

Expand Down Expand Up @@ -421,7 +430,6 @@ public void UpdateLocation()
if(stepUIPopup is PopupWindow)
{
var stepUiPopupWindow = (PopupWindow)stepUIPopup;
UpdatePopupLocationInvoke(stepUiPopupWindow?.webBrowserWindow);
}
}

Expand Down Expand Up @@ -485,7 +493,7 @@ private void UpdatePopupLocationInvoke(Popup popUp)
positionMethod.Invoke(popUp, null);
}
}

/// <summary>
/// This method will execute an UIAutomation action over a specific UIElement
/// </summary>
Expand Down
9 changes: 0 additions & 9 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2508,12 +2508,6 @@ private void FileTrustWarning_Click(object sender, RoutedEventArgs e)

private void DynamoView_Activated(object sender, EventArgs e)
{
if (dynamoViewModel.MainGuideManager != null && dynamoViewModel.MainGuideManager.currentGuide != null)
{
dynamoViewModel.MainGuideManager.ManagePopupActivation(true);
}


if (fileTrustWarningPopup != null)
{
fileTrustWarningPopup.ManagePopupActivation(true);
Expand All @@ -2522,9 +2516,6 @@ private void DynamoView_Activated(object sender, EventArgs e)

private void DynamoView_Deactivated(object sender, EventArgs e)
{
if (dynamoViewModel.MainGuideManager != null && dynamoViewModel.MainGuideManager.currentGuide != null)
dynamoViewModel.MainGuideManager.ManagePopupActivation(false);

if(fileTrustWarningPopup != null)
fileTrustWarningPopup.ManagePopupActivation(false);
}
Expand Down
4 changes: 3 additions & 1 deletion src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
</Grid.RowDefinitions>

<Grid Grid.Row="0"
x:Name="contentGrid"
DataContext="{Binding Step}">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
Expand All @@ -102,7 +103,7 @@
<SolidColorBrush Color="{StaticResource PopupTitleBorderColor}"
Opacity="0.3"/>
</Border.BorderBrush>
<Grid>
<Grid x:Name="titleGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="*"/>
Expand Down Expand Up @@ -135,6 +136,7 @@


<Grid Grid.Row="1"
x:Name="bottomGrid"
Margin="0,10,0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
Expand Down
92 changes: 85 additions & 7 deletions src/DynamoCoreWpf/Views/GuidedTour/PopupWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Dynamo.Utilities;
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.ViewModels.GuidedTour;
using Microsoft.Web.WebView2.Wpf;

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

internal WebBrowserWindow webBrowserWindow;
internal WebView2 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
private const string resourcesPath = "Dynamo.Wpf.Views.GuidedTour.HtmlPages.Resources";

public PopupWindow(PopupWindowViewModel viewModel, HostControlInfo hInfo)
{
Expand Down Expand Up @@ -75,10 +84,13 @@ private void EnsureStandardPopupAlignment()

private void PopupWindow_Closed(object sender, EventArgs e)
{
if (webBrowserWindow != null)
webBrowserWindow.IsOpen = false;
if (webBrowserComponent != null)
{
webBrowserComponent.Visibility = Visibility.Collapsed;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RobertGlobant20 Can you explain when is the webBrowserComponent disposed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QilongTang the WebView2 control is created every time the Popup is opened and disposed when the Popup is Hidden (specifically in the Step.Hide() method that is executed when we click the next or prev Popup button), the problem with Popup.IsOpen = false is that it generates an async call to Popup.OnClose() so if we don't dispose the WebView2 instance before we close the PackageManagerSearch, the WebView2 crashes (in the image below you will see that we set IsOpen = false and then we execute the UIAutomation Steps - inside one of this we are closing PackageManagerSearch).
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RobertGlobant20 Gotcha, thanks!

}


if(isClosingTour)
if (isClosingTour)
{
Opened -= PopupWindow_Opened;
Closed -= PopupWindow_Closed;
Expand All @@ -89,11 +101,77 @@ private void PopupWindow_Opened(object sender, EventArgs e)
{
if (hostControlInfo.HtmlPage != null && !string.IsNullOrEmpty(hostControlInfo.HtmlPage.FileName))
{
ContentRichTextBox.Visibility = Visibility.Hidden;
ContentRichTextBox.Visibility = Visibility.Hidden;
InitWebView2Component();
}
}

webBrowserWindow = new WebBrowserWindow(popupViewModel, hostControlInfo);
webBrowserWindow.IsOpen = true;
private async void InitWebView2Component()
{
webBrowserComponent = new WebView2();
webBrowserComponent.Margin = new System.Windows.Thickness(popupBordersOffSet, 0, 0, 0);
webBrowserComponent.Width = popupViewModel.Width;
//The height is subtracted by a const that sums the height of the header and footer of the popup
var heightBottom = bottomGrid.ActualHeight;
var heightTitle = titleGrid.ActualHeight;
//popupBordersOffSet * 2 because is one offset at the top and the other one at the bottom of the Popup
webBrowserComponent.Height = popupViewModel.Height - (heightBottom + heightTitle + popupBordersOffSet * 2);
contentGrid.Children.Add(webBrowserComponent);
Grid.SetRow(webBrowserComponent, 1);

LoadWebBrowser(hostControlInfo.HtmlPage);
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Loads HTML file from resource assembly and replace it's key values by base64 files
/// </summary>
/// <param name="htmlPage">Contains filename and resources to be loaded in page</param>
private async void LoadWebBrowser(HtmlPage htmlPage)
{
var bodyHtmlPage = ResourceUtilities.LoadContentFromResources(htmlPage.FileName, GetType().Assembly, false, false);

bodyHtmlPage = LoadResouces(bodyHtmlPage, htmlPage.Resources);
bodyHtmlPage = LoadResourceAndReplaceByKey(bodyHtmlPage, "#fontStyle", mainFontStylePath);
await webBrowserComponent.EnsureCoreWebView2Async();
webBrowserComponent.NavigateToString(bodyHtmlPage);
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Loads resource from a dictionary and replaces its key by an embedded file
/// </summary>
/// <param name="bodyHtmlPage">Html page string</param>
/// <param name="resources">Resources to be loaded</param>
/// <returns></returns>
private string LoadResouces(string bodyHtmlPage, Dictionary<string, string> resources)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a good thing is to add those methods in the Utils class, so they can be reused. (For example in our notifications extension method). Let me know your opinion about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods LoadWebBrowser, LoadResouces and LoadResourceAndReplaceByKey were moved to the ResourceUtilities class so they can be used in other places, please check the next commit:
f8c0b4b

{
if (resources != null && resources.Any())
{
foreach (var resource in resources)
{
bodyHtmlPage = LoadResourceAndReplaceByKey(bodyHtmlPage, resource.Key, $"{resourcesPath}.{resource.Value}");
}
}
return bodyHtmlPage;
}

/// <summary>
/// Finds a key word inside the html page and replace by a resource file
/// </summary>
/// <param name="bodyHtmlPage">Current html page</param>
/// <param name="key">Key that is going to be replaced</param>
/// <param name="resourceFile">Resource file to be included in the page</param>
/// <returns></returns>
private string LoadResourceAndReplaceByKey(string bodyHtmlPage, string key, string resourceFile)
{
Stream resourceStream = ResourceUtilities.LoadResourceByUrl(resourceFile);

if (resourceStream != null)
{
var resourceBase64 = ResourceUtilities.ConvertToBase64(resourceStream);
bodyHtmlPage = bodyHtmlPage.Replace(key, resourceBase64);
}

return bodyHtmlPage;
}

private void StartTourButton_Click(object sender, System.Windows.RoutedEventArgs e)
Expand Down
17 changes: 0 additions & 17 deletions src/DynamoCoreWpf/Views/GuidedTour/WebBrowserWindow.xaml

This file was deleted.

114 changes: 0 additions & 114 deletions src/DynamoCoreWpf/Views/GuidedTour/WebBrowserWindow.xaml.cs

This file was deleted.