Add type constraints for resource managers. Generalize detail panel.

This commit is contained in:
2023-11-29 13:52:34 +08:00
parent a471272c52
commit 111c500f3b
7 changed files with 88 additions and 45 deletions

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System;
using System.Linq;
namespace Cryville.Crtr.Browsing.UI {
internal static class MetaPropertyExtensions {
public static IEnumerable<KeyValuePair<string, object>> EnumerateProperty(this MetaProperty prop, bool shortDistinct = false) {
if (prop.Values.ContainsKey("distinct-primary") && shortDistinct) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-primary"]);
if (prop.Values.ContainsKey("distinct-secondary")) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-secondary"]);
if (prop.Values.ContainsKey("distinct-tertiary")) {
yield return new KeyValuePair<string, object>(prop.Name, prop.Values["distinct-tertiary"]);
}
}
}
else yield return new KeyValuePair<string, object>(prop.Name, prop.MainValue);
}
public static IEnumerable<KeyValuePair<string, object>> EnumerateProperties(this IResourceMeta meta, bool shortDistinct = false) {
foreach (var prop in meta.Properties) {
foreach (var item in EnumerateProperty(prop, shortDistinct)) yield return item;
}
}
public static IEnumerable<KeyValuePair<string, object>> EnumerateBasicProperties(this IResourceMeta meta, bool shortDistinct = false) {
foreach (var prop in EnumerateProperties(meta, shortDistinct)) {
if (prop.Value is string || prop.Value is IFormattable) {
yield return new KeyValuePair<string, object>(prop.Key, prop.Value);
}
}
}
public static IEnumerable<KeyValuePair<string, T>> EnumerateProperties<T>(this IResourceMeta meta, bool shortDistinct = false) {
return EnumerateProperties(meta, shortDistinct)
.Where(prop => prop.Value is T)
.Select(prop => new KeyValuePair<string, T>(prop.Key, (T)prop.Value));
}
}
}