| | | 1 | | using System; |
| | | 2 | | using System.Windows.Input; |
| | | 3 | | using SwitchBlade.Contracts; |
| | | 4 | | using SwitchBlade.ViewModels; |
| | | 5 | | |
| | | 6 | | namespace SwitchBlade.Services |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Implementation of INumberShortcutService for handling number-based shortcuts. |
| | | 10 | | /// Following SOLID principles by isolating shortcut logic from input handling. |
| | | 11 | | /// </summary> |
| | | 12 | | public class NumberShortcutService : INumberShortcutService |
| | | 13 | | { |
| | | 14 | | private readonly ISettingsService _settingsService; |
| | | 15 | | private readonly ILogger _logger; |
| | | 16 | | |
| | 19 | 17 | | public NumberShortcutService(ISettingsService settingsService, ILogger logger) |
| | 19 | 18 | | { |
| | 19 | 19 | | _settingsService = settingsService ?? throw new ArgumentNullException(nameof(settingsService)); |
| | 18 | 20 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | 17 | 21 | | } |
| | | 22 | | |
| | | 23 | | public bool HandleShortcut(Key key, ModifierKeys modifiers, IWindowListViewModel viewModel, Action<WindowItem?> |
| | 20 | 24 | | { |
| | 21 | 25 | | if (!_settingsService.Settings.EnableNumberShortcuts) return false; |
| | | 26 | | |
| | 19 | 27 | | var settings = _settingsService.Settings; |
| | | 28 | | |
| | | 29 | | // Check if the required modifier key is pressed |
| | 19 | 30 | | if (IsModifierKeyPressed(settings.NumberShortcutModifier, modifiers)) |
| | 17 | 31 | | { |
| | 17 | 32 | | int? index = GetNumberKeyIndex(key); |
| | 17 | 33 | | if (index.HasValue) |
| | 16 | 34 | | { |
| | 16 | 35 | | ActivateWindowByIndex(index.Value, viewModel, activateWindow); |
| | 16 | 36 | | return true; |
| | | 37 | | } |
| | 1 | 38 | | } |
| | | 39 | | |
| | 3 | 40 | | return false; |
| | 20 | 41 | | } |
| | | 42 | | |
| | | 43 | | private static bool IsModifierKeyPressed(uint modifier, ModifierKeys currentModifiers) |
| | 19 | 44 | | { |
| | 19 | 45 | | return modifier switch |
| | 19 | 46 | | { |
| | 14 | 47 | | ModifierKeyFlags.None => true, // No modifier required |
| | 2 | 48 | | ModifierKeyFlags.Alt => currentModifiers.HasFlag(ModifierKeys.Alt), |
| | 1 | 49 | | ModifierKeyFlags.Ctrl => currentModifiers.HasFlag(ModifierKeys.Control), |
| | 1 | 50 | | ModifierKeyFlags.Shift => currentModifiers.HasFlag(ModifierKeys.Shift), |
| | 1 | 51 | | _ => false |
| | 19 | 52 | | }; |
| | 19 | 53 | | } |
| | | 54 | | |
| | | 55 | | private static int? GetNumberKeyIndex(Key key) |
| | 17 | 56 | | { |
| | 17 | 57 | | return key switch |
| | 17 | 58 | | { |
| | 5 | 59 | | Key.D1 or Key.NumPad1 => 0, |
| | 2 | 60 | | Key.D2 or Key.NumPad2 => 1, |
| | 1 | 61 | | Key.D3 or Key.NumPad3 => 2, |
| | 1 | 62 | | Key.D4 or Key.NumPad4 => 3, |
| | 1 | 63 | | Key.D5 or Key.NumPad5 => 4, |
| | 1 | 64 | | Key.D6 or Key.NumPad6 => 5, |
| | 1 | 65 | | Key.D7 or Key.NumPad7 => 6, |
| | 1 | 66 | | Key.D8 or Key.NumPad8 => 7, |
| | 1 | 67 | | Key.D9 or Key.NumPad9 => 8, |
| | 2 | 68 | | Key.D0 or Key.NumPad0 => 9, |
| | 1 | 69 | | _ => null |
| | 17 | 70 | | }; |
| | 17 | 71 | | } |
| | | 72 | | |
| | | 73 | | private void ActivateWindowByIndex(int index, IWindowListViewModel viewModel, Action<WindowItem?> activateWindow |
| | 16 | 74 | | { |
| | 16 | 75 | | if (index < viewModel.FilteredWindows.Count) |
| | 15 | 76 | | { |
| | 15 | 77 | | var windowItem = viewModel.FilteredWindows[index]; |
| | 15 | 78 | | _logger.Log($"Number shortcut activated: index {index} -> '{windowItem.Title}'"); |
| | 15 | 79 | | activateWindow(windowItem); |
| | 15 | 80 | | } |
| | 16 | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |