Files
bluflame/intromat/Intromat/ViewModels/EndpointPropertyDescriptor.cs
2026-04-18 22:31:51 +02:00

104 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using NodeNetwork.Toolkit.ValueNode;
using NodeNetwork.ViewModels;
using ReactiveUI;
using Xceed.Wpf.Toolkit.PropertyGrid;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
namespace Intromat.ViewModels
{
public class EndpointEditor : ITypeEditor
{
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
var editor = ((EndpointPropertyDescriptor)propertyItem.PropertyDescriptor).Endpoint.Editor;
return new ViewModelViewHost { ViewModel = editor, HorizontalContentAlignment = HorizontalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Stretch };
}
}
public class EndpointPropertyDescriptor : PropertyDescriptor
{
public EndpointPropertyDescriptor(CodeGenNodeViewModel node, Endpoint endpoint, Attribute[] attrs)
: base(endpoint.Name, attrs)
{
Node = node;
Endpoint = endpoint;
ComponentType = typeof(CodeGenNodeViewModel);
PropertyType = endpoint.Editor.GetType();
IsReadOnly = false;
}
public static EndpointPropertyDescriptor Create(CodeGenNodeViewModel node, Endpoint endpoint, int index)
{
var attributes = new List<Attribute>();
if (endpoint.Group != null)
{
attributes.Add(new CategoryAttribute(endpoint.Group.Name));
}
else
{
attributes.Add(new CategoryAttribute(node.Name));
}
attributes.Add(new PropertyOrderAttribute(index));
attributes.Add(new EditorAttribute(typeof(EndpointEditor), typeof(EndpointEditor)));
return new EndpointPropertyDescriptor(node, endpoint, attributes.ToArray());
}
public override bool CanResetValue(object component)
{
return true;
}
public override object GetValue(object? component)
{
return GetEditorValue((dynamic)Endpoint.Editor);
}
public override void ResetValue(object component)
{
ResetEditorValue((dynamic)Endpoint.Editor);
}
public override void SetValue(object? component, object? value)
{
SetEditorValue((dynamic)Endpoint.Editor, (dynamic?)value);
}
private void SetEditorValue<T>(ValueEditorViewModel<T> editor, T value)
{
editor.Value = value;
Node.RaisePropertyChanged(Name);
}
private void ResetEditorValue<T>(ValueEditorViewModel<T> editor)
{
SetEditorValue(editor!, default!);
}
private static T GetEditorValue<T>(ValueEditorViewModel<T> editor)
{
return editor.Value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override Type ComponentType { get; }
public override bool IsReadOnly { get; }
public override Type PropertyType { get; }
public CodeGenNodeViewModel Node { get; }
public Endpoint Endpoint { get; }
}
}