From b715904439467cfe7917077b72c6e4f6094662e3 Mon Sep 17 00:00:00 2001 From: home-PC Date: Sun, 10 May 2026 02:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=B7=A5=E5=85=B7/=E7=BB=84?= =?UTF-8?q?=E5=90=88=E9=9A=8F=E8=BD=AF=E4=BB=B6=E5=90=AF=E5=8A=A8=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=BF=90=E8=A1=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ToolItem 模型新增 AutoRunOnStart 属性,持久化到配置文件 - App.OnStartup 启动后自动执行标记为自启动的工具/组合(间隔500ms) - 工具编辑窗口和组合编辑窗口新增「随软件启动自动运行」CheckBox - 主界面工具卡片左上角 Rocket 角标标识已启用自启的项目 --- PersonalToolBox/App.xaml.cs | 6 +++++- PersonalToolBox/Models/ToolItem.cs | 5 +++++ .../ViewModels/GroupEditViewModel.cs | 8 +++++++- PersonalToolBox/ViewModels/MainViewModel.cs | 18 ++++++++++++++++++ .../ViewModels/ToolEditViewModel.cs | 8 +++++++- PersonalToolBox/Views/GroupEditWindow.xaml | 15 ++++++++++++--- PersonalToolBox/Views/MainWindow.xaml | 17 +++++++++++++++++ PersonalToolBox/Views/ToolEditWindow.xaml | 13 +++++++++++-- 8 files changed, 82 insertions(+), 8 deletions(-) diff --git a/PersonalToolBox/App.xaml.cs b/PersonalToolBox/App.xaml.cs index f1ce50f..8041e55 100644 --- a/PersonalToolBox/App.xaml.cs +++ b/PersonalToolBox/App.xaml.cs @@ -34,7 +34,7 @@ public partial class App : System.Windows.Application }; } - protected override void OnStartup(StartupEventArgs e) + protected override async void OnStartup(StartupEventArgs e) { try { @@ -49,6 +49,7 @@ public partial class App : System.Windows.Application dataService.Load(); var mainWindow = Services.GetRequiredService(); + var mainViewModel = Services.GetRequiredService(); // -autostart 参数:开机自启时隐藏窗口 if (e.Args.Contains("-autostart", StringComparer.OrdinalIgnoreCase)) @@ -59,6 +60,9 @@ public partial class App : System.Windows.Application { mainWindow.Show(); } + + // 启动标记为自动运行的工具和组合 + await mainViewModel.ExecuteAutoRunToolsAsync(); } catch (Exception ex) { diff --git a/PersonalToolBox/Models/ToolItem.cs b/PersonalToolBox/Models/ToolItem.cs index 7bc89fa..4a15d3d 100644 --- a/PersonalToolBox/Models/ToolItem.cs +++ b/PersonalToolBox/Models/ToolItem.cs @@ -57,4 +57,9 @@ public class ToolItem /// 当 IsGroup 为 true 时,存储需批量启动的子工具 ID 列表 /// public List SubToolIds { get; set; } = new(); + + /// + /// 是否在软件启动时自动运行 + /// + public bool AutoRunOnStart { get; set; } } diff --git a/PersonalToolBox/ViewModels/GroupEditViewModel.cs b/PersonalToolBox/ViewModels/GroupEditViewModel.cs index a5a725d..b47cd03 100644 --- a/PersonalToolBox/ViewModels/GroupEditViewModel.cs +++ b/PersonalToolBox/ViewModels/GroupEditViewModel.cs @@ -35,6 +35,9 @@ public partial class GroupEditViewModel : ObservableObject [ObservableProperty] private IconProvider.IconOption? _selectedIcon; + [ObservableProperty] + private bool _autoRunOnStart; + /// /// 可供勾选的普通工具列表(IsGroup == false) /// @@ -85,6 +88,7 @@ public partial class GroupEditViewModel : ObservableObject Name = groupToEdit.Name; HotKey = groupToEdit.HotKey; SelectedCategory = Categories.FirstOrDefault(c => c.Id == groupToEdit.CategoryId); + AutoRunOnStart = groupToEdit.AutoRunOnStart; if (!string.IsNullOrEmpty(groupToEdit.IconCode)) { @@ -119,6 +123,7 @@ public partial class GroupEditViewModel : ObservableObject _editingGroup.CategoryId = SelectedCategory?.Id ?? string.Empty; _editingGroup.SubToolIds = selectedIds; _editingGroup.IsValid = true; + _editingGroup.AutoRunOnStart = AutoRunOnStart; _logService.Info($"已更新组合: {Name.Trim()}(包含 {selectedIds.Count} 个工具)"); } else @@ -131,7 +136,8 @@ public partial class GroupEditViewModel : ObservableObject CategoryId = SelectedCategory?.Id ?? string.Empty, IsGroup = true, SubToolIds = selectedIds, - IsValid = true + IsValid = true, + AutoRunOnStart = AutoRunOnStart }); _logService.Info($"已添加组合: {Name.Trim()}(包含 {selectedIds.Count} 个工具)"); } diff --git a/PersonalToolBox/ViewModels/MainViewModel.cs b/PersonalToolBox/ViewModels/MainViewModel.cs index 7b469a2..e669172 100644 --- a/PersonalToolBox/ViewModels/MainViewModel.cs +++ b/PersonalToolBox/ViewModels/MainViewModel.cs @@ -257,6 +257,24 @@ public partial class MainViewModel : ObservableObject RefreshData(); } + // ───────────────────────────── 自动运行 ───────────────────────────── + + /// + /// 启动时自动运行标记为 AutoRunOnStart 的工具和组合 + /// + public async Task ExecuteAutoRunToolsAsync() + { + var autoTools = _dataService.Config.Tools.Where(t => t.AutoRunOnStart).ToList(); + if (autoTools.Count == 0) return; + + _logService.Info($"正在启动 {autoTools.Count} 个自启动工具..."); + foreach (var tool in autoTools) + { + await _processService.ExecuteAsync(tool); + await Task.Delay(500); + } + } + // ───────────────────────────── 数据刷新 ───────────────────────────── /// diff --git a/PersonalToolBox/ViewModels/ToolEditViewModel.cs b/PersonalToolBox/ViewModels/ToolEditViewModel.cs index e396692..f1af386 100644 --- a/PersonalToolBox/ViewModels/ToolEditViewModel.cs +++ b/PersonalToolBox/ViewModels/ToolEditViewModel.cs @@ -43,6 +43,9 @@ public partial class ToolEditViewModel : ObservableObject [ObservableProperty] private IconProvider.IconOption? _selectedIcon; + [ObservableProperty] + private bool _autoRunOnStart; + /// /// 分类下拉列表 /// @@ -87,6 +90,7 @@ public partial class ToolEditViewModel : ObservableObject Arguments = toolToEdit.Arguments; HotKey = toolToEdit.HotKey; SelectedCategory = Categories.FirstOrDefault(c => c.Id == toolToEdit.CategoryId); + AutoRunOnStart = toolToEdit.AutoRunOnStart; if (!string.IsNullOrEmpty(toolToEdit.IconCode)) { @@ -149,6 +153,7 @@ public partial class ToolEditViewModel : ObservableObject _editingTool.HotKey = HotKey.Trim(); _editingTool.CategoryId = SelectedCategory?.Id ?? string.Empty; _editingTool.IsValid = IsExecutablePathValid(ExecutablePath.Trim()); + _editingTool.AutoRunOnStart = AutoRunOnStart; _logService.Info($"已更新工具: {Name.Trim()}"); } @@ -163,7 +168,8 @@ public partial class ToolEditViewModel : ObservableObject Arguments = Arguments.Trim(), HotKey = HotKey.Trim(), CategoryId = SelectedCategory?.Id ?? string.Empty, - IsValid = IsExecutablePathValid(ExecutablePath.Trim()) + IsValid = IsExecutablePathValid(ExecutablePath.Trim()), + AutoRunOnStart = AutoRunOnStart }; _dataService.Config.Tools.Add(newTool); diff --git a/PersonalToolBox/Views/GroupEditWindow.xaml b/PersonalToolBox/Views/GroupEditWindow.xaml index 7bec5d3..8541ce7 100644 --- a/PersonalToolBox/Views/GroupEditWindow.xaml +++ b/PersonalToolBox/Views/GroupEditWindow.xaml @@ -105,6 +105,7 @@ + @@ -185,12 +186,20 @@ VerticalContentAlignment="Center" Height="28" Margin="0,0,0,10" Padding="6,0"/> + + + - - -