|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Windows.Media; |
| 4 | + |
| 5 | +namespace MaterialDesignThemes.Wpf; |
| 6 | + |
| 7 | +internal sealed class InheritSystemColorTypeConverter : TypeConverter |
| 8 | +{ |
| 9 | + private const string Inherit = "Inherit"; |
| 10 | + |
| 11 | + private ColorConverter ColorConverter { get; } = new(); |
| 12 | + |
| 13 | + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) |
| 14 | + => sourceType == typeof(string) || |
| 15 | + ColorConverter.CanConvertFrom(context, sourceType) || |
| 16 | + base.CanConvertFrom(context, sourceType); |
| 17 | + |
| 18 | + public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType) |
| 19 | + => ColorConverter.CanConvertTo(context, destinationType) || |
| 20 | + base.CanConvertTo(context, destinationType); |
| 21 | + |
| 22 | + public override object ConvertFrom(ITypeDescriptorContext? td, System.Globalization.CultureInfo? ci, object? value) |
| 23 | + { |
| 24 | + if (value is null) |
| 25 | + { |
| 26 | + throw GetConvertFromException(value); |
| 27 | + } |
| 28 | + |
| 29 | + string? s = value as string ?? throw new ArgumentNullException(nameof(value)); |
| 30 | + |
| 31 | + if (string.Equals(s, Inherit, StringComparison.OrdinalIgnoreCase)) |
| 32 | + { |
| 33 | + return Theme.GetSystemAccentColor() ?? default; |
| 34 | + } |
| 35 | + |
| 36 | + return ColorConverter.ConvertFrom(td, ci, s); |
| 37 | + } |
| 38 | + |
| 39 | + public override object ConvertTo(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, Type destinationType) |
| 40 | + { |
| 41 | + if (value is Color color && |
| 42 | + color != default && |
| 43 | + color == Theme.GetSystemAccentColor()) |
| 44 | + { |
| 45 | + return Inherit; |
| 46 | + } |
| 47 | + return ColorConverter.ConvertTo(context, culture, value, destinationType); |
| 48 | + } |
| 49 | +} |
0 commit comments