| | | 1 | | using System; |
| | | 2 | | using System.Collections.ObjectModel; |
| | | 3 | | using System.ComponentModel; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Windows.Input; |
| | | 8 | | using SwitchBlade.Services; |
| | | 9 | | using SwitchBlade.Core; |
| | | 10 | | using SwitchBlade.Contracts; |
| | | 11 | | |
| | | 12 | | namespace SwitchBlade.ViewModels |
| | | 13 | | { |
| | | 14 | | public class SettingsViewModel : INotifyPropertyChanged |
| | | 15 | | { |
| | | 16 | | private readonly ISettingsService _settingsService; |
| | | 17 | | private readonly ThemeService _themeService; |
| | | 18 | | private readonly IUIService _uiService; |
| | | 19 | | private string _selectedTheme; |
| | | 20 | | private System.Threading.Timer? _saveTimer; |
| | | 21 | | private const int SaveDebounceMs = 300; |
| | | 22 | | |
| | 44 | 23 | | public ObservableCollection<string> AvailableThemes { get; set; } |
| | 44 | 24 | | public ObservableCollection<PluginInfo> LoadedPlugins { get; private set; } |
| | | 25 | | |
| | | 26 | | |
| | | 27 | | |
| | | 28 | | public string SelectedTheme |
| | | 29 | | { |
| | 1 | 30 | | get => _selectedTheme; |
| | | 31 | | set |
| | 2 | 32 | | { |
| | 2 | 33 | | if (_selectedTheme != value) |
| | 1 | 34 | | { |
| | 1 | 35 | | _selectedTheme = value; |
| | 1 | 36 | | OnPropertyChanged(); |
| | 1 | 37 | | _themeService.ApplyTheme(_selectedTheme); |
| | 1 | 38 | | } |
| | 2 | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public bool EnablePreviews |
| | | 43 | | { |
| | 1 | 44 | | get => _settingsService.Settings.EnablePreviews; |
| | 5 | 45 | | set { _settingsService.Settings.EnablePreviews = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public bool HideTaskbarIcon |
| | | 49 | | { |
| | 1 | 50 | | get => _settingsService.Settings.HideTaskbarIcon; |
| | 5 | 51 | | set { _settingsService.Settings.HideTaskbarIcon = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public bool ShowIcons |
| | | 55 | | { |
| | 1 | 56 | | get => _settingsService.Settings.ShowIcons; |
| | 5 | 57 | | set { _settingsService.Settings.ShowIcons = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public bool LaunchOnStartup |
| | | 61 | | { |
| | 2 | 62 | | get => _settingsService.Settings.LaunchOnStartup; |
| | | 63 | | set |
| | 1 | 64 | | { |
| | 1 | 65 | | if (_settingsService.Settings.LaunchOnStartup != value) |
| | 1 | 66 | | { |
| | 1 | 67 | | _settingsService.Settings.LaunchOnStartup = value; |
| | 1 | 68 | | OnPropertyChanged(); |
| | 1 | 69 | | _settingsService.SaveSettings(); |
| | 1 | 70 | | } |
| | 1 | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public bool RunAsAdministrator |
| | | 75 | | { |
| | 1 | 76 | | get => _settingsService.Settings.RunAsAdministrator; |
| | | 77 | | set |
| | 6 | 78 | | { |
| | 6 | 79 | | if (_settingsService.Settings.RunAsAdministrator != value) |
| | 5 | 80 | | { |
| | | 81 | | // Don't save yet - ask user first |
| | 5 | 82 | | bool needsRestart = (value && !_uiService.IsRunningAsAdmin()) || (!value && _uiService.IsRunningAsAd |
| | | 83 | | |
| | 5 | 84 | | if (needsRestart) |
| | 3 | 85 | | { |
| | 3 | 86 | | string message = value |
| | 3 | 87 | | ? "This setting requires restarting SwitchBlade with Administrator privileges. Restart now?" |
| | 3 | 88 | | : "To run without Administrator privileges, SwitchBlade needs to restart. Restart now?"; |
| | | 89 | | |
| | 3 | 90 | | var result = _uiService.ShowMessageBox( |
| | 3 | 91 | | message, |
| | 3 | 92 | | "Restart Required", |
| | 3 | 93 | | System.Windows.MessageBoxButton.YesNo, |
| | 3 | 94 | | System.Windows.MessageBoxImage.Question); |
| | | 95 | | |
| | 3 | 96 | | if (result == System.Windows.MessageBoxResult.Yes) |
| | 2 | 97 | | { |
| | | 98 | | // User confirmed - now save the setting and restart |
| | 2 | 99 | | _settingsService.Settings.RunAsAdministrator = value; |
| | 2 | 100 | | OnPropertyChanged(); |
| | 2 | 101 | | _settingsService.SaveSettings(); |
| | 2 | 102 | | _uiService.RestartApplication(); |
| | 2 | 103 | | } |
| | | 104 | | // else: User clicked No - don't change anything, checkbox reverts automatically via binding |
| | 3 | 105 | | } |
| | | 106 | | else |
| | 2 | 107 | | { |
| | | 108 | | // No restart needed (rare case: setting matches current state) |
| | 2 | 109 | | _settingsService.Settings.RunAsAdministrator = value; |
| | 2 | 110 | | OnPropertyChanged(); |
| | 2 | 111 | | _settingsService.SaveSettings(); |
| | 2 | 112 | | } |
| | 5 | 113 | | } |
| | 6 | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // RestartApplication logic moved to IUIService implementation |
| | | 118 | | |
| | | 119 | | public int FadeDurationMs |
| | | 120 | | { |
| | 1 | 121 | | get => _settingsService.Settings.FadeDurationMs; |
| | 5 | 122 | | set { _settingsService.Settings.FadeDurationMs = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public double WindowOpacity |
| | | 126 | | { |
| | 1 | 127 | | get => _settingsService.Settings.WindowOpacity; |
| | 5 | 128 | | set { _settingsService.Settings.WindowOpacity = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public double ItemHeight |
| | | 132 | | { |
| | 1 | 133 | | get => _settingsService.Settings.ItemHeight; |
| | 5 | 134 | | set { _settingsService.Settings.ItemHeight = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public bool EnableBackgroundPolling |
| | | 138 | | { |
| | 1 | 139 | | get => _settingsService.Settings.EnableBackgroundPolling; |
| | 5 | 140 | | set { _settingsService.Settings.EnableBackgroundPolling = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | public int BackgroundPollingIntervalSeconds |
| | | 144 | | { |
| | 1 | 145 | | get => _settingsService.Settings.BackgroundPollingIntervalSeconds; |
| | 5 | 146 | | set { _settingsService.Settings.BackgroundPollingIntervalSeconds = value; OnPropertyChanged(); ScheduleSave( |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public bool EnableNumberShortcuts |
| | | 150 | | { |
| | 1 | 151 | | get => _settingsService.Settings.EnableNumberShortcuts; |
| | 5 | 152 | | set { _settingsService.Settings.EnableNumberShortcuts = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | public bool EnableBadgeAnimations |
| | | 156 | | { |
| | 1 | 157 | | get => _settingsService.Settings.EnableBadgeAnimations; |
| | 5 | 158 | | set { _settingsService.Settings.EnableBadgeAnimations = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | public int RegexCacheSize |
| | | 162 | | { |
| | 1 | 163 | | get => _settingsService.Settings.RegexCacheSize; |
| | 5 | 164 | | set { _settingsService.Settings.RegexCacheSize = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public bool EnableFuzzySearch |
| | | 168 | | { |
| | 1 | 169 | | get => _settingsService.Settings.EnableFuzzySearch; |
| | 5 | 170 | | set { _settingsService.Settings.EnableFuzzySearch = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | public bool EnableSearchHighlighting |
| | | 174 | | { |
| | 1 | 175 | | get => _settingsService.Settings.EnableSearchHighlighting; |
| | 5 | 176 | | set { _settingsService.Settings.EnableSearchHighlighting = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | public string SearchHighlightColor |
| | | 180 | | { |
| | 1 | 181 | | get => _settingsService.Settings.SearchHighlightColor; |
| | | 182 | | set |
| | 3 | 183 | | { |
| | 3 | 184 | | if (_settingsService.Settings.SearchHighlightColor != value) |
| | 3 | 185 | | { |
| | 3 | 186 | | _settingsService.Settings.SearchHighlightColor = value; |
| | 3 | 187 | | OnPropertyChanged(); |
| | 3 | 188 | | ScheduleSave(); |
| | 3 | 189 | | } |
| | 3 | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | 1 | 193 | | public ICommand SetHighlightColorCommand { get; } |
| | | 194 | | |
| | | 195 | | public int IconCacheSize |
| | | 196 | | { |
| | 1 | 197 | | get => _settingsService.Settings.IconCacheSize; |
| | 5 | 198 | | set { _settingsService.Settings.IconCacheSize = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | public int UiaWorkerTimeoutSeconds |
| | | 202 | | { |
| | 1 | 203 | | get => _settingsService.Settings.UiaWorkerTimeoutSeconds; |
| | 5 | 204 | | set { _settingsService.Settings.UiaWorkerTimeoutSeconds = value; OnPropertyChanged(); ScheduleSave(); } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | public bool IsPreserveScrollSelected |
| | | 208 | | { |
| | 2 | 209 | | get => _settingsService.Settings.RefreshBehavior == RefreshBehavior.PreserveScroll; |
| | | 210 | | set |
| | 1 | 211 | | { |
| | 1 | 212 | | if (value) |
| | 1 | 213 | | { |
| | 1 | 214 | | _settingsService.Settings.RefreshBehavior = RefreshBehavior.PreserveScroll; |
| | 1 | 215 | | OnPropertyChanged(); |
| | 1 | 216 | | ScheduleSave(); |
| | 1 | 217 | | } |
| | 1 | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | public bool IsPreserveIdentitySelected |
| | | 222 | | { |
| | 3 | 223 | | get => _settingsService.Settings.RefreshBehavior == RefreshBehavior.PreserveIdentity; |
| | | 224 | | set |
| | 1 | 225 | | { |
| | 1 | 226 | | if (value) |
| | 1 | 227 | | { |
| | 1 | 228 | | _settingsService.Settings.RefreshBehavior = RefreshBehavior.PreserveIdentity; |
| | 1 | 229 | | OnPropertyChanged(); |
| | 1 | 230 | | ScheduleSave(); |
| | 1 | 231 | | } |
| | 1 | 232 | | } |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | public bool IsPreserveIndexSelected |
| | | 236 | | { |
| | 2 | 237 | | get => _settingsService.Settings.RefreshBehavior == RefreshBehavior.PreserveIndex; |
| | | 238 | | set |
| | 1 | 239 | | { |
| | 1 | 240 | | if (value) |
| | 1 | 241 | | { |
| | 1 | 242 | | _settingsService.Settings.RefreshBehavior = RefreshBehavior.PreserveIndex; |
| | 1 | 243 | | OnPropertyChanged(); |
| | 1 | 244 | | ScheduleSave(); |
| | 1 | 245 | | } |
| | 1 | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | 45 | 249 | | public ObservableCollection<string> AvailableShortcutModifiers { get; } = new ObservableCollection<string> |
| | 42 | 250 | | { |
| | 42 | 251 | | "Alt", "Ctrl", "Shift", "None" |
| | 42 | 252 | | }; |
| | | 253 | | |
| | | 254 | | public string SelectedShortcutModifier |
| | | 255 | | { |
| | 1 | 256 | | get => ModifierValueToString(_settingsService.Settings.NumberShortcutModifier); |
| | | 257 | | set |
| | 1 | 258 | | { |
| | 1 | 259 | | _settingsService.Settings.NumberShortcutModifier = StringToModifierValue(value); |
| | 1 | 260 | | OnPropertyChanged(); |
| | 1 | 261 | | ScheduleSave(); |
| | 1 | 262 | | } |
| | | 263 | | } |
| | | 264 | | |
| | 1 | 265 | | private static string ModifierValueToString(uint value) => Services.ModifierKeyFlags.ToString(value); |
| | | 266 | | |
| | 1 | 267 | | private static uint StringToModifierValue(string value) => Services.ModifierKeyFlags.FromString(value); |
| | | 268 | | |
| | | 269 | | |
| | | 270 | | |
| | 42 | 271 | | public SettingsViewModel(ISettingsService settingsService, ThemeService themeService, IPluginService pluginServi |
| | 42 | 272 | | { |
| | 42 | 273 | | _settingsService = settingsService; |
| | 42 | 274 | | _themeService = themeService; |
| | 42 | 275 | | _uiService = uiService; |
| | | 276 | | |
| | 42 | 277 | | var plugins = pluginService.GetPluginInfos().ToList(); |
| | | 278 | | |
| | | 279 | | // Initialize enabled state |
| | 130 | 280 | | foreach (var plugin in plugins) |
| | 2 | 281 | | { |
| | 2 | 282 | | if (_settingsService.Settings.DisabledPlugins.Contains(plugin.Name)) |
| | 1 | 283 | | { |
| | 1 | 284 | | plugin.IsEnabled = false; |
| | 1 | 285 | | } |
| | 2 | 286 | | } |
| | | 287 | | |
| | 42 | 288 | | LoadedPlugins = new ObservableCollection<PluginInfo>(plugins); |
| | | 289 | | |
| | 42 | 290 | | ExcludedProcesses = new ObservableCollection<string>(_settingsService.Settings.ExcludedProcesses); |
| | 336 | 291 | | AvailableThemes = new ObservableCollection<string>(_themeService.AvailableThemes.Select(t => t.Name)); |
| | | 292 | | |
| | 42 | 293 | | _selectedTheme = _settingsService.Settings.CurrentTheme; |
| | | 294 | | |
| | 45 | 295 | | TogglePluginCommand = new RelayCommand(param => TogglePlugin(param)); |
| | 47 | 296 | | AddExcludedProcessCommand = new RelayCommand(_ => AddExcludedProcess(), _ => !string.IsNullOrWhiteSpace(NewE |
| | 47 | 297 | | RemoveExcludedProcessCommand = new RelayCommand(_ => RemoveExcludedProcess(), _ => !string.IsNullOrEmpty(Sel |
| | 46 | 298 | | SetHighlightColorCommand = new RelayCommand(param => { if (param is string color) SearchHighlightColor = col |
| | 42 | 299 | | } |
| | | 300 | | |
| | | 301 | | |
| | | 302 | | |
| | | 303 | | public string HotKeyString |
| | | 304 | | { |
| | | 305 | | get |
| | 2 | 306 | | { |
| | 2 | 307 | | var mods = (uint)_settingsService.Settings.HotKeyModifiers; |
| | 2 | 308 | | var key = (uint)_settingsService.Settings.HotKeyKey; |
| | | 309 | | |
| | 2 | 310 | | var parts = new System.Collections.Generic.List<string>(); |
| | 3 | 311 | | if ((mods & 1) != 0) parts.Add("Alt"); |
| | 4 | 312 | | if ((mods & 2) != 0) parts.Add("Ctrl"); |
| | 4 | 313 | | if ((mods & 4) != 0) parts.Add("Shift"); |
| | 3 | 314 | | if ((mods & 8) != 0) parts.Add("Win"); |
| | | 315 | | |
| | 2 | 316 | | parts.Add(((System.Windows.Forms.Keys)key).ToString()); |
| | 2 | 317 | | return string.Join(" + ", parts); |
| | 2 | 318 | | } |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | public void UpdateHotKey(uint modifiers, uint key) |
| | 1 | 322 | | { |
| | 1 | 323 | | _settingsService.Settings.HotKeyModifiers = modifiers; |
| | 1 | 324 | | _settingsService.Settings.HotKeyKey = key; |
| | 1 | 325 | | _settingsService.SaveSettings(); |
| | 1 | 326 | | OnPropertyChanged(nameof(HotKeyString)); |
| | 1 | 327 | | } |
| | | 328 | | |
| | | 329 | | // Excluded Processes Logic |
| | 42 | 330 | | private string _newExcludedProcessName = ""; |
| | 42 | 331 | | private string _selectedExcludedProcess = ""; |
| | | 332 | | |
| | 55 | 333 | | public ObservableCollection<string> ExcludedProcesses { get; set; } |
| | | 334 | | |
| | | 335 | | public string NewExcludedProcessName |
| | | 336 | | { |
| | 6 | 337 | | get => _newExcludedProcessName; |
| | 28 | 338 | | set { _newExcludedProcessName = value; OnPropertyChanged(); } |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | public string SelectedExcludedProcess |
| | | 342 | | { |
| | 6 | 343 | | get => _selectedExcludedProcess; |
| | 24 | 344 | | set { _selectedExcludedProcess = value; OnPropertyChanged(); } |
| | | 345 | | } |
| | | 346 | | |
| | 7 | 347 | | public ICommand AddExcludedProcessCommand { get; } |
| | 7 | 348 | | public ICommand RemoveExcludedProcessCommand { get; } |
| | 4 | 349 | | public ICommand TogglePluginCommand { get; } |
| | | 350 | | |
| | | 351 | | private void TogglePlugin(object? param) |
| | 3 | 352 | | { |
| | 3 | 353 | | if (param is PluginInfo plugin) |
| | 3 | 354 | | { |
| | | 355 | | // IsEnabled is bound to the CheckBox, so it's already updated in the object |
| | | 356 | | // We just need to sync with Settings |
| | 3 | 357 | | if (plugin.IsEnabled) |
| | 1 | 358 | | { |
| | 1 | 359 | | _settingsService.Settings.DisabledPlugins.Remove(plugin.Name); |
| | 1 | 360 | | } |
| | | 361 | | else |
| | 2 | 362 | | { |
| | 2 | 363 | | if (!_settingsService.Settings.DisabledPlugins.Contains(plugin.Name)) |
| | 1 | 364 | | { |
| | 1 | 365 | | _settingsService.Settings.DisabledPlugins.Add(plugin.Name); |
| | 1 | 366 | | } |
| | 2 | 367 | | } |
| | 3 | 368 | | _settingsService.SaveSettings(); |
| | 3 | 369 | | } |
| | 3 | 370 | | } |
| | | 371 | | |
| | | 372 | | private void AddExcludedProcess() |
| | 3 | 373 | | { |
| | 3 | 374 | | var sanitized = SanitizationUtils.SanitizeProcessName(NewExcludedProcessName); |
| | 3 | 375 | | if (!string.IsNullOrEmpty(sanitized) && !ExcludedProcesses.Contains(sanitized)) |
| | 1 | 376 | | { |
| | 1 | 377 | | ExcludedProcesses.Add(sanitized); |
| | 1 | 378 | | _settingsService.Settings.ExcludedProcesses.Add(sanitized); |
| | 1 | 379 | | _settingsService.SaveSettings(); |
| | 1 | 380 | | NewExcludedProcessName = ""; |
| | 1 | 381 | | } |
| | 3 | 382 | | } |
| | | 383 | | |
| | | 384 | | private void RemoveExcludedProcess() |
| | 3 | 385 | | { |
| | 3 | 386 | | var processToRemove = SelectedExcludedProcess; |
| | 3 | 387 | | if (!string.IsNullOrEmpty(processToRemove) && ExcludedProcesses.Contains(processToRemove)) |
| | 1 | 388 | | { |
| | 1 | 389 | | ExcludedProcesses.Remove(processToRemove); |
| | 1 | 390 | | _settingsService.Settings.ExcludedProcesses.Remove(processToRemove); |
| | 1 | 391 | | _settingsService.SaveSettings(); |
| | 1 | 392 | | } |
| | 3 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Schedules a debounced save. Multiple rapid calls reset the timer, |
| | | 397 | | /// resulting in a single save after the debounce period. |
| | | 398 | | /// </summary> |
| | | 399 | | private void ScheduleSave() |
| | 22 | 400 | | { |
| | 22 | 401 | | _saveTimer?.Dispose(); |
| | 24 | 402 | | _saveTimer = new System.Threading.Timer(_ => _settingsService.SaveSettings(), null, SaveDebounceMs, Timeout. |
| | 22 | 403 | | } |
| | | 404 | | |
| | | 405 | | /// <summary> |
| | | 406 | | /// Flushes any pending debounced save immediately. |
| | | 407 | | /// Call this in tests or during teardown to ensure all changes are persisted. |
| | | 408 | | /// </summary> |
| | | 409 | | public void FlushPendingSave() |
| | 19 | 410 | | { |
| | 19 | 411 | | _saveTimer?.Dispose(); |
| | 19 | 412 | | _saveTimer = null; |
| | 19 | 413 | | _settingsService.SaveSettings(); |
| | 19 | 414 | | } |
| | | 415 | | |
| | | 416 | | public event PropertyChangedEventHandler? PropertyChanged; |
| | | 417 | | protected void OnPropertyChanged([CallerMemberName] string? name = null) |
| | 42 | 418 | | { |
| | 42 | 419 | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); |
| | 42 | 420 | | } |
| | | 421 | | } |
| | | 422 | | } |