A:增加设置页面,样式设置、透明度、颜色设置等内容

This commit is contained in:
jimmy committed 2025-07-27 16:58:25 +08:00
1 parent 66d535d1f8
commit 0405a874e4
4 files changed
+220 -62

No files matched your search

@@ -15,6 +15,7 @@
</d:UserControl.DataContext>
<Border
x:Name="MainBorder"
CornerRadius="8"
Background="#F0FFFFFF"
BorderBrush="#20000000"
@@ -30,68 +31,55 @@
</Border.Effect>
<Grid Margin="10,8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="5" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel x:Name="SpeedPanel" Orientation="Vertical">
<!-- Upload Speed -->
<Grid x:Name="UploadGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Text="↑"
Foreground="#FF6B6B"
FontSize="12"
FontWeight="Bold"
VerticalAlignment="Center"
Margin="0,0,6,0" />
<TextBlock
x:Name="UploadSpeedText"
Grid.Column="1"
Text="{Binding UploadSpeed}"
FontSize="12"
FontWeight="SemiBold"
Foreground="#333333"
VerticalAlignment="Center" />
</Grid>
<!-- 上传速度 -->
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Text="↑"
Foreground="#FF6B6B"
FontSize="12"
FontWeight="Bold"
VerticalAlignment="Center"
Margin="0,0,6,0" />
<TextBlock
Grid.Column="1"
Text="{Binding UploadSpeed}"
FontSize="12"
FontWeight="SemiBold"
Foreground="#333333"
VerticalAlignment="Center" />
</Grid>
<!-- 下载速度 -->
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Text="↓"
Foreground="#4ECDC4"
FontSize="12"
FontWeight="Bold"
VerticalAlignment="Center"
Margin="0,0,6,0" />
<TextBlock
Grid.Column="1"
Text="{Binding DownloadSpeed}"
FontSize="12"
FontWeight="SemiBold"
Foreground="#333333"
VerticalAlignment="Center" />
</Grid>
<!-- 状态信息(可选显示) -->
<TextBlock
Grid.Row="3"
Text="{Binding StatusText}"
FontSize="8"
Foreground="#666666"
HorizontalAlignment="Center"
Margin="0,4,0,0"
Visibility="Collapsed" />
<!-- Download Speed -->
<Grid x:Name="DownloadGrid" Margin="0,5,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Text="↓"
Foreground="#4ECDC4"
FontSize="12"
FontWeight="Bold"
VerticalAlignment="Center"
Margin="0,0,6,0" />
<TextBlock
x:Name="DownloadSpeedText"
Grid.Column="1"
Text="{Binding DownloadSpeed}"
FontSize="12"
FontWeight="SemiBold"
Foreground="#333333"
VerticalAlignment="Center" />
</Grid>
</StackPanel>
</Grid>
</Border>
</UserControl>
@@ -1,7 +1,9 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Common.Logger;
using Shell.ViewModel;
using Shell.Models;
namespace Shell.UserControls
{
@@ -11,6 +13,7 @@ namespace Shell.UserControls
public partial class NetworkMonitorView : System.Windows.Controls.UserControl
{
private NetworkMonitorViewModel _viewModel;
private NetViewSettings _settings;
public NetworkMonitorView()
{
@@ -21,21 +24,84 @@ namespace Shell.UserControls
_viewModel = new NetworkMonitorViewModel();
DataContext = _viewModel;
// 获取设置实例
_settings = NetViewSettings.Instance;
// 订阅控件生命周期事件
Loaded += NetworkMonitorView_Loaded;
Unloaded += NetworkMonitorView_Unloaded;
// 订阅设置变化事件
_settings.PropertyChanged += Settings_PropertyChanged;
}
private void NetworkMonitorView_Loaded(object sender, RoutedEventArgs e)
{
Log.Info("NetworkMonitorView 加载完成");
ApplySettings();
}
private void NetworkMonitorView_Unloaded(object sender, RoutedEventArgs e)
{
Log.Info("NetworkMonitorView 卸载");
// 取消订阅设置变化事件
_settings.PropertyChanged -= Settings_PropertyChanged;
// 释放ViewModel资源
_viewModel?.Dispose();
}
private void Settings_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// 当设置发生变化时,重新应用设置
System.Windows.Application.Current.Dispatcher.Invoke(ApplySettings);
}
private void ApplySettings()
{
try
{
// 应用背景颜色
var backgroundColor = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(_settings.BackgroundColor);
MainBorder.Background = new SolidColorBrush(backgroundColor);
// 应用文字颜色
var textColor = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(_settings.TextColor);
var textBrush = new SolidColorBrush(textColor);
UploadSpeedText.Foreground = textBrush;
DownloadSpeedText.Foreground = textBrush;
// 应用布局方向
if (_settings.LayoutDirection == LayoutDirection.Horizontal)
{
SpeedPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
DownloadGrid.Margin = new Thickness(15, 0, 0, 0); // 横向时添加间距
}
else
{
SpeedPanel.Orientation = System.Windows.Controls.Orientation.Vertical;
DownloadGrid.Margin = new Thickness(0, 5, 0, 0); // 纵向时添加间距
}
// 通知ViewModel应用设置(单位显示等)
if (_viewModel is ISettingsAware settingsAware)
{
settingsAware.ApplySettings(_settings);
}
Log.Info("设置已应用到NetworkMonitorView");
}
catch (Exception ex)
{
Log.Error4Ctx($"应用设置失败: {ex.Message}");
}
}
}
// 接口用于通知ViewModel应用设置
public interface ISettingsAware
{
void ApplySettings(NetViewSettings settings);
}
}
@@ -64,6 +64,11 @@
<TextBlock Text="" FontFamily="Segoe Fluent Icons" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="设置" Click="OpenSettings_Click">
<MenuItem.Icon>
<TextBlock Text="" FontFamily="Segoe Fluent Icons" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="隐藏到托盘" Click="HideToTray_Click">
<MenuItem.Icon>
@@ -3,6 +3,8 @@ using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using Shell.Utils;
namespace Shell.Views;
@@ -15,6 +17,11 @@ public partial class NetView : Window
private bool _snapToScreen = false;
private bool _showDetailedInfo = false;
// Double-click detection
private DispatcherTimer _doubleClickTimer;
private bool _isDoubleClick = false;
private const int DoubleClickTimeMs = 500; // 500ms for double-click detection
// Win32 API for click-through functionality
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
@@ -38,6 +45,9 @@ public partial class NetView : Window
// Prevent window snapping by setting ResizeMode appropriately
this.ResizeMode = ResizeMode.NoResize;
// Initialize double-click timer
InitializeDoubleClickTimer();
}
private void NetView_Loaded(object sender, RoutedEventArgs e)
@@ -154,6 +164,51 @@ public partial class NetView : Window
_trayIconManager = new TrayIconManager(this);
}
private void InitializeDoubleClickTimer()
{
_doubleClickTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(DoubleClickTimeMs)
};
_doubleClickTimer.Tick += DoubleClickTimer_Tick;
}
private void DoubleClickTimer_Tick(object sender, EventArgs e)
{
_doubleClickTimer.Stop();
if (!_isDoubleClick)
{
// Single click - no action needed for now
}
_isDoubleClick = false;
}
private void HandleDoubleClick()
{
try
{
var settings = Shell.Models.NetViewSettings.Instance;
switch (settings.DoubleClickAction)
{
case Shell.Models.DoubleClickAction.TrafficAnalysis:
TrafficAnalysisWindow.ShowWindow();
break;
case Shell.Models.DoubleClickAction.Settings:
SettingsWindow.ShowWindow();
break;
case Shell.Models.DoubleClickAction.None:
default:
// 无动作
break;
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"执行双击动作失败: {ex.Message}", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
protected override void OnClosing(CancelEventArgs e)
{
// 阻止窗口关闭,而是隐藏到托盘
@@ -184,6 +239,20 @@ public partial class NetView : Window
System.Diagnostics.Debug.WriteLine($"Error closing TrafficAnalysisWindow: {ex.Message}");
}
// Close SettingsWindow if it exists
try
{
if (SettingsWindow.Instance != null)
{
SettingsWindow.Instance.ForceClose();
}
}
catch (Exception ex)
{
// Log but don't prevent shutdown
System.Diagnostics.Debug.WriteLine($"Error closing SettingsWindow: {ex.Message}");
}
_trayIconManager?.Dispose();
System.Windows.Application.Current.Shutdown();
}
@@ -302,9 +371,39 @@ public partial class NetView : Window
}
}
// Handle window dragging
private void OpenSettings_Click(object sender, RoutedEventArgs e)
{
try
{
SettingsWindow.ShowWindow();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"无法打开设置窗口: {ex.Message}", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
// Handle window dragging and double-click detection
private void Grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// Handle double-click detection
if (_doubleClickTimer.IsEnabled)
{
// This is a second click within the timeout period
_doubleClickTimer.Stop();
_isDoubleClick = true;
HandleDoubleClick();
return;
}
else
{
// Start timer for double-click detection
_isDoubleClick = false;
_doubleClickTimer.Start();
}
// Only allow dragging if position is not locked and click-through is disabled
if (!_isPositionLocked && !_isClickThrough)
{