|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Collections.Specialized; |
3 | 4 | using System.ComponentModel; |
4 | 5 | using System.Linq; |
5 | 6 | using System.Windows; |
@@ -40,6 +41,7 @@ internal static void Initialize() |
40 | 41 | InitializeRegisteredHotkeys(); |
41 | 42 |
|
42 | 43 | _settings.PropertyChanged += Settings_PropertyChanged; |
| 44 | + _settings.CustomPluginHotkeys.CollectionChanged += CustomPluginHotkeys_CollectionChanged; |
43 | 45 | } |
44 | 46 |
|
45 | 47 | private static void InitializeRegisteredHotkeys() |
@@ -109,7 +111,7 @@ private static void InitializeRegisteredHotkeys() |
109 | 111 | // Custom query global hotkeys |
110 | 112 | foreach (var customPluginHotkey in _settings.CustomPluginHotkeys) |
111 | 113 | { |
112 | | - list.Add(new(RegisteredHotkeyType.CustomQuery, HotkeyType.Global, customPluginHotkey.Hotkey, "customQueryHotkey", CustomQueryHotkeyCommand, customPluginHotkey, () => customPluginHotkey.Hotkey = "")); |
| 114 | + list.Add(GetRegisteredHotkeyData(customPluginHotkey)); |
113 | 115 | } |
114 | 116 |
|
115 | 117 | // Plugin hotkeys |
@@ -159,6 +161,8 @@ private static void InitializeRegisteredHotkeys() |
159 | 161 | App.API.LogDebug(ClassName, $"Initialize {_settings.RegisteredHotkeys.Count} hotkeys:\n[\n\t{string.Join(",\n\t", _settings.RegisteredHotkeys)}\n]"); |
160 | 162 | } |
161 | 163 |
|
| 164 | + #region Hotkey Change Events |
| 165 | + |
162 | 166 | private static void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e) |
163 | 167 | { |
164 | 168 | switch (e.PropertyName) |
@@ -214,6 +218,83 @@ private static void Settings_PropertyChanged(object sender, PropertyChangedEvent |
214 | 218 | } |
215 | 219 | } |
216 | 220 |
|
| 221 | + private static void CustomPluginHotkeys_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
| 222 | + { |
| 223 | + switch (e.Action) |
| 224 | + { |
| 225 | + case NotifyCollectionChangedAction.Add: |
| 226 | + foreach (var item in e.NewItems) |
| 227 | + { |
| 228 | + if (item is CustomPluginHotkey customPluginHotkey) |
| 229 | + { |
| 230 | + var hotkeyData = GetRegisteredHotkeyData(customPluginHotkey); |
| 231 | + _settings.RegisteredHotkeys.Add(hotkeyData); |
| 232 | + SetHotkey(hotkeyData); |
| 233 | + } |
| 234 | + } |
| 235 | + break; |
| 236 | + case NotifyCollectionChangedAction.Remove: |
| 237 | + foreach (var item in e.OldItems) |
| 238 | + { |
| 239 | + if (item is CustomPluginHotkey customPluginHotkey) |
| 240 | + { |
| 241 | + var hotkeyData = SearchRegisteredHotkeyData(customPluginHotkey); |
| 242 | + _settings.RegisteredHotkeys.Remove(hotkeyData); |
| 243 | + RemoveHotkey(hotkeyData); |
| 244 | + } |
| 245 | + } |
| 246 | + break; |
| 247 | + case NotifyCollectionChangedAction.Replace: |
| 248 | + foreach (var item in e.OldItems) |
| 249 | + { |
| 250 | + if (item is CustomPluginHotkey customPluginHotkey) |
| 251 | + { |
| 252 | + var hotkeyData = SearchRegisteredHotkeyData(customPluginHotkey); |
| 253 | + _settings.RegisteredHotkeys.Remove(hotkeyData); |
| 254 | + RemoveHotkey(hotkeyData); |
| 255 | + } |
| 256 | + } |
| 257 | + foreach (var item in e.NewItems) |
| 258 | + { |
| 259 | + if (item is CustomPluginHotkey customPluginHotkey) |
| 260 | + { |
| 261 | + var hotkeyData = GetRegisteredHotkeyData(customPluginHotkey); |
| 262 | + _settings.RegisteredHotkeys.Add(hotkeyData); |
| 263 | + SetHotkey(hotkeyData); |
| 264 | + } |
| 265 | + } |
| 266 | + break; |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + #endregion |
| 271 | + |
| 272 | + #endregion |
| 273 | + |
| 274 | + #region Custom Query Hotkey |
| 275 | + |
| 276 | + private static RegisteredHotkeyData GetRegisteredHotkeyData(CustomPluginHotkey customPluginHotkey) |
| 277 | + { |
| 278 | + return new(RegisteredHotkeyType.CustomQuery, HotkeyType.Global, customPluginHotkey.Hotkey, "customQueryHotkey", CustomQueryHotkeyCommand, customPluginHotkey, () => ClearHotkeyForCustomQueryHotkey(customPluginHotkey)); |
| 279 | + } |
| 280 | + |
| 281 | + private static RegisteredHotkeyData SearchRegisteredHotkeyData(CustomPluginHotkey customPluginHotkey) |
| 282 | + { |
| 283 | + return _settings.RegisteredHotkeys.FirstOrDefault(h => h.RegisteredType == RegisteredHotkeyType.CustomQuery && |
| 284 | + customPluginHotkey.Equals(h.CommandParameter)); |
| 285 | + } |
| 286 | + |
| 287 | + private static void ClearHotkeyForCustomQueryHotkey(CustomPluginHotkey customPluginHotkey) |
| 288 | + { |
| 289 | + // Clear hotkey for custom query hotkey |
| 290 | + customPluginHotkey.Hotkey = string.Empty; |
| 291 | + |
| 292 | + // Remove hotkey events |
| 293 | + var hotkeyData = SearchRegisteredHotkeyData(customPluginHotkey); |
| 294 | + _settings.RegisteredHotkeys.Remove(hotkeyData); |
| 295 | + RemoveHotkey(hotkeyData); |
| 296 | + } |
| 297 | + |
217 | 298 | #endregion |
218 | 299 |
|
219 | 300 | // TODO: Deprecated |
@@ -575,19 +656,6 @@ private static void WindowPluginHotkey(WindowPluginHotkeyPair pair) |
575 | 656 |
|
576 | 657 | #endregion |
577 | 658 |
|
578 | | - // TODO: Deprecated |
579 | | - internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey) |
580 | | - { |
581 | | - SetHotkey(hotkey.Hotkey, (s, e) => |
582 | | - { |
583 | | - if (_mainViewModel.ShouldIgnoreHotkeys()) |
584 | | - return; |
585 | | - |
586 | | - App.API.ShowMainWindow(); |
587 | | - App.API.ChangeQuery(hotkey.ActionKeyword, true); |
588 | | - }); |
589 | | - } |
590 | | - |
591 | 659 | // TODO: Deprecated |
592 | 660 | internal static void SetGlobalPluginHotkey(GlobalPluginHotkey globalHotkey, PluginMetadata metadata, string hotkeyStr) |
593 | 661 | { |
|
0 commit comments