Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
- dynamically adding locale to front end
- extracted repeated webView2 to DynamoUtilities helper methods (inside the PathHelper)
- fixed dynamically loading of the Artifakt font resource
- added new public functions to the respective API text files
- removed unneeded stopwatch in StartPage.xaml.cs
  • Loading branch information
dnenov committed Feb 26, 2024
1 parent d1e3b7b commit 98ee40b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 68 deletions.
7 changes: 0 additions & 7 deletions src/DynamoCoreWpf/Controls/StartPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,6 @@ private void RefreshFileList(ObservableCollection<StartPageListItem> files,
{
files.Clear();


Stopwatch stopwatch = new Stopwatch();

foreach (var filePath in filePaths.Where(x => x != null))
{
try
Expand Down Expand Up @@ -418,10 +415,6 @@ private void RefreshFileList(ObservableCollection<StartPageListItem> files,
DynamoViewModel.Model.Logger.Log("File path is not valid: " + ex.StackTrace);
}
}

stopwatch.Stop(); // Stop the stopwatch

Debug.WriteLine($"Time taken to load {filePaths.Count()} files: {stopwatch.ElapsedMilliseconds} ms");
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/DynamoCoreWpf/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Dynamo.Controls.DynamoView.ExtensionsCollapsed.get -> bool
Dynamo.Controls.DynamoView.InitializeComponent() -> void
Dynamo.Controls.DynamoView.LibraryCollapsed.get -> bool
Dynamo.Controls.DynamoView.PlacePopup(System.Windows.Size popupSize, System.Windows.Size targetSize, System.Windows.Point offset) -> System.Windows.Controls.Primitives.CustomPopupPlacement[]
Dynamo.Controls.DynamoView.UseNewDynamoHomePage.get -> bool
Dynamo.Controls.ElementGroupToColorConverter
Dynamo.Controls.ElementGroupToColorConverter.Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) -> object
Dynamo.Controls.ElementGroupToColorConverter.ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) -> object
Expand Down
6 changes: 1 addition & 5 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Dynamo.Configuration;
using Dynamo.Core;
using Dynamo.Graph;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Notes;
Expand All @@ -24,7 +23,6 @@
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Nodes;
using Dynamo.Nodes.Prompts;
using Dynamo.PackageManager;
using Dynamo.PackageManager.UI;
using Dynamo.Search.SearchElements;
Expand Down Expand Up @@ -2640,9 +2638,7 @@ public bool UseNewDynamoHomePage
{
get
{
return true;
// TODO: replace with a similar flag to the below when deploying the feature
//return DynamoModel.FeatureFlags?.CheckFeatureFlag("NodeAutocompleteMachineLearningIsBeta", false) ?? false;
return DynamoModel.FeatureFlags?.CheckFeatureFlag("IsNewAppHomeEnabled", false) ?? false;
}
}

Expand Down
99 changes: 43 additions & 56 deletions src/DynamoCoreWpf/Views/HomePage/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -30,6 +31,7 @@ public partial class HomePage : UserControl, IDisposable
private static readonly string jsEmbeddedFile = "Dynamo.Wpf.Packages.DynamoHome.build.index.bundle.js";
private static readonly string fontStylePath = "Dynamo.Wpf.Views.GuidedTour.HtmlPages.Resources.ArtifaktElement-Regular.woff";
private static readonly string virtualFolderName = "embeddedFonts";
private static readonly string fontUrl = $"http://{virtualFolderName}/ArtifaktElement-Regular.woff";
private static readonly string virtualFolderPath = Path.Combine(Path.GetTempPath(), virtualFolderName);

private string fontFilePath;
Expand Down Expand Up @@ -146,70 +148,55 @@ private async void UserControl_Loaded(object sender, System.Windows.RoutedEventA
};

//ContentRendered ensures that the webview2 component is visible.
await dynWebView.Initialize();
// Context menu disabled
this.dynWebView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
// Zoom control disabled
this.dynWebView.CoreWebView2.Settings.IsZoomControlEnabled = false;
this.dynWebView.CoreWebView2.Settings.AreDevToolsEnabled = true;

var assembly = Assembly.GetExecutingAssembly();

using (Stream stream = assembly.GetManifestResourceStream(htmlEmbeddedFile))
using (StreamReader reader = new StreamReader(stream))
try
{
htmlString = reader.ReadToEnd();
}
await dynWebView.Initialize();

using (Stream stream = assembly.GetManifestResourceStream(jsEmbeddedFile))
using (StreamReader reader = new StreamReader(stream))
{
var jsString = reader.ReadToEnd();
jsonString = jsString;
}
// Set WebView2 settings
this.dynWebView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
this.dynWebView.CoreWebView2.Settings.IsZoomControlEnabled = false;
this.dynWebView.CoreWebView2.Settings.AreDevToolsEnabled = true;

// Copy the embedded resource to a temporary folder
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fontStylePath))
{
if (stream != null)
{
var fontData = new byte[stream.Length];
stream.Read(fontData, 0, fontData.Length);
// Load the embeded resources
var assembly = Assembly.GetExecutingAssembly();

// Create the temporary folder if it doesn't exist
Directory.CreateDirectory(virtualFolderPath);
htmlString = PathHelper.LoadEmbeddedResourceAsString(htmlEmbeddedFile, assembly);
jsonString = PathHelper.LoadEmbeddedResourceAsString(jsEmbeddedFile, assembly);

// Write the font file to the temporary folder
fontFilePath = Path.Combine(virtualFolderPath, "ArtifaktElement-Regular.woff");
File.WriteAllBytes(fontFilePath, fontData);
}
}
// Embed the font
PathHelper.ExtractAndSaveEmbeddedFont(fontStylePath, virtualFolderPath, "ArtifaktElement-Regular.woff", assembly);

// Set up virtual host name to folder mapping
dynWebView.CoreWebView2.SetVirtualHostNameToFolderMapping(virtualFolderName, virtualFolderPath, CoreWebView2HostResourceAccessKind.DenyCors);
// Set up virtual host name to folder mapping
dynWebView.CoreWebView2.SetVirtualHostNameToFolderMapping(virtualFolderName, virtualFolderPath, CoreWebView2HostResourceAccessKind.Allow);

htmlString = htmlString.Replace("mainJs", jsonString);
htmlString = htmlString.Replace("mainJs", jsonString);
htmlString = htmlString.Replace("#fontStyle", fontUrl);

try
{
dynWebView.NavigateToString(htmlString);
try
{
dynWebView.NavigateToString(htmlString);
}
catch (Exception ex)
{
this.startPage.DynamoViewModel.Model.Logger.Log(ex.Message);
}

// Exposing commands to the React front-end
dynWebView.CoreWebView2.AddHostObjectToScript("scriptObject",
new ScriptHomeObject(RequestOpenFile,
RequestNewWorkspace,
RequestOpenWorkspace,
RequestNewCustomNodeWorkspace,
RequestApplicationLoaded,
RequestShowGuidedTour,
RequestShowSampleFilesInFolder,
RequestShowBackupFilesInFolder,
RequestShowTemplate));
}
catch (Exception ex)
catch (ObjectDisposedException ex)
{
Debug.WriteLine(ex.Message);
this.startPage.DynamoViewModel.Model.Logger.Log(ex.Message);
}

// Exposing commands to the React front-end
dynWebView.CoreWebView2.AddHostObjectToScript("scriptObject",
new ScriptHomeObject(RequestOpenFile,
RequestNewWorkspace,
RequestOpenWorkspace,
RequestNewCustomNodeWorkspace,
RequestApplicationLoaded,
RequestShowGuidedTour,
RequestShowSampleFilesInFolder,
RequestShowBackupFilesInFolder,
RequestShowTemplate));
}

internal async void LoadingDone()
Expand All @@ -224,9 +211,9 @@ internal async void LoadingDone()
if (recentFiles == null || !recentFiles.Any()) { return; }

LoadGraphs(recentFiles);

var userLocale = "en";

var userLocale = CultureInfo.CurrentCulture.Name;
if (dynWebView?.CoreWebView2 != null)
{
await dynWebView.CoreWebView2.ExecuteScriptAsync(@$"window.setLocale('{userLocale}');");
Expand Down
40 changes: 40 additions & 0 deletions src/DynamoUtilities/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Security.AccessControl;
using System.Security.Principal;
Expand Down Expand Up @@ -498,5 +499,44 @@ public static string getServiceConfigValues(object o, string serviceKey)
}
return val;
}

/// <summary>
/// This function will load embedded resources such as HTML and JS files and return the content as a string
/// </summary>
/// <param name="resourcePath">The resource path to rreturn</param>
/// <returns>The embeded resource as string</returns>
public static string LoadEmbeddedResourceAsString(string resourcePath, Assembly assembly)

Check warning on line 508 in src/DynamoUtilities/PathHelper.cs

View workflow job for this annotation

GitHub Actions / analyze

Parameter 'assembly' has no matching param tag in the XML comment for 'PathHelper.LoadEmbeddedResourceAsString(string, Assembly)' (but other parameters do)
{
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}

/// <summary>
/// This function will extract the embedded font file and save it to a specified directory
/// </summary>
/// <param name="resourcePath">The location of the font resource</param>
/// <param name="outputPath">The temporary path to save the resource to</param>
/// <param name="outputFileName">The name of the temporary resource file</param>
public static void ExtractAndSaveEmbeddedFont(string resourcePath, string outputPath, string outputFileName, Assembly assembly)

Check warning on line 523 in src/DynamoUtilities/PathHelper.cs

View workflow job for this annotation

GitHub Actions / analyze

Parameter 'assembly' has no matching param tag in the XML comment for 'PathHelper.ExtractAndSaveEmbeddedFont(string, string, string, Assembly)' (but other parameters do)
{
using (var stream = assembly.GetManifestResourceStream(resourcePath))
{
if (stream != null)
{
var fontData = new byte[stream.Length];
stream.Read(fontData, 0, fontData.Length);

// Create the output directory if it doesn't exist
Directory.CreateDirectory(outputPath);

// Write the font file to the output directory
var fontFilePath = Path.Combine(outputPath, outputFileName);
File.WriteAllBytes(fontFilePath, fontData);
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/DynamoUtilities/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ static Dynamo.Utilities.TypeExtensions.ImplementsGeneric(System.Type generic, Sy
static Dynamo.Utilities.VersionUtilities.PartialParse(string versionString, int numberOfFields = 3) -> System.Version
static DynamoUtilities.CertificateVerification.CheckAssemblyForValidCertificate(string assemblyPath) -> bool
static DynamoUtilities.PathHelper.CreateFolderIfNotExist(string folderPath) -> System.Exception
static DynamoUtilities.PathHelper.ExtractAndSaveEmbeddedFont(string resourcePath, string outputPath, string outputFileName, System.Reflection.Assembly assembly) -> void
static DynamoUtilities.PathHelper.FileInfoAtPath(string path, out bool fileExists, out string size) -> void
static DynamoUtilities.PathHelper.GetFileSize(string path) -> string
static DynamoUtilities.PathHelper.GetScreenCaptureNameFromPath(string filePath, bool isTimeStampIncluded) -> string
Expand All @@ -226,6 +227,7 @@ static DynamoUtilities.PathHelper.IsReadOnlyPath(string filePath) -> bool
static DynamoUtilities.PathHelper.isValidJson(string path, out string fileContents, out System.Exception ex) -> bool
static DynamoUtilities.PathHelper.IsValidPath(string filePath) -> bool
static DynamoUtilities.PathHelper.isValidXML(string path, out System.Xml.XmlDocument xmlDoc, out System.Exception ex) -> bool
static DynamoUtilities.PathHelper.LoadEmbeddedResourceAsString(string resourcePath, System.Reflection.Assembly assembly) -> string
static DynamoUtilities.TypeSwitch.Case<T>(System.Action action) -> DynamoUtilities.TypeSwitch.CaseInfo
static DynamoUtilities.TypeSwitch.Case<T>(System.Action<T> action) -> DynamoUtilities.TypeSwitch.CaseInfo
static DynamoUtilities.TypeSwitch.Default(System.Action action) -> DynamoUtilities.TypeSwitch.CaseInfo
Expand Down

0 comments on commit 98ee40b

Please sign in to comment.