From 209150836c70e766f6118457c531556956684cc3 Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Sun, 30 Mar 2025 17:06:54 -0400 Subject: [PATCH 1/3] Init --- .../Data/Contracts/IFoldersSettingsService.cs | 5 +++++ src/Files.App/Data/Enums/SizeUnitTypes.cs | 18 ++++++++++++++++++ src/Files.App/Data/Models/ByteSize.cs | 7 +++++-- src/Files.App/Extensions/StringExtensions.cs | 8 ++++++-- .../Settings/FoldersSettingsService.cs | 7 +++++++ src/Files.App/Strings/en-US/Resources.resw | 9 +++++++++ .../ViewModels/Settings/FoldersViewModel.cs | 19 +++++++++++++++++++ src/Files.App/Views/Settings/FoldersPage.xaml | 13 +++++++++++++ 8 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/Files.App/Data/Enums/SizeUnitTypes.cs diff --git a/src/Files.App/Data/Contracts/IFoldersSettingsService.cs b/src/Files.App/Data/Contracts/IFoldersSettingsService.cs index 92a3a760986f..f916ed3cbb2c 100644 --- a/src/Files.App/Data/Contracts/IFoldersSettingsService.cs +++ b/src/Files.App/Data/Contracts/IFoldersSettingsService.cs @@ -84,5 +84,10 @@ public interface IFoldersSettingsService : IBaseSettingsService, INotifyProperty /// Gets or sets a value indicating whether or not to show checkboxes when selecting items. /// bool ShowCheckboxesWhenSelectingItems { get; set; } + + /// + /// Gets or sets a value indicating which format to use when displaying item sizes. + /// + SizeUnitTypes SizeUnitFormat { get; set; } } } diff --git a/src/Files.App/Data/Enums/SizeUnitTypes.cs b/src/Files.App/Data/Enums/SizeUnitTypes.cs new file mode 100644 index 000000000000..d3f93c2f176a --- /dev/null +++ b/src/Files.App/Data/Enums/SizeUnitTypes.cs @@ -0,0 +1,18 @@ +// Copyright (c) Files Community +// Licensed under the MIT License. + +namespace Files.App.Data.Enums +{ + public enum SizeUnitTypes + { + /// + /// Displays sizes using Binary values. + /// + BinaryUnits, + + /// + /// Displays sizes using Decimal values. + /// + DecimalUnits, + } +} diff --git a/src/Files.App/Data/Models/ByteSize.cs b/src/Files.App/Data/Models/ByteSize.cs index 1ba1ec935544..5739f4a54871 100644 --- a/src/Files.App/Data/Models/ByteSize.cs +++ b/src/Files.App/Data/Models/ByteSize.cs @@ -7,6 +7,8 @@ namespace Files.App.Data.Models { public struct ByteSize : IEquatable, IComparable { + private static IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetRequiredService(); + private static readonly FrozenDictionary units = new Dictionary { [ByteSizeLib.ByteSize.BitSymbol] = Strings.ByteSymbol.ToLocalized(), @@ -27,8 +29,9 @@ public struct ByteSize : IEquatable, IComparable 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]})"; diff --git a/src/Files.App/Extensions/StringExtensions.cs b/src/Files.App/Extensions/StringExtensions.cs index 4758c7619755..c1409a2446f5 100644 --- a/src/Files.App/Extensions/StringExtensions.cs +++ b/src/Files.App/Extensions/StringExtensions.cs @@ -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; @@ -11,6 +10,8 @@ namespace Files.App.Extensions { public static class StringExtensions { + private static IFoldersSettingsService FoldersSettingsService { get; } = Ioc.Default.GetRequiredService(); + /// /// Returns true if starts with the path . /// The comparison is case-insensitive, handles / and \ slashes as folder separators and @@ -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(); diff --git a/src/Files.App/Services/Settings/FoldersSettingsService.cs b/src/Files.App/Services/Settings/FoldersSettingsService.cs index 6742d9007b9c..45c16b0bcd31 100644 --- a/src/Files.App/Services/Settings/FoldersSettingsService.cs +++ b/src/Files.App/Services/Settings/FoldersSettingsService.cs @@ -107,6 +107,13 @@ public bool ShowCheckboxesWhenSelectingItems set => Set(value); } + /// + public SizeUnitTypes SizeUnitFormat + { + get => Get(SizeUnitTypes.BinaryUnits); + set => Set(value); + } + protected override void RaiseOnSettingChangedEvent(object sender, SettingChangedEventArgs e) { base.RaiseOnSettingChangedEvent(sender, e); diff --git a/src/Files.App/Strings/en-US/Resources.resw b/src/Files.App/Strings/en-US/Resources.resw index 7fc0bf73a209..73109f35902f 100644 --- a/src/Files.App/Strings/en-US/Resources.resw +++ b/src/Files.App/Strings/en-US/Resources.resw @@ -4172,4 +4172,13 @@ Compare a file Button that appears in file hash properties that allows the user to compare two files + + Size format + + + Binary + + + Decimal + \ No newline at end of file diff --git a/src/Files.App/ViewModels/Settings/FoldersViewModel.cs b/src/Files.App/ViewModels/Settings/FoldersViewModel.cs index 10a11f87e3a2..bf73b83a098b 100644 --- a/src/Files.App/ViewModels/Settings/FoldersViewModel.cs +++ b/src/Files.App/ViewModels/Settings/FoldersViewModel.cs @@ -8,10 +8,16 @@ public sealed partial class FoldersViewModel : ObservableObject private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService(); + public Dictionary 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 @@ -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; + } + } + } } } diff --git a/src/Files.App/Views/Settings/FoldersPage.xaml b/src/Files.App/Views/Settings/FoldersPage.xaml index bd872db448c4..ce319d4be368 100644 --- a/src/Files.App/Views/Settings/FoldersPage.xaml +++ b/src/Files.App/Views/Settings/FoldersPage.xaml @@ -166,6 +166,19 @@ + + + + + + + + + From 41902f99db084d490749490440c2474879daf8fa Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Tue, 1 Apr 2025 12:09:02 -0400 Subject: [PATCH 2/3] Update file area when setting changes --- src/Files.App/ViewModels/ShellViewModel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Files.App/ViewModels/ShellViewModel.cs b/src/Files.App/ViewModels/ShellViewModel.cs index a4d1565c34ac..adcd0fe4d795 100644 --- a/src/Files.App/ViewModels/ShellViewModel.cs +++ b/src/Files.App/ViewModels/ShellViewModel.cs @@ -654,6 +654,7 @@ private async void UserSettingsService_OnSettingChangedEvent(object? sender, Set case nameof(UserSettingsService.FoldersSettingsService.CalculateFolderSizes): case nameof(UserSettingsService.FoldersSettingsService.SelectFilesOnHover): case nameof(UserSettingsService.FoldersSettingsService.ShowCheckboxesWhenSelectingItems): + case nameof(UserSettingsService.FoldersSettingsService.SizeUnitFormat): await dispatcherQueue.EnqueueOrInvokeAsync(() => { if (WorkingDirectory != "Home") From 5149375f72b1f426ffbfebecfef917a920c52cb7 Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Tue, 1 Apr 2025 16:10:12 -0400 Subject: [PATCH 3/3] Update home page --- .../UserControls/Widgets/DrivesWidgetViewModel.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Files.App/ViewModels/UserControls/Widgets/DrivesWidgetViewModel.cs b/src/Files.App/ViewModels/UserControls/Widgets/DrivesWidgetViewModel.cs index d6590477f799..5faf80c57720 100644 --- a/src/Files.App/ViewModels/UserControls/Widgets/DrivesWidgetViewModel.cs +++ b/src/Files.App/ViewModels/UserControls/Widgets/DrivesWidgetViewModel.cs @@ -44,10 +44,18 @@ public DrivesWidgetViewModel() EjectDeviceCommand = new RelayCommand(ExecuteEjectDeviceCommand); OpenPropertiesCommand = new RelayCommand(ExecuteOpenPropertiesCommand); DisconnectNetworkDriveCommand = new RelayCommand(ExecuteDisconnectNetworkDriveCommand); + + UserSettingsService.OnSettingChangedEvent += UserSettingsService_OnSettingChangedEvent; } // Methods + private async void UserSettingsService_OnSettingChangedEvent(object? sender, SettingChangedEventArgs e) + { + if (e.SettingName == nameof(UserSettingsService.FoldersSettingsService.SizeUnitFormat)) + await RefreshWidgetAsync(); + } + public async Task RefreshWidgetAsync() { var updateTasks = Items.Select(item => item.Item.UpdatePropertiesAsync());