diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/NetworkSpeedUnit.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/NetworkSpeedUnit.cs new file mode 100644 index 0000000000..0578bb1d88 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/NetworkSpeedUnit.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Ext.PerformanceMonitor; + +/// +/// Controls the unit used to display network transmission speed. +/// +internal enum NetworkSpeedUnit +{ + /// Bits per second (Kbps, Mbps, Gbps) — SI decimal prefixes. + BitsPerSecond, + + /// Bytes per second (KB/s, MB/s, GB/s) — SI decimal prefixes. + BytesPerSecond, + + /// Bytes per second (KiB/s, MiB/s, GiB/s) — IEC binary prefixes. + BinaryBytesPerSecond, +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceMonitorCommandsProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceMonitorCommandsProvider.cs index 3f00a99395..f80d84ae70 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceMonitorCommandsProvider.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceMonitorCommandsProvider.cs @@ -21,6 +21,7 @@ public partial class PerformanceMonitorCommandsProvider : CommandProvider internal static ProviderCrashSentinel CrashSentinel { get; } = new(ProviderIdValue); private readonly Lock _stateLock = new(); + private readonly SettingsManager _settingsManager = new(); private ICommandItem[] _commands = []; private ICommandItem _band = new CommandItem(); private PerformanceWidgetsPage? _mainPage; @@ -33,6 +34,8 @@ public partial class PerformanceMonitorCommandsProvider : CommandProvider Id = ProviderIdValue; Icon = Icons.PerformanceMonitorIcon; + Settings = _settingsManager.Settings; + if (softDisabled) { SetDisabledState(); @@ -121,12 +124,16 @@ public partial class PerformanceMonitorCommandsProvider : CommandProvider { DisposeActivePages(); - _mainPage = new PerformanceWidgetsPage(false); - _bandPage = new PerformanceWidgetsPage(true); + _mainPage = new PerformanceWidgetsPage(_settingsManager, false); + _bandPage = new PerformanceWidgetsPage(_settingsManager, true); _band = new CommandItem(_bandPage) { Title = DisplayName }; _commands = [ - new CommandItem(_mainPage) { Title = DisplayName }, + new CommandItem(_mainPage) + { + Title = DisplayName, + MoreCommands = [new CommandContextItem(_settingsManager.Settings.SettingsPage)], + }, ]; _softDisabled = false; } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceWidgetsPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceWidgetsPage.cs index f82e45bc57..f898079ca2 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceWidgetsPage.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/PerformanceWidgetsPage.cs @@ -43,7 +43,7 @@ internal sealed partial class PerformanceWidgetsPage : OnLoadStaticListPage, IDi private readonly SystemMemoryUsageWidgetPage _memoryPage = new(); private readonly ListItem _memoryItem; - private readonly SystemNetworkUsageWidgetPage _networkPage = new(); + private readonly SystemNetworkUsageWidgetPage _networkPage; private readonly ListItem _networkItem; private readonly SystemGPUUsageWidgetPage _gpuPage = new(); @@ -55,9 +55,10 @@ internal sealed partial class PerformanceWidgetsPage : OnLoadStaticListPage, IDi private string _networkUpSpeed = string.Empty; private string _networkDownSpeed = string.Empty; - public PerformanceWidgetsPage(bool isBandPage = false) + public PerformanceWidgetsPage(SettingsManager settingsManager, bool isBandPage = false) { _isBandPage = isBandPage; + _networkPage = new SystemNetworkUsageWidgetPage(settingsManager); _cpuItem = new ListItem(_cpuPage) { Title = _cpuPage.GetItemTitle(isBandPage), @@ -532,10 +533,12 @@ internal sealed partial class SystemNetworkUsageWidgetPage : WidgetPage, IDispos public override IconInfo Icon => Icons.NetworkIcon; private readonly DataManager _dataManager; + private readonly SettingsManager _settingsManager; private int _networkIndex; - public SystemNetworkUsageWidgetPage() + public SystemNetworkUsageWidgetPage(SettingsManager settingsManager) { + _settingsManager = settingsManager; _dataManager = new(DataType.Network, () => UpdateWidget()); Commands = [ new CommandContextItem(new PrevNetworkCommand(this) { Name = Resources.GetResource("Previous_Network_Title") }), @@ -561,8 +564,8 @@ internal sealed partial class SystemNetworkUsageWidgetPage : WidgetPage, IDispos var networkStats = currentData.GetNetworkUsage(_networkIndex); ContentData["networkUsage"] = FloatToPercentString(networkStats.Usage); - ContentData["netSent"] = BytesToBitsPerSecString(networkStats.Sent); - ContentData["netReceived"] = BytesToBitsPerSecString(networkStats.Received); + ContentData["netSent"] = SpeedToString(networkStats.Sent); + ContentData["netReceived"] = SpeedToString(networkStats.Received); ContentData["networkName"] = netName; ContentData["netGraphUrl"] = currentData.CreateNetImageUrl(_networkIndex); ContentData["chartHeight"] = ChartHelper.ChartHeight + "px"; @@ -627,7 +630,17 @@ internal sealed partial class SystemNetworkUsageWidgetPage : WidgetPage, IDispos } } - private string BytesToBitsPerSecString(float value) + private string SpeedToString(float bytesPerSec) + { + return _settingsManager.NetworkSpeedUnit switch + { + NetworkSpeedUnit.BytesPerSecond => FormatAsBytesPerSecString(bytesPerSec), + NetworkSpeedUnit.BinaryBytesPerSecond => FormatAsBinaryBytesPerSecString(bytesPerSec), + _ => FormatAsBitsPerSecString(bytesPerSec), + }; + } + + private static string FormatAsBitsPerSecString(float value) { // Bytes to bits value *= 8; @@ -666,6 +679,78 @@ internal sealed partial class SystemNetworkUsageWidgetPage : WidgetPage, IDispos return string.Format(CultureInfo.InvariantCulture, "{0:0} Gbps", value); } + private static string FormatAsBytesPerSecString(float value) + { + // Bytes to KB + value /= 1024; + if (value < 1024) + { + if (value < 100) + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.0} KB/s", value); + } + + return string.Format(CultureInfo.InvariantCulture, "{0:0} KB/s", value); + } + + // KB to MB + value /= 1024; + if (value < 1024) + { + if (value < 100) + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.0} MB/s", value); + } + + return string.Format(CultureInfo.InvariantCulture, "{0:0} MB/s", value); + } + + // MB to GB + value /= 1024; + if (value < 100) + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.0} GB/s", value); + } + + return string.Format(CultureInfo.InvariantCulture, "{0:0} GB/s", value); + } + + private static string FormatAsBinaryBytesPerSecString(float value) + { + // Bytes to KiB + value /= 1024; + if (value < 1024) + { + if (value < 100) + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.0} KiB/s", value); + } + + return string.Format(CultureInfo.InvariantCulture, "{0:0} KiB/s", value); + } + + // KiB to MiB + value /= 1024; + if (value < 1024) + { + if (value < 100) + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.0} MiB/s", value); + } + + return string.Format(CultureInfo.InvariantCulture, "{0:0} MiB/s", value); + } + + // MiB to GiB + value /= 1024; + if (value < 100) + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.0} GiB/s", value); + } + + return string.Format(CultureInfo.InvariantCulture, "{0:0} GiB/s", value); + } + internal override void PushActivate() { base.PushActivate(); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/SettingsManager.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/SettingsManager.cs new file mode 100644 index 0000000000..2c32ddb9f4 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/SettingsManager.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using CoreWidgetProvider.Helpers; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace Microsoft.CmdPal.Ext.PerformanceMonitor; + +internal sealed class SettingsManager : JsonSettingsManager +{ + private const string Namespace = "performanceMonitor"; + + private static string Namespaced(string propertyName) => $"{Namespace}.{propertyName}"; + + private readonly ChoiceSetSetting _networkSpeedUnit = new( + Namespaced(nameof(NetworkSpeedUnit)), + Resources.GetResource("Network_Speed_Unit_Setting_Title"), + Resources.GetResource("Network_Speed_Unit_Setting_Description"), + [ + new ChoiceSetSetting.Choice(Resources.GetResource("Network_Speed_Unit_BitsPerSec"), NetworkSpeedUnit.BitsPerSecond.ToString("G")), + new ChoiceSetSetting.Choice(Resources.GetResource("Network_Speed_Unit_BytesPerSec"), NetworkSpeedUnit.BytesPerSecond.ToString("G")), + new ChoiceSetSetting.Choice(Resources.GetResource("Network_Speed_Unit_BinaryBytesPerSec"), NetworkSpeedUnit.BinaryBytesPerSecond.ToString("G")), + ]); + + public NetworkSpeedUnit NetworkSpeedUnit => + Enum.TryParse(_networkSpeedUnit.Value, out var unit) + ? unit + : NetworkSpeedUnit.BitsPerSecond; + + private static string SettingsJsonPath() + { + var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal"); + Directory.CreateDirectory(directory); + return Path.Combine(directory, "settings.json"); + } + + public SettingsManager() + { + FilePath = SettingsJsonPath(); + + Settings.Add(_networkSpeedUnit); + + LoadSettings(); + + Settings.SettingsChanged += (_, _) => SaveSettings(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/Strings/en-US/Resources.resw b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/Strings/en-US/Resources.resw index 1d49265baf..92d5c7be1d 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/Strings/en-US/Resources.resw +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PerformanceMonitor/Strings/en-US/Resources.resw @@ -276,4 +276,19 @@ Receive ↓ + + Network speed unit + + + Choose the unit used to display network transmission speed + + + Bits per second (Kbps, Mbps, Gbps) + + + Bytes per second (KB/s, MB/s, GB/s) + + + Binary bytes per second (KiB/s, MiB/s, GiB/s) +