From 8c31039328858b311e810f5199a6a2d005800bd8 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Tue, 13 Jul 2021 21:53:02 +1000 Subject: [PATCH] refactor- point CustomQueryHotkey to HotkeyMapper --- .../CustomQueryHotkeySetting.xaml.cs | 44 ++----------------- Flow.Launcher/Helper/HotKeyMapper.cs | 33 ++++++++------ Flow.Launcher/HotkeyControl.xaml.cs | 4 +- 3 files changed, 27 insertions(+), 54 deletions(-) diff --git a/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs b/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs index 813c5e254..8493d192d 100644 --- a/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs +++ b/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs @@ -1,4 +1,5 @@ using Flow.Launcher.Core.Resource; +using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Infrastructure.UserSettings; using NHotkey; @@ -52,11 +53,7 @@ namespace Flow.Launcher }; _settings.CustomPluginHotkeys.Add(pluginHotkey); - SetHotkey(ctlHotkey.CurrentHotkey, delegate - { - App.API.ChangeQuery(pluginHotkey.ActionKeyword); - ShowMainWindow(); - }); + HotKeyMapper.SetCustomQueryHotkey(pluginHotkey); } else { @@ -69,22 +66,11 @@ namespace Flow.Launcher updateCustomHotkey.ActionKeyword = tbAction.Text; updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString(); //remove origin hotkey - RemoveHotkey(oldHotkey); - SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate - { - App.API.ChangeQuery(updateCustomHotkey.ActionKeyword); - ShowMainWindow(); - }); + HotKeyMapper.RemoveHotkey(oldHotkey); + HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey); } Close(); - - static void ShowMainWindow() - { - Window mainWindow = Application.Current.MainWindow; - mainWindow.Visibility = Visibility.Visible; - mainWindow.Focus(); - } } public void UpdateItem(CustomPluginHotkey item) @@ -109,28 +95,6 @@ namespace Flow.Launcher Application.Current.MainWindow.Visibility = Visibility.Visible; } - private void RemoveHotkey(string hotkeyStr) - { - if (!string.IsNullOrEmpty(hotkeyStr)) - { - HotkeyManager.Current.Remove(hotkeyStr); - } - } - - private void SetHotkey(HotkeyModel hotkey, EventHandler action) - { - string hotkeyStr = hotkey.ToString(); - try - { - HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action); - } - catch (Exception) - { - string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr); - MessageBox.Show(errorMsg); - } - } - private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e) { Close(); diff --git a/Flow.Launcher/Helper/HotKeyMapper.cs b/Flow.Launcher/Helper/HotKeyMapper.cs index 93bc25bbb..fbfcb274b 100644 --- a/Flow.Launcher/Helper/HotKeyMapper.cs +++ b/Flow.Launcher/Helper/HotKeyMapper.cs @@ -20,7 +20,7 @@ namespace Flow.Launcher.Helper settings = mainViewModel._settings; SetHotkey(settings.Hotkey, OnHotkey); - SetCustomPluginHotkey(); + LoadCustomPluginHotkey(); } private static void SetHotkey(string hotkeyStr, EventHandler action) @@ -91,22 +91,29 @@ namespace Flow.Launcher.Helper } } - private static void SetCustomPluginHotkey() + internal static void LoadCustomPluginHotkey() { - if (settings.CustomPluginHotkeys == null) return; + if (settings.CustomPluginHotkeys == null) + return; + foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys) { - SetHotkey(hotkey.Hotkey, (s, e) => - { - if (ShouldIgnoreHotkeys()) - return; - - UpdateLastQUeryMode(); - - mainViewModel.MainWindowVisibility = Visibility.Visible; - mainViewModel.ChangeQueryText(hotkey.ActionKeyword); - }); + SetCustomQueryHotkey(hotkey); } } + + internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey) + { + SetHotkey(hotkey.Hotkey, (s, e) => + { + if (ShouldIgnoreHotkeys()) + return; + + UpdateLastQUeryMode(); + + mainViewModel.MainWindowVisibility = Visibility.Visible; + mainViewModel.ChangeQueryText(hotkey.ActionKeyword); + }); + } } } diff --git a/Flow.Launcher/HotkeyControl.xaml.cs b/Flow.Launcher/HotkeyControl.xaml.cs index 5c7141d1c..ed90e40af 100644 --- a/Flow.Launcher/HotkeyControl.xaml.cs +++ b/Flow.Launcher/HotkeyControl.xaml.cs @@ -21,7 +21,9 @@ namespace Flow.Launcher protected virtual void OnHotkeyChanged() { EventHandler handler = HotkeyChanged; - if (handler != null) handler(this, EventArgs.Empty); + + if (handler != null) + handler(this, EventArgs.Empty); } public HotkeyControl()