-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Displaying Sizes and Numbers using Humanize
- Loading branch information
1 parent
d68a68b
commit 50f2ca4
Showing
5 changed files
with
75 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/NexusMods.App.UI/Converters/SizeToStringTypeConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Humanizer; | ||
using Humanizer.Bytes; | ||
using NexusMods.Paths; | ||
using ReactiveUI; | ||
|
||
namespace NexusMods.App.UI.Converters; | ||
|
||
/// <summary> | ||
/// NexusMods.Paths.Size To String Type Converter. | ||
/// </summary> | ||
public class SizeToStringTypeConverter : IBindingTypeConverter | ||
{ | ||
/// <inheritdoc/> | ||
public int GetAffinityForObjects(Type fromType, Type toType) | ||
{ | ||
if (fromType == typeof(Size) && toType == typeof(string)) | ||
{ | ||
return 999; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public bool TryConvert(object? from, Type toType, object? conversionHint, out object result) | ||
{ | ||
if (toType == typeof(string) && from is Size fromSize) | ||
{ | ||
var byteSize = ByteSize.FromBytes(fromSize.Value); | ||
result = byteSize.Gigabytes < 1 ? byteSize.Humanize("0") : byteSize.Humanize("0.0"); | ||
|
||
return true; | ||
} | ||
|
||
result = null!; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters