Phase 3 缺陷修复 + 分类管理功能 + ComboBox 主题适配
缺陷修复: 日志自动滚动、StaticResource 顺序修复、ContextMenu PlacementTarget 绑定、PATH 命令支持、异常全量捕获、RefreshData 状态保留、全局崩溃日志 分类管理: 添加/编辑/删除分类,删除时工具移入全部,CategoryEditViewModel/Window UI 修复: ComboBox 自定义模板,暗色模式弹出层/下拉项主题配色,文本绑定修复
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using Moq;
|
||||
using PersonalToolBox.Models;
|
||||
using PersonalToolBox.Services;
|
||||
using PersonalToolBox.ViewModels;
|
||||
|
||||
namespace PersonalToolBox.Tests.ViewModels;
|
||||
|
||||
public class CategoryEditViewModelTests
|
||||
{
|
||||
private readonly Mock<IDataService> _dataServiceMock = new();
|
||||
private readonly Mock<ILogService> _logServiceMock = new();
|
||||
private readonly AppConfig _config;
|
||||
|
||||
public CategoryEditViewModelTests()
|
||||
{
|
||||
_config = new AppConfig();
|
||||
_dataServiceMock.Setup(d => d.Config).Returns(_config);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_AddMode_SetsTitle()
|
||||
{
|
||||
var vm = new CategoryEditViewModel(_dataServiceMock.Object, _logServiceMock.Object);
|
||||
|
||||
Assert.Equal("添加分类", vm.WindowTitle);
|
||||
Assert.Empty(vm.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_EditMode_SetsTitleAndName()
|
||||
{
|
||||
var cat = new Category { Name = "开发工具" };
|
||||
var vm = new CategoryEditViewModel(_dataServiceMock.Object, _logServiceMock.Object, cat);
|
||||
|
||||
Assert.Equal("编辑分类", vm.WindowTitle);
|
||||
Assert.Equal("开发工具", vm.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Save_EmptyName_LogsWarning()
|
||||
{
|
||||
var vm = new CategoryEditViewModel(_dataServiceMock.Object, _logServiceMock.Object);
|
||||
|
||||
vm.SaveCommand.Execute(null);
|
||||
|
||||
_logServiceMock.Verify(x => x.Warning(It.Is<string>(s => s.Contains("名称不能为空"))), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Save_AddMode_AddsCategoryAndCloses()
|
||||
{
|
||||
var vm = new CategoryEditViewModel(_dataServiceMock.Object, _logServiceMock.Object) { Name = "新分类" };
|
||||
bool? closeResult = null;
|
||||
vm.CloseAction = (r) => closeResult = r;
|
||||
|
||||
vm.SaveCommand.Execute(null);
|
||||
|
||||
Assert.True(vm.Saved);
|
||||
Assert.True(closeResult);
|
||||
Assert.Single(_config.Categories);
|
||||
Assert.Equal("新分类", _config.Categories[0].Name);
|
||||
_dataServiceMock.Verify(x => x.Save(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Save_EditMode_UpdatesCategoryAndCloses()
|
||||
{
|
||||
var cat = new Category { Name = "旧名称" };
|
||||
var vm = new CategoryEditViewModel(_dataServiceMock.Object, _logServiceMock.Object, cat) { Name = "新名称" };
|
||||
bool? closeResult = null;
|
||||
vm.CloseAction = (r) => closeResult = r;
|
||||
|
||||
vm.SaveCommand.Execute(null);
|
||||
|
||||
Assert.True(vm.Saved);
|
||||
Assert.True(closeResult);
|
||||
Assert.Equal("新名称", cat.Name);
|
||||
_dataServiceMock.Verify(x => x.Save(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Cancel_ClosesWithoutSaving()
|
||||
{
|
||||
var vm = new CategoryEditViewModel(_dataServiceMock.Object, _logServiceMock.Object);
|
||||
bool? closeResult = null;
|
||||
vm.CloseAction = (r) => closeResult = r;
|
||||
|
||||
vm.CancelCommand.Execute(null);
|
||||
|
||||
Assert.False(vm.Saved);
|
||||
Assert.False(closeResult);
|
||||
_dataServiceMock.Verify(x => x.Save(), Times.Never);
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,7 @@ public partial class App : Application
|
||||
services.AddSingleton<IProcessExecutionService, ProcessExecutionService>();
|
||||
services.AddSingleton<ViewModels.MainViewModel>();
|
||||
services.AddTransient<ViewModels.ToolEditViewModel>();
|
||||
services.AddTransient<ViewModels.CategoryEditViewModel>();
|
||||
services.AddSingleton<MainWindow>();
|
||||
}
|
||||
|
||||
|
||||
80
PersonalToolBox/ViewModels/CategoryEditViewModel.cs
Normal file
80
PersonalToolBox/ViewModels/CategoryEditViewModel.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using PersonalToolBox.Models;
|
||||
using PersonalToolBox.Services;
|
||||
|
||||
namespace PersonalToolBox.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// 分类编辑窗口 ViewModel,管理添加/编辑分类的交互逻辑
|
||||
/// </summary>
|
||||
public partial class CategoryEditViewModel : ObservableObject
|
||||
{
|
||||
private readonly IDataService _dataService;
|
||||
private readonly ILogService _logService;
|
||||
private readonly Category? _editingCategory;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _windowTitle = "添加分类";
|
||||
|
||||
[ObservableProperty]
|
||||
private string _name = string.Empty;
|
||||
|
||||
public Action<bool?>? CloseAction { get; set; }
|
||||
public bool Saved { get; private set; }
|
||||
|
||||
public CategoryEditViewModel(IDataService dataService, ILogService logService, Category? categoryToEdit = null)
|
||||
{
|
||||
_dataService = dataService;
|
||||
_logService = logService;
|
||||
_editingCategory = categoryToEdit;
|
||||
|
||||
if (categoryToEdit != null)
|
||||
{
|
||||
WindowTitle = "编辑分类";
|
||||
Name = categoryToEdit.Name;
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
{
|
||||
_logService.Warning("分类名称不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_editingCategory != null)
|
||||
{
|
||||
_editingCategory.Name = Name.Trim();
|
||||
_logService.Info($"已更新分类: {Name.Trim()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_dataService.Config.Categories.Add(new Category
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Name = Name.Trim()
|
||||
});
|
||||
_logService.Info($"已添加分类: {Name.Trim()}");
|
||||
}
|
||||
|
||||
_dataService.Save();
|
||||
Saved = true;
|
||||
CloseAction?.Invoke(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logService.Error($"保存分类失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Cancel()
|
||||
{
|
||||
CloseAction?.Invoke(false);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -121,6 +122,63 @@ public partial class MainViewModel : ObservableObject
|
||||
_processService.Execute(tool);
|
||||
}
|
||||
|
||||
// ───────────────────────────── 分类管理命令 ─────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 添加分类
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void AddCategory()
|
||||
{
|
||||
var vm = _serviceProvider.GetRequiredService<CategoryEditViewModel>();
|
||||
var window = new CategoryEditWindow(vm);
|
||||
window.ShowDialog();
|
||||
|
||||
if (vm.Saved) RefreshData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑分类(右键菜单触发)
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void EditCategory(Category? category)
|
||||
{
|
||||
if (category == null || string.IsNullOrEmpty(category.Id)) return;
|
||||
|
||||
var dataService = _serviceProvider.GetRequiredService<IDataService>();
|
||||
var logService = _serviceProvider.GetRequiredService<ILogService>();
|
||||
var editVm = new CategoryEditViewModel(dataService, logService, category);
|
||||
var window = new CategoryEditWindow(editVm);
|
||||
window.ShowDialog();
|
||||
|
||||
if (editVm.Saved) RefreshData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除分类,其下所有工具移入"全部"
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void DeleteCategory(Category? category)
|
||||
{
|
||||
if (category == null || string.IsNullOrEmpty(category.Id)) return;
|
||||
|
||||
if (MessageBox.Show(
|
||||
$"确定删除分类 \"{category.Name}\" 吗?\n该分类下的所有工具将移入「全部」。",
|
||||
"确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes)
|
||||
return;
|
||||
|
||||
// 将属于该分类的工具设为"全部"
|
||||
foreach (var tool in _dataService.Config.Tools.Where(t => t.CategoryId == category.Id))
|
||||
tool.CategoryId = string.Empty;
|
||||
|
||||
// 从配置中移除分类
|
||||
_dataService.Config.Categories.Remove(category);
|
||||
_dataService.Save();
|
||||
|
||||
_logService.Info($"已删除分类: {category.Name}");
|
||||
RefreshData();
|
||||
}
|
||||
|
||||
// ───────────────────────────── 数据刷新 ─────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
|
||||
54
PersonalToolBox/Views/CategoryEditWindow.xaml
Normal file
54
PersonalToolBox/Views/CategoryEditWindow.xaml
Normal file
@@ -0,0 +1,54 @@
|
||||
<Window x:Class="PersonalToolBox.Views.CategoryEditWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="{Binding WindowTitle}" Height="170" Width="380"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
ResizeMode="NoResize"
|
||||
Background="{DynamicResource Theme.Background}">
|
||||
|
||||
<Grid Margin="20">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="60"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0" Grid.Column="0"
|
||||
Text="名称:" Foreground="{DynamicResource Theme.Foreground}"
|
||||
VerticalAlignment="Center" Margin="0,0,0,12"/>
|
||||
<TextBox Grid.Row="0" Grid.Column="1"
|
||||
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
Height="30" Margin="0,0,0,16" Padding="6,0"/>
|
||||
|
||||
<Border Grid.Row="2" Grid.ColumnSpan="2"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
BorderThickness="0,1,0,0" Margin="0,0,0,14"/>
|
||||
|
||||
<StackPanel Grid.Row="3" Grid.Column="1"
|
||||
Orientation="Horizontal" HorizontalAlignment="Right">
|
||||
<Button Content="保存"
|
||||
Command="{Binding SaveCommand}"
|
||||
Background="{DynamicResource Theme.ButtonBackground}"
|
||||
Foreground="{DynamicResource Theme.ButtonForeground}"
|
||||
BorderThickness="0"
|
||||
Width="80" Height="30" FontSize="13"
|
||||
Cursor="Hand" Margin="0,0,10,0"/>
|
||||
<Button Content="取消"
|
||||
Command="{Binding CancelCommand}"
|
||||
Background="{DynamicResource Theme.CardBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
BorderThickness="1"
|
||||
Width="80" Height="30" FontSize="13"
|
||||
Cursor="Hand"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
17
PersonalToolBox/Views/CategoryEditWindow.xaml.cs
Normal file
17
PersonalToolBox/Views/CategoryEditWindow.xaml.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Windows;
|
||||
using PersonalToolBox.ViewModels;
|
||||
|
||||
namespace PersonalToolBox.Views;
|
||||
|
||||
public partial class CategoryEditWindow : Window
|
||||
{
|
||||
public CategoryEditWindow(CategoryEditViewModel viewModel)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
viewModel.CloseAction = (result) => { DialogResult = result; };
|
||||
|
||||
Owner = Application.Current.MainWindow;
|
||||
DataContext = viewModel;
|
||||
}
|
||||
}
|
||||
@@ -78,8 +78,9 @@
|
||||
<Border Grid.Column="0" Background="{DynamicResource Theme.SidebarBackground}">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="60"/>
|
||||
<RowDefinition Height="50"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="42"/>
|
||||
<RowDefinition Height="45"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
@@ -91,6 +92,7 @@
|
||||
HorizontalAlignment="Center"/>
|
||||
|
||||
<ListBox Grid.Row="1"
|
||||
x:Name="CategoryListBox"
|
||||
ItemsSource="{Binding Categories}"
|
||||
SelectedItem="{Binding SelectedCategory}"
|
||||
Background="Transparent"
|
||||
@@ -100,6 +102,7 @@
|
||||
<ListBox.ItemContainerStyle>
|
||||
<Style TargetType="ListBoxItem">
|
||||
<Setter Property="Padding" Value="16,10"/>
|
||||
<Setter Property="Tag" Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window}}"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="ListBoxItem">
|
||||
@@ -107,7 +110,18 @@
|
||||
Background="Transparent"
|
||||
BorderBrush="Transparent"
|
||||
BorderThickness="3,0,0,0"
|
||||
Padding="{TemplateBinding Padding}">
|
||||
Padding="{TemplateBinding Padding}"
|
||||
Tag="{TemplateBinding Tag}">
|
||||
<Border.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="编辑"
|
||||
Command="{Binding PlacementTarget.Tag.EditCategoryCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
|
||||
CommandParameter="{Binding}"/>
|
||||
<MenuItem Header="删除"
|
||||
Command="{Binding PlacementTarget.Tag.DeleteCategoryCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
|
||||
CommandParameter="{Binding}"/>
|
||||
</ContextMenu>
|
||||
</Border.ContextMenu>
|
||||
<TextBlock Text="{Binding Name}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
FontSize="14"/>
|
||||
@@ -128,7 +142,24 @@
|
||||
</ListBox.ItemContainerStyle>
|
||||
</ListBox>
|
||||
|
||||
<!-- 分类管理按钮 -->
|
||||
<Button Grid.Row="2"
|
||||
Command="{Binding AddCategoryCommand}"
|
||||
Background="Transparent"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
FontSize="12"
|
||||
Height="35" Margin="10,0,10,3"
|
||||
Cursor="Hand">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="+" FontSize="16" Margin="0,0,8,0"
|
||||
VerticalAlignment="Center"/>
|
||||
<TextBlock Text="添加分类" VerticalAlignment="Center"/>
|
||||
</StackPanel>
|
||||
</Button>
|
||||
|
||||
<Button Grid.Row="3"
|
||||
Command="{Binding ToggleThemeCommand}"
|
||||
Background="Transparent"
|
||||
BorderThickness="1"
|
||||
|
||||
@@ -7,6 +7,93 @@
|
||||
ResizeMode="NoResize"
|
||||
Background="{DynamicResource Theme.Background}">
|
||||
|
||||
<Window.Resources>
|
||||
<!-- ComboBox 下拉项统一样式 -->
|
||||
<Style TargetType="ComboBoxItem">
|
||||
<Setter Property="Background" Value="{DynamicResource Theme.InputBackground}"/>
|
||||
<Setter Property="Foreground" Value="{DynamicResource Theme.Foreground}"/>
|
||||
<Setter Property="Padding" Value="6,3"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="ComboBoxItem">
|
||||
<Border x:Name="Border"
|
||||
Background="{TemplateBinding Background}"
|
||||
Padding="{TemplateBinding Padding}">
|
||||
<ContentPresenter/>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsHighlighted" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Theme.Accent}"/>
|
||||
<Setter Property="Foreground" Value="{DynamicResource Theme.ButtonForeground}"/>
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<!-- ComboBox 整体样式(覆盖弹出层背景) -->
|
||||
<Style TargetType="ComboBox">
|
||||
<Setter Property="Background" Value="{DynamicResource Theme.InputBackground}"/>
|
||||
<Setter Property="Foreground" Value="{DynamicResource Theme.Foreground}"/>
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource Theme.InputBorder}"/>
|
||||
<Setter Property="BorderThickness" Value="1"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="ComboBox">
|
||||
<Grid>
|
||||
<ToggleButton x:Name="ToggleButton"
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
Foreground="{TemplateBinding Foreground}"
|
||||
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
|
||||
Padding="{TemplateBinding Padding}">
|
||||
<ToggleButton.Template>
|
||||
<ControlTemplate TargetType="ToggleButton">
|
||||
<Border Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
Padding="{TemplateBinding Padding}">
|
||||
<Grid>
|
||||
<TextBlock Text="{Binding Text, RelativeSource={RelativeSource AncestorType=ComboBox}}"
|
||||
Foreground="{TemplateBinding Foreground}"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalAlignment="Left"
|
||||
Margin="6,0,0,0"/>
|
||||
<Path x:Name="Arrow" Data="M0,0 L4,4 8,0" Stroke="{TemplateBinding Foreground}"
|
||||
StrokeThickness="1.5" HorizontalAlignment="Right" Width="8"
|
||||
VerticalAlignment="Center" Margin="0,0,6,0"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
</ToggleButton.Template>
|
||||
</ToggleButton>
|
||||
<Popup x:Name="Popup"
|
||||
IsOpen="{TemplateBinding IsDropDownOpen}"
|
||||
Placement="Bottom"
|
||||
AllowsTransparency="True"
|
||||
Focusable="False"
|
||||
PopupAnimation="Slide">
|
||||
<Grid MaxHeight="{TemplateBinding MaxDropDownHeight}"
|
||||
MinWidth="{TemplateBinding ActualWidth}">
|
||||
<Border Background="{DynamicResource Theme.InputBackground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
BorderThickness="1"
|
||||
SnapsToDevicePixels="True">
|
||||
<ScrollViewer>
|
||||
<ItemsPresenter/>
|
||||
</ScrollViewer>
|
||||
</Border>
|
||||
</Grid>
|
||||
</Popup>
|
||||
</Grid>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</Window.Resources>
|
||||
|
||||
<Grid Margin="20">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
@@ -73,9 +160,6 @@
|
||||
ItemsSource="{Binding Categories}"
|
||||
SelectedItem="{Binding SelectedCategory}"
|
||||
DisplayMemberPath="Name"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
Height="30" Margin="0,0,0,12" Padding="4,0"/>
|
||||
|
||||
<!-- 全局快捷键 -->
|
||||
|
||||
Reference in New Issue
Block a user