Skip to content

Commit c38c680

Browse files
authored
Feature: Added support for changing the size format (#16996)
1 parent 0aabcb6 commit c38c680

File tree

10 files changed

+91
-4
lines changed

10 files changed

+91
-4
lines changed

src/Files.App/Data/Contracts/IFoldersSettingsService.cs

+5
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,10 @@ public interface IFoldersSettingsService : IBaseSettingsService, INotifyProperty
8484
/// Gets or sets a value indicating whether or not to show checkboxes when selecting items.
8585
/// </summary>
8686
bool ShowCheckboxesWhenSelectingItems { get; set; }
87+
88+
/// <summary>
89+
/// Gets or sets a value indicating which format to use when displaying item sizes.
90+
/// </summary>
91+
SizeUnitTypes SizeUnitFormat { get; set; }
8792
}
8893
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
namespace Files.App.Data.Enums
5+
{
6+
public enum SizeUnitTypes
7+
{
8+
/// <summary>
9+
/// Displays sizes using Binary values.
10+
/// </summary>
11+
BinaryUnits,
12+
13+
/// <summary>
14+
/// Displays sizes using Decimal values.
15+
/// </summary>
16+
DecimalUnits,
17+
}
18+
}

src/Files.App/Data/Models/ByteSize.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Files.App.Data.Models
77
{
88
public struct ByteSize : IEquatable<ByteSize>, IComparable<ByteSize>
99
{
10+
private static IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetRequiredService<IFoldersSettingsService>();
11+
1012
private static readonly FrozenDictionary<string, string> units = new Dictionary<string, string>
1113
{
1214
[ByteSizeLib.ByteSize.BitSymbol] = Strings.ByteSymbol.ToLocalized(),
@@ -27,8 +29,9 @@ public struct ByteSize : IEquatable<ByteSize>, IComparable<ByteSize>
2729
public ulong Bytes
2830
=> (ulong)size.Bytes;
2931

30-
public string ShortString
31-
=> $"{size.LargestWholeNumberDecimalValue:0.##} {units[size.LargestWholeNumberDecimalSymbol]}";
32+
public string ShortString => FoldersSettingsService.SizeUnitFormat == SizeUnitTypes.BinaryUnits
33+
? $"{size.LargestWholeNumberBinaryValue:0.##} {units[size.LargestWholeNumberBinarySymbol]}"
34+
: $"{size.LargestWholeNumberDecimalValue:0.##} {units[size.LargestWholeNumberDecimalSymbol]}";
3235

3336
public string LongString
3437
=> $"{ShortString} ({size.Bytes:#,##0} {units[ByteSizeLib.ByteSize.ByteSymbol]})";

src/Files.App/Extensions/StringExtensions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using ByteSizeLib;
54
using Microsoft.Windows.ApplicationModel.Resources;
65
using System.Collections.Concurrent;
76
using System.IO;
@@ -11,6 +10,8 @@ namespace Files.App.Extensions
1110
{
1211
public static class StringExtensions
1312
{
13+
private static IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetRequiredService<IFoldersSettingsService>();
14+
1415
/// <summary>
1516
/// Returns true if <paramref name="path"/> starts with the path <paramref name="baseDirPath"/>.
1617
/// The comparison is case-insensitive, handles / and \ slashes as folder separators and
@@ -83,7 +84,10 @@ public static string ConvertSizeAbbreviation(this string value)
8384
public static string ToSizeString(this long size) => ByteSize.FromBytes(size).ToSizeString();
8485
public static string ToSizeString(this ulong size) => ByteSize.FromBytes(size).ToSizeString();
8586
public static string ToSizeString(this decimal size) => ByteSize.FromBytes((double)size).ToSizeString();
86-
public static string ToSizeString(this ByteSize size) => size.ToString().ConvertSizeAbbreviation();
87+
public static string ToSizeString(this ByteSize size) => FoldersSettingsService.SizeUnitFormat is SizeUnitTypes.BinaryUnits
88+
? size.ToBinaryString().ConvertSizeAbbreviation()
89+
: size.ToString().ConvertSizeAbbreviation();
90+
8791

8892
public static string ToLongSizeString(this long size) => ByteSize.FromBytes(size).ToLongSizeString();
8993
public static string ToLongSizeString(this ulong size) => ByteSize.FromBytes(size).ToLongSizeString();

src/Files.App/Services/Settings/FoldersSettingsService.cs

+7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ public bool ShowCheckboxesWhenSelectingItems
107107
set => Set(value);
108108
}
109109

110+
/// <inheritdoc/>
111+
public SizeUnitTypes SizeUnitFormat
112+
{
113+
get => Get(SizeUnitTypes.BinaryUnits);
114+
set => Set(value);
115+
}
116+
110117
protected override void RaiseOnSettingChangedEvent(object sender, SettingChangedEventArgs e)
111118
{
112119
base.RaiseOnSettingChangedEvent(sender, e);

src/Files.App/Strings/en-US/Resources.resw

+9
Original file line numberDiff line numberDiff line change
@@ -4178,4 +4178,13 @@
41784178
<value>Compare a file</value>
41794179
<comment>Button that appears in file hash properties that allows the user to compare two files</comment>
41804180
</data>
4181+
<data name="SizeFormat" xml:space="preserve">
4182+
<value>Size format</value>
4183+
</data>
4184+
<data name="Binary" xml:space="preserve">
4185+
<value>Binary</value>
4186+
</data>
4187+
<data name="Decimal" xml:space="preserve">
4188+
<value>Decimal</value>
4189+
</data>
41814190
</root>

src/Files.App/ViewModels/Settings/FoldersViewModel.cs

+19
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ public sealed partial class FoldersViewModel : ObservableObject
88
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
99

1010

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

1213
public FoldersViewModel()
1314
{
1415
SelectedDeleteConfirmationPolicyIndex = (int)DeleteConfirmationPolicy;
16+
17+
// Size unit format
18+
SizeUnitsOptions.Add(SizeUnitTypes.BinaryUnits, Strings.Binary.GetLocalizedResource());
19+
SizeUnitsOptions.Add(SizeUnitTypes.DecimalUnits, Strings.Decimal.GetLocalizedResource());
20+
SizeUnitFormat = SizeUnitsOptions[UserSettingsService.FoldersSettingsService.SizeUnitFormat];
1521
}
1622

1723
// Properties
@@ -255,5 +261,18 @@ public bool ShowCheckboxesWhenSelectingItems
255261
}
256262
}
257263
}
264+
265+
private string sizeUnitFormat;
266+
public string SizeUnitFormat
267+
{
268+
get => sizeUnitFormat;
269+
set
270+
{
271+
if (SetProperty(ref sizeUnitFormat, value))
272+
{
273+
UserSettingsService.FoldersSettingsService.SizeUnitFormat = SizeUnitsOptions.First(e => e.Value == value).Key;
274+
}
275+
}
276+
}
258277
}
259278
}

src/Files.App/ViewModels/ShellViewModel.cs

+1
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ private async void UserSettingsService_OnSettingChangedEvent(object? sender, Set
654654
case nameof(UserSettingsService.FoldersSettingsService.CalculateFolderSizes):
655655
case nameof(UserSettingsService.FoldersSettingsService.SelectFilesOnHover):
656656
case nameof(UserSettingsService.FoldersSettingsService.ShowCheckboxesWhenSelectingItems):
657+
case nameof(UserSettingsService.FoldersSettingsService.SizeUnitFormat):
657658
await dispatcherQueue.EnqueueOrInvokeAsync(() =>
658659
{
659660
if (WorkingDirectory != "Home")

src/Files.App/ViewModels/UserControls/Widgets/DrivesWidgetViewModel.cs

+8
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ public DrivesWidgetViewModel()
4444
EjectDeviceCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteEjectDeviceCommand);
4545
OpenPropertiesCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteOpenPropertiesCommand);
4646
DisconnectNetworkDriveCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteDisconnectNetworkDriveCommand);
47+
48+
UserSettingsService.OnSettingChangedEvent += UserSettingsService_OnSettingChangedEvent;
4749
}
4850

4951
// Methods
5052

53+
private async void UserSettingsService_OnSettingChangedEvent(object? sender, SettingChangedEventArgs e)
54+
{
55+
if (e.SettingName == nameof(UserSettingsService.FoldersSettingsService.SizeUnitFormat))
56+
await RefreshWidgetAsync();
57+
}
58+
5159
public async Task RefreshWidgetAsync()
5260
{
5361
var updateTasks = Items.Select(item => item.Item.UpdatePropertiesAsync());

src/Files.App/Views/Settings/FoldersPage.xaml

+13
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@
166166
<ToggleSwitch AutomationProperties.Name="{helpers:ResourceString Name=ScrollToPreviousFolderWhenNavigatingUp}" IsOn="{x:Bind ViewModel.ScrollToPreviousFolderWhenNavigatingUp, Mode=TwoWay}" />
167167
</wctcontrols:SettingsCard>
168168

169+
<!-- Size format -->
170+
<wctcontrols:SettingsCard Header="{helpers:ResourceString Name=SizeFormat}">
171+
<wctcontrols:SettingsCard.HeaderIcon>
172+
<FontIcon Glyph="&#xE67A;" />
173+
</wctcontrols:SettingsCard.HeaderIcon>
174+
175+
<uc:ComboBoxEx
176+
MinWidth="140"
177+
AutomationProperties.Name="{helpers:ResourceString Name=SizeFormat}"
178+
ItemsSource="{x:Bind ViewModel.SizeUnitsOptions.Values}"
179+
SelectedItem="{x:Bind ViewModel.SizeUnitFormat, Mode=TwoWay}" />
180+
</wctcontrols:SettingsCard>
181+
169182
<!-- Calculate folder sizes -->
170183
<wctcontrols:SettingsExpander Header="{helpers:ResourceString Name=CalculateFolderSizes}" IsExpanded="True">
171184
<wctcontrols:SettingsExpander.HeaderIcon>

0 commit comments

Comments
 (0)