Skip to content

Commit 5fa3bec

Browse files
committed
Add hotkey change events for custom query hotkeys
1 parent ff99669 commit 5fa3bec

File tree

1 file changed

+82
-14
lines changed

1 file changed

+82
-14
lines changed

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Specialized;
34
using System.ComponentModel;
45
using System.Linq;
56
using System.Windows;
@@ -40,6 +41,7 @@ internal static void Initialize()
4041
InitializeRegisteredHotkeys();
4142

4243
_settings.PropertyChanged += Settings_PropertyChanged;
44+
_settings.CustomPluginHotkeys.CollectionChanged += CustomPluginHotkeys_CollectionChanged;
4345
}
4446

4547
private static void InitializeRegisteredHotkeys()
@@ -109,7 +111,7 @@ private static void InitializeRegisteredHotkeys()
109111
// Custom query global hotkeys
110112
foreach (var customPluginHotkey in _settings.CustomPluginHotkeys)
111113
{
112-
list.Add(new(RegisteredHotkeyType.CustomQuery, HotkeyType.Global, customPluginHotkey.Hotkey, "customQueryHotkey", CustomQueryHotkeyCommand, customPluginHotkey, () => customPluginHotkey.Hotkey = ""));
114+
list.Add(GetRegisteredHotkeyData(customPluginHotkey));
113115
}
114116

115117
// Plugin hotkeys
@@ -159,6 +161,8 @@ private static void InitializeRegisteredHotkeys()
159161
App.API.LogDebug(ClassName, $"Initialize {_settings.RegisteredHotkeys.Count} hotkeys:\n[\n\t{string.Join(",\n\t", _settings.RegisteredHotkeys)}\n]");
160162
}
161163

164+
#region Hotkey Change Events
165+
162166
private static void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
163167
{
164168
switch (e.PropertyName)
@@ -214,6 +218,83 @@ private static void Settings_PropertyChanged(object sender, PropertyChangedEvent
214218
}
215219
}
216220

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+
217298
#endregion
218299

219300
// TODO: Deprecated
@@ -575,19 +656,6 @@ private static void WindowPluginHotkey(WindowPluginHotkeyPair pair)
575656

576657
#endregion
577658

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-
591659
// TODO: Deprecated
592660
internal static void SetGlobalPluginHotkey(GlobalPluginHotkey globalHotkey, PluginMetadata metadata, string hotkeyStr)
593661
{

0 commit comments

Comments
 (0)