A:调整文字展示效果

This commit is contained in:
jimmy committed 2025-08-08 10:03:02 +08:00
1 parent 5b7abd1ef3
commit c8207a2c85
6 files changed
+162 -16

No files matched your search

+3 -3
View File
@@ -2,8 +2,8 @@
x:Class="Shell.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NetVizor"
xmlns:converters="clr-namespace:NetVizor.Views.Converters">
xmlns:local="clr-namespace:Shell"
xmlns:converters="clr-namespace:Shell.Converters">
<Application.Resources>
<ResourceDictionary>
<converters:NullToCollapsedConverter
@@ -27,4 +27,4 @@
</ResourceDictionary>
</Application.Resources>
</Application>
</Application>
@@ -3,7 +3,7 @@ using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace NetVizor.Views.Converters
namespace Shell.Converters
{
public class NullToCollapsedConverter : IValueConverter
{
@@ -30,4 +30,27 @@ namespace NetVizor.Views.Converters
throw new NotImplementedException();
}
}
}
public class InvertedBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool boolValue)
{
return boolValue ? Visibility.Collapsed : Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Visibility visibility)
{
return visibility == Visibility.Collapsed;
}
return false;
}
}
}
@@ -5,11 +5,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModel="clr-namespace:Shell.ViewModel"
xmlns:converters="clr-namespace:Shell.Converters"
mc:Ignorable="d"
d:DesignHeight="120"
d:DesignWidth="200"
Background="Transparent">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:InvertedBooleanToVisibilityConverter x:Key="InvertedBooleanToVisibilityConverter" />
</UserControl.Resources>
<d:UserControl.DataContext>
<viewModel:NetworkMonitorViewModel />
</d:UserControl.DataContext>
@@ -102,10 +108,24 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!-- App Icon -->
<Image Grid.Column="0" Width="12" Height="12"
Source="{Binding IconSource}"
VerticalAlignment="Center" />
<!-- App Icon or Initial Letter -->
<Grid Grid.Column="0" Width="12" Height="12" VerticalAlignment="Center">
<!-- Process Icon -->
<Image Source="{Binding IconSource}"
Visibility="{Binding HasIcon, Converter={StaticResource BooleanToVisibilityConverter}}" />
<!-- Initial Letter Fallback -->
<Border Background="{Binding InitialBackgroundColor}"
CornerRadius="6"
Visibility="{Binding HasIcon, Converter={StaticResource InvertedBooleanToVisibilityConverter}}">
<TextBlock Text="{Binding InitialLetter}"
FontSize="8"
FontWeight="Bold"
Foreground="White"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,-1,0,0" />
</Border>
</Grid>
<!-- App Name -->
<TextBlock Grid.Column="1"
@@ -125,11 +145,11 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="↑"
Foreground="#FF6B6B" FontSize="9"
Foreground="#FF6B6B" FontSize="10"
VerticalAlignment="Center" Margin="0,0,2,0" />
<TextBlock Grid.Column="1"
Text="{Binding FormattedUploadSpeed}"
FontSize="9" FontWeight="Normal"
FontSize="10" FontWeight="Normal"
Foreground="#666666"
VerticalAlignment="Center"
Width="40"
@@ -144,11 +164,11 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="↓"
Foreground="#4ECDC4" FontSize="9"
Foreground="#4ECDC4" FontSize="10"
VerticalAlignment="Center" Margin="0,0,2,0" />
<TextBlock Grid.Column="1"
Text="{Binding FormattedDownloadSpeed}"
FontSize="9" FontWeight="Normal"
FontSize="10" FontWeight="Normal"
Foreground="#666666"
VerticalAlignment="Center"
Width="40"
@@ -156,6 +156,10 @@ public class NetworkMonitorViewModel : INotifyPropertyChanged
// 初始化数据
LoadNetworkInterfaces();
LoadTopListSettings();
// 监听NetViewSettings的变更
NetViewSettings.Instance.PropertyChanged += OnNetViewSettingsChanged;
_updateTimer.Start();
}
}
@@ -460,6 +464,19 @@ public class NetworkMonitorViewModel : INotifyPropertyChanged
}
}
/// <summary>
/// 处理NetViewSettings变更事件
/// </summary>
private void OnNetViewSettingsChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(NetViewSettings.ShowNetworkTopList) ||
e.PropertyName == nameof(NetViewSettings.NetworkTopListCount) ||
e.PropertyName == nameof(NetViewSettings.LayoutDirection))
{
UpdateTopListVisibility();
}
}
/// <summary>
/// 更新Top榜可见性
/// </summary>
@@ -626,6 +643,9 @@ public class NetworkMonitorViewModel : INotifyPropertyChanged
Log.Info("NetworkMonitorViewModel 销毁");
_updateTimer?.Stop();
// 取消事件监听
NetViewSettings.Instance.PropertyChanged -= OnNetViewSettingsChanged;
}
#endregion
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Utils.ETW.Models;
@@ -61,9 +62,25 @@ public class ProcessNetworkStatsViewModel : INotifyPropertyChanged
{
_iconSource = value;
OnPropertyChanged();
OnPropertyChanged(nameof(HasIcon));
}
}
/// <summary>
/// 是否有图标
/// </summary>
public bool HasIcon => _iconSource != null;
/// <summary>
/// 首字母
/// </summary>
public string InitialLetter => GetInitialLetter();
/// <summary>
/// 首字母背景色
/// </summary>
public System.Windows.Media.Brush InitialBackgroundColor => GetInitialBackgroundColor();
/// <summary>
/// 更新统计数据
/// </summary>
@@ -76,6 +93,10 @@ public class ProcessNetworkStatsViewModel : INotifyPropertyChanged
OnPropertyChanged(nameof(FormattedTotalSpeed));
OnPropertyChanged(nameof(TotalSpeed));
// 通知首字母相关属性更新
OnPropertyChanged(nameof(InitialLetter));
OnPropertyChanged(nameof(InitialBackgroundColor));
// 如果图标还未加载,尝试重新加载
if (_iconSource == null)
{
@@ -117,6 +138,59 @@ public class ProcessNetworkStatsViewModel : INotifyPropertyChanged
}
}
/// <summary>
/// 获取进程名首字母
/// </summary>
private string GetInitialLetter()
{
if (string.IsNullOrEmpty(ProcessName))
return "?";
var name = ProcessName.Trim();
if (name.Length == 0)
return "?";
// 获取第一个字符,转为大写
var firstChar = name[0];
if (char.IsLetter(firstChar))
{
return char.ToUpper(firstChar).ToString();
}
else if (char.IsDigit(firstChar))
{
return firstChar.ToString();
}
else
{
return "?";
}
}
/// <summary>
/// 根据进程名生成背景色
/// </summary>
private System.Windows.Media.Brush GetInitialBackgroundColor()
{
var colors = new[]
{
"#FF5722", "#E91E63", "#9C27B0", "#673AB7", "#3F51B5",
"#2196F3", "#03A9F4", "#00BCD4", "#009688", "#4CAF50",
"#8BC34A", "#CDDC39", "#FFC107", "#FF9800", "#FF5722"
};
if (string.IsNullOrEmpty(ProcessName))
{
return new SolidColorBrush(
(System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#9E9E9E"));
}
// 使用进程名的哈希值来选择颜色,确保同名进程使用相同颜色
var hash = ProcessName.GetHashCode();
var index = Math.Abs(hash) % colors.Length;
return new SolidColorBrush(
(System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(colors[index]));
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
@@ -594,9 +594,18 @@ public partial class SettingsWindow : Window
.GetMethod("OnPropertyChanged",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
propertyChangedMethod?.Invoke(netViewSettings, new object[] { "ShowNetworkTopList" });
propertyChangedMethod?.Invoke(netViewSettings, new object[] { "NetworkTopListCount" });
propertyChangedMethod?.Invoke(netViewSettings, new object[] { "IsTopListAvailable" });
if (propertyChangedMethod != null)
{
propertyChangedMethod.Invoke(netViewSettings, new object[] { "ShowNetworkTopList" });
propertyChangedMethod.Invoke(netViewSettings, new object[] { "NetworkTopListCount" });
propertyChangedMethod.Invoke(netViewSettings, new object[] { "LayoutDirection" });
propertyChangedMethod.Invoke(netViewSettings, new object[] { "IsTopListAvailable" });
}
else
{
// 备用方案:保存设置到NetViewSettings的json文件
netViewSettings.SaveSettings();
}
Log.Info("NetworkMonitorViewModel 排行榜显示已刷新");
}