From df0cff5112810732d5a901fe6ad3f11109818ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Sat, 4 Oct 2025 22:05:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=83=AD=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GearTask/GearTriggerStorageService.cs | 11 +++++ .../View/Pages/GearTriggerPage.xaml | 30 ++++++++---- .../Windows/GearTask/AddTriggerDialog.xaml | 48 ++++++++++++++----- .../Pages/Component/GearTriggerViewModel.cs | 12 +++++ .../GearTask/AddTriggerDialogViewModel.cs | 38 +++++++++++++-- 5 files changed, 115 insertions(+), 24 deletions(-) diff --git a/BetterGenshinImpact/Service/GearTask/GearTriggerStorageService.cs b/BetterGenshinImpact/Service/GearTask/GearTriggerStorageService.cs index 44b97929..ff7d4f92 100644 --- a/BetterGenshinImpact/Service/GearTask/GearTriggerStorageService.cs +++ b/BetterGenshinImpact/Service/GearTask/GearTriggerStorageService.cs @@ -156,6 +156,7 @@ public class GearTriggerStorageService TriggerType = viewModel.TriggerType.ToString(), CronExpression = viewModel.CronExpression, Hotkey = viewModel.Hotkey?.ToString() ?? string.Empty, + HotkeyType = viewModel.HotkeyType.ToString(), TaskDefinitionName = viewModel.TaskDefinitionName, CreatedTime = DateTime.Now, ModifiedTime = DateTime.Now @@ -175,11 +176,18 @@ public class GearTriggerStorageService return null; } + // 解析快捷键类型,如果解析失败则使用默认值 + if (!Enum.TryParse(data.HotkeyType, out var hotkeyType)) + { + hotkeyType = HotKeyTypeEnum.KeyboardMonitor; // 默认值 + } + var viewModel = new GearTriggerViewModel(data.Name, triggerType) { IsEnabled = data.IsEnabled, CronExpression = data.CronExpression, Hotkey = string.IsNullOrEmpty(data.Hotkey) ? null : HotKey.FromString(data.Hotkey), + HotkeyType = hotkeyType, TaskDefinitionName = data.TaskDefinitionName }; @@ -225,6 +233,9 @@ public class GearTriggerData [JsonProperty("hotkey")] public string Hotkey { get; set; } = string.Empty; + [JsonProperty("hotkey_type")] + public string HotkeyType { get; set; } = string.Empty; + [JsonProperty("task_definition_name")] public string TaskDefinitionName { get; set; } = string.Empty; diff --git a/BetterGenshinImpact/View/Pages/GearTriggerPage.xaml b/BetterGenshinImpact/View/Pages/GearTriggerPage.xaml index dde35955..20e102a7 100644 --- a/BetterGenshinImpact/View/Pages/GearTriggerPage.xaml +++ b/BetterGenshinImpact/View/Pages/GearTriggerPage.xaml @@ -124,15 +124,17 @@ - - - + + + + - + + @@ -164,9 +166,10 @@ - - - + + + + @@ -197,8 +200,19 @@ Padding="5,0" Visibility="{Binding TriggerType, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter=Hotkey}" /> - + + + + - + + + + + + + + + + + + + + + + - - - - - + + + + diff --git a/BetterGenshinImpact/ViewModel/Pages/Component/GearTriggerViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/Component/GearTriggerViewModel.cs index 74c4daff..9fe17a85 100644 --- a/BetterGenshinImpact/ViewModel/Pages/Component/GearTriggerViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/Component/GearTriggerViewModel.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Linq; using BetterGenshinImpact.Model.Gear.Triggers; using BetterGenshinImpact.Model; +using BetterGenshinImpact.Helpers.Extensions; using CommunityToolkit.Mvvm.ComponentModel; namespace BetterGenshinImpact.ViewModel.Pages.Component; @@ -48,6 +49,17 @@ public partial class GearTriggerViewModel : ObservableObject [ObservableProperty] private HotKey? _hotkey; + /// + /// 快捷键类型 (全局热键/键鼠监听) + /// + [ObservableProperty] + private HotKeyTypeEnum _hotkeyType = HotKeyTypeEnum.KeyboardMonitor; + + /// + /// 快捷键类型名称 (中文显示) + /// + public string HotkeyTypeName => HotkeyType.ToChineseName(); + public GearTriggerViewModel() { } diff --git a/BetterGenshinImpact/ViewModel/Windows/GearTask/AddTriggerDialogViewModel.cs b/BetterGenshinImpact/ViewModel/Windows/GearTask/AddTriggerDialogViewModel.cs index a8622cab..8556f8f0 100644 --- a/BetterGenshinImpact/ViewModel/Windows/GearTask/AddTriggerDialogViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Windows/GearTask/AddTriggerDialogViewModel.cs @@ -9,6 +9,7 @@ using BetterGenshinImpact.Model; using BetterGenshinImpact.Service; using Microsoft.Extensions.Logging; using Wpf.Ui.Violeta.Controls; +using BetterGenshinImpact.Helpers.Extensions; namespace BetterGenshinImpact.ViewModel.Windows.GearTask; @@ -32,6 +33,14 @@ public partial class AddTriggerDialogViewModel : ObservableObject [ObservableProperty] private HotKey? _selectedHotkey; + [ObservableProperty] + private HotKeyTypeEnum _hotkeyType = HotKeyTypeEnum.KeyboardMonitor; + + /// + /// 快捷键类型显示名称 + /// + public string HotkeyTypeName => HotkeyType.ToChineseName(); + [ObservableProperty] private string _selectedTaskDefinitionName = string.Empty; @@ -151,6 +160,18 @@ public partial class AddTriggerDialogViewModel : ObservableObject } } + /// + /// 快捷键类型改变时的处理 + /// + partial void OnHotkeyTypeChanged(HotKeyTypeEnum value) + { + // 通知UI更新快捷键类型显示名称 + OnPropertyChanged(nameof(HotkeyTypeName)); + + // 清空当前快捷键设置 + SelectedHotkey = null; + } + /// /// 触发器类型改变时的处理 /// @@ -189,7 +210,8 @@ public partial class AddTriggerDialogViewModel : ObservableObject IsEnabled = IsEnabled, TaskDefinitionName = SelectedTaskDefinitionName, CronExpression = SelectedTriggerType == TriggerType.Timed ? CronExpression : null, - Hotkey = SelectedTriggerType == TriggerType.Hotkey ? SelectedHotkey : null + Hotkey = SelectedTriggerType == TriggerType.Hotkey ? SelectedHotkey : null, + HotkeyType = SelectedTriggerType == TriggerType.Hotkey ? HotkeyType : HotKeyTypeEnum.KeyboardMonitor }; RequestClose?.Invoke(this, true); @@ -204,14 +226,22 @@ public partial class AddTriggerDialogViewModel : ObservableObject RequestClose?.Invoke(this, false); } + /// + /// 切换快捷键类型 + /// + [RelayCommand] + private void SwitchHotKeyType() + { + HotkeyType = HotkeyType == HotKeyTypeEnum.GlobalRegister ? HotKeyTypeEnum.KeyboardMonitor : HotKeyTypeEnum.GlobalRegister; + } + /// /// 选择热键 /// [RelayCommand] private void SelectHotkey() { - // TODO: 打开热键选择对话框 - // 这里先创建一个示例热键 - SelectedHotkey = new HotKey(System.Windows.Input.Key.F1, System.Windows.Input.ModifierKeys.Control); + // 移除旧的示例代码,现在使用HotKeyTextBox直接设置 + // HotKeyTextBox会直接绑定到SelectedHotkey属性 } } \ No newline at end of file