Browse Source

Modify byte to readable translation.

pull/2777/head
Jumar Macato 7 years ago
parent
commit
c2cbe448e9
No known key found for this signature in database GPG Key ID: B19884DAC3A5BF3F
  1. 29
      src/Avalonia.Dialogs/ByteSizeHelper.cs
  2. 2
      src/Avalonia.Dialogs/FileSizeStringConverter.cs
  3. 28
      src/Avalonia.Dialogs/ManagedFileChooserSources.cs

29
src/Avalonia.Dialogs/ByteSizeHelper.cs

@ -1,25 +1,40 @@
using System;
namespace Avalonia.Dialogs
{
internal static class ByteSizeHelper
{
private const string formatTemplate = "{0}{1:0.#} {2}";
private static readonly string[] Prefixes =
{
"B",
"KB",
"MB",
"GB",
"TB"
"TB",
"PB",
"EB",
"ZB",
"YB"
};
public static string ToString(long bytes)
public static string ToString(ulong bytes)
{
var index = 0;
while (bytes >= 1000)
if (bytes == 0)
{
bytes /= 1000;
++index;
return string.Format(formatTemplate, null, 0, Prefixes[0]);
}
return $"{bytes:N} {Prefixes[index]}";
var absSize = Math.Abs((double)bytes);
var fpPower = Math.Log(absSize, 1000);
var intPower = (int)fpPower;
var iUnit = intPower >= Prefixes.Length
? Prefixes.Length - 1
: intPower;
var normSize = absSize / Math.Pow(1000, iUnit);
return string.Format(formatTemplate,bytes < 0 ? "-" : null, normSize, Prefixes[iUnit]);
}
}
}

2
src/Avalonia.Dialogs/FileSizeStringConverter.cs

@ -12,7 +12,7 @@ namespace Avalonia.Dialogs
{
if (value is long size && size > 0)
{
return ByteSizeHelper.ToString(size);
return ByteSizeHelper.ToString((ulong)size);
}
return "";

28
src/Avalonia.Dialogs/ManagedFileChooserSources.cs

@ -36,9 +36,6 @@ namespace Avalonia.Dialogs
Environment.SpecialFolder.MyVideos
};
private static string[] s_sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
private const string formatTemplate = "{0}{1:0.#} {2}";
public static ManagedFileChooserNavigationItem[] DefaultGetUserDirectories()
{
return s_folders.Select(Environment.GetFolderPath).Distinct()
@ -51,29 +48,6 @@ namespace Avalonia.Dialogs
}).ToArray();
}
public static event EventHandler SourcesChanged;
private static string ByteToHumanReadableUnits(ulong size)
{
if (size == 0)
{
return string.Format(formatTemplate, null, 0, s_sizeSuffixes[0]);
}
var absSize = Math.Abs((double)size);
var fpPower = Math.Log(absSize, 1000);
var intPower = (int)fpPower;
var iUnit = intPower >= s_sizeSuffixes.Length
? s_sizeSuffixes.Length - 1
: intPower;
var normSize = absSize / Math.Pow(1000, iUnit);
return string.Format(
formatTemplate,
size < 0 ? "-" : null, normSize, s_sizeSuffixes[iUnit]);
}
public static ManagedFileChooserNavigationItem[] DefaultGetFileSystemRoots()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@ -119,7 +93,7 @@ namespace Avalonia.Dialogs
return new ManagedFileChooserNavigationItem
{
DisplayName = dNameEmpty ? $"{ByteToHumanReadableUnits(x.DriveSizeBytes)} Volume"
DisplayName = dNameEmpty ? $"{ByteSizeHelper.ToString(x.DriveSizeBytes)} Volume"
: x.DriveLabel,
Path = x.MountPath
};

Loading…
Cancel
Save