chore: Clean BTSColorExtension

This commit is contained in:
John Colvin committed 2022-07-17 12:58:59 -05:00
1 parent d042da8708
commit db22dc2853
2 files changed
+79 -109

No files matched your search

@@ -1,118 +1,76 @@
using bts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
#nullable enable
namespace mbts.ui {
[MarkupExtensionReturnType(typeof(object))]
public class BTSColorExtension : MarkupExtension {
using Spark.Core.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
[MarkupExtensionReturnType]
class BTSColorExtension : MarkupExtension {
static readonly Dictionary<string, Func<BTSColor>>
_color_map = _normalized_color_props(typeof(BTSColors), null).ToDictionary();
public BTSColorExtension() { }
public BTSColorExtension(string name) {
public BTSColorExtension(string? name) =>
Name = name;
}
// The color_dictionary needs to be populated once
static BTSColorExtension() {
// get all the colors in the nested types
foreach (Type t in typeof(BTSColors).GetNestedTypes(BindingFlags.Public | BindingFlags.Static)) {
foreach (PropertyInfo p in t.GetProperties(BindingFlags.Public | BindingFlags.Static)) {
if (p.PropertyType == typeof(BTSColor))
color_dictionary.Add("{0}.{1}".fmt(t.Name, p.Name), (BTSColor)p.GetValue(null));
else if (p.PropertyType == typeof(Color))
color_dictionary.Add("{0}.{1}".fmt(t.Name, p.Name), (Color)p.GetValue(null));
}
// get the colors defined in types nested within nested types
foreach (Type st in t.GetNestedTypes(BindingFlags.Public | BindingFlags.Static)) {
foreach (PropertyInfo np in st.GetProperties(BindingFlags.Public | BindingFlags.Static)) {
if (np.PropertyType == typeof(BTSColor))
color_dictionary.Add("{0}.{1}.{2}".fmt(t.Name, st.Name, np.Name), (BTSColor)np.GetValue(null));
}
}
}
// get the colors defined in the base class
foreach (PropertyInfo p in typeof(BTSColors).GetProperties(BindingFlags.Public | BindingFlags.Static)) {
if (p.PropertyType == typeof(BTSColor))
color_dictionary.Add(p.Name, (BTSColor)p.GetValue(null));
else if (p.PropertyType == typeof(Color))
color_dictionary.Add(p.Name, (Color)p.GetValue(null));
}
}
public override object ProvideValue(IServiceProvider serviceProvider) {
if (serviceProvider == null) { return null; }
// get the target of the extension from the IServiceProvider interface
IProvideValueTarget _ipvt = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
// Get the Name of the attribute
String property_name = "";
Type property_type = null;
//if property is not part of the VisualTree (Style defined in Resources, or ControlTemplate Triggers etc.)
if (_ipvt.TargetProperty == null)
property_type = typeof(Color);
else if (_ipvt.TargetProperty is DependencyProperty) {
DependencyProperty target_property = _ipvt.TargetProperty as DependencyProperty;
property_name = target_property.Name;
property_type = target_property.PropertyType;
} else { // else it must be PropertyInfo
PropertyInfo target_property = _ipvt.TargetProperty as PropertyInfo;
property_name = target_property.Name;
}
// if the color name exists in the dictionary then return the color/brush
BTSColor out_color = null;
if (color_dictionary.TryGetValue(Name, out out_color)) {
if (Lighten != 0) {
out_color = out_color.lighten(Lighten);
}
if (Darken != 0) {
out_color = out_color.darken(Darken);
}
if (Alpha != 0) {
out_color = new BTSColor((Byte)Alpha, out_color.r, out_color.g, out_color.b);
}
// if the property type is a Color then return a Color
// else return a brush
if (property_type != null && property_type == typeof(Color)) {
return (Color)out_color;
} else {
return (Brush)out_color;
}
} else { // if the color is not in the dictionary then throw an exception
Log.assert_d(false, "The color '{0}' could not be found".fmt(Name));
throw new ColorNotFoundException();
}
}
// Create dictionary of colors
public static readonly Dictionary<String, BTSColor> color_dictionary = new Dictionary<string, BTSColor>();
[ConstructorArgument("name")]
public string Name { get; set; }
public string? Name { get; set; }
public int Lighten { get; set; }
public int Darken { get; set; }
public int Alpha { get; set; }
public override object? ProvideValue(IServiceProvider service_provider) {
if (service_provider is null) {
return null;
}
if (Name is null) {
throw new InvalidOperationException("Name is null");
}
var pvt = service_provider.GetValueTargetProvider() ??
throw new InvalidOperationException("IProvideValueTarget is null");
var tgt_type = _deduce_target_type(pvt);
var color = _get_color(Name);
return tgt_type == typeof(Color) ? (Color)color : (Brush)color;
}
static Type _deduce_target_type(IProvideValueTarget pvt) {
if (pvt.TargetProperty is null) {
return typeof(Color);
}
if (pvt.TargetProperty is DependencyProperty dp) {
return dp.PropertyType ?? throw new InvalidOperationException();
}
if (pvt.TargetObject is Setter setter) {
return setter.Property?.PropertyType ?? typeof(Brush);
}
return typeof(Brush);
}
static BTSColor _get_color(string name) =>
_color_map.TryGetValue(name, out var color) ?
color() : throw new ColorNotFoundException();
static IEnumerable<(string, Func<BTSColor>)> _normalized_color_props(
Type type, string? prefix) {
var bts_colors = ReflectionHelper.static_property_getters<BTSColor>(type)
.Select(tup => (full_name(tup.name), tup.get));
var media_colors = ReflectionHelper.static_property_getters<Color>(type)
.Select(tup => (full_name(tup.name), new Func<BTSColor>(() => tup.get())));
var nested_types = type.GetNestedTypes(BindingFlags.Public | BindingFlags.Static);
var nested_colors = nested_types.SelectMany(
t => _normalized_color_props(t, full_name(t.Name))).ToList();
return bts_colors
.Concat(media_colors)
.Concat(nested_colors);
string full_name(string prop) =>
prefix is null ? prop : $"{prefix}.{prop}";
}
}
}
@@ -3,8 +3,20 @@ namespace mbts.ui {
using System;
static class BTSColorExtensions {
public static System.Windows.Media.SolidColorBrush to_brush(this BTSColor color) =>
new(color);
public static System.Windows.Media.SolidColorBrush to_brush(
this BTSColor color) {
var brush = new System.Windows.Media.SolidColorBrush(color);
return brush;
}
public static System.Windows.Media.SolidColorBrush to_brush(
this BTSColor color, bool freeze) {
var brush = new System.Windows.Media.SolidColorBrush(color);
if (freeze) {
brush.Freeze();
}
return brush;
}
public static System.Windows.Media.Color to_media_color(this BTSColor color) =>
System.Windows.Media.Color.FromArgb(color.a, color.r, color.g, color.b);