Phase 2-3: UI layout, theme switching, CRUD tools, process execution

- Phase 2: MainWindow 3-section layout (sidebar/content/log bar), Dark/Light theme with ThemeHelper, MainViewModel with ObservableProperty/RelayCommand, tool card filtering by search + category

- Phase 3: ToolEditWindow for add/edit tools, ProcessExecutionService (Process.Start + error handling), double-click + right-click context menu (run/edit), path browse dialog

- Bugfix: ContextMenu commands now use PlacementTarget.Tag binding (ContextMenu in separate visual tree)

- Bugfix: StaticResource converters moved to XAML before DataTemplate to fix XamlParseException on tool card render

- Bugfix: Pure filenames (no path separators) treated as PATH commands, not marked invalid

- Bugfix: RefreshData preserves SelectedCategory; Load() catches all exceptions; Save() wrapped in try-catch; auto-scroll log to newest entry

- Tests: xUnit project with 55 tests covering models, services, converters, and view models
This commit is contained in:
2026-05-09 21:52:31 +08:00
parent 752f09a7e4
commit 71be5da54b
22 changed files with 1991 additions and 22 deletions

View File

@@ -4,10 +4,273 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PersonalToolBox.Views"
xmlns:vm="clr-namespace:PersonalToolBox.ViewModels"
xmlns:models="clr-namespace:PersonalToolBox.Models"
mc:Ignorable="d"
Title="个人工具箱" Height="600" Width="900"
WindowStartupLocation="CenterScreen">
<Grid>
Title="个人工具箱" Height="650" Width="960"
WindowStartupLocation="CenterScreen"
Background="{DynamicResource Theme.Background}">
<Window.Resources>
<!-- 值转换器(必须定义在 DataTemplate 之前,确保 StaticResource 能解析) -->
<local:BoolToOpacityConverter x:Key="BoolToOpacityConverter"/>
<local:FirstCharConverter x:Key="FirstCharConverter"/>
<!-- 工具卡片模板 -->
<DataTemplate x:Key="ToolCardTemplate" DataType="{x:Type models:ToolItem}">
<Border Width="140" Height="100"
Margin="6"
Background="{DynamicResource Theme.CardBackground}"
BorderBrush="{DynamicResource Theme.CardBorder}"
BorderThickness="1"
CornerRadius="8"
Opacity="{Binding IsValid, Converter={StaticResource BoolToOpacityConverter}}"
Cursor="Hand"
ToolTip="{Binding Name}"
Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window}}">
<Border.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding DataContext.ExecuteToolCommand, RelativeSource={RelativeSource AncestorType=Window}}"
CommandParameter="{Binding}"/>
</Border.InputBindings>
<Border.ContextMenu>
<ContextMenu>
<MenuItem Header="运行"
Command="{Binding PlacementTarget.Tag.ExecuteToolCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding}"/>
<MenuItem Header="编辑"
Command="{Binding PlacementTarget.Tag.EditToolCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding}"/>
</ContextMenu>
</Border.ContextMenu>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="{Binding Name, Converter={StaticResource FirstCharConverter}}"
FontSize="28"
Foreground="{DynamicResource Theme.Accent}"
HorizontalAlignment="Center"
Margin="0,0,0,6"/>
<TextBlock Text="{Binding Name}"
FontSize="12"
Foreground="{DynamicResource Theme.Foreground}"
HorizontalAlignment="Center"
TextTrimming="CharacterEllipsis"
MaxWidth="120"/>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="3"/>
<RowDefinition Height="120"/>
</Grid.RowDefinitions>
<!-- ──────────────── 内容区 ──────────────── -->
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- ─── 左侧边栏 ─── -->
<Border Grid.Column="0" Background="{DynamicResource Theme.SidebarBackground}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
<RowDefinition Height="45"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="个人工具箱"
FontSize="18" FontWeight="Bold"
Foreground="{DynamicResource Theme.Accent}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
<ListBox Grid.Row="1"
ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory}"
Background="Transparent"
BorderThickness="0"
Foreground="{DynamicResource Theme.Foreground}"
SelectionMode="Single">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="16,10"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="Border"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="3,0,0,0"
Padding="{TemplateBinding Padding}">
<TextBlock Text="{Binding Name}"
Foreground="{DynamicResource Theme.Foreground}"
FontSize="14"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="#20FFFFFF"/>
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource Theme.Accent}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="#10FFFFFF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Grid.Row="2"
Command="{Binding ToggleThemeCommand}"
Background="Transparent"
BorderThickness="1"
BorderBrush="{DynamicResource Theme.CardBorder}"
Foreground="{DynamicResource Theme.Foreground}"
FontSize="12"
Height="35" Margin="10,0,10,10"
Cursor="Hand">
<StackPanel Orientation="Horizontal">
<TextBlock Text="◐" FontSize="16" Margin="0,0,8,0"
VerticalAlignment="Center"/>
<TextBlock Text="切换主题" VerticalAlignment="Center"/>
</StackPanel>
</Button>
</Grid>
</Border>
<!-- ─── 右侧主体区 ─── -->
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="{DynamicResource Theme.Background}"
BorderBrush="{DynamicResource Theme.CardBorder}"
BorderThickness="0,0,0,1">
<Grid Margin="12,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"
Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
Background="{DynamicResource Theme.InputBackground}"
Foreground="{DynamicResource Theme.Foreground}"
BorderBrush="{DynamicResource Theme.InputBorder}"
BorderThickness="1"
FontSize="14"
VerticalContentAlignment="Center"
Height="32"
Margin="0,0,12,0"
Padding="8,0"/>
<Button Grid.Column="1"
Content="+ 添加工具"
Command="{Binding AddToolCommand}"
Background="{DynamicResource Theme.ButtonBackground}"
Foreground="{DynamicResource Theme.ButtonForeground}"
BorderThickness="0"
FontSize="12"
Height="32"
Cursor="Hand">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="4"/>
</Style>
</Button.Resources>
</Button>
</Grid>
</Border>
<ScrollViewer Grid.Row="1"
VerticalScrollBarVisibility="Auto"
Background="{DynamicResource Theme.Background}">
<ItemsControl ItemsSource="{Binding FilteredTools}"
Margin="8">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<StaticResource ResourceKey="ToolCardTemplate"/>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Grid>
<!-- ──────────────── 分隔线 ──────────────── -->
<Border Grid.Row="1" Background="{DynamicResource Theme.CardBorder}"/>
<!-- ──────────────── 底部日志栏 ──────────────── -->
<Border Grid.Row="2" Background="{DynamicResource Theme.LogBackground}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0"
Background="{DynamicResource Theme.SidebarBackground}"
BorderBrush="{DynamicResource Theme.CardBorder}"
BorderThickness="0,0,0,1">
<Grid Margin="10,0">
<TextBlock Text="运行日志"
Foreground="{DynamicResource Theme.Foreground}"
FontSize="12" FontWeight="SemiBold"
VerticalAlignment="Center"/>
<Button Content="清空日志"
Command="{Binding ClearLogsCommand}"
Background="Transparent"
Foreground="{DynamicResource Theme.TextSecondary}"
BorderThickness="0"
FontSize="11"
HorizontalAlignment="Right"
Height="22"
Cursor="Hand"/>
</Grid>
</Border>
<ListBox Grid.Row="1"
x:Name="LogListBox"
ItemsSource="{Binding Logs}"
Background="Transparent"
BorderThickness="0"
Foreground="{DynamicResource Theme.Foreground}"
FontFamily="Consolas"
FontSize="12"
VirtualizingPanel.IsVirtualizing="True">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0,1,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:LogEntry}">
<TextBlock Text="{Binding .}" Padding="8,1"
Foreground="{DynamicResource Theme.Foreground}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>
</Grid>
</Window>

View File

@@ -1,11 +1,63 @@
using System.Windows;
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using PersonalToolBox.ViewModels;
namespace PersonalToolBox.Views;
public partial class MainWindow : Window
{
public MainWindow()
private readonly MainViewModel _viewModel;
public MainWindow(MainViewModel viewModel)
{
InitializeComponent();
_viewModel = viewModel;
DataContext = viewModel;
viewModel.Logs.CollectionChanged += OnLogsCollectionChanged;
}
private void OnLogsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && LogListBox.Items.Count > 0)
{
Dispatcher.BeginInvoke(new Action(() =>
{
LogListBox.ScrollIntoView(LogListBox.Items[^1]);
}), System.Windows.Threading.DispatcherPriority.Background);
}
}
}
/// <summary>
/// bool → double: true=1.0, false=0.4 (工具卡片失效时置灰)
/// </summary>
public class BoolToOpacityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is bool b && b ? 1.0 : 0.4;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
/// <summary>
/// string → string: 取字符串首字符作为图标占位
/// </summary>
public class FirstCharConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = value as string;
return string.IsNullOrEmpty(s) ? "?" : s[0].ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}

View File

@@ -0,0 +1,126 @@
<Window x:Class="PersonalToolBox.Views.ToolEditWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:models="clr-namespace:PersonalToolBox.Models"
Title="{Binding WindowTitle}" Height="380" Width="480"
WindowStartupLocation="CenterOwner"
ResizeMode="NoResize"
Background="{DynamicResource Theme.Background}">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</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" Grid.ColumnSpan="2"
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
Background="{DynamicResource Theme.InputBackground}"
Foreground="{DynamicResource Theme.Foreground}"
BorderBrush="{DynamicResource Theme.InputBorder}"
Height="30" Margin="0,0,0,12" Padding="6,0"/>
<!-- 工具路径 -->
<TextBlock Grid.Row="1" Grid.Column="0"
Text="路径:" Foreground="{DynamicResource Theme.Foreground}"
VerticalAlignment="Center" Margin="0,0,0,12"/>
<TextBox Grid.Row="1" Grid.Column="1"
Text="{Binding ExecutablePath, UpdateSourceTrigger=PropertyChanged}"
Background="{DynamicResource Theme.InputBackground}"
Foreground="{DynamicResource Theme.Foreground}"
BorderBrush="{DynamicResource Theme.InputBorder}"
Height="30" Margin="0,0,0,12" Padding="6,0"/>
<Button Grid.Row="1" Grid.Column="2"
Content="..."
Command="{Binding BrowseFileCommand}"
Background="{DynamicResource Theme.ButtonBackground}"
Foreground="{DynamicResource Theme.ButtonForeground}"
BorderThickness="0"
Width="24" Height="24" Margin="6,3,0,12"
FontWeight="Bold" Cursor="Hand"/>
<!-- 运行参数 -->
<TextBlock Grid.Row="2" Grid.Column="0"
Text="参数:" Foreground="{DynamicResource Theme.Foreground}"
VerticalAlignment="Center" Margin="0,0,0,12"/>
<TextBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"
Text="{Binding Arguments, UpdateSourceTrigger=PropertyChanged}"
Background="{DynamicResource Theme.InputBackground}"
Foreground="{DynamicResource Theme.Foreground}"
BorderBrush="{DynamicResource Theme.InputBorder}"
Height="30" Margin="0,0,0,12" Padding="6,0"/>
<!-- 所属分类 -->
<TextBlock Grid.Row="3" Grid.Column="0"
Text="分类:" Foreground="{DynamicResource Theme.Foreground}"
VerticalAlignment="Center" Margin="0,0,0,12"/>
<ComboBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"
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"/>
<!-- 全局快捷键 -->
<TextBlock Grid.Row="4" Grid.Column="0"
Text="快捷键:" Foreground="{DynamicResource Theme.Foreground}"
VerticalAlignment="Center" Margin="0,0,0,12"/>
<TextBox Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"
Text="{Binding HotKey, UpdateSourceTrigger=PropertyChanged}"
Background="{DynamicResource Theme.InputBackground}"
Foreground="{DynamicResource Theme.Foreground}"
BorderBrush="{DynamicResource Theme.InputBorder}"
Height="30" Margin="0,0,0,12" Padding="6,0"/>
<!-- 提示文字 -->
<TextBlock Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2"
Text="快捷键格式示例: Ctrl+Alt+T"
Foreground="{DynamicResource Theme.TextSecondary}"
FontSize="11" Margin="0,0,0,16"/>
<!-- 分隔线 -->
<Border Grid.Row="6" Grid.ColumnSpan="3"
BorderBrush="{DynamicResource Theme.CardBorder}"
BorderThickness="0,1,0,0" Margin="0,0,0,14"/>
<!-- 按钮 -->
<StackPanel Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="2"
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>

View File

@@ -0,0 +1,21 @@
using System.Windows;
using PersonalToolBox.ViewModels;
namespace PersonalToolBox.Views;
public partial class ToolEditWindow : Window
{
public ToolEditWindow(ToolEditViewModel viewModel)
{
InitializeComponent();
viewModel.CloseAction = (result) =>
{
// DialogResult 会自动关闭由 ShowDialog() 显示的窗口,无需显式调用 Close()
DialogResult = result;
};
Owner = Application.Current.MainWindow;
DataContext = viewModel;
}
}