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;
/// <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>
/// <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)
{
Target = target ?? throw new ArgumentNullException(nameof(target));
Path = path ?? throw new ArgumentNullException(nameof(path));
if (string.IsNullOrWhiteSpace(Path))
throw new ArgumentException("Path cannot be empty");
_segments = new LinkedList<DataModelPathSegment>();
Initialize(path);
Initialize(Path);
}
/// <summary>
@ -40,7 +50,7 @@ namespace Artemis.Core
public object Target { get; }
/// <summary>
/// Gets a string representation of the <see cref="DataModelPath" />
/// Gets the point-separated path associated with this <see cref="DataModelPath" />
/// </summary>
public string Path { get; }
@ -106,7 +116,7 @@ namespace Artemis.Core
/// <inheritdoc />
public override string ToString()
{
return Path;
return string.IsNullOrWhiteSpace(Path) ? "this" : Path;
}
private void Initialize(string path)
@ -114,12 +124,16 @@ namespace Artemis.Core
DataModelPathSegment startSegment = new DataModelPathSegment(this, "target", "target");
startSegment.Node = _segments.AddFirst(startSegment);
string[] segments = path.Split(".");
for (int index = 0; index < segments.Length; index++)
// On an empty path don't bother processing segments
if (!string.IsNullOrWhiteSpace(path))
{
string identifier = segments[index];
LinkedListNode<DataModelPathSegment> node = _segments.AddLast(new DataModelPathSegment(this, identifier, string.Join('.', segments.Take(index + 1))));
node.Value.Node = node;
string[] segments = path.Split(".");
for (int index = 0; index < segments.Length; index++)
{
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");

View File

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

View File

@ -34,7 +34,7 @@ namespace Artemis.UI.Shared.Services
{
DataModelPropertiesViewModel viewModel = new DataModelPropertiesViewModel(null, null, null);
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
viewModel.Update(this);