mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-06-19 08:14:02 +03:00
Add BytesValueConverter
This commit is contained in:
parent
65ecb0177c
commit
d129a60d1c
@ -105,6 +105,7 @@
|
||||
<Application.Resources>
|
||||
|
||||
<common:NumberValueConverter x:Key="NumberValueConverter" />
|
||||
<common:BytesValueConverter x:Key="BytesValueConverter" />
|
||||
|
||||
<system:Double x:Key="ControlContentThemeFontSize">14</system:Double>
|
||||
<CornerRadius x:Key="ControlCornerRadius">0</CornerRadius>
|
||||
|
45
app/Desktop/Common/BytesValueConverter.cs
Normal file
45
app/Desktop/Common/BytesValueConverter.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia.Data.Converters;
|
||||
|
||||
namespace DHT.Desktop.Common {
|
||||
sealed class BytesValueConverter : IValueConverter {
|
||||
private static readonly string[] Units = {
|
||||
"B",
|
||||
"kB",
|
||||
"MB",
|
||||
"GB",
|
||||
"TB"
|
||||
};
|
||||
|
||||
private const int Scale = 1000;
|
||||
|
||||
private static string Convert(ulong size) {
|
||||
int power = size == 0L ? 0 : (int) Math.Log(size, Scale);
|
||||
int unit = power >= Units.Length ? Units.Length - 1 : power;
|
||||
if (unit == 0) {
|
||||
return string.Format(Program.Culture, "{0:n0}", size) + " " + Units[unit];
|
||||
}
|
||||
else {
|
||||
double humanReadableSize = size / Math.Pow(Scale, unit);
|
||||
return string.Format(Program.Culture, "{0:n0}", humanReadableSize) + " " + Units[unit];
|
||||
}
|
||||
}
|
||||
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
||||
if (value is long size and >= 0L) {
|
||||
return Convert((ulong) size);
|
||||
}
|
||||
else if (value is ulong usize) {
|
||||
return Convert(usize);
|
||||
}
|
||||
else {
|
||||
return "-";
|
||||
}
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user