< Summary

Information
Class: SwitchBlade.Services.NumberShortcutService
Assembly: SwitchBlade
File(s): D:\a\switchblade\switchblade\Services\NumberShortcutService.cs
Tag: 203_23722840422
Line coverage
100%
Covered lines: 52
Uncovered lines: 0
Coverable lines: 52
Total lines: 83
Line coverage: 100%
Branch coverage
100%
Covered branches: 40
Total branches: 40
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%44100%
HandleShortcut(...)100%66100%
IsModifierKeyPressed(...)100%66100%
GetNumberKeyIndex(...)100%2222100%
ActivateWindowByIndex(...)100%22100%

File(s)

D:\a\switchblade\switchblade\Services\NumberShortcutService.cs

#LineLine coverage
 1using System;
 2using System.Windows.Input;
 3using SwitchBlade.Contracts;
 4using SwitchBlade.ViewModels;
 5
 6namespace 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
 1917        public NumberShortcutService(ISettingsService settingsService, ILogger logger)
 1918        {
 1919            _settingsService = settingsService ?? throw new ArgumentNullException(nameof(settingsService));
 1820            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 1721        }
 22
 23        public bool HandleShortcut(Key key, ModifierKeys modifiers, IWindowListViewModel viewModel, Action<WindowItem?> 
 2024        {
 2125            if (!_settingsService.Settings.EnableNumberShortcuts) return false;
 26
 1927            var settings = _settingsService.Settings;
 28
 29            // Check if the required modifier key is pressed
 1930            if (IsModifierKeyPressed(settings.NumberShortcutModifier, modifiers))
 1731            {
 1732                int? index = GetNumberKeyIndex(key);
 1733                if (index.HasValue)
 1634                {
 1635                    ActivateWindowByIndex(index.Value, viewModel, activateWindow);
 1636                    return true;
 37                }
 138            }
 39
 340            return false;
 2041        }
 42
 43        private static bool IsModifierKeyPressed(uint modifier, ModifierKeys currentModifiers)
 1944        {
 1945            return modifier switch
 1946            {
 1447                ModifierKeyFlags.None => true, // No modifier required
 248                ModifierKeyFlags.Alt => currentModifiers.HasFlag(ModifierKeys.Alt),
 149                ModifierKeyFlags.Ctrl => currentModifiers.HasFlag(ModifierKeys.Control),
 150                ModifierKeyFlags.Shift => currentModifiers.HasFlag(ModifierKeys.Shift),
 151                _ => false
 1952            };
 1953        }
 54
 55        private static int? GetNumberKeyIndex(Key key)
 1756        {
 1757            return key switch
 1758            {
 559                Key.D1 or Key.NumPad1 => 0,
 260                Key.D2 or Key.NumPad2 => 1,
 161                Key.D3 or Key.NumPad3 => 2,
 162                Key.D4 or Key.NumPad4 => 3,
 163                Key.D5 or Key.NumPad5 => 4,
 164                Key.D6 or Key.NumPad6 => 5,
 165                Key.D7 or Key.NumPad7 => 6,
 166                Key.D8 or Key.NumPad8 => 7,
 167                Key.D9 or Key.NumPad9 => 8,
 268                Key.D0 or Key.NumPad0 => 9,
 169                _ => null
 1770            };
 1771        }
 72
 73        private void ActivateWindowByIndex(int index, IWindowListViewModel viewModel, Action<WindowItem?> activateWindow
 1674        {
 1675            if (index < viewModel.FilteredWindows.Count)
 1576            {
 1577                var windowItem = viewModel.FilteredWindows[index];
 1578                _logger.Log($"Number shortcut activated: index {index} -> '{windowItem.Title}'");
 1579                activateWindow(windowItem);
 1580            }
 1681        }
 82    }
 83}