1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Data model paths - Allow empty paths pointing directly to the target

This commit is contained in:
Robert 2020-10-06 19:28:18 +02:00
parent 9e39a849e9
commit e2918798a4
3 changed files with 47 additions and 31 deletions

View File

@ -18,20 +18,30 @@ namespace Artemis.Core
private readonly LinkedList<DataModelPathSegment> _segments; private readonly LinkedList<DataModelPathSegment> _segments;
/// <summary> /// <summary>
/// Creates a new instance of the <see cref="DataModelPath" /> class /// Creates a new instance of the <see cref="DataModelPath" /> class pointing directly to the target
/// </summary> /// </summary>
/// <param name="target">The target at which this path starts</param> /// <param name="target">The target at which this path starts</param>
/// <param name="path">A string representation of the <see cref="DataModelPath" /></param> public DataModelPath(object target)
{
Target = target ?? throw new ArgumentNullException(nameof(target));
Path = "";
_segments = new LinkedList<DataModelPathSegment>();
Initialize(Path);
}
/// <summary>
/// Creates a new instance of the <see cref="DataModelPath" /> class pointing to the provided path
/// </summary>
/// <param name="target">The target at which this path starts</param>
/// <param name="path">A point-separated path</param>
public DataModelPath(object target, string path) public DataModelPath(object target, string path)
{ {
Target = target ?? throw new ArgumentNullException(nameof(target)); Target = target ?? throw new ArgumentNullException(nameof(target));
Path = path ?? throw new ArgumentNullException(nameof(path)); Path = path ?? throw new ArgumentNullException(nameof(path));
if (string.IsNullOrWhiteSpace(Path))
throw new ArgumentException("Path cannot be empty");
_segments = new LinkedList<DataModelPathSegment>(); _segments = new LinkedList<DataModelPathSegment>();
Initialize(path); Initialize(Path);
} }
/// <summary> /// <summary>
@ -40,7 +50,7 @@ namespace Artemis.Core
public object Target { get; } public object Target { get; }
/// <summary> /// <summary>
/// Gets a string representation of the <see cref="DataModelPath" /> /// Gets the point-separated path associated with this <see cref="DataModelPath" />
/// </summary> /// </summary>
public string Path { get; } public string Path { get; }
@ -106,7 +116,7 @@ namespace Artemis.Core
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() public override string ToString()
{ {
return Path; return string.IsNullOrWhiteSpace(Path) ? "this" : Path;
} }
private void Initialize(string path) private void Initialize(string path)
@ -114,12 +124,16 @@ namespace Artemis.Core
DataModelPathSegment startSegment = new DataModelPathSegment(this, "target", "target"); DataModelPathSegment startSegment = new DataModelPathSegment(this, "target", "target");
startSegment.Node = _segments.AddFirst(startSegment); startSegment.Node = _segments.AddFirst(startSegment);
string[] segments = path.Split("."); // On an empty path don't bother processing segments
for (int index = 0; index < segments.Length; index++) if (!string.IsNullOrWhiteSpace(path))
{ {
string identifier = segments[index]; string[] segments = path.Split(".");
LinkedListNode<DataModelPathSegment> node = _segments.AddLast(new DataModelPathSegment(this, identifier, string.Join('.', segments.Take(index + 1)))); for (int index = 0; index < segments.Length; index++)
node.Value.Node = node; {
string identifier = segments[index];
LinkedListNode<DataModelPathSegment> node = _segments.AddLast(new DataModelPathSegment(this, identifier, string.Join('.', segments.Take(index + 1))));
node.Value.Node = node;
}
} }
ParameterExpression parameter = Expression.Parameter(typeof(object), "t"); ParameterExpression parameter = Expression.Parameter(typeof(object), "t");

View File

@ -39,12 +39,12 @@ namespace Artemis.UI.Shared
public DataModelPath DataModelPath { get; } public DataModelPath DataModelPath { get; }
public string Path => DataModelPath?.Path; public string Path => DataModelPath?.Path;
public int Depth { get; set; } public int Depth { get; private set; }
public DataModel DataModel public DataModel DataModel
{ {
get => _dataModel; get => _dataModel;
set => SetAndNotify(ref _dataModel, value); protected set => SetAndNotify(ref _dataModel, value);
} }
public DataModelPropertyAttribute PropertyDescription public DataModelPropertyAttribute PropertyDescription
@ -140,12 +140,15 @@ namespace Artemis.UI.Shared
public DataModelVisualizationViewModel GetChildByPath(Guid dataModelGuid, string propertyPath) public DataModelVisualizationViewModel GetChildByPath(Guid dataModelGuid, string propertyPath)
{ {
if (DataModel.PluginInfo.Guid != dataModelGuid) if (!IsRootViewModel)
return null; {
if (propertyPath == null) if (DataModel.PluginInfo.Guid != dataModelGuid)
return null; return null;
if (Path.StartsWith(propertyPath)) if (propertyPath == null)
return null; return null;
if (Path.StartsWith(propertyPath, StringComparison.OrdinalIgnoreCase))
return null;
}
// Ensure children are populated by requesting an update // Ensure children are populated by requesting an update
if (!IsVisualizationExpanded) if (!IsVisualizationExpanded)
@ -191,7 +194,7 @@ namespace Artemis.UI.Shared
// Add missing static children // Add missing static children
foreach (PropertyInfo propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) foreach (PropertyInfo propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{ {
string childPath = Path != null ? $"{Path}.{propertyInfo.Name}" : propertyInfo.Name; string childPath = AppendToPath(propertyInfo.Name);
if (Children.Any(c => c.Path != null && c.Path.Equals(childPath))) if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))
continue; continue;
if (propertyInfo.GetCustomAttribute<DataModelIgnoreAttribute>() != null) if (propertyInfo.GetCustomAttribute<DataModelIgnoreAttribute>() != null)
@ -210,7 +213,7 @@ namespace Artemis.UI.Shared
hiddenProperties = DataModel.GetHiddenProperties(); hiddenProperties = DataModel.GetHiddenProperties();
foreach (PropertyInfo hiddenProperty in hiddenProperties) foreach (PropertyInfo hiddenProperty in hiddenProperties)
{ {
string childPath = Path != null ? $"{Path}.{hiddenProperty.Name}" : hiddenProperty.Name; string childPath = AppendToPath(hiddenProperty.Name);
DataModelVisualizationViewModel toRemove = Children.FirstOrDefault(c => c.Path != null && c.Path == childPath); DataModelVisualizationViewModel toRemove = Children.FirstOrDefault(c => c.Path != null && c.Path == childPath);
if (toRemove != null) if (toRemove != null)
Children.Remove(toRemove); Children.Remove(toRemove);
@ -226,7 +229,7 @@ namespace Artemis.UI.Shared
{ {
foreach (KeyValuePair<string, DataModel> kvp in dataModel.DynamicDataModels) foreach (KeyValuePair<string, DataModel> kvp in dataModel.DynamicDataModels)
{ {
string childPath = Path != null ? $"{Path}.{kvp.Key}" : kvp.Key; string childPath = AppendToPath(kvp.Key);
if (Children.Any(c => c.Path != null && c.Path.Equals(childPath))) if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))
continue; continue;
@ -277,6 +280,11 @@ namespace Artemis.UI.Shared
return null; return null;
} }
private string AppendToPath(string toAppend)
{
return !string.IsNullOrEmpty(Path) ? $"{Path}.{toAppend}" : toAppend;
}
private void RequestUpdate() private void RequestUpdate()
{ {
Parent?.RequestUpdate(); Parent?.RequestUpdate();
@ -294,10 +302,4 @@ namespace Artemis.UI.Shared
#endregion #endregion
} }
public enum DataModelConditionSide
{
Left,
Right
}
} }

View File

@ -34,7 +34,7 @@ namespace Artemis.UI.Shared.Services
{ {
DataModelPropertiesViewModel viewModel = new DataModelPropertiesViewModel(null, null, null); DataModelPropertiesViewModel viewModel = new DataModelPropertiesViewModel(null, null, null);
foreach (DataModel dataModelExpansion in _dataModelService.GetDataModels()) foreach (DataModel dataModelExpansion in _dataModelService.GetDataModels())
viewModel.Children.Add(new DataModelPropertiesViewModel(dataModelExpansion, viewModel, null)); viewModel.Children.Add(new DataModelPropertiesViewModel(dataModelExpansion, viewModel, new DataModelPath(dataModelExpansion)));
// Update to populate children // Update to populate children
viewModel.Update(this); viewModel.Update(this);