mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Core - Added GetDisplayName Type extension
Data models - Improved nullable value type handling
This commit is contained in:
parent
e6417585b5
commit
7b7b75be78
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Humanizer;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
@ -63,6 +64,25 @@ namespace Artemis.Core
|
||||
|| value is decimal;
|
||||
}
|
||||
|
||||
private static readonly Dictionary<Type, string> TypeKeywords = new Dictionary<Type, string>()
|
||||
{
|
||||
{typeof(bool), "bool"},
|
||||
{typeof(byte), "byte"},
|
||||
{typeof(sbyte), "sbyte"},
|
||||
{typeof(char), "char"},
|
||||
{typeof(decimal), "decimal"},
|
||||
{typeof(double), "double"},
|
||||
{typeof(float), "float"},
|
||||
{typeof(int), "int"},
|
||||
{typeof(uint), "uint"},
|
||||
{typeof(long), "long"},
|
||||
{typeof(ulong), "ulong"},
|
||||
{typeof(short), "short"},
|
||||
{typeof(ushort), "ushort"},
|
||||
{typeof(object), "object"},
|
||||
{typeof(string), "string"},
|
||||
};
|
||||
|
||||
// From https://stackoverflow.com/a/2224421/5015269 but inverted and renamed to match similar framework methods
|
||||
/// <summary>
|
||||
/// Determines whether an instance of a specified type can be casted to a variable of the current type
|
||||
@ -127,5 +147,27 @@ namespace Artemis.Core
|
||||
|
||||
return enumerableType?.GenericTypeArguments[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines a display name for the given type
|
||||
/// </summary>
|
||||
/// <param name="type">The type to determine the name for</param>
|
||||
/// <param name="humanize">Whether or not to humanize the result, defaults to false</param>
|
||||
/// <returns></returns>
|
||||
public static string GetDisplayName(this Type type, bool humanize = false)
|
||||
{
|
||||
if (!type.IsGenericType)
|
||||
{
|
||||
string displayValue = TypeKeywords.TryGetValue(type, out string? keyword) ? keyword! : type.Name;
|
||||
return humanize ? displayValue.Humanize() : displayValue;
|
||||
}
|
||||
|
||||
Type genericTypeDefinition = type.GetGenericTypeDefinition();
|
||||
if (genericTypeDefinition == typeof(Nullable<>))
|
||||
return type.GenericTypeArguments[0].GetDisplayName(humanize) + "?";
|
||||
|
||||
string stripped = genericTypeDefinition.Name.Split('`')[0];
|
||||
return $"{stripped}<{string.Join(", ", type.GenericTypeArguments.Select(t => t.GetDisplayName(humanize)))}>";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -260,7 +260,7 @@ namespace Artemis.UI.Shared
|
||||
if (typeViewModel != null)
|
||||
return new DataModelPropertyViewModel(DataModel, this, dataModelPath) {DisplayViewModel = typeViewModel, Depth = depth};
|
||||
// For primitives, create a property view model, it may be null that is fine
|
||||
if (propertyType.IsPrimitive || propertyType.IsEnum || propertyType == typeof(string))
|
||||
if (propertyType.IsPrimitive || propertyType.IsEnum || propertyType == typeof(string) || propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
return new DataModelPropertyViewModel(DataModel, this, dataModelPath) {Depth = depth};
|
||||
if (propertyType.IsGenericEnumerable())
|
||||
return new DataModelListViewModel(DataModel, this, dataModelPath) {Depth = depth};
|
||||
|
||||
@ -12,12 +12,17 @@ namespace Artemis.Plugins.DataModelExpansions.TestData.DataModels
|
||||
ListItems = new List<SomeListItem>();
|
||||
for (int i = 0; i < 20; i++)
|
||||
ListItems.Add(new SomeListItem {ItemName = $"Item {i + 1}", Number = i});
|
||||
|
||||
GenericTest = new Test<string> {Value = "Generic string value"};
|
||||
}
|
||||
|
||||
// Your datamodel can have regular properties and you can annotate them if you'd like
|
||||
[DataModelProperty(Name = "A test string", Description = "It doesn't do much, but it's there.")]
|
||||
public string TemplateDataModelString { get; set; }
|
||||
|
||||
public int? NullableInt { get; set; }
|
||||
public Test<string> GenericTest { get; set; }
|
||||
|
||||
public SKColor TestColorA { get; set; }
|
||||
public SKColor TestColorB { get; set; }
|
||||
|
||||
@ -31,6 +36,11 @@ namespace Artemis.Plugins.DataModelExpansions.TestData.DataModels
|
||||
public List<SomeListItem> ListItems { get; set; }
|
||||
}
|
||||
|
||||
public class Test<T>
|
||||
{
|
||||
public T Value { get; set; }
|
||||
}
|
||||
|
||||
public class SomeListItem
|
||||
{
|
||||
public string ItemName { get; set; }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user