HotkeyManager.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using CommunityToolkit.Mvvm.ComponentModel;
 6  using Microsoft.CmdPal.UI.ViewModels.Settings;
 7  
 8  namespace Microsoft.CmdPal.UI.ViewModels;
 9  
10  public partial class HotkeyManager : ObservableObject
11  {
12      private readonly TopLevelCommandManager _topLevelCommandManager;
13      private readonly List<TopLevelHotkey> _commandHotkeys;
14  
15      public HotkeyManager(TopLevelCommandManager tlcManager, SettingsModel settings)
16      {
17          _topLevelCommandManager = tlcManager;
18          _commandHotkeys = settings.CommandHotkeys;
19      }
20  
21      public void UpdateHotkey(string commandId, HotkeySettings? hotkey)
22      {
23          // If any of the commands were already bound to this hotkey, remove that
24          foreach (var item in _commandHotkeys)
25          {
26              if (item.Hotkey == hotkey)
27              {
28                  item.Hotkey = null;
29              }
30          }
31  
32          _commandHotkeys.RemoveAll(item => item.Hotkey is null);
33  
34          foreach (var item in _commandHotkeys)
35          {
36              if (item.CommandId == commandId)
37              {
38                  _commandHotkeys.Remove(item);
39                  break;
40              }
41          }
42  
43          _commandHotkeys.Add(new(hotkey, commandId));
44      }
45  }