< Summary

Information
Class: SwitchBlade.Handlers.KeyboardInputHandler
Assembly: SwitchBlade
File(s): D:\a\switchblade\switchblade\Handlers\KeyboardInputHandler.cs
Tag: 203_23722840422
Line coverage
100%
Covered lines: 63
Uncovered lines: 0
Coverable lines: 63
Total lines: 143
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleKeyInput(...)100%2222100%
CalculatePageSize()100%44100%

File(s)

D:\a\switchblade\switchblade\Handlers\KeyboardInputHandler.cs

#LineLine coverage
 1using System;
 2using System.Windows;
 3using System.Windows.Input;
 4using SwitchBlade.Core;
 5using SwitchBlade.Services;
 6using SwitchBlade.ViewModels;
 7using SwitchBlade.Contracts;
 8
 9namespace 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>
 1333        public KeyboardInputHandler(
 1334            IWindowListViewModel viewModel,
 1335            ISettingsService settingsService,
 1336            INumberShortcutService numberShortcutService,
 1337            Action hideWindow,
 1338            Action<WindowItem?> activateWindow,
 1339            Func<double> getListBoxHeight)
 1340        {
 1341            _viewModel = viewModel;
 1342            _settingsService = settingsService;
 1343            _numberShortcutService = numberShortcutService;
 1344            _hideWindow = hideWindow;
 1345            _activateWindow = activateWindow;
 1346            _getListBoxHeight = getListBoxHeight;
 1347        }
 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)
 1474        {
 1475            if (key == Key.Escape)
 176            {
 177                _hideWindow();
 178                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            }
 1381            else if (key == Key.Down)
 182            {
 183                _viewModel.MoveSelection(1);
 184                return true;
 85            }
 1286            else if (key == Key.Up)
 187            {
 188                _viewModel.MoveSelection(-1);
 189                return true;
 90            }
 1191            else if (key == Key.Enter)
 192            {
 193                _activateWindow(_viewModel.SelectedWindow);
 194                return true;
 95            }
 1096            else if (key == Key.Home && modifiers.HasFlag(ModifierKeys.Control))
 197            {
 198                _viewModel.MoveSelectionToFirst();
 199                return true;
 100            }
 9101            else if (key == Key.End && modifiers.HasFlag(ModifierKeys.Control))
 1102            {
 1103                _viewModel.MoveSelectionToLast();
 1104                return true;
 105            }
 8106            else if (key == Key.PageUp)
 1107            {
 1108                int pageSize = CalculatePageSize();
 1109                _viewModel.MoveSelectionByPage(-1, pageSize);
 1110                return true;
 111            }
 7112            else if (key == Key.PageDown)
 4113            {
 4114                int pageSize = CalculatePageSize();
 4115                _viewModel.MoveSelectionByPage(1, pageSize);
 4116                return true;
 117            }
 118
 119            // Delegate Number Shortcuts to the dedicated service
 3120            if (_numberShortcutService.HandleShortcut(key, modifiers, _viewModel, _activateWindow))
 1121            {
 1122                return true;
 123            }
 124
 2125            return false;
 14126        }
 127
 128        /// <summary>
 129        /// Calculates the number of items visible in the ListBox (page size).
 130        /// </summary>
 131        private int CalculatePageSize()
 5132        {
 5133            double itemHeight = _settingsService.Settings.ItemHeight;
 6134            if (itemHeight <= 0) itemHeight = 64; // Fallback default
 135
 5136            double listBoxHeight = _getListBoxHeight();
 6137            if (listBoxHeight <= 0) listBoxHeight = 400; // Fallback default
 138
 5139            int pageSize = (int)(listBoxHeight / itemHeight);
 5140            return Math.Max(1, pageSize); // At least 1
 5141        }
 142    }
 143}