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-4543 Localize Default Group Styles #12760

Merged
merged 4 commits into from
Apr 4, 2022
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
19 changes: 18 additions & 1 deletion src/DynamoCore/Configuration/GroupStyleItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
namespace Dynamo.Configuration
using System.Collections.ObjectModel;
using Dynamo.Properties;

namespace Dynamo.Configuration
{
/// <summary>
/// Group specific style item
/// Note: This class does not contain special property yet comparing to base class
/// </summary>
public class GroupStyleItem: StyleItem
{
/// <summary>
/// Static set of default group styles defined by Dynamo Team
/// </summary>
public static ObservableCollection<GroupStyleItem> DefaultGroupStyleItems =
new ObservableCollection<GroupStyleItem>() {
new GroupStyleItem() { Name = Resources.GroupStyleDefaultActions, HexColorString = Resources.GroupStyleDefaultActionsColor, IsDefault = true },
new GroupStyleItem() { Name = Resources.GroupStyleDefaultInputs, HexColorString = Resources.GroupStyleDefaultInputsColor, IsDefault = true },
new GroupStyleItem() { Name = Resources.GroupStyleDefaultOutputs, HexColorString = Resources.GroupStyleDefaultOutputsColor, IsDefault = true },
new GroupStyleItem() { Name = Resources.GroupStyleDefaultReview, HexColorString = Resources.GroupStyleDefaultReviewColor, IsDefault = true }
};
}
}
27 changes: 4 additions & 23 deletions src/DynamoCore/Configuration/PreferenceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Dynamo.Graph.Connectors;
using Dynamo.Interfaces;
using Dynamo.Models;
using Dynamo.Properties;

namespace Dynamo.Configuration
{
Expand Down Expand Up @@ -534,9 +533,6 @@ public PreferenceSettings()
DefaultPythonEngine = string.Empty;
ViewExtensionSettings = new List<ViewExtensionSettings>();
GroupStyleItemsList = new List<GroupStyleItem>();

//Add the default group styles
AddDefaultStyles();
}

/// <summary>
Expand Down Expand Up @@ -585,7 +581,7 @@ public bool SaveInternal(string preferenceFilePath)

/// <summary>
/// Loads PreferenceSettings from specified XML file if possible,
/// else initialises PreferenceSettings with default values.
/// else initializes PreferenceSettings with default values.
/// </summary>
/// <param name="filePath">Path of the XML File</param>
/// <returns>
Expand All @@ -594,10 +590,11 @@ public bool SaveInternal(string preferenceFilePath)
/// </returns>
public static PreferenceSettings Load(string filePath)
{
var settings = new PreferenceSettings();
// Constructor will be called anyway in either condition below so no need to initialize now.
PreferenceSettings settings = null;

if (String.IsNullOrEmpty(filePath) || (!File.Exists(filePath)))
return settings;
return new PreferenceSettings();

try
{
Expand All @@ -615,22 +612,6 @@ public static PreferenceSettings Load(string filePath)
return settings;
}

/// <summary>
/// This method will add the Defaul GroupStyles that are shown in the Preferences panel
/// </summary>
public void AddDefaultStyles()
{
var defaultGroupStylesList = GroupStyleItemsList.Where(style => style.IsDefault == true);
//Just in case the Default profiles have not been added then are added.
if (defaultGroupStylesList != null && defaultGroupStylesList.Count() == 0)
{
GroupStyleItemsList.Add(new GroupStyleItem() { Name = Resources.GroupStyleDefaultActions, HexColorString = Resources.GroupStyleDefaultActionsColor, IsDefault = true });
GroupStyleItemsList.Add(new GroupStyleItem() { Name = Resources.GroupStyleDefaultInputs, HexColorString = Resources.GroupStyleDefaultInputsColor, IsDefault = true });
GroupStyleItemsList.Add(new GroupStyleItem() { Name = Resources.GroupStyleDefaultOutputs, HexColorString = Resources.GroupStyleDefaultOutputsColor, IsDefault = true });
GroupStyleItemsList.Add(new GroupStyleItem() { Name = Resources.GroupStyleDefaultReview, HexColorString = Resources.GroupStyleDefaultReviewColor, IsDefault = true });
}
}

/// <summary>
/// Returns the static Python template file path.
/// When the file exists and is not empty, its contents are used to populate new Python Script nodes added to the Dynamo workspace.
Expand Down
115 changes: 58 additions & 57 deletions src/DynamoCoreWpf/ViewModels/Menu/PreferencesViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using Dynamo.Configuration;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using Dynamo.Configuration;
using Dynamo.Core;
using Dynamo.Events;
using Dynamo.Graph.Workspaces;
Expand All @@ -8,14 +16,6 @@
using Dynamo.PythonServices;
using Dynamo.Utilities;
using Dynamo.Wpf.ViewModels.Core.Converters;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using Res = Dynamo.Wpf.Properties.Resources;

namespace Dynamo.ViewModels
Expand All @@ -39,7 +39,6 @@ public class PreferencesViewModel : ViewModelBase, INotifyPropertyChanged
private ObservableCollection<string> packagePathsForInstall;
private ObservableCollection<string> fontSizeList;
private ObservableCollection<string> numberFormatList;
private ObservableCollection<StyleItem> styleItemsList;
private StyleItem addStyleControl;
private ObservableCollection<string> pythonEngineList;

Expand Down Expand Up @@ -411,24 +410,27 @@ public ObservableCollection<string> NumberFormatList
/// <summary>
/// This will contain a list of all the Styles created by the user in the Styles list ( Visual Settings -> Group Styles section)
/// </summary>
public ObservableCollection<StyleItem> StyleItemsList
public ObservableCollection<GroupStyleItem> StyleItemsList
{
get { return styleItemsList; }
get { return preferenceSettings.GroupStyleItemsList.ToObservableCollection(); }
set
{
styleItemsList = value;
preferenceSettings.GroupStyleItemsList = value.ToList<GroupStyleItem>();
RaisePropertyChanged(nameof(StyleItemsList));
}
}

/// <summary>
/// Used to add styles to the StyleItemsListe while also update the saved changes label
/// </summary>
/// <param name="style"></param>
/// <param name="style">style to be added</param>
public void AddStyle(StyleItem style)
{
StyleItemsList.Add(style);
preferenceSettings.GroupStyleItemsList.Add(new GroupStyleItem { HexColorString = style.HexColorString, Name = style.Name, IsDefault = style.IsDefault });
preferenceSettings.GroupStyleItemsList.Add(new GroupStyleItem {
HexColorString = style.HexColorString,
Name = style.Name,
IsDefault = style.IsDefault
});
RaisePropertyChanged(nameof(StyleItemsList));
}

Expand Down Expand Up @@ -815,19 +817,24 @@ public PreferencesViewModel(DynamoViewModel dynamoViewModel)
LanguagesList = new ObservableCollection<string>(languages.Split(','));
SelectedLanguage = languages.Split(',').First();

FontSizeList = new ObservableCollection<string>();
FontSizeList.Add(Wpf.Properties.Resources.ScalingSmallButton);
FontSizeList.Add(Wpf.Properties.Resources.ScalingMediumButton);
FontSizeList.Add(Wpf.Properties.Resources.ScalingLargeButton);
FontSizeList.Add(Wpf.Properties.Resources.ScalingExtraLargeButton);
FontSizeList = new ObservableCollection<string>
{
Wpf.Properties.Resources.ScalingSmallButton,
Wpf.Properties.Resources.ScalingMediumButton,
Wpf.Properties.Resources.ScalingLargeButton,
Wpf.Properties.Resources.ScalingExtraLargeButton
};
SelectedFontSize = Wpf.Properties.Resources.ScalingMediumButton;

NumberFormatList = new ObservableCollection<string>();
NumberFormatList.Add(Wpf.Properties.Resources.DynamoViewSettingMenuNumber0);
NumberFormatList.Add(Wpf.Properties.Resources.DynamoViewSettingMenuNumber00);
NumberFormatList.Add(Wpf.Properties.Resources.DynamoViewSettingMenuNumber000);
NumberFormatList.Add(Wpf.Properties.Resources.DynamoViewSettingMenuNumber0000);
NumberFormatList.Add(Wpf.Properties.Resources.DynamoViewSettingMenuNumber00000);
// Number format settings
NumberFormatList = new ObservableCollection<string>
{
Wpf.Properties.Resources.DynamoViewSettingMenuNumber0,
Wpf.Properties.Resources.DynamoViewSettingMenuNumber00,
Wpf.Properties.Resources.DynamoViewSettingMenuNumber000,
Wpf.Properties.Resources.DynamoViewSettingMenuNumber0000,
Wpf.Properties.Resources.DynamoViewSettingMenuNumber00000
};
SelectedNumberFormat = preferenceSettings.NumberFormat;

runSettingsIsChecked = preferenceSettings.DefaultRunType;
Expand All @@ -836,8 +843,9 @@ public PreferencesViewModel(DynamoViewModel dynamoViewModel)
//By Default the warning state of the Visual Settings tab (Group Styles section) will be disabled
isWarningEnabled = false;

StyleItemsList = LoadStyles(preferenceSettings.GroupStyleItemsList);

// Initialize group styles with default and non-default GroupStyleItems
StyleItemsList = GroupStyleItem.DefaultGroupStyleItems.AddRange(preferenceSettings.GroupStyleItemsList.Where(style => style.IsDefault != true)).ToObservableCollection();

//When pressing the "Add Style" button some controls will be shown with some values by default so later they can be populated by the user
AddStyleControl = new StyleItem() { Name = string.Empty, HexColorString = GetRandomHexStringColor() };

Expand All @@ -859,6 +867,7 @@ public PreferencesViewModel(DynamoViewModel dynamoViewModel)
SavedChangesLabel = string.Empty;
SavedChangesTooltip = string.Empty;

// Add tabs
preferencesTabs = new Dictionary<string, TabSettings>();
preferencesTabs.Add("General", new TabSettings() { Name = "General", ExpanderActive = string.Empty });
preferencesTabs.Add("Features",new TabSettings() { Name = "Features", ExpanderActive = string.Empty });
Expand All @@ -881,22 +890,7 @@ public PreferencesViewModel(DynamoViewModel dynamoViewModel)
}

/// <summary>
/// This method loads the group styles defined by the user and stored in the xml file
/// </summary>
/// <param name="styleItemsList"></param>
/// <returns></returns>
private ObservableCollection<StyleItem> LoadStyles(IEnumerable<Configuration.StyleItem> styleItemsList)
{
ObservableCollection<StyleItem> styles = new ObservableCollection<StyleItem>();
foreach (var style in styleItemsList)
{
styles.Add(new StyleItem { Name = style.Name, HexColorString = style.HexColorString, IsDefault = style.IsDefault });
}
return styles;
}

/// <summary>
/// This method will be executed everytime the WorkspaceModel.ScaleFactor value is updated.
/// This method will be executed every time the WorkspaceModel.ScaleFactor value is updated.
/// </summary>
/// <param name="args"></param>
private void PreferencesViewModel_WorkspaceSettingsChanged(WorkspacesSettingsChangedEventArgs args)
Expand Down Expand Up @@ -1090,28 +1084,35 @@ internal void UpdateSavedChangesLabel()
}

/// <summary>
/// This method will remove the current Style selected from the Styles list
/// This method will remove the current Style selected from the Styles list by name
/// </summary>
/// <param name="groupName"></param>
internal void RemoveStyleEntry(string groupName)
/// <param name="styleName"></param>
internal void RemoveStyleEntry(string styleName)
{
StyleItem itemToRemove = (from item in StyleItemsList where item.Name.Equals(groupName) select item).FirstOrDefault();
StyleItemsList.Remove(itemToRemove);

GroupStyleItem itemToRemovePreferences = preferenceSettings.GroupStyleItemsList.FirstOrDefault(x => x.Name.Equals(groupName));
GroupStyleItem itemToRemovePreferences = preferenceSettings.GroupStyleItemsList.FirstOrDefault(x => x.Name.Equals(styleName));
preferenceSettings.GroupStyleItemsList.Remove(itemToRemovePreferences);

RaisePropertyChanged(nameof(StyleItemsList));
UpdateSavedChangesLabel();
}

/// <summary>
/// This method will check if the Style that is being created already exists in the Styles list
/// This method will check if the name of Style that is being created already exists in the Styles list
/// </summary>
/// <param name="item">target style item to check</param>
/// <returns></returns>
internal bool IsStyleNameValid(StyleItem item)
{
return StyleItemsList.Where(x => x.Name.Equals(item.Name)).Any();
}

/// <summary>
/// This method will check if the name of Style that is being created already exists in the Styles list
/// </summary>
/// <param name="item1"></param>
/// <param name="item">target style to be checked</param>
/// <returns></returns>
internal bool ValidateExistingStyle(StyleItem item1)
internal bool ValidateStyleGuid(StyleItem item)
{
return StyleItemsList.Where(x => x.Name.Equals(item1.Name)).Any();
return StyleItemsList.Where(x => x.Name.Equals(item.Name)).Any();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private void AddStyle_SaveButton_Click(object sender, RoutedEventArgs e)
viewModel.EnableGroupStyleWarningState(Res.PreferencesViewEmptyStyleWarning);
}
//Means that the Style name to be created already exists
else if (viewModel.ValidateExistingStyle(newItem))
else if (viewModel.IsStyleNameValid(newItem))
{
viewModel.EnableGroupStyleWarningState(Res.PreferencesViewAlreadyExistingStyleWarning);
}
Expand Down
10 changes: 5 additions & 5 deletions test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Dynamo.Configuration;
using System.IO;
using Dynamo.Configuration;
using Dynamo.Models;
using NUnit.Framework;
using System.IO;

namespace Dynamo.Tests.Configuration
{
Expand Down Expand Up @@ -132,9 +132,9 @@ public void TestSettingsSerialization()
Assert.AreEqual(windowSettings.Height, 321);
Assert.AreEqual(windowSettings.Width, 654);
Assert.AreEqual(windowSettings.Status, WindowStatus.Maximized);
// 5 styles in total, 4 default ones plus the one added in test
Assert.AreEqual(settings.GroupStyleItemsList.Count, 5);
var styleItemsList = settings.GroupStyleItemsList[4];
// Load function will only deserialize the customized style
Assert.AreEqual(settings.GroupStyleItemsList.Count, 1);
var styleItemsList = settings.GroupStyleItemsList[0];
Assert.AreEqual(styleItemsList.Name, "TestGroup");
Assert.AreEqual(styleItemsList.HexColorString, "000000");
}
Expand Down
16 changes: 8 additions & 8 deletions test/DynamoCoreWpfTests/CoreUITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public void PreferenceSetting_RenderPrecision()
[Category("DynamoUI")]
public void PreferenceSetting_GroupStyles()
{
// Test that thte group style list is being initialized with an empty list
// Test that the group style list is being initialized with a non-empty list
Assert.NotNull(ViewModel.PreferenceSettings.GroupStyleItemsList);

//Now by default we will have always 4 GroupStyles added by Dynamo
Expand All @@ -534,17 +534,17 @@ public void PreferenceSetting_GroupStyles()
initalSetting.Save(tempPath);
resultSetting = PreferenceSettings.Load(tempPath);

// Test if the fields are being saved
Assert.AreEqual(5, initalSetting.GroupStyleItemsList.Count);
Assert.AreEqual(resultSetting.GroupStyleItemsList[4].Name, initalSetting.GroupStyleItemsList[4].Name);
Assert.AreEqual(resultSetting.GroupStyleItemsList[4].HexColorString, initalSetting.GroupStyleItemsList[4].HexColorString);
// Test if the customized group styles can be loaded
Assert.AreEqual(1, initalSetting.GroupStyleItemsList.Count);
Assert.AreEqual(resultSetting.GroupStyleItemsList[0].Name, initalSetting.GroupStyleItemsList[0].Name);
Assert.AreEqual(resultSetting.GroupStyleItemsList[0].HexColorString, initalSetting.GroupStyleItemsList[0].HexColorString);

// Test loading the settings defined in the xml configuration file
var filePath = Path.Combine(GetTestDirectory(ExecutingDirectory), @"settings\DynamoSettings-OneGroupStyle.xml");
PreferenceSettings OneGroupStyle = PreferenceSettings.Load(filePath);
Assert.AreEqual(5, OneGroupStyle.GroupStyleItemsList.Count);
Assert.AreEqual(OneGroupStyle.GroupStyleItemsList[4].Name, initalSetting.GroupStyleItemsList[4].Name);
Assert.AreEqual(OneGroupStyle.GroupStyleItemsList[4].HexColorString, initalSetting.GroupStyleItemsList[4].HexColorString);
Assert.AreEqual(1, OneGroupStyle.GroupStyleItemsList.Count);
Assert.AreEqual(OneGroupStyle.GroupStyleItemsList[0].Name, initalSetting.GroupStyleItemsList[0].Name);
Assert.AreEqual(OneGroupStyle.GroupStyleItemsList[0].HexColorString, initalSetting.GroupStyleItemsList[0].HexColorString);
}

[Test]
Expand Down
Loading