feat.网速显示组件引入了一套更好的等距。

This commit is contained in:
lincube
2026-04-15 15:42:11 +08:00
parent c2cc62b58b
commit 03e32ee6cb
3 changed files with 21 additions and 131 deletions

View File

@@ -30,7 +30,10 @@
FontWeight="SemiBold"
Margin="2,0,0,0"
VerticalAlignment="Center"
Foreground="{DynamicResource AdaptiveTextPrimaryBrush}"/>
Foreground="{DynamicResource AdaptiveTextPrimaryBrush}"
FontFamily="Consolas, Courier New, monospace"
MinWidth="42"
TextAlignment="Right"/>
</StackPanel>
<!-- 分隔符 -->
@@ -55,7 +58,10 @@
FontWeight="SemiBold"
Margin="2,0,0,0"
VerticalAlignment="Center"
Foreground="{DynamicResource AdaptiveTextPrimaryBrush}"/>
Foreground="{DynamicResource AdaptiveTextPrimaryBrush}"
FontFamily="Consolas, Courier New, monospace"
MinWidth="42"
TextAlignment="Right"/>
</StackPanel>
<!-- 网络类型图标 -->

View File

@@ -317,31 +317,32 @@ public partial class NetworkSpeedWidget : UserControl, IDesktopComponentWidget
private static string FormatSpeed(long bytesPerSecond)
{
// 根据数值大小决定显示格式始终保持3个字符宽度
// 例如: 1.23, 12.3, 123
// 根据数值大小选择合适的单位确保显示始终在1-99.9范围内
// 当数值达到100时自动切换到更大的单位
return bytesPerSecond switch
{
>= 1024 * 1024 * 1024 => FormatWithThreeDigits(bytesPerSecond / (1024.0 * 1024.0 * 1024.0), "G"),
>= 1024 * 1024 => FormatWithThreeDigits(bytesPerSecond / (1024.0 * 1024.0), "M"),
>= 1024 => FormatWithThreeDigits(bytesPerSecond / 1024.0, "K"),
>= 100L * 1024 * 1024 * 1024 => FormatWithThreeDigits(bytesPerSecond / (1024.0 * 1024.0 * 1024.0), "G"),
>= 100L * 1024 * 1024 => FormatWithThreeDigits(bytesPerSecond / (1024.0 * 1024.0), "M"),
>= 100L * 1024 => FormatWithThreeDigits(bytesPerSecond / 1024.0, "K"),
>= 100 => FormatWithThreeDigits(bytesPerSecond / 1024.0, "K"), // 100B+ 显示为 0.1K
_ => FormatWithThreeDigits(bytesPerSecond, "B")
};
}
/// <summary>
/// 格式化数字始终保持3个有效数字的显示宽度
/// 格式化数字始终保持3位数字+小数点,确保宽度恒定
/// 数值范围始终在1-99.9之间
/// </summary>
private static string FormatWithThreeDigits(double value, string unit)
{
// 根据数值大小决定小数位数,确保总宽度一致
// 始终保持3位数字小数点始终存在
// 数值范围: 0.0 - 99.9
// < 10: 显示两位小数 (如 1.23)
// 10-99: 显示一位小数 (如 12.3)
// >= 100: 显示整数 (如 123)
// >= 10: 显示一位小数 (如 12.3, 99.9)
string formatted = value switch
{
< 10 => $"{value:F2}",
< 100 => $"{value:F1}",
_ => $"{value:F0}"
< 10 => $"{value:F2}", // 1.23
_ => $"{value:F1}" // 12.3, 99.9
};
return formatted + unit;