|
| 1 | +using Flow.Launcher.Infrastructure.Hotkey; |
| 2 | +using Flow.Launcher.Infrastructure.UserSettings; |
| 3 | +using System; |
| 4 | +using NHotkey; |
| 5 | +using NHotkey.Wpf; |
| 6 | +using Flow.Launcher.Core.Resource; |
| 7 | +using System.Windows; |
| 8 | +using Flow.Launcher.ViewModel; |
| 9 | + |
| 10 | +namespace Flow.Launcher.Helper |
| 11 | +{ |
| 12 | + internal static class HotKeyMapper |
| 13 | + { |
| 14 | + private static Settings settings; |
| 15 | + private static MainViewModel mainViewModel; |
| 16 | + |
| 17 | + internal static void Initialize(MainViewModel mainVM) |
| 18 | + { |
| 19 | + mainViewModel = mainVM; |
| 20 | + settings = mainViewModel._settings; |
| 21 | + |
| 22 | + SetHotkey(settings.Hotkey, OnHotkey); |
| 23 | + LoadCustomPluginHotkey(); |
| 24 | + } |
| 25 | + |
| 26 | + private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action) |
| 27 | + { |
| 28 | + var hotkey = new HotkeyModel(hotkeyStr); |
| 29 | + SetHotkey(hotkey, action); |
| 30 | + } |
| 31 | + |
| 32 | + internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action) |
| 33 | + { |
| 34 | + string hotkeyStr = hotkey.ToString(); |
| 35 | + try |
| 36 | + { |
| 37 | + HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action); |
| 38 | + } |
| 39 | + catch (Exception) |
| 40 | + { |
| 41 | + string errorMsg = |
| 42 | + string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), |
| 43 | + hotkeyStr); |
| 44 | + MessageBox.Show(errorMsg); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + internal static void RemoveHotkey(string hotkeyStr) |
| 49 | + { |
| 50 | + if (!string.IsNullOrEmpty(hotkeyStr)) |
| 51 | + { |
| 52 | + HotkeyManager.Current.Remove(hotkeyStr); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + internal static void OnHotkey(object sender, HotkeyEventArgs e) |
| 57 | + { |
| 58 | + if (!ShouldIgnoreHotkeys()) |
| 59 | + { |
| 60 | + UpdateLastQUeryMode(); |
| 61 | + |
| 62 | + mainViewModel.ToggleFlowLauncher(); |
| 63 | + e.Handled = true; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Checks if Flow Launcher should ignore any hotkeys |
| 69 | + /// </summary> |
| 70 | + private static bool ShouldIgnoreHotkeys() |
| 71 | + { |
| 72 | + return settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen(); |
| 73 | + } |
| 74 | + |
| 75 | + private static void UpdateLastQUeryMode() |
| 76 | + { |
| 77 | + switch(settings.LastQueryMode) |
| 78 | + { |
| 79 | + case LastQueryMode.Empty: |
| 80 | + mainViewModel.ChangeQueryText(string.Empty); |
| 81 | + break; |
| 82 | + case LastQueryMode.Preserved: |
| 83 | + mainViewModel.LastQuerySelected = true; |
| 84 | + break; |
| 85 | + case LastQueryMode.Selected: |
| 86 | + mainViewModel.LastQuerySelected = false; |
| 87 | + break; |
| 88 | + default: |
| 89 | + throw new ArgumentException($"wrong LastQueryMode: <{settings.LastQueryMode}>"); |
| 90 | + |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + internal static void LoadCustomPluginHotkey() |
| 95 | + { |
| 96 | + if (settings.CustomPluginHotkeys == null) |
| 97 | + return; |
| 98 | + |
| 99 | + foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys) |
| 100 | + { |
| 101 | + SetCustomQueryHotkey(hotkey); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey) |
| 106 | + { |
| 107 | + SetHotkey(hotkey.Hotkey, (s, e) => |
| 108 | + { |
| 109 | + if (ShouldIgnoreHotkeys()) |
| 110 | + return; |
| 111 | + |
| 112 | + mainViewModel.MainWindowVisibility = Visibility.Visible; |
| 113 | + mainViewModel.ChangeQueryText(hotkey.ActionKeyword); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + internal static bool CheckAvailability(HotkeyModel currentHotkey) |
| 118 | + { |
| 119 | + try |
| 120 | + { |
| 121 | + HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", currentHotkey.CharKey, currentHotkey.ModifierKeys, (sender, e) => { }); |
| 122 | + |
| 123 | + return true; |
| 124 | + } |
| 125 | + catch |
| 126 | + { |
| 127 | + } |
| 128 | + finally |
| 129 | + { |
| 130 | + HotkeyManager.Current.Remove("HotkeyAvailabilityTest"); |
| 131 | + } |
| 132 | + |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments