using System;
using System.Collections.Generic;
using Avalonia.PropertyGrid.Models;
namespace Avalonia.PropertyGrid.Editors;
///
/// Registry for custom property editors
///
public static class EditorRegistry
{
private static readonly Dictionary _typeEditors = new();
private static readonly Dictionary _propertyEditors = new();
///
/// Register a custom editor for a specific type
///
public static void RegisterEditor(Type propertyType, Type editorType)
{
if (!typeof(PropertyEditor).IsAssignableFrom(editorType))
{
throw new ArgumentException($"Editor type must inherit from PropertyEditor", nameof(editorType));
}
_typeEditors[propertyType] = editorType;
}
///
/// Register a custom editor for a specific property by name
///
public static void RegisterEditor(string propertyName, Type editorType)
{
if (!typeof(PropertyEditor).IsAssignableFrom(editorType))
{
throw new ArgumentException($"Editor type must inherit from PropertyEditor", nameof(editorType));
}
_propertyEditors[propertyName] = editorType;
}
///
/// Get the editor type for a property item
///
public static Type? GetEditorType(PropertyItem item)
{
// Check for explicit editor type on the item
if (item.EditorType != null)
{
return item.EditorType;
}
// Check for property-specific editor
if (_propertyEditors.TryGetValue(item.Name, out var propertyEditor))
{
return propertyEditor;
}
// Check for type-specific editor
if (_typeEditors.TryGetValue(item.PropertyType, out var typeEditor))
{
return typeEditor;
}
// Return default editor based on type
return GetDefaultEditorType(item.PropertyType);
}
private static Type? GetDefaultEditorType(Type propertyType)
{
if (propertyType == typeof(string))
return typeof(StringEditor);
if (propertyType == typeof(bool))
return typeof(BooleanEditor);
if (propertyType == typeof(int) || propertyType == typeof(long) ||
propertyType == typeof(short) || propertyType == typeof(byte) ||
propertyType == typeof(uint) || propertyType == typeof(ulong) ||
propertyType == typeof(ushort) || propertyType == typeof(sbyte) ||
propertyType == typeof(float) || propertyType == typeof(double) ||
propertyType == typeof(decimal))
return typeof(NumericEditor);
if (propertyType.IsEnum)
return typeof(EnumEditor);
if (propertyType == typeof(DateTime))
return typeof(DateTimeEditor);
return null; // Will use default TextBox
}
}