ShortcutController.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Collections.ObjectModel;
  2. using System.Linq;
  3. using System.Windows.Input;
  4. namespace PixiEditor.Models.Controllers.Shortcuts
  5. {
  6. public class ShortcutController
  7. {
  8. public ShortcutController(params ShortcutGroup[] shortcutGroups)
  9. {
  10. ShortcutGroups = new ObservableCollection<ShortcutGroup>(shortcutGroups);
  11. }
  12. public static bool BlockShortcutExecution { get; set; }
  13. public ObservableCollection<ShortcutGroup> ShortcutGroups { get; init; }
  14. public Shortcut LastShortcut { get; private set; }
  15. public const Key MoveViewportToolTransientChangeKey = Key.Space;
  16. public void KeyPressed(Key key, ModifierKeys modifiers)
  17. {
  18. if (!BlockShortcutExecution)
  19. {
  20. Shortcut[] shortcuts = ShortcutGroups.SelectMany(x => x.Shortcuts).ToList().FindAll(x => x.ShortcutKey == key).ToArray();
  21. if (shortcuts.Length < 1)
  22. {
  23. return;
  24. }
  25. shortcuts = shortcuts.OrderByDescending(x => x.Modifier).ToArray();
  26. for (int i = 0; i < shortcuts.Length; i++)
  27. {
  28. if (modifiers.HasFlag(shortcuts[i].Modifier))
  29. {
  30. shortcuts[i].Execute();
  31. LastShortcut = shortcuts[i];
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }