CommandCollection.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Avalonia.Input;
  5. using PixiEditor.AvaloniaUI.Models.Input;
  6. using PixiEditor.AvaloniaUI.Models.Structures;
  7. using Command = PixiEditor.AvaloniaUI.Models.Commands.Commands.Command;
  8. namespace PixiEditor.AvaloniaUI.Models.Commands;
  9. [DebuggerDisplay("Count = {Count}")]
  10. internal class CommandCollection : ICollection<Commands.Command>
  11. {
  12. private readonly Dictionary<string, Command> commandInternalNames;
  13. private readonly OneToManyDictionary<KeyCombination, Command> _commandShortcuts;
  14. public int Count => commandInternalNames.Count;
  15. public bool IsReadOnly => false;
  16. public Command this[string name] => commandInternalNames[name];
  17. public bool ContainsKey(string key) => commandInternalNames.ContainsKey(key);
  18. public List<Command> this[KeyCombination shortcut] => _commandShortcuts[shortcut];
  19. public CommandCollection()
  20. {
  21. commandInternalNames = new();
  22. _commandShortcuts = new();
  23. }
  24. public void Add(Command item)
  25. {
  26. commandInternalNames.Add(item.InternalName, item);
  27. _commandShortcuts.Add(item.Shortcut, item);
  28. }
  29. public void Clear()
  30. {
  31. commandInternalNames.Clear();
  32. _commandShortcuts.Clear();
  33. }
  34. public void ClearShortcuts() => _commandShortcuts.Clear();
  35. public bool Contains(Command item) => commandInternalNames.ContainsKey(item.InternalName);
  36. public void CopyTo(Command[] array, int arrayIndex) => commandInternalNames.Values.CopyTo(array, arrayIndex);
  37. public IEnumerator<Command> GetEnumerator() => commandInternalNames.Values.GetEnumerator();
  38. public bool Remove(Command item)
  39. {
  40. bool anyRemoved = false;
  41. anyRemoved |= commandInternalNames.Remove(item.InternalName);
  42. anyRemoved |= _commandShortcuts.Remove(item);
  43. return anyRemoved;
  44. }
  45. public void AddShortcut(Command command, KeyCombination shortcut)
  46. {
  47. _commandShortcuts.Remove(KeyCombination.None, command);
  48. _commandShortcuts.Add(shortcut, command);
  49. }
  50. public void RemoveShortcut(Command command, KeyCombination shortcut)
  51. {
  52. _commandShortcuts.Remove(shortcut, command);
  53. _commandShortcuts.Add(KeyCombination.None, command);
  54. }
  55. public void ClearShortcut(KeyCombination shortcut)
  56. {
  57. if (shortcut is { Key: Key.None, Modifiers: KeyModifiers.None })
  58. return;
  59. _commandShortcuts.AddRange(KeyCombination.None, _commandShortcuts[shortcut]);
  60. _commandShortcuts.Clear(shortcut);
  61. }
  62. public IEnumerable<KeyValuePair<KeyCombination, IEnumerable<Command>>> GetShortcuts() =>
  63. _commandShortcuts;
  64. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  65. }