< Summary

Information
Class: SwitchBlade.Services.NavigationService
Assembly: SwitchBlade
File(s): D:\a\switchblade\switchblade\Services\NavigationService.cs
Tag: 203_23722840422
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 92
Line coverage: 100%
Branch coverage
100%
Covered branches: 34
Total branches: 34
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ResolveSelection(...)100%1010100%
ResolveByIdentity(...)100%44100%
ResolveByIndex(...)100%11100%
ResolveByScroll(...)100%88100%
CalculateMoveIndex(...)100%66100%
CalculatePageMoveIndex(...)100%66100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using SwitchBlade.Contracts;
 5
 6namespace SwitchBlade.Services
 7{
 8    /// <summary>
 9    /// Implementation of INavigationService.
 10    /// Handles selection preservation and navigation calculations.
 11    /// </summary>
 12    public class NavigationService : INavigationService
 13    {
 14        public WindowItem? ResolveSelection(
 15            IList<WindowItem> filteredWindows,
 16            IntPtr? previousHwnd,
 17            string? previousTitle,
 18            int previousIndex,
 19            RefreshBehavior behavior,
 20            bool resetSelection)
 921        {
 922            if (filteredWindows == null || filteredWindows.Count == 0)
 223                return null;
 24
 25            // Force first item if reset requested (user typing)
 726            if (resetSelection)
 127                return filteredWindows[0];
 28
 629            switch (behavior)
 30            {
 31                case RefreshBehavior.PreserveIdentity:
 232                    return ResolveByIdentity(filteredWindows, previousHwnd, previousTitle);
 33
 34                case RefreshBehavior.PreserveIndex:
 135                    return ResolveByIndex(filteredWindows, previousIndex);
 36
 37                case RefreshBehavior.PreserveScroll:
 38                default:
 339                    return ResolveByScroll(filteredWindows, previousHwnd, previousTitle, previousIndex);
 40            }
 941        }
 42
 43        private static WindowItem ResolveByIdentity(IList<WindowItem> windows, IntPtr? hwnd, string? title)
 244        {
 645            var match = windows.FirstOrDefault(w => w.Hwnd == hwnd && w.Title == title);
 246            return match ?? windows[0];
 247        }
 48
 49        private static WindowItem ResolveByIndex(IList<WindowItem> windows, int previousIndex)
 150        {
 151            int idx = Math.Clamp(previousIndex, 0, windows.Count - 1);
 152            return windows[idx];
 153        }
 54
 55        private static WindowItem ResolveByScroll(IList<WindowItem> windows, IntPtr? hwnd, string? title, int previousIn
 356        {
 57            // No previous selection -> select first
 358            if (hwnd == null || hwnd == IntPtr.Zero)
 159                return windows[0];
 60
 61            // Try to find same item
 662            var sameItem = windows.FirstOrDefault(w => w.Hwnd == hwnd && w.Title == title);
 263            if (sameItem != null)
 164                return sameItem;
 65
 66            // Fallback to index
 167            int idx = Math.Clamp(previousIndex, 0, windows.Count - 1);
 168            return windows[idx];
 369        }
 70
 71        public int CalculateMoveIndex(int currentIndex, int direction, int itemCount)
 772        {
 873            if (itemCount == 0) return -1;
 74
 75            // Nothing selected
 676            if (currentIndex < 0)
 277                return direction > 0 ? 0 : itemCount - 1;
 78
 479            int newIndex = currentIndex + direction;
 480            return Math.Clamp(newIndex, 0, itemCount - 1);
 781        }
 82
 83        public int CalculatePageMoveIndex(int currentIndex, int direction, int pageSize, int itemCount)
 684        {
 885            if (itemCount == 0 || pageSize <= 0) return -1;
 86
 487            int idx = currentIndex < 0 ? 0 : currentIndex;
 488            int newIndex = idx + (direction * pageSize);
 489            return Math.Clamp(newIndex, 0, itemCount - 1);
 690        }
 91    }
 92}