| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Collections.ObjectModel; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Windows.Input; |
| | | 10 | | using SwitchBlade.Core; |
| | | 11 | | using SwitchBlade.Contracts; |
| | | 12 | | using SwitchBlade.Services; |
| | | 13 | | |
| | | 14 | | namespace SwitchBlade.ViewModels |
| | | 15 | | { |
| | | 16 | | public class MainViewModel : INotifyPropertyChanged, IWindowListViewModel |
| | | 17 | | { |
| | | 18 | | private readonly IWindowOrchestrationService _orchestrationService; |
| | | 19 | | private readonly IWindowSearchService _searchService; |
| | | 20 | | private readonly INavigationService _navigationService; |
| | | 21 | | private readonly ISettingsService? _settingsService; |
| | | 22 | | private readonly IDispatcherService _dispatcherService; |
| | 71 | 23 | | private ObservableCollection<WindowItem> _filteredWindows = []; |
| | | 24 | | private WindowItem? _selectedWindow; |
| | 71 | 25 | | private string _searchText = ""; |
| | 71 | 26 | | private bool _enablePreviews = true; |
| | 71 | 27 | | private bool _isUpdating = false; |
| | 71 | 28 | | private HashSet<string> _disabledPlugins = []; |
| | 71 | 29 | | private readonly System.Threading.Lock _settingsLock = new(); |
| | 71 | 30 | | private readonly System.Threading.Lock _updateLock = new(); |
| | | 31 | | |
| | | 32 | | /// <summary>Event fired when filtered results are updated.</summary> |
| | | 33 | | public event EventHandler? ResultsUpdated; |
| | | 34 | | |
| | | 35 | | /// <summary>Event fired when search text changes (user typing).</summary> |
| | | 36 | | public event EventHandler? SearchTextChanged; |
| | | 37 | | |
| | | 38 | | /// <summary>Event fired when settings opening is requested.</summary> |
| | | 39 | | public event EventHandler? OpenSettingsRequested; |
| | | 40 | | |
| | | 41 | | /// <summary>Command to open the Settings window.</summary> |
| | 3 | 42 | | public ICommand OpenSettingsCommand { get; } |
| | | 43 | | |
| | | 44 | | /// <summary>Gets the window providers from the orchestration service.</summary> |
| | | 45 | | public IReadOnlyList<IWindowProvider> WindowProviders => |
| | 3 | 46 | | [.. _orchestrationService.AllWindows |
| | 5 | 47 | | .Where(w => w.Source != null) |
| | 5 | 48 | | .Select(w => w.Source!) |
| | 3 | 49 | | .Distinct()]; |
| | | 50 | | |
| | | 51 | | // Primary constructor with all dependencies |
| | 71 | 52 | | public MainViewModel( |
| | 71 | 53 | | IWindowOrchestrationService orchestrationService, |
| | 71 | 54 | | IWindowSearchService searchService, |
| | 71 | 55 | | INavigationService navigationService, |
| | 71 | 56 | | ISettingsService? settingsService = null, |
| | 71 | 57 | | IDispatcherService? dispatcherService = null) |
| | 71 | 58 | | { |
| | 71 | 59 | | _orchestrationService = orchestrationService ?? throw new ArgumentNullException(nameof(orchestrationService) |
| | 70 | 60 | | _searchService = searchService ?? throw new ArgumentNullException(nameof(searchService)); |
| | 69 | 61 | | _navigationService = navigationService ?? throw new ArgumentNullException(nameof(navigationService)); |
| | 68 | 62 | | _settingsService = settingsService; |
| | 68 | 63 | | _dispatcherService = dispatcherService ?? new WpfDispatcherService(); |
| | | 64 | | |
| | | 65 | | // Subscribe to orchestration updates |
| | 68 | 66 | | _orchestrationService.WindowListUpdated += OnWindowListUpdated; |
| | | 67 | | |
| | 68 | 68 | | if (_settingsService != null) |
| | 23 | 69 | | { |
| | | 70 | | lock (_settingsLock) |
| | 23 | 71 | | { |
| | 23 | 72 | | _disabledPlugins = [.._settingsService.Settings.DisabledPlugins]; |
| | 23 | 73 | | } |
| | 23 | 74 | | EnablePreviews = _settingsService.Settings.EnablePreviews; |
| | | 75 | | |
| | 23 | 76 | | _settingsService.SettingsChanged += () => |
| | 1 | 77 | | { |
| | 23 | 78 | | lock (_settingsLock) |
| | 1 | 79 | | { |
| | 1 | 80 | | _disabledPlugins = [.. _settingsService.Settings.DisabledPlugins]; |
| | 1 | 81 | | } |
| | 1 | 82 | | EnablePreviews = _settingsService.Settings.EnablePreviews; |
| | 1 | 83 | | OnPropertyChanged(nameof(ShowInTaskbar)); |
| | 1 | 84 | | OnPropertyChanged(nameof(ShowIcons)); |
| | 1 | 85 | | OnPropertyChanged(nameof(EnableNumberShortcuts)); |
| | 1 | 86 | | OnPropertyChanged(nameof(ShortcutModifierText)); |
| | 1 | 87 | | OnPropertyChanged(nameof(ItemHeight)); |
| | 1 | 88 | | OnPropertyChanged(nameof(EnableSearchHighlighting)); |
| | 1 | 89 | | OnPropertyChanged(nameof(EnableFuzzySearch)); |
| | 24 | 90 | | }; |
| | 23 | 91 | | } |
| | | 92 | | |
| | 70 | 93 | | OpenSettingsCommand = new RelayCommand(_ => OpenSettingsRequested?.Invoke(this, EventArgs.Empty)); |
| | 68 | 94 | | } |
| | | 95 | | |
| | | 96 | | private void OnWindowListUpdated(object? sender, WindowListUpdatedEventArgs e) |
| | 19 | 97 | | { |
| | 38 | 98 | | _dispatcherService.Invoke(() => UpdateSearch()); |
| | 18 | 99 | | } |
| | | 100 | | |
| | 2 | 101 | | public double ItemHeight => _settingsService?.Settings.ItemHeight ?? 64.0; |
| | | 102 | | |
| | | 103 | | public bool EnablePreviews |
| | | 104 | | { |
| | 3 | 105 | | get => _enablePreviews; |
| | 104 | 106 | | set { _enablePreviews = value; OnPropertyChanged(); } |
| | | 107 | | } |
| | | 108 | | |
| | 2 | 109 | | public bool EnableNumberShortcuts => _settingsService?.Settings.EnableNumberShortcuts ?? true; |
| | | 110 | | |
| | 2 | 111 | | public bool EnableSearchHighlighting => _settingsService?.Settings.EnableSearchHighlighting ?? true; |
| | 2 | 112 | | public string SearchHighlightColor => _settingsService?.Settings.SearchHighlightColor ?? "#FF0078D4"; |
| | | 113 | | |
| | 2 | 114 | | public bool EnableFuzzySearch => _settingsService?.Settings.EnableFuzzySearch ?? true; |
| | | 115 | | |
| | | 116 | | public string ShortcutModifierText |
| | | 117 | | { |
| | | 118 | | get |
| | 2 | 119 | | { |
| | 2 | 120 | | var modifier = _settingsService?.Settings.NumberShortcutModifier ?? ModifierKeyFlags.Alt; |
| | 2 | 121 | | return ModifierKeyFlags.ToString(modifier); |
| | 2 | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | 2 | 125 | | public bool ShowInTaskbar => !_settingsService?.Settings.HideTaskbarIcon ?? true; |
| | | 126 | | |
| | 2 | 127 | | public bool ShowIcons => _settingsService?.Settings.ShowIcons ?? true; |
| | | 128 | | |
| | | 129 | | public ObservableCollection<WindowItem> FilteredWindows |
| | | 130 | | { |
| | 306 | 131 | | get => _filteredWindows; |
| | | 132 | | set |
| | 10 | 133 | | { |
| | 10 | 134 | | if (value != null) |
| | 9 | 135 | | { |
| | 9 | 136 | | _filteredWindows = value; |
| | 9 | 137 | | OnPropertyChanged(); |
| | 9 | 138 | | } |
| | 10 | 139 | | } |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | public WindowItem? SelectedWindow |
| | | 143 | | { |
| | 196 | 144 | | get => _selectedWindow; |
| | | 145 | | set |
| | 53 | 146 | | { |
| | 53 | 147 | | if (_selectedWindow != value) |
| | 25 | 148 | | { |
| | 25 | 149 | | _selectedWindow = value; |
| | 25 | 150 | | if (!_isUpdating) |
| | 11 | 151 | | OnPropertyChanged(); |
| | 25 | 152 | | } |
| | 53 | 153 | | } |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public string SearchText |
| | | 157 | | { |
| | 37 | 158 | | get => _searchText; |
| | | 159 | | set |
| | 16 | 160 | | { |
| | 16 | 161 | | if (_searchText != value) |
| | 16 | 162 | | { |
| | 16 | 163 | | _searchText = value; |
| | 16 | 164 | | OnPropertyChanged(); |
| | 16 | 165 | | SearchTextChanged?.Invoke(this, EventArgs.Empty); |
| | 16 | 166 | | UpdateSearch(resetSelection: true); |
| | 16 | 167 | | } |
| | 16 | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | public async Task RefreshWindows() |
| | 20 | 172 | | { |
| | | 173 | | HashSet<string> disabled; |
| | | 174 | | lock (_settingsLock) |
| | 20 | 175 | | { |
| | 20 | 176 | | disabled = [.. _disabledPlugins]; |
| | 20 | 177 | | } |
| | 20 | 178 | | await _orchestrationService.RefreshAsync(disabled); |
| | 20 | 179 | | } |
| | | 180 | | |
| | | 181 | | private void UpdateSearch(bool resetSelection = false) |
| | 35 | 182 | | { |
| | | 183 | | lock (_updateLock) |
| | 35 | 184 | | { |
| | 35 | 185 | | _isUpdating = true; |
| | | 186 | | try |
| | 35 | 187 | | { |
| | | 188 | | // Capture current state |
| | 35 | 189 | | IntPtr? selectedHwnd = SelectedWindow?.Hwnd; |
| | 35 | 190 | | string? selectedTitle = SelectedWindow?.Title; |
| | 35 | 191 | | int selectedIndex = SelectedWindow != null ? FilteredWindows.IndexOf(SelectedWindow) : -1; |
| | 35 | 192 | | WindowItem? previousSelection = SelectedWindow; |
| | | 193 | | |
| | | 194 | | // Delegate search to service |
| | 35 | 195 | | bool useFuzzy = _settingsService?.Settings.EnableFuzzySearch ?? true; |
| | 35 | 196 | | var allWindows = _orchestrationService.AllWindows; |
| | 35 | 197 | | var sortedResults = _searchService.Search(allWindows, SearchText, useFuzzy); |
| | | 198 | | |
| | | 199 | | // Sync collection in-place |
| | 35 | 200 | | SyncCollection(FilteredWindows, sortedResults); |
| | | 201 | | |
| | | 202 | | // Update shortcut indices |
| | 174 | 203 | | for (int i = 0; i < FilteredWindows.Count; i++) |
| | 53 | 204 | | FilteredWindows[i].ShortcutIndex = (i < 10) ? i : -1; |
| | | 205 | | |
| | | 206 | | // Delegate selection resolution to navigation service |
| | 34 | 207 | | var behavior = _settingsService?.Settings.RefreshBehavior ?? RefreshBehavior.PreserveScroll; |
| | 34 | 208 | | var newSelection = _navigationService.ResolveSelection( |
| | 34 | 209 | | FilteredWindows, selectedHwnd, selectedTitle, selectedIndex, behavior, resetSelection); |
| | | 210 | | |
| | 34 | 211 | | SelectedWindow = newSelection; |
| | | 212 | | |
| | | 213 | | // Fire notification if selection changed meaningfully |
| | 34 | 214 | | if (resetSelection || (SelectedWindow != previousSelection && behavior != RefreshBehavior.PreserveSc |
| | 19 | 215 | | { |
| | 19 | 216 | | _isUpdating = false; |
| | 19 | 217 | | OnPropertyChanged(nameof(SelectedWindow)); |
| | 19 | 218 | | } |
| | 34 | 219 | | } |
| | | 220 | | finally |
| | 35 | 221 | | { |
| | 35 | 222 | | _isUpdating = false; |
| | 35 | 223 | | } |
| | 34 | 224 | | } |
| | | 225 | | |
| | | 226 | | // Fire event outside the lock to prevent deadlocks in listeners |
| | 34 | 227 | | ResultsUpdated?.Invoke(this, EventArgs.Empty); |
| | 34 | 228 | | } |
| | | 229 | | |
| | | 230 | | private static void SyncCollection(ObservableCollection<WindowItem> collection, IList<WindowItem> source) |
| | 35 | 231 | | { |
| | 35 | 232 | | ObservableCollectionSync.Sync(collection, source); |
| | 34 | 233 | | } |
| | | 234 | | |
| | | 235 | | public void MoveSelection(int direction) |
| | 6 | 236 | | { |
| | 8 | 237 | | if (FilteredWindows.Count == 0) return; |
| | 4 | 238 | | int currentIndex = SelectedWindow != null ? FilteredWindows.IndexOf(SelectedWindow) : -1; |
| | 4 | 239 | | int newIndex = _navigationService.CalculateMoveIndex(currentIndex, direction, FilteredWindows.Count); |
| | 4 | 240 | | if (newIndex >= 0 && newIndex < FilteredWindows.Count) |
| | 3 | 241 | | SelectedWindow = FilteredWindows[newIndex]; |
| | 6 | 242 | | } |
| | | 243 | | |
| | | 244 | | public void MoveSelectionToFirst() |
| | 2 | 245 | | { |
| | 2 | 246 | | if (FilteredWindows.Count > 0) |
| | 1 | 247 | | SelectedWindow = FilteredWindows[0]; |
| | 2 | 248 | | } |
| | | 249 | | |
| | | 250 | | public void MoveSelectionToLast() |
| | 2 | 251 | | { |
| | 2 | 252 | | if (FilteredWindows.Count > 0) |
| | 1 | 253 | | SelectedWindow = FilteredWindows[^1]; |
| | 2 | 254 | | } |
| | | 255 | | |
| | | 256 | | public void MoveSelectionByPage(int direction, int pageSize) |
| | 5 | 257 | | { |
| | 8 | 258 | | if (FilteredWindows.Count == 0 || pageSize <= 0) return; |
| | 2 | 259 | | int currentIndex = SelectedWindow != null ? FilteredWindows.IndexOf(SelectedWindow) : 0; |
| | 2 | 260 | | int newIndex = _navigationService.CalculatePageMoveIndex(currentIndex, direction, pageSize, FilteredWindows. |
| | 2 | 261 | | if (newIndex >= 0) |
| | 2 | 262 | | SelectedWindow = FilteredWindows[newIndex]; |
| | 5 | 263 | | } |
| | | 264 | | |
| | | 265 | | public event PropertyChangedEventHandler? PropertyChanged; |
| | | 266 | | protected void OnPropertyChanged([CallerMemberName] string? name = null) |
| | 88 | 267 | | { |
| | 88 | 268 | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); |
| | 88 | 269 | | } |
| | | 270 | | } |
| | | 271 | | } |