Skip to content

Commit db1c1b2

Browse files
committed
Use changed event for plugin hotkeys
1 parent 503bc48 commit db1c1b2

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ public static class PluginManager
3434
public static readonly HashSet<PluginPair> GlobalPlugins = new();
3535
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new();
3636

37+
public static Action<PluginHotkeyChangedEvent> PluginHotkeyChanged { get; set; }
38+
3739
// We should not initialize API in static constructor because it will create another API instance
3840
private static IPublicAPI api = null;
3941
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
4042

4143
private static PluginsSettings Settings;
4244
private static List<PluginMetadata> _metadatas;
4345
private static readonly List<string> _modifiedPlugins = new();
46+
4447
private static readonly Dictionary<HotkeyModel, List<(PluginMetadata, SearchWindowPluginHotkey)>> _windowPluginHotkeys = new();
4548

4649
/// <summary>
@@ -491,36 +494,37 @@ private static void InitializeWindowPluginHotkeys(Dictionary<PluginPair, List<Ba
491494
}
492495
}
493496

494-
public static string ChangePluginHotkey(PluginMetadata plugin, GlobalPluginHotkey pluginHotkey, HotkeyModel newHotkey)
497+
public static void ChangePluginHotkey(PluginMetadata plugin, GlobalPluginHotkey pluginHotkey, HotkeyModel newHotkey)
495498
{
496499
var oldHotkeyItem = plugin.PluginHotkeys.First(h => h.Id == pluginHotkey.Id);
497500
var settingHotkeyItem = Settings.GetPluginSettings(plugin.ID).pluginHotkeys.First(h => h.Id == pluginHotkey.Id);
498-
var oldHotkey = settingHotkeyItem.Hotkey;
501+
var oldHotkeyStr = settingHotkeyItem.Hotkey;
502+
var oldHotkey = new HotkeyModel(oldHotkeyStr);
499503
var newHotkeyStr = newHotkey.ToString();
500504

501505
// Update hotkey in plugin metadata & setting
502506
oldHotkeyItem.Hotkey = newHotkeyStr;
503507
settingHotkeyItem.Hotkey = newHotkeyStr;
504508

505-
return oldHotkey;
509+
PluginHotkeyChanged?.Invoke(new PluginHotkeyChangedEvent(oldHotkey, newHotkey, plugin, pluginHotkey));
506510
}
507511

508-
public static (HotkeyModel Old, HotkeyModel New) ChangePluginHotkey(PluginMetadata plugin, SearchWindowPluginHotkey pluginHotkey, HotkeyModel newHotkey)
512+
public static void ChangePluginHotkey(PluginMetadata plugin, SearchWindowPluginHotkey pluginHotkey, HotkeyModel newHotkey)
509513
{
510514
var oldHotkeyItem = plugin.PluginHotkeys.First(h => h.Id == pluginHotkey.Id);
511515
var settingHotkeyItem = Settings.GetPluginSettings(plugin.ID).pluginHotkeys.First(h => h.Id == pluginHotkey.Id);
512-
var oldHotkey = settingHotkeyItem.Hotkey;
516+
var oldHotkeyStr = settingHotkeyItem.Hotkey;
513517
var converter = new KeyGestureConverter();
514-
var oldHotkeyModel = new HotkeyModel(oldHotkey);
518+
var oldHotkey = new HotkeyModel(oldHotkeyStr);
515519
var newHotkeyStr = newHotkey.ToString();
516520

517521
// Update hotkey in plugin metadata & setting
518522
oldHotkeyItem.Hotkey = newHotkeyStr;
519523
settingHotkeyItem.Hotkey = newHotkeyStr;
520524

521525
// Update window plugin hotkey dictionary
522-
var oldHotkeyModels = _windowPluginHotkeys[oldHotkeyModel];
523-
_windowPluginHotkeys[oldHotkeyModel] = oldHotkeyModels.Where(x => x.Item1.ID != plugin.ID || x.Item2.Id != pluginHotkey.Id).ToList();
526+
var oldHotkeyModels = _windowPluginHotkeys[oldHotkey];
527+
_windowPluginHotkeys[oldHotkey] = oldHotkeyModels.Where(x => x.Item1.ID != plugin.ID || x.Item2.Id != pluginHotkey.Id).ToList();
524528

525529
if (_windowPluginHotkeys.TryGetValue(newHotkey, out var newHotkeyModels))
526530
{
@@ -536,7 +540,7 @@ public static (HotkeyModel Old, HotkeyModel New) ChangePluginHotkey(PluginMetada
536540
};
537541
}
538542

539-
return (oldHotkeyModel, newHotkey);
543+
PluginHotkeyChanged?.Invoke(new PluginHotkeyChangedEvent(oldHotkey, newHotkey, plugin, pluginHotkey));
540544
}
541545

542546
public static bool ActionKeywordRegistered(string actionKeyword)
@@ -802,5 +806,28 @@ internal static async Task UninstallPluginAsync(PluginMetadata plugin, bool remo
802806
}
803807

804808
#endregion
809+
810+
#region Class
811+
812+
public class PluginHotkeyChangedEvent
813+
{
814+
public HotkeyModel NewHotkey { get; }
815+
816+
public HotkeyModel OldHotkey { get; }
817+
818+
public PluginMetadata Metadata { get; }
819+
820+
public BasePluginHotkey PluginHotkey { get; }
821+
822+
public PluginHotkeyChangedEvent(HotkeyModel oldHotkey, HotkeyModel newHotkey, PluginMetadata metadata, BasePluginHotkey pluginHotkey)
823+
{
824+
OldHotkey = oldHotkey;
825+
NewHotkey = newHotkey;
826+
Metadata = metadata;
827+
PluginHotkey = pluginHotkey;
828+
}
829+
}
830+
831+
#endregion
805832
}
806833
}

0 commit comments

Comments
 (0)