using System.Globalization; using Avalonia.Data.Converters; namespace GUI.Converters; public class DurationFormatConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is not double duration) return "--:--"; TimeSpan ts = TimeSpan.FromSeconds(duration); if (ts.TotalHours >= 1) return ts.ToString(@"h\:mm\:ss\.ff"); return ts.ToString(@"m\:ss\.ff"); } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public class SampleRateFormatConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is not double rate) return "--"; return $"{rate / 1000.0:F1}k"; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public class ChannelsFormatConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { return value switch { int ch when ch == 1 => "Mono", int ch when ch == 2 => "Stereo", null => "--", int ch => $"{ch}ch", _ => "--", }; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public class RatingStarsConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is not uint rating || rating == 0) return "☆☆☆☆☆"; return new string('★', (int)rating) + new string('☆', 5 - (int)rating); } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public class DurationToSliderMaxConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is not double duration) return 1.0; return Math.Ceiling(duration); } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public class PositionFormatConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is not double pos) return "0:00"; TimeSpan ts = TimeSpan.FromSeconds(pos); return ts.ToString(@"m\:ss"); } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public class InverseBoolConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is bool b) return !b; return true; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is bool b) return !b; return true; } } public class HalfDoubleConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is double d && !double.IsNaN(d) && !double.IsInfinity(d)) return d / 2.0; return 400.0; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public class MiddleTruncateConverter : IValueConverter { private const int KeepTail = 10; public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value is not string text || string.IsNullOrEmpty(text)) return value; int maxLength = 60; if (parameter != null && int.TryParse(parameter.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int parsed)) maxLength = parsed; if (text.Length <= maxLength) return text; int head = maxLength - KeepTail - 3; if (head <= 0) return text[..maxLength]; return string.Concat(text.AsSpan(0, head), "...", text.AsSpan(text.Length - KeepTail, KeepTail)); } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException(); } public static class ValueConverters { public static readonly IValueConverter DurationFormat = new DurationFormatConverter(); public static readonly IValueConverter SampleRateFormat = new SampleRateFormatConverter(); public static readonly IValueConverter ChannelsFormat = new ChannelsFormatConverter(); public static readonly IValueConverter RatingStars = new RatingStarsConverter(); public static readonly IValueConverter DurationToSliderMax = new DurationToSliderMaxConverter(); public static readonly IValueConverter PositionFormat = new PositionFormatConverter(); public static readonly IValueConverter InverseBool = new InverseBoolConverter(); public static readonly IValueConverter HalfDouble = new HalfDoubleConverter(); public static readonly IValueConverter MiddleTruncate = new MiddleTruncateConverter(); }