Refactor hotkey initialization logic

Simplify hotkey initialization by using the null-coalescing
assignment operator (`??=`) to ensure `metadata.PluginHotkeys`
is initialized to an empty collection if null. Add a call to
`Clear()` to reset the collection before reuse. Replace
`List<PluginHotkey>()` with shorthand `[]` syntax for creating
empty collections, improving code readability and consistency.
This commit is contained in:
Jack251970 2025-10-16 22:36:51 +08:00
parent f8e588b4e1
commit 522ff153be

View file

@ -100,12 +100,14 @@ namespace Flow.Launcher.Infrastructure.UserSettings
var pluginPair = info.Key; var pluginPair = info.Key;
var hotkeyInfo = info.Value; var hotkeyInfo = info.Value;
var metadata = pluginPair.Metadata; var metadata = pluginPair.Metadata;
metadata.PluginHotkeys ??= [];
metadata.PluginHotkeys.Clear();
if (Plugins.TryGetValue(pluginPair.Metadata.ID, out var plugin)) if (Plugins.TryGetValue(pluginPair.Metadata.ID, out var plugin))
{ {
if (plugin.pluginHotkeys == null || plugin.pluginHotkeys.Count == 0) if (plugin.pluginHotkeys == null || plugin.pluginHotkeys.Count == 0)
{ {
// If plugin hotkeys does not exist, create a new one and initialize with default values // If plugin hotkeys does not exist, create a new one and initialize with default values
plugin.pluginHotkeys = new List<PluginHotkey>(); plugin.pluginHotkeys = [];
foreach (var hotkey in hotkeyInfo) foreach (var hotkey in hotkeyInfo)
{ {
plugin.pluginHotkeys.Add(new PluginHotkey plugin.pluginHotkeys.Add(new PluginHotkey