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-6857-homepage-preferences #15139

Merged
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions src/DynamoCore/Configuration/PreferenceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,21 @@ public bool IsBackgroundPreviewActive
/// </summary>
public bool UseHardwareAcceleration { get; set; }

/// <summary>
/// Persistence for Dynamo HomePage
/// </summary>
[XmlIgnore]
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻

internal Dictionary<string, object> HomePageSettings { get; set; }

/// <summary>
/// A helper intermediary string to allow the serialization of the HomePageSettings dictionary
/// </summary>
public string HomePageSettingsSerialized
{
get => Newtonsoft.Json.JsonConvert.SerializeObject(HomePageSettings);
set => HomePageSettings = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(value);
}

#endregion

#region Dynamo application settings
Expand Down
6 changes: 4 additions & 2 deletions src/DynamoCore/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
abstract Dynamo.Extensions.LinterExtensionBase.Name.get -> string
abstract Dynamo.Extensions.LinterExtensionBase.Name.get -> string
abstract Dynamo.Extensions.LinterExtensionBase.Shutdown() -> void
abstract Dynamo.Extensions.LinterExtensionBase.UniqueId.get -> string
abstract Dynamo.Graph.ModelBase.DeserializeCore(System.Xml.XmlElement nodeElement, Dynamo.Graph.SaveContext context) -> void
Expand Down Expand Up @@ -177,6 +177,8 @@ Dynamo.Configuration.PreferenceSettings.HideAutocompleteMethodOptions.get -> boo
Dynamo.Configuration.PreferenceSettings.HideAutocompleteMethodOptions.set -> void
Dynamo.Configuration.PreferenceSettings.HideNodesBelowSpecificConfidenceLevel.get -> bool
Dynamo.Configuration.PreferenceSettings.HideNodesBelowSpecificConfidenceLevel.set -> void
Dynamo.Configuration.PreferenceSettings.HomePageSettingsSerialized.get -> string
Dynamo.Configuration.PreferenceSettings.HomePageSettingsSerialized.set -> void
Dynamo.Configuration.PreferenceSettings.IronPythonResolveTargetVersion.get -> string
Dynamo.Configuration.PreferenceSettings.IronPythonResolveTargetVersion.set -> void
Dynamo.Configuration.PreferenceSettings.IsADPAnalyticsReportingApproved.get -> bool
Expand Down Expand Up @@ -3139,4 +3141,4 @@ virtual Dynamo.Search.SearchElements.NodeSearchElement.FullName.get -> string
virtual Dynamo.Search.SearchElements.NodeSearchElement.GenerateInputParameters() -> System.Collections.Generic.IEnumerable<System.Tuple<string, string>>
virtual Dynamo.Search.SearchElements.NodeSearchElement.GenerateOutputParameters() -> System.Collections.Generic.IEnumerable<string>
virtual Dynamo.Search.SearchElements.NodeSearchElement.OnItemProduced(Dynamo.Graph.Nodes.NodeModel obj) -> void
virtual Dynamo.Search.SearchElements.SearchElementBase.CreationName.get -> string
virtual Dynamo.Search.SearchElements.SearchElementBase.CreationName.get -> string
3 changes: 2 additions & 1 deletion src/DynamoCoreWpf/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,8 @@ Dynamo.UI.Views.ScriptHomeObject.NewCustomNodeWorkspace() -> void
Dynamo.UI.Views.ScriptHomeObject.NewWorkspace() -> void
Dynamo.UI.Views.ScriptHomeObject.OpenFile(string path) -> void
Dynamo.UI.Views.ScriptHomeObject.OpenWorkspace() -> void
Dynamo.UI.Views.ScriptHomeObject.ScriptHomeObject(System.Action<string> requestOpenFile, System.Action requestNewWorkspace, System.Action requestOpenWorkspace, System.Action requestNewCustomNodeWorkspace, System.Action requestApplicationLoaded, System.Action<string> requestShowGuidedTour, System.Action requestShowSampleFilesInFolder, System.Action requestShowBackupFilesInFolder, System.Action requestShowTemplate) -> void
Dynamo.UI.Views.ScriptHomeObject.SaveSettings(string settings) -> void
Dynamo.UI.Views.ScriptHomeObject.ScriptHomeObject(System.Action<string> requestOpenFile, System.Action requestNewWorkspace, System.Action requestOpenWorkspace, System.Action requestNewCustomNodeWorkspace, System.Action requestApplicationLoaded, System.Action<string> requestShowGuidedTour, System.Action requestShowSampleFilesInFolder, System.Action requestShowBackupFilesInFolder, System.Action requestShowTemplate, System.Action<string> requestSaveSettings) -> void
Dynamo.UI.Views.ScriptHomeObject.ShowBackupFilesInFolder() -> void
Dynamo.UI.Views.ScriptHomeObject.ShowSampleFilesInFolder() -> void
Dynamo.UI.Views.ScriptHomeObject.ShowTempalte() -> void
Expand Down
95 changes: 76 additions & 19 deletions src/DynamoCoreWpf/Views/HomePage/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public partial class HomePage : UserControl, IDisposable
internal Action RequestShowSampleFilesInFolder;
internal Action RequestShowBackupFilesInFolder;
internal Action RequestShowTemplate;
internal Action<string> RequestSaveSettings;

internal List<GuidedTourItem> GuidedTourItems;

Expand Down Expand Up @@ -83,6 +84,7 @@ public HomePage()
RequestShowBackupFilesInFolder = ShowBackupFilesInFolder;
RequestShowTemplate = ShowTemplate;
RequestApplicationLoaded = ApplicationLoaded;
RequestSaveSettings = SaveSettings;

DataContextChanged += OnDataContextChanged;

Expand Down Expand Up @@ -193,7 +195,8 @@ private async void UserControl_Loaded(object sender, System.Windows.RoutedEventA
RequestShowGuidedTour,
RequestShowSampleFilesInFolder,
RequestShowBackupFilesInFolder,
RequestShowTemplate));
RequestShowTemplate,
RequestSaveSettings));
}
catch (ObjectDisposedException ex)
{
Expand Down Expand Up @@ -238,25 +241,14 @@ internal bool ProcessUri(string uri)
return false;
}

internal async void LoadingDone()
internal void LoadingDone()
{
SendGuidesData();

if (startPage == null) { return; }

SendGuidesData();
SendSamplesData();

var recentFiles = startPage.RecentFiles;
if (recentFiles == null || !recentFiles.Any()) { return; }

LoadGraphs(recentFiles);

var userLocale = CultureInfo.CurrentCulture.Name;

if (dynWebView?.CoreWebView2 != null)
{
await dynWebView.CoreWebView2.ExecuteScriptAsync(@$"window.setLocale('{userLocale}');");
}
SendRecentGraphsData();
SetLocale();
}

#region FrontEnd Initialization Calls
Expand Down Expand Up @@ -289,6 +281,37 @@ private async void SendSamplesData()
}
}

private async void SendRecentGraphsData()
{
// Send user preferences
if (dynWebView?.CoreWebView2 != null)
{
if (startPage.DynamoViewModel.PreferenceSettings.HomePageSettings != null)
{
var settingsJson = Newtonsoft.Json.JsonConvert.SerializeObject(startPage.DynamoViewModel.PreferenceSettings.HomePageSettings);
settingsJson = System.Web.HttpUtility.JavaScriptStringEncode(settingsJson);

await dynWebView.CoreWebView2.ExecuteScriptAsync(@$"window.setHomePageSettings('{settingsJson}');");
}
}

// Load recent files
var recentFiles = startPage.RecentFiles;
if (recentFiles != null && recentFiles.Any())
{
LoadGraphs(recentFiles);
}
}

private async void SetLocale()
{
var userLocale = CultureInfo.CurrentCulture.Name;

if (dynWebView?.CoreWebView2 != null)
{
await dynWebView.CoreWebView2.ExecuteScriptAsync(@$"window.setLocale('{userLocale}');");
}
}

/// <summary>
/// Sends guided tour data to react app
Expand Down Expand Up @@ -402,6 +425,34 @@ internal void StartGuidedTour(string path)
ShowGuidedTour(path);
}

internal void SaveSettings(string settingsJson)
{
if (!string.IsNullOrEmpty(settingsJson) && this.startPage != null)
{
var settingsDict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(settingsJson);

// If the HomePageSettings have not been previously created, initialize it now
if (startPage.DynamoViewModel.PreferenceSettings.HomePageSettings == null)
{
startPage.DynamoViewModel.PreferenceSettings.HomePageSettings = new Dictionary<string, object>();
}

// Change existing values, or add new ones
foreach (var setting in settingsDict)
{
if (startPage.DynamoViewModel.PreferenceSettings.HomePageSettings.ContainsKey(setting.Key)
&& startPage.DynamoViewModel.PreferenceSettings.HomePageSettings[setting.Key] != setting.Value)
{
startPage.DynamoViewModel.PreferenceSettings.HomePageSettings[setting.Key] = setting.Value;
}
else
{
startPage.DynamoViewModel.PreferenceSettings.HomePageSettings.Add(setting.Key, setting.Value);
}
}
}
}

internal void NewWorkspace()
{
this.startPage?.DynamoViewModel?.NewHomeWorkspaceCommand.Execute(null);
Expand Down Expand Up @@ -506,6 +557,7 @@ public class ScriptHomeObject
readonly Action RequestShowSampleFilesInFolder;
readonly Action RequestShowBackupFilesInFolder;
readonly Action RequestShowTemplate;
readonly Action<string> RequestSaveSettings;

public ScriptHomeObject(Action<string> requestOpenFile,
Action requestNewWorkspace,
Expand All @@ -515,7 +567,8 @@ public ScriptHomeObject(Action<string> requestOpenFile,
Action<string> requestShowGuidedTour,
Action requestShowSampleFilesInFolder,
Action requestShowBackupFilesInFolder,
Action requestShowTemplate)
Action requestShowTemplate,
Action<string> requestSaveSettings)
{
RequestOpenFile = requestOpenFile;
RequestNewWorkspace = requestNewWorkspace;
Expand All @@ -526,7 +579,7 @@ public ScriptHomeObject(Action<string> requestOpenFile,
RequestShowSampleFilesInFolder = requestShowSampleFilesInFolder;
RequestShowBackupFilesInFolder = requestShowBackupFilesInFolder;
RequestShowTemplate = requestShowTemplate;

RequestSaveSettings = requestSaveSettings;
}
[DynamoJSInvokable]
public void OpenFile(string path)
Expand Down Expand Up @@ -574,7 +627,11 @@ public void ApplicationLoaded()
{
RequestApplicationLoaded();
}

[DynamoJSInvokable]
public void SaveSettings(string settings)
{
RequestSaveSettings(settings);
}
}

public enum GuidedTourType
Expand Down
22 changes: 22 additions & 0 deletions test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,5 +479,27 @@ public void TestSanitizeValues()

Assert.IsTrue(allTheGroupStylesHaveAValidFontSize, $"All the GroupStyles have a valid Font size : {allTheGroupStylesHaveAValidFontSize}");
}

[Test]
[Category("UnitTests")]
public void TestSerializingHomePageSettings()
{
string tempPath = System.IO.Path.GetTempPath();
tempPath = Path.Combine(tempPath, "homePagePreference.xml");

PreferenceSettings settings = new PreferenceSettings();

// Assert defaults
Assert.AreEqual(settings.HomePageSettings, null);
Assert.AreEqual(settings.HomePageSettingsSerialized, "null");

settings.HomePageSettings = new Dictionary<string, object> { { "greeting", "Hello World" } };

// Save
settings.Save(tempPath);
settings = PreferenceSettings.Load(tempPath);

Assert.AreEqual(settings.HomePageSettings["greeting"], "Hello World");
}
}
}
30 changes: 30 additions & 0 deletions test/DynamoCoreWpfTests/HomePageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ public void ActionDelegates_ShouldBeProperlySetAfterConstruction()
Assert.IsNotNull(homePage.RequestShowBackupFilesInFolder);
Assert.IsNotNull(homePage.RequestShowTemplate);
}

[Test]
public void HomePage_NewSettingsAreAddedCorrectly()
{
// Arrange
var vm = View.DataContext as DynamoViewModel;
var startPage = new StartPageViewModel(vm, true);
var preferences = startPage.DynamoViewModel.PreferenceSettings;
var homePage = new HomePage();

homePage.DataContext = startPage;

Assert.IsNull(preferences.HomePageSettings);

// Act
var pair1 = @"{""Name"": ""Alice""}";
homePage.SaveSettings(pair1);

// Assert
Assert.IsNotNull(preferences.HomePageSettings);
Assert.AreEqual(preferences.HomePageSettings.Count, 1);

// Act
var pair2 = @"{""Number"": 12 }";
homePage.SaveSettings(pair2);

// Assert
Assert.AreEqual(preferences.HomePageSettings.Count, 2);
}

#endregion

#region integration tests
Expand Down
Loading