| | | 1 | | using System; |
| | | 2 | | using System.Windows; |
| | | 3 | | using System.Windows.Input; |
| | | 4 | | using SwitchBlade.Core; |
| | | 5 | | using SwitchBlade.Services; |
| | | 6 | | using SwitchBlade.ViewModels; |
| | | 7 | | using SwitchBlade.Contracts; |
| | | 8 | | |
| | | 9 | | namespace SwitchBlade.Handlers |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Handles keyboard input for the main window. |
| | | 13 | | /// Extracted from MainWindow.xaml.cs for Single Responsibility Principle. |
| | | 14 | | /// </summary> |
| | | 15 | | public class KeyboardInputHandler |
| | | 16 | | { |
| | | 17 | | private readonly IWindowListViewModel _viewModel; |
| | | 18 | | private readonly ISettingsService _settingsService; |
| | | 19 | | private readonly INumberShortcutService _numberShortcutService; |
| | | 20 | | private readonly Action _hideWindow; |
| | | 21 | | private readonly Action<WindowItem?> _activateWindow; |
| | | 22 | | private readonly Func<double> _getListBoxHeight; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Creates a new keyboard input handler. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="viewModel">The main view model.</param> |
| | | 28 | | /// <param name="settingsService">Settings service for configuration.</param> |
| | | 29 | | /// <param name="numberShortcutService">Service for handling number shortcuts.</param> |
| | | 30 | | /// <param name="hideWindow">Action to hide the window.</param> |
| | | 31 | | /// <param name="activateWindow">Action to activate a selected window.</param> |
| | | 32 | | /// <param name="getListBoxHeight">Function to get the current list box height for page calculations.</param> |
| | 13 | 33 | | public KeyboardInputHandler( |
| | 13 | 34 | | IWindowListViewModel viewModel, |
| | 13 | 35 | | ISettingsService settingsService, |
| | 13 | 36 | | INumberShortcutService numberShortcutService, |
| | 13 | 37 | | Action hideWindow, |
| | 13 | 38 | | Action<WindowItem?> activateWindow, |
| | 13 | 39 | | Func<double> getListBoxHeight) |
| | 13 | 40 | | { |
| | 13 | 41 | | _viewModel = viewModel; |
| | 13 | 42 | | _settingsService = settingsService; |
| | 13 | 43 | | _numberShortcutService = numberShortcutService; |
| | 13 | 44 | | _hideWindow = hideWindow; |
| | 13 | 45 | | _activateWindow = activateWindow; |
| | 13 | 46 | | _getListBoxHeight = getListBoxHeight; |
| | 13 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Handles the PreviewKeyDown event for the main window. |
| | | 51 | | /// </summary> |
| | | 52 | | [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] |
| | | 53 | | public void HandleKeyDown(object sender, System.Windows.Input.KeyEventArgs e) |
| | | 54 | | { |
| | | 55 | | // Only log non-character keys to avoid spam |
| | | 56 | | if (e.Key == Key.Escape || e.Key == Key.Enter || e.Key == Key.Down || e.Key == Key.Up) |
| | | 57 | | { |
| | | 58 | | Logger.Log($"KeyboardInputHandler KeyDown: {e.Key}"); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | // Extract modifiers relative to this event if possible, but Keyboard.Modifiers is static. |
| | | 62 | | // For the purpose of this handler, we use global modifiers. |
| | | 63 | | if (HandleKeyInput(e.Key == Key.System ? e.SystemKey : e.Key, Keyboard.Modifiers)) |
| | | 64 | | { |
| | | 65 | | e.Handled = true; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Processes key input. Returns true if handled. |
| | | 71 | | /// Public for unit testing. |
| | | 72 | | /// </summary> |
| | | 73 | | public bool HandleKeyInput(Key key, ModifierKeys modifiers) |
| | 14 | 74 | | { |
| | 14 | 75 | | if (key == Key.Escape) |
| | 1 | 76 | | { |
| | 1 | 77 | | _hideWindow(); |
| | 1 | 78 | | return false; // Escape hides window but doesn't strictly "handle" input in a way that prevents bubble? |
| | | 79 | | // But looking at original code: _hideWindow(); -> implicitly handled? Original didn't set e.Handled = t |
| | | 80 | | } |
| | 13 | 81 | | else if (key == Key.Down) |
| | 1 | 82 | | { |
| | 1 | 83 | | _viewModel.MoveSelection(1); |
| | 1 | 84 | | return true; |
| | | 85 | | } |
| | 12 | 86 | | else if (key == Key.Up) |
| | 1 | 87 | | { |
| | 1 | 88 | | _viewModel.MoveSelection(-1); |
| | 1 | 89 | | return true; |
| | | 90 | | } |
| | 11 | 91 | | else if (key == Key.Enter) |
| | 1 | 92 | | { |
| | 1 | 93 | | _activateWindow(_viewModel.SelectedWindow); |
| | 1 | 94 | | return true; |
| | | 95 | | } |
| | 10 | 96 | | else if (key == Key.Home && modifiers.HasFlag(ModifierKeys.Control)) |
| | 1 | 97 | | { |
| | 1 | 98 | | _viewModel.MoveSelectionToFirst(); |
| | 1 | 99 | | return true; |
| | | 100 | | } |
| | 9 | 101 | | else if (key == Key.End && modifiers.HasFlag(ModifierKeys.Control)) |
| | 1 | 102 | | { |
| | 1 | 103 | | _viewModel.MoveSelectionToLast(); |
| | 1 | 104 | | return true; |
| | | 105 | | } |
| | 8 | 106 | | else if (key == Key.PageUp) |
| | 1 | 107 | | { |
| | 1 | 108 | | int pageSize = CalculatePageSize(); |
| | 1 | 109 | | _viewModel.MoveSelectionByPage(-1, pageSize); |
| | 1 | 110 | | return true; |
| | | 111 | | } |
| | 7 | 112 | | else if (key == Key.PageDown) |
| | 4 | 113 | | { |
| | 4 | 114 | | int pageSize = CalculatePageSize(); |
| | 4 | 115 | | _viewModel.MoveSelectionByPage(1, pageSize); |
| | 4 | 116 | | return true; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | // Delegate Number Shortcuts to the dedicated service |
| | 3 | 120 | | if (_numberShortcutService.HandleShortcut(key, modifiers, _viewModel, _activateWindow)) |
| | 1 | 121 | | { |
| | 1 | 122 | | return true; |
| | | 123 | | } |
| | | 124 | | |
| | 2 | 125 | | return false; |
| | 14 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Calculates the number of items visible in the ListBox (page size). |
| | | 130 | | /// </summary> |
| | | 131 | | private int CalculatePageSize() |
| | 5 | 132 | | { |
| | 5 | 133 | | double itemHeight = _settingsService.Settings.ItemHeight; |
| | 6 | 134 | | if (itemHeight <= 0) itemHeight = 64; // Fallback default |
| | | 135 | | |
| | 5 | 136 | | double listBoxHeight = _getListBoxHeight(); |
| | 6 | 137 | | if (listBoxHeight <= 0) listBoxHeight = 400; // Fallback default |
| | | 138 | | |
| | 5 | 139 | | int pageSize = (int)(listBoxHeight / itemHeight); |
| | 5 | 140 | | return Math.Max(1, pageSize); // At least 1 |
| | 5 | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |