Skip to content

Feature: Added support for changing the size format #16996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Files.App/Data/Contracts/IFoldersSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,10 @@ public interface IFoldersSettingsService : IBaseSettingsService, INotifyProperty
/// Gets or sets a value indicating whether or not to show checkboxes when selecting items.
/// </summary>
bool ShowCheckboxesWhenSelectingItems { get; set; }

/// <summary>
/// Gets or sets a value indicating which format to use when displaying item sizes.
/// </summary>
SizeUnitTypes SizeUnitFormat { get; set; }
}
}
18 changes: 18 additions & 0 deletions src/Files.App/Data/Enums/SizeUnitTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

namespace Files.App.Data.Enums
{
public enum SizeUnitTypes
{
/// <summary>
/// Displays sizes using Binary values.
/// </summary>
BinaryUnits,

/// <summary>
/// Displays sizes using Decimal values.
/// </summary>
DecimalUnits,
}
}
7 changes: 5 additions & 2 deletions src/Files.App/Data/Models/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Files.App.Data.Models
{
public struct ByteSize : IEquatable<ByteSize>, IComparable<ByteSize>
{
private static IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetRequiredService<IFoldersSettingsService>();

private static readonly FrozenDictionary<string, string> units = new Dictionary<string, string>
{
[ByteSizeLib.ByteSize.BitSymbol] = Strings.ByteSymbol.ToLocalized(),
Expand All @@ -27,8 +29,9 @@ public struct ByteSize : IEquatable<ByteSize>, IComparable<ByteSize>
public ulong Bytes
=> (ulong)size.Bytes;

public string ShortString
=> $"{size.LargestWholeNumberDecimalValue:0.##} {units[size.LargestWholeNumberDecimalSymbol]}";
public string ShortString => FoldersSettingsService.SizeUnitFormat == SizeUnitTypes.BinaryUnits
? $"{size.LargestWholeNumberBinaryValue:0.##} {units[size.LargestWholeNumberBinarySymbol]}"
: $"{size.LargestWholeNumberDecimalValue:0.##} {units[size.LargestWholeNumberDecimalSymbol]}";

public string LongString
=> $"{ShortString} ({size.Bytes:#,##0} {units[ByteSizeLib.ByteSize.ByteSymbol]})";
Expand Down
8 changes: 6 additions & 2 deletions src/Files.App/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using ByteSizeLib;
using Microsoft.Windows.ApplicationModel.Resources;
using System.Collections.Concurrent;
using System.IO;
Expand All @@ -11,6 +10,8 @@ namespace Files.App.Extensions
{
public static class StringExtensions
{
private static IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetRequiredService<IFoldersSettingsService>();

/// <summary>
/// Returns true if <paramref name="path"/> starts with the path <paramref name="baseDirPath"/>.
/// The comparison is case-insensitive, handles / and \ slashes as folder separators and
Expand Down Expand Up @@ -83,7 +84,10 @@ public static string ConvertSizeAbbreviation(this string value)
public static string ToSizeString(this long size) => ByteSize.FromBytes(size).ToSizeString();
public static string ToSizeString(this ulong size) => ByteSize.FromBytes(size).ToSizeString();
public static string ToSizeString(this decimal size) => ByteSize.FromBytes((double)size).ToSizeString();
public static string ToSizeString(this ByteSize size) => size.ToString().ConvertSizeAbbreviation();
public static string ToSizeString(this ByteSize size) => FoldersSettingsService.SizeUnitFormat is SizeUnitTypes.BinaryUnits
? size.ToBinaryString().ConvertSizeAbbreviation()
: size.ToString().ConvertSizeAbbreviation();


public static string ToLongSizeString(this long size) => ByteSize.FromBytes(size).ToLongSizeString();
public static string ToLongSizeString(this ulong size) => ByteSize.FromBytes(size).ToLongSizeString();
Expand Down
7 changes: 7 additions & 0 deletions src/Files.App/Services/Settings/FoldersSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public bool ShowCheckboxesWhenSelectingItems
set => Set(value);
}

/// <inheritdoc/>
public SizeUnitTypes SizeUnitFormat
{
get => Get(SizeUnitTypes.BinaryUnits);
set => Set(value);
}

protected override void RaiseOnSettingChangedEvent(object sender, SettingChangedEventArgs e)
{
base.RaiseOnSettingChangedEvent(sender, e);
Expand Down
9 changes: 9 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -4172,4 +4172,13 @@
<value>Compare a file</value>
<comment>Button that appears in file hash properties that allows the user to compare two files</comment>
</data>
<data name="SizeFormat" xml:space="preserve">
<value>Size format</value>
</data>
<data name="Binary" xml:space="preserve">
<value>Binary</value>
</data>
<data name="Decimal" xml:space="preserve">
<value>Decimal</value>
</data>
</root>
19 changes: 19 additions & 0 deletions src/Files.App/ViewModels/Settings/FoldersViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ public sealed partial class FoldersViewModel : ObservableObject
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();


public Dictionary<SizeUnitTypes, string> SizeUnitsOptions { get; private set; } = [];

public FoldersViewModel()
{
SelectedDeleteConfirmationPolicyIndex = (int)DeleteConfirmationPolicy;

// Size unit format
SizeUnitsOptions.Add(SizeUnitTypes.BinaryUnits, Strings.Binary.GetLocalizedResource());
SizeUnitsOptions.Add(SizeUnitTypes.DecimalUnits, Strings.Decimal.GetLocalizedResource());
SizeUnitFormat = SizeUnitsOptions[UserSettingsService.FoldersSettingsService.SizeUnitFormat];
}

// Properties
Expand Down Expand Up @@ -255,5 +261,18 @@ public bool ShowCheckboxesWhenSelectingItems
}
}
}

private string sizeUnitFormat;
public string SizeUnitFormat
{
get => sizeUnitFormat;
set
{
if (SetProperty(ref sizeUnitFormat, value))
{
UserSettingsService.FoldersSettingsService.SizeUnitFormat = SizeUnitsOptions.First(e => e.Value == value).Key;
}
}
}
}
}
13 changes: 13 additions & 0 deletions src/Files.App/Views/Settings/FoldersPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@
<ToggleSwitch AutomationProperties.Name="{helpers:ResourceString Name=ScrollToPreviousFolderWhenNavigatingUp}" IsOn="{x:Bind ViewModel.ScrollToPreviousFolderWhenNavigatingUp, Mode=TwoWay}" />
</wctcontrols:SettingsCard>

<!-- Size format -->
<wctcontrols:SettingsCard Header="{helpers:ResourceString Name=SizeFormat}">
<wctcontrols:SettingsCard.HeaderIcon>
<FontIcon Glyph="&#xE67A;" />
</wctcontrols:SettingsCard.HeaderIcon>

<uc:ComboBoxEx
MinWidth="140"
AutomationProperties.Name="{helpers:ResourceString Name=SizeFormat}"
ItemsSource="{x:Bind ViewModel.SizeUnitsOptions.Values}"
SelectedItem="{x:Bind ViewModel.SizeUnitFormat, Mode=TwoWay}" />
</wctcontrols:SettingsCard>

<!-- Calculate folder sizes -->
<wctcontrols:SettingsExpander Header="{helpers:ResourceString Name=CalculateFolderSizes}" IsExpanded="True">
<wctcontrols:SettingsExpander.HeaderIcon>
Expand Down
Loading