From f98c68f9e822f0a691e6584ac03e99063c6e9cd7 Mon Sep 17 00:00:00 2001 From: MakesYT <42534870+MakesYT@users.noreply.github.com> Date: Sun, 5 Nov 2023 20:34:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=83=85=E6=99=AF?= =?UTF-8?q?=E6=BF=80=E6=B4=BB/=E5=81=9C=E6=AD=A2=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E9=94=AE=20=E4=BC=98=E5=8C=96=20=E5=BF=AB=E6=8D=B7=E9=94=AE?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E6=B5=81=E7=A8=8B=20=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A1=86=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/SDKs/CustomScenario/CustomScenario.cs | 16 +++ Core/SDKs/Services/IHotKeyEditor.cs | 5 +- Core/ViewModel/SearchWindowViewModel.cs | 5 +- .../TaskEditor/TaskEditorViewModel.cs | 63 ++++++++++ uToolkitopia/Controls/HotKeyShow.cs | 6 +- uToolkitopia/Controls/HotKeyShow.xaml | 24 ++++ uToolkitopia/SDKs/HotKeyHelper.cs | 35 +++--- uToolkitopia/Services/HotKeyEditorService.cs | 8 +- uToolkitopia/View/HotKeyEditorWindow.xaml.cs | 6 + uToolkitopia/View/MainWindow.xaml.cs | 108 ++++++++++-------- uToolkitopia/View/TaskEditor.xaml | 14 ++- 11 files changed, 221 insertions(+), 69 deletions(-) diff --git a/Core/SDKs/CustomScenario/CustomScenario.cs b/Core/SDKs/CustomScenario/CustomScenario.cs index 5b3832a..f28ce38 100644 --- a/Core/SDKs/CustomScenario/CustomScenario.cs +++ b/Core/SDKs/CustomScenario/CustomScenario.cs @@ -7,6 +7,8 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using Core.SDKs.CustomType; +using Core.SDKs.HotKey; +using Core.SDKs.Services.Config; using Core.SDKs.Services.Plugin; using Core.SDKs.Tools; using Core.ViewModel.TaskEditor; @@ -32,6 +34,9 @@ public partial class CustomScenario : ObservableRecipient, IDisposable [JsonIgnore] [ObservableProperty] private bool _isRunning = false; [JsonIgnore] [ObservableProperty] private string _name = "任务"; + + [JsonIgnore] [ObservableProperty] private HotKeyModel? _startHotKeyModel; + [JsonIgnore] [ObservableProperty] private HotKeyModel? _stopHotKeyModel; private Dictionary _tickTasks = new(); private TickUtil? _tickUtil; @@ -709,4 +714,15 @@ private void ParsePointItem(Dictionary threads, PointItem no private void OnDeserializing(StreamingContext context) //反序列化时hotkeys的默认值会被添加,需要先清空 { } + + [OnDeserialized] + // ReSharper disable once UnusedMember.Local + // ReSharper disable once UnusedParameter.Local + private void OnDeserialized(StreamingContext context) //反序列化时hotkeys的默认值会被添加,需要先清空 + { + StartHotKeyModel = + ConfigManger.Config.hotKeys.FirstOrDefault(e => e.SignName == $"Kitopia情景_{UUID}_激活快捷键"); + StopHotKeyModel = + ConfigManger.Config.hotKeys.FirstOrDefault(e => e.SignName == $"Kitopia情景_{UUID}_停止快捷键"); + } } \ No newline at end of file diff --git a/Core/SDKs/Services/IHotKeyEditor.cs b/Core/SDKs/Services/IHotKeyEditor.cs index b44026d..548c73f 100644 --- a/Core/SDKs/Services/IHotKeyEditor.cs +++ b/Core/SDKs/Services/IHotKeyEditor.cs @@ -1,6 +1,9 @@ -namespace Core.SDKs.Services; +using Core.SDKs.HotKey; + +namespace Core.SDKs.Services; public interface IHotKeyEditor { void EditByName(string name, object? owner); + void EditByHotKeyModel(HotKeyModel name, object? owner); } \ No newline at end of file diff --git a/Core/ViewModel/SearchWindowViewModel.cs b/Core/ViewModel/SearchWindowViewModel.cs index f79d416..2f9cd5e 100644 --- a/Core/ViewModel/SearchWindowViewModel.cs +++ b/Core/ViewModel/SearchWindowViewModel.cs @@ -342,7 +342,7 @@ partial void OnSearchChanged(string? value) Log.Debug("搜索变更:" + Search); - + Items.RaiseListChangedEvents = false; #region 清除上次搜索结果 @@ -618,6 +618,9 @@ partial void OnSearchChanged(string? value) }; Items.Add(item); } + + Items.RaiseListChangedEvents = true; + Items.ResetBindings(); }); } diff --git a/Core/ViewModel/TaskEditor/TaskEditorViewModel.cs b/Core/ViewModel/TaskEditor/TaskEditorViewModel.cs index aa327d7..7195090 100644 --- a/Core/ViewModel/TaskEditor/TaskEditorViewModel.cs +++ b/Core/ViewModel/TaskEditor/TaskEditorViewModel.cs @@ -2,11 +2,13 @@ using System.Collections.ObjectModel; using System.ComponentModel; +using System.Runtime.InteropServices; using System.Windows; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using Core.SDKs.CustomScenario; +using Core.SDKs.HotKey; using Core.SDKs.Services; using Core.SDKs.Services.Config; using Core.SDKs.Services.Plugin; @@ -149,6 +151,12 @@ public TaskEditorViewModel() } }; Scenario.nodes.Add(nodify3); + if (Scenario.UUID == null) + { + Scenario.UUID = Guid.NewGuid().ToString(); + } + + //nodeMethods.Add("new PointItem(){Title = \"Test\"}"); } @@ -163,6 +171,61 @@ public PendingConnectionViewModel PendingConnection get; } + [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] + public static extern IntPtr GetForegroundWindow(); + + [RelayCommand] + private void StartHotkeyEditor() + { + var hwndSource = System.Windows.Interop.HwndSource.FromHwnd(GetForegroundWindow()); + if (hwndSource == null) + { + return; + } + + var xx = (Window)hwndSource.RootVisual; + + if (Scenario.StartHotKeyModel == null) + { + var hotKeyModel = new HotKeyModel() + { MainName = $"Kitopia情景", Name = $"{Scenario.UUID}_激活快捷键", IsUsable = true }; + Scenario.StartHotKeyModel = hotKeyModel; + ((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByHotKeyModel(hotKeyModel, + xx); + } + else + { + ((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByHotKeyModel( + Scenario.StartHotKeyModel, xx); + } + } + + [RelayCommand] + private void StopHotkeyEditor() + { + var hwndSource = System.Windows.Interop.HwndSource.FromHwnd(GetForegroundWindow()); + if (hwndSource == null) + { + return; + } + + var xx = (Window)hwndSource.RootVisual; + + if (Scenario.StopHotKeyModel == null) + { + var hotKeyModel = new HotKeyModel() + { MainName = $"Kitopia情景", Name = $"{Scenario.UUID}_停止快捷键", IsUsable = true }; + Scenario.StopHotKeyModel = hotKeyModel; + ((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByHotKeyModel(hotKeyModel, + xx); + } + else + { + ((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByHotKeyModel( + Scenario.StopHotKeyModel, xx); + } + } + [RelayCommand] private void VerifyNode() { diff --git a/uToolkitopia/Controls/HotKeyShow.cs b/uToolkitopia/Controls/HotKeyShow.cs index aa178c2..93a988e 100644 --- a/uToolkitopia/Controls/HotKeyShow.cs +++ b/uToolkitopia/Controls/HotKeyShow.cs @@ -35,6 +35,10 @@ public enum KeyTypeE { ((HotKeyShow)o).HotKeyModel = ConfigManger.Config.hotKeys.FirstOrDefault(e => ($"{e.MainName}_{e.Name}".Equals(args.NewValue))); + if (((HotKeyShow)o).HotKeyModel is null) + { + ((HotKeyShow)o).KeyType = KeyTypeE.None; + } })); public static readonly DependencyProperty HotKeyModelProperty = DependencyProperty.Register(nameof(HotKeyModel), @@ -46,7 +50,7 @@ public enum KeyTypeE public static readonly DependencyProperty KeyTypeProperty = DependencyProperty.Register(nameof(KeyType), typeof(KeyTypeE), typeof(HotKeyShow), - new PropertyMetadata(KeyTypeE.Alt)); + new PropertyMetadata(KeyTypeE.None)); public static readonly DependencyProperty KeyNameProperty = DependencyProperty.Register(nameof(KeyName), typeof(string), typeof(HotKeyShow), diff --git a/uToolkitopia/Controls/HotKeyShow.xaml b/uToolkitopia/Controls/HotKeyShow.xaml index 9a2c366..17b60ed 100644 --- a/uToolkitopia/Controls/HotKeyShow.xaml +++ b/uToolkitopia/Controls/HotKeyShow.xaml @@ -61,10 +61,12 @@ + + @@ -75,66 +77,88 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/uToolkitopia/SDKs/HotKeyHelper.cs b/uToolkitopia/SDKs/HotKeyHelper.cs index 26fb82d..5256e1b 100644 --- a/uToolkitopia/SDKs/HotKeyHelper.cs +++ b/uToolkitopia/SDKs/HotKeyHelper.cs @@ -15,9 +15,16 @@ namespace Kitopia.SDKs; /// public class HotKeyHelper { - private static readonly Dictionary m_HotKeySettingsDic = new(); + private static Dictionary m_HotKeySettingsDic = new(); + private static readonly ILog log = LogManager.GetLogger(nameof(HotKeyHelper)); + public static Dictionary MHotKeySettingsDic + { + get => m_HotKeySettingsDic; + set => m_HotKeySettingsDic = value ?? throw new ArgumentNullException(nameof(value)); + } + /// /// 注册全局快捷键 /// @@ -53,31 +60,27 @@ private static bool RegisterHotKey(HotKeyModel hotKeyModel, IntPtr hWnd) { var fsModifierKey = new User32.HotKeyModifiers(); var hotKeySetting = $"{hotKeyModel.MainName}_{hotKeyModel.Name}"; + if (!hotKeyModel.IsUsable) + { + return true; + } log.Debug("注册热键:" + hotKeySetting); - - - if (!m_HotKeySettingsDic.ContainsKey(hotKeySetting)) + if (!Kernel32.GlobalFindAtom(hotKeySetting).IsInvalid) { - // 全局原子不会在应用程序终止时自动删除。每次调用GlobalAddAtom函数,必须相应的调用GlobalDeleteAtom函数删除原子。 - if (!Kernel32.GlobalFindAtom(hotKeySetting).IsInvalid) - { - Kernel32.GlobalDeleteAtom(Kernel32.GlobalFindAtom(hotKeySetting)); - } + Kernel32.GlobalDeleteAtom(Kernel32.GlobalFindAtom(hotKeySetting)); + } - // 获取唯一标识符 + // 获取唯一标识符 + if (m_HotKeySettingsDic.ContainsKey(hotKeySetting)) + { m_HotKeySettingsDic[hotKeySetting] = Kernel32.GlobalAddAtom(hotKeySetting).GetHashCode(); } else { - // 注销旧的热键 - User32.UnregisterHotKey(hWnd, m_HotKeySettingsDic[hotKeySetting]); + m_HotKeySettingsDic.Add(hotKeySetting, Kernel32.GlobalAddAtom(hotKeySetting).GetHashCode()); } - if (!hotKeyModel.IsUsable) - { - return true; - } // 注册热键 if (hotKeyModel.IsSelectShift) diff --git a/uToolkitopia/Services/HotKeyEditorService.cs b/uToolkitopia/Services/HotKeyEditorService.cs index 492ec06..5c603fa 100644 --- a/uToolkitopia/Services/HotKeyEditorService.cs +++ b/uToolkitopia/Services/HotKeyEditorService.cs @@ -1,5 +1,6 @@ using System.Linq; using System.Windows; +using Core.SDKs.HotKey; using Core.SDKs.Services; using Core.SDKs.Services.Config; using Kitopia.View; @@ -25,7 +26,12 @@ public void EditByName(string name, object? owner) return; } - var hotKeyEditor = new HotKeyEditorWindow(hotKeyModel); + EditByHotKeyModel(hotKeyModel, owner); + } + + public void EditByHotKeyModel(HotKeyModel name, object? owner) + { + var hotKeyEditor = new HotKeyEditorWindow(name); hotKeyEditor.Height = ServiceManager.Services.GetService().Height / 2; hotKeyEditor.Width = ServiceManager.Services.GetService().Width / 2; if (owner is Window) diff --git a/uToolkitopia/View/HotKeyEditorWindow.xaml.cs b/uToolkitopia/View/HotKeyEditorWindow.xaml.cs index 95456ff..2554bb2 100644 --- a/uToolkitopia/View/HotKeyEditorWindow.xaml.cs +++ b/uToolkitopia/View/HotKeyEditorWindow.xaml.cs @@ -1,5 +1,6 @@ #region +using System.Linq; using System.Windows; using System.Windows.Input; using Windows.System; @@ -164,6 +165,11 @@ private void ButtonBase_OnClick(object sender, RoutedEventArgs e) } _hotKeyModel.SelectKey = selectedKey.Value; + if (ConfigManger.Config.hotKeys.All(e2 => e2.SignName != _hotKeyModel.SignName)) + { + ConfigManger.Config.hotKeys.Add(_hotKeyModel); + } + ConfigManger.Save(); setSuccess = ((MainWindow)ServiceManager.Services.GetService(typeof(MainWindow))).HotKeySet(_hotKeyModel); if (!setSuccess) diff --git a/uToolkitopia/View/MainWindow.xaml.cs b/uToolkitopia/View/MainWindow.xaml.cs index 3be4d94..d0d24ae 100644 --- a/uToolkitopia/View/MainWindow.xaml.cs +++ b/uToolkitopia/View/MainWindow.xaml.cs @@ -1,7 +1,6 @@ #region using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; @@ -29,11 +28,6 @@ public partial class MainWindow { private static readonly ILog log = LogManager.GetLogger(nameof(MainWindow)); - /// - /// 记录快捷键注册项的唯一标识符 - /// - private Dictionary m_HotKeySettings = new(); - /// /// 当前窗口句柄 /// @@ -161,17 +155,6 @@ public bool HotKeySet(HotKeyModel hotKeyModel) { if (!HotKeyHelper.RegisterGlobalHotKey(new[] { hotKeyModel }, m_Hwnd, out var hotKeySettingsDic).Any()) { - foreach (var (key, value) in hotKeySettingsDic) - { - if (m_HotKeySettings.ContainsKey(key)) - { - m_HotKeySettings[key] = value; - continue; - } - - m_HotKeySettings.Add(key, value); - } - return true; } @@ -182,17 +165,6 @@ public void InitHotKey() { var list = ConfigManger.Config.hotKeys; var failList = HotKeyHelper.RegisterGlobalHotKey(list, m_Hwnd, out var hotKeySettingsDic); - foreach (var (key, value) in hotKeySettingsDic) - { - if (m_HotKeySettings.ContainsKey(key)) - { - m_HotKeySettings[key] = value; - continue; - } - - m_HotKeySettings.Add(key, value); - } - if (!failList.Any()) { return; @@ -257,31 +229,75 @@ private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wideParam, IntPtr longParam, } case User32.WindowMessage.WM_HOTKEY: var sid = wideParam.ToInt32(); - if (sid == m_HotKeySettings["Kitopia_显示搜索框"]) + if (!HotKeyHelper.MHotKeySettingsDic.ContainsValue(sid)) { - log.Debug("显示搜索框热键被触发"); - if (ServiceManager.Services.GetService()!.Visibility == Visibility.Visible) + return IntPtr.Zero; + } + + var key = HotKeyHelper.MHotKeySettingsDic.FirstOrDefault(e => e.Value == sid).Key.Split("_", 2); + switch (key[0]) + { + case "Kitopia": { - ServiceManager.Services.GetService()!.Visibility = Visibility.Hidden; + switch (key[1]) + { + case "显示搜索框": + { + log.Debug("显示搜索框热键被触发"); + if (ServiceManager.Services.GetService()!.Visibility == + Visibility.Visible) + { + ServiceManager.Services.GetService()!.Visibility = Visibility.Hidden; + } + else + { + ServiceManager.Services.GetService()!.CheckClipboard(); + + ServiceManager.Services.GetService()!.Show(); + + User32.SetForegroundWindow( + new WindowInteropHelper(ServiceManager.Services.GetService()!) + .Handle); + ServiceManager.Services.GetService()!.tx.Focus(); + ServiceManager.Services.GetService()!.tx.SelectAll(); + ThreadPool.QueueUserWorkItem(_ => + { + ServiceManager.Services.GetService()!.ReloadApps(); + }); + } + + break; + } + } + + break; } - else + case "Kitopia情景": { - ServiceManager.Services.GetService()!.CheckClipboard(); - - ServiceManager.Services.GetService()!.Show(); + var strings = key[1].Split("_", 2); + var firstOrDefault = + CustomScenarioManger.CustomScenarios.FirstOrDefault(e => e.UUID == strings[0]); + if (firstOrDefault == null) + { + return IntPtr.Zero; + } - User32.SetForegroundWindow( - new WindowInteropHelper(ServiceManager.Services.GetService()!) - .Handle); - ServiceManager.Services.GetService()!.tx.Focus(); - ServiceManager.Services.GetService()!.tx.SelectAll(); - ThreadPool.QueueUserWorkItem(_ => + switch (strings[1]) { - ServiceManager.Services.GetService()!.ReloadApps(); - }); - } + case "激活快捷键": + { + firstOrDefault.Run(); + break; + } + case "停止快捷键": + { + firstOrDefault.Stop(); + break; + } + } - break; + break; + } } handled = true; diff --git a/uToolkitopia/View/TaskEditor.xaml b/uToolkitopia/View/TaskEditor.xaml index f88445b..61f4e9e 100644 --- a/uToolkitopia/View/TaskEditor.xaml +++ b/uToolkitopia/View/TaskEditor.xaml @@ -623,10 +623,18 @@ - + - + + + + + + + +