forked from amkovkov/GranuSightSoftware2
feat: logic converters
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System.Globalization;
|
||||
|
||||
using Avalonia.Data.Converters;
|
||||
|
||||
namespace GSS2.UI.Core.Converters;
|
||||
|
||||
public class GreaterThanConverter : IValueConverter
|
||||
{
|
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is null && parameter is null)
|
||||
return true;
|
||||
|
||||
if (value is null || parameter is null)
|
||||
return false;
|
||||
|
||||
var valueType = value.GetType();
|
||||
var paramType = parameter.GetType();
|
||||
|
||||
if (valueType == paramType)
|
||||
{
|
||||
var greaterThan = valueType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
|
||||
.FirstOrDefault(m => m.Name.EndsWith("op_GreaterThan"));
|
||||
if (greaterThan is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return greaterThan.Invoke(null, [value, parameter]);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
||||
if (parameter is string ps)
|
||||
return value switch
|
||||
{
|
||||
byte v => byte.TryParse(ps, out var p) && v > p,
|
||||
short v => short.TryParse(ps, out var p) && v > p,
|
||||
ushort v => ushort.TryParse(ps, out var p) && v > p,
|
||||
int v => int.TryParse(ps, out var p) && v > p,
|
||||
uint v => uint.TryParse(ps, out var p) && v > p,
|
||||
long v => long.TryParse(ps, out var p) && v > p,
|
||||
ulong v => ulong.TryParse(ps, out var p) && v > p,
|
||||
float v => float.TryParse(ps, out var p) && v > p,
|
||||
double v => double.TryParse(ps, out var p) && v > p,
|
||||
decimal v => decimal.TryParse(ps, out var p) && v > p,
|
||||
_ => false
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user