| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using SwitchBlade.Contracts; |
| | | 5 | | |
| | | 6 | | namespace SwitchBlade.Core |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides window search and filtering capabilities. |
| | | 10 | | /// Extracted from MainViewModel to follow Single Responsibility Principle. |
| | | 11 | | /// </summary> |
| | | 12 | | public class WindowSearchService : IWindowSearchService |
| | | 13 | | { |
| | | 14 | | private readonly IRegexCache _regexCache; |
| | | 15 | | private readonly IMatcher _matcher; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Creates a new WindowSearchService with the specified regex cache and matcher. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="regexCache">The regex cache to use for pattern compilation.</param> |
| | | 21 | | /// <param name="matcher">The matching algorithm to use for fuzzy search.</param> |
| | 16 | 22 | | public WindowSearchService(IRegexCache regexCache, IMatcher? matcher = null) |
| | 16 | 23 | | { |
| | 16 | 24 | | _regexCache = regexCache ?? throw new ArgumentNullException(nameof(regexCache)); |
| | 15 | 25 | | _matcher = matcher ?? new FuzzyMatcherAdapter(); |
| | 15 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public IList<WindowItem> Search(IEnumerable<WindowItem> windows, string query, bool useFuzzy) |
| | 22 | 30 | | { |
| | 22 | 31 | | if (windows == null) |
| | 1 | 32 | | return new List<WindowItem>(); |
| | | 33 | | |
| | 21 | 34 | | var windowList = windows.ToList(); |
| | | 35 | | |
| | 21 | 36 | | if (string.IsNullOrWhiteSpace(query)) |
| | 7 | 37 | | { |
| | | 38 | | // Empty query: return all windows sorted alphabetically |
| | 7 | 39 | | return windowList |
| | 7 | 40 | | .Distinct() |
| | 7 | 41 | | .OrderBy(w => w.ProcessName) |
| | 7 | 42 | | .ThenBy(w => w.Title) |
| | 7 | 43 | | .ThenBy(w => w.Hwnd.ToInt64()) |
| | 7 | 44 | | .ToList(); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | List<WindowItem> results; |
| | | 48 | | |
| | 14 | 49 | | if (useFuzzy) |
| | 10 | 50 | | { |
| | | 51 | | // Fuzzy search: Score all items and filter/sort by score |
| | 10 | 52 | | results = windowList |
| | 13 | 53 | | .Select(w => new { Item = w, Score = _matcher.Score(w.Title, query) }) |
| | 13 | 54 | | .Where(x => x.Score > 0) |
| | 7 | 55 | | .OrderByDescending(x => x.Score) |
| | 7 | 56 | | .ThenBy(x => x.Item.ProcessName) |
| | 7 | 57 | | .ThenBy(x => x.Item.Title) |
| | 7 | 58 | | .Select(x => x.Item) |
| | 10 | 59 | | .Distinct() |
| | 10 | 60 | | .ToList(); |
| | 10 | 61 | | } |
| | | 62 | | else |
| | 4 | 63 | | { |
| | | 64 | | // Regex/substring matching |
| | 4 | 65 | | var regex = _regexCache.GetOrCreate(query); |
| | | 66 | | |
| | 4 | 67 | | if (regex != null) |
| | 3 | 68 | | { |
| | 3 | 69 | | results = windowList |
| | 5 | 70 | | .Where(w => regex.IsMatch(w.Title)) |
| | 3 | 71 | | .Distinct() |
| | 3 | 72 | | .ToList(); |
| | 3 | 73 | | } |
| | | 74 | | else |
| | 1 | 75 | | { |
| | | 76 | | // Fallback to substring matching if regex is invalid |
| | 1 | 77 | | results = windowList |
| | 2 | 78 | | .Where(w => w.Title.Contains(query, StringComparison.OrdinalIgnoreCase)) |
| | 1 | 79 | | .Distinct() |
| | 1 | 80 | | .ToList(); |
| | 1 | 81 | | } |
| | | 82 | | |
| | | 83 | | // Apply stable sort: Process Name -> Title -> Hwnd |
| | 4 | 84 | | results = results |
| | 2 | 85 | | .OrderBy(w => w.ProcessName) |
| | 2 | 86 | | .ThenBy(w => w.Title) |
| | 2 | 87 | | .ThenBy(w => w.Hwnd.ToInt64()) |
| | 4 | 88 | | .ToList(); |
| | 4 | 89 | | } |
| | | 90 | | |
| | 14 | 91 | | return results; |
| | 22 | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |