Files
ThreadPilot/MainWindow.Behaviors.partial.cs
T
Kool 27e7b04913 Harden persistence, monitoring, and shutdown reliability (#46)
Addresses the audit findings across persistent-rule recovery, settings merge behavior, CPU identity, monitoring lifecycle, notifications, autostart, tray updates, and shutdown disposal. Includes regression coverage and the corrected disabled-monitoring view. CI DevSecOps and CodeQL pass.
2026-08-12 01:00:17 +02:00

2139 lines
86 KiB
C#

namespace ThreadPilot
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using Microsoft.Extensions.DependencyInjection;
using ThreadPilot.Helpers;
using ThreadPilot.Models;
using ThreadPilot.Services;
using ThreadPilot.ViewModels;
using ThreadPilot.Views;
public partial class MainWindow : Wpf.Ui.Controls.FluentWindow
{
private void SetDataContexts()
{
// Set DataContext for the main window
this.DataContext = this.mainWindowViewModel;
// Set DataContext for the power plans view
this.PowerPlanViewControl.DataContext = this.powerPlanViewModel;
// Set DataContext for the association view
this.AssociationView.DataContext = this.associationViewModel;
// Set DataContext for the log viewer view
this.LogViewerViewControl.DataContext = this.logViewerViewModel;
// Set DataContext for the system tweaks view
this.SystemTweaksView.DataContext = this.systemTweaksViewModel;
// Set DataContext for the settings view
this.SettingsView.DataContext = this.settingsViewModel;
}
private void InitializeLoadingOverlay()
{
try
{
var loadingOverlay = this.FindName("LoadingOverlay") as Grid;
// Ensure overlay is visible while initialization runs
if (loadingOverlay != null)
{
loadingOverlay.Visibility = this.isSilentStartupMode ? Visibility.Collapsed : Visibility.Visible;
loadingOverlay.Opacity = this.isSilentStartupMode ? 0 : 1;
}
if (!this.isSilentStartupMode)
{
this.ApplyUIContentBlur(15);
}
// Start spinner animation if available
var spinnerAnimation = this.FindResource("SpinnerAnimation") as Storyboard;
if (!this.isSilentStartupMode)
{
spinnerAnimation?.Begin();
}
// Set a timeout guard for initialization
this.initializationTimeoutTimer = new System.Timers.Timer(15000)
{
AutoReset = false,
};
this.initializationTimeoutTimer.Elapsed += this.OnInitializationTimeout;
this.initializationTimeoutTimer.Start();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to initialize loading overlay: {ex.Message}");
}
}
private async Task InitializeApplicationAsync()
{
try
{
this.LogDebug("=== Starting InitializeApplicationAsync ===");
await this.Dispatcher.InvokeAsync(() => this.UpdateLoadingStatus("Loading view models...", "Loading process, power plan and rules data."));
this.LogDebug("About to call LoadViewModelsAsync...");
await this.LoadViewModelsAsync();
this.LogDebug("LoadViewModelsAsync completed successfully");
this.CompleteInitializationTask("ViewModels");
this.LogDebug("About to initialize MainWindowViewModel...");
await this.mainWindowViewModel.InitializeAsync();
this.LogDebug("MainWindowViewModel initialized successfully");
this.CompleteInitializationTask("MainWindowViewModel");
await this.Dispatcher.InvokeAsync(() => this.UpdateLoadingStatus("Initializing services...", "Starting monitoring, tray and notification services."));
this.LogDebug("About to call InitializeServicesAsync...");
await this.InitializeServicesAsync();
this.LogDebug("InitializeServicesAsync completed successfully");
this.CompleteInitializationTask("Services");
this.QueueStartupUpdateCheck();
await this.Dispatcher.InvokeAsync(() => this.UpdateLoadingStatus("Finalizing startup...", "Applying final UI state and startup checks."));
this.LogDebug("Finalizing startup...");
await Task.Delay(500); // Brief delay to show final status
this.CompleteInitializationTask("Finalization");
// All initialization complete
this.LogDebug("All initialization complete, hiding overlay...");
await this.Dispatcher.InvokeAsync(() => this.HideLoadingOverlay());
this.LogDebug("=== InitializeApplicationAsync completed successfully ===");
}
catch (Exception ex)
{
this.LogDebug($"=== ERROR in InitializeApplicationAsync: {ex} ===");
await this.Dispatcher.InvokeAsync(() => this.ShowInitializationError(ex));
}
}
private void QueueStartupUpdateCheck()
{
if (Interlocked.Exchange(ref this.startupUpdateCheckStarted, 1) != 0)
{
return;
}
TaskSafety.FireAndForget(this.CheckForUpdatesAtStartupAsync(), ex =>
{
this.LogDebug($"Startup update check failed: {ex.Message}");
});
}
private async Task CheckForUpdatesAtStartupAsync()
{
try
{
this.LogDebug("Startup update check started");
var updateService = this.serviceProvider.GetRequiredService<IUpdateService>();
var result = await updateService.CheckForUpdatesAsync(new UpdateCheckRequest(UpdateCheckTrigger.Startup));
if (result.Status == UpdateCheckStatus.Skipped)
{
this.LogDebug($"Startup update check skipped: {result.Message}");
return;
}
if (!result.IsUpdateAvailable || result.Release == null)
{
this.LogDebug($"Startup update check complete: {result.Message}");
return;
}
var title = Application.Current.TryFindResource("Settings_UpdateConfirmTitle") as string
?? "Install ThreadPilot update";
var messageFormat = Application.Current.TryFindResource("Settings_UpdateConfirmMessageFormat") as string
?? "ThreadPilot will download, verify and install version {0}, then restart automatically. Continue?";
var confirmation = MessageBox.Show(
this,
string.Format(messageFormat, result.Release.Version),
title,
MessageBoxButton.YesNo,
MessageBoxImage.Information);
if (confirmation != MessageBoxResult.Yes)
{
this.LogDebug($"Startup update declined: {result.Release.Version}");
return;
}
var downloadingMessage = Application.Current.TryFindResource("Settings_StatusDownloadingUpdate") as string
?? "Downloading and verifying update...";
await this.notificationService.ShowNotificationAsync(title, downloadingMessage, NotificationType.Information);
var installResult = await updateService.DownloadAndInstallAsync(result.Release);
if (installResult.Status != UpdateInstallStatus.Started)
{
var failureFormat = Application.Current.TryFindResource("Settings_StatusUpdateInstallFailedFormat") as string
?? "Update install failed: {0}";
MessageBox.Show(
this,
string.Format(failureFormat, installResult.Message),
title,
MessageBoxButton.OK,
MessageBoxImage.Error);
}
this.LogDebug($"Startup update check found update: installed {result.CurrentVersion}, latest {result.Release.Version}");
}
catch (Exception ex)
{
this.LogDebug($"Startup update check ignored failure: {ex.Message}");
}
}
private static Version GetCurrentApplicationVersion()
{
var rawVersion = typeof(App).Assembly
.GetCustomAttributes(typeof(System.Reflection.AssemblyInformationalVersionAttribute), false)
.OfType<System.Reflection.AssemblyInformationalVersionAttribute>()
.FirstOrDefault()?
.InformationalVersion
?? typeof(App).Assembly.GetName().Version?.ToString()
?? "0.0.0";
var sanitized = rawVersion.Trim();
if (sanitized.StartsWith("v", StringComparison.OrdinalIgnoreCase))
{
sanitized = sanitized[1..];
}
sanitized = sanitized.Split('-', '+')[0];
return Version.TryParse(sanitized, out var version)
? version
: new Version(0, 0, 0);
}
private void UpdateLoadingStatus(string stage, string details = "")
{
if (this.mainWindowViewModel != null)
{
this.mainWindowViewModel.InitializationStage = stage;
this.mainWindowViewModel.InitializationDetails = details;
}
}
private void CompleteInitializationTask(string taskName)
{
lock (this.initializationLock)
{
this.initializationTasks.Add(taskName);
System.Diagnostics.Debug.WriteLine($"Initialization task completed: {taskName}");
}
}
private void HideLoadingOverlay()
{
try
{
System.Diagnostics.Debug.WriteLine("=== Starting HideLoadingOverlay ===");
this.isInitializationComplete = true;
this.initializationTimeoutTimer?.Stop();
this.initializationTimeoutTimer?.Dispose();
if (this.isSilentStartupMode)
{
var silentLoadingOverlay = this.FindName("LoadingOverlay") as Grid;
if (silentLoadingOverlay != null)
{
silentLoadingOverlay.Visibility = Visibility.Collapsed;
silentLoadingOverlay.Opacity = 0;
}
this.ClearUIContentBlur();
this.ApplyAppRefreshPolicy(AppActivityState.TrayHidden);
return;
}
// Stop spinner animation
var spinnerAnimation = this.FindResource("SpinnerAnimation") as Storyboard;
spinnerAnimation?.Stop();
System.Diagnostics.Debug.WriteLine("Spinner animation stopped");
// Start fade-out animation
var fadeOutAnimation = this.FindResource("FadeOutAnimation") as Storyboard;
if (fadeOutAnimation != null)
{
System.Diagnostics.Debug.WriteLine("Starting fade-out animation");
fadeOutAnimation.Completed += (s, e) =>
{
System.Diagnostics.Debug.WriteLine("Fade-out animation completed, hiding overlay");
var loadingOverlay = this.FindName("LoadingOverlay") as Grid;
if (loadingOverlay != null)
{
loadingOverlay.Visibility = Visibility.Collapsed;
System.Diagnostics.Debug.WriteLine("Loading overlay visibility set to Collapsed");
}
// Disable app content blur and restore style-driven behavior.
this.ClearUIContentBlur();
System.Diagnostics.Debug.WriteLine("=== Loading overlay hidden successfully ===");
// Show elevation warning if needed
this.TryShowElevationWarning();
this.TryShowStartupMinimizedSuggestion();
};
fadeOutAnimation.Begin();
}
else
{
System.Diagnostics.Debug.WriteLine("WARNING: FadeOutAnimation not found, hiding overlay immediately");
// Fallback: hide overlay immediately if animation fails
var loadingOverlay = this.FindName("LoadingOverlay") as Grid;
if (loadingOverlay != null)
{
loadingOverlay.Visibility = Visibility.Collapsed;
}
this.ClearUIContentBlur();
System.Diagnostics.Debug.WriteLine("=== Loading overlay hidden immediately (fallback) ===");
// Show elevation warning if needed
this.TryShowElevationWarning();
this.TryShowStartupMinimizedSuggestion();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"=== ERROR hiding loading overlay: {ex} ===");
// Emergency fallback: hide overlay without animation
try
{
var loadingOverlay = this.FindName("LoadingOverlay") as Grid;
if (loadingOverlay != null)
{
loadingOverlay.Visibility = Visibility.Collapsed;
}
this.ClearUIContentBlur();
System.Diagnostics.Debug.WriteLine("Emergency fallback: overlay hidden without animation");
// Show elevation warning if needed
this.TryShowElevationWarning();
this.TryShowStartupMinimizedSuggestion();
}
catch (Exception fallbackEx)
{
System.Diagnostics.Debug.WriteLine($"Emergency fallback also failed: {fallbackEx}");
}
}
}
private void ApplyUIContentBlur(double radius)
{
if (this.UIContent.Effect is not BlurEffect blur)
{
blur = new BlurEffect();
this.UIContent.Effect = blur;
}
blur.KernelType = KernelType.Gaussian;
blur.Radius = radius;
}
private void ClearUIContentBlur()
{
this.UIContent.Effect = null;
}
private void OnInitializationTimeout(object? sender, ElapsedEventArgs e)
{
this.Dispatcher.InvokeAsync(() =>
{
if (!this.isInitializationComplete)
{
this.ShowInitializationError(new TimeoutException("Application initialization timed out after 15 seconds"));
}
});
}
private void ShowInitializationError(Exception ex)
{
try
{
this.UpdateLoadingStatus("Initialization failed", ex.Message);
var result = System.Windows.MessageBox.Show(
$"ThreadPilot failed to initialize properly:\n\n{ex.Message}\n\nDebug log: {this.debugLogPath}\n\nWould you like to retry initialization or close the application?",
"Initialization Error",
MessageBoxButton.YesNo,
MessageBoxImage.Error);
if (result == MessageBoxResult.Yes)
{
// Retry initialization - marshal to UI thread to prevent cross-thread access exceptions
this.isInitializationComplete = false;
this.initializationTasks.Clear();
this.UpdateLoadingStatus("Retrying initialization...", "Restarting startup sequence.");
this.LogDebug("=== RETRYING INITIALIZATION ===");
_ = this.Dispatcher.InvokeAsync(async () => await this.InitializeApplicationAsync());
}
else
{
// Close application
this.LogDebug("User chose to close application");
System.Windows.Application.Current.Shutdown();
}
}
catch (Exception overlayEx)
{
this.LogDebug($"Error showing initialization error: {overlayEx.Message}");
System.Windows.Application.Current.Shutdown();
}
}
private async Task LoadViewModelsAsync()
{
try
{
this.LogDebug("=== Starting LoadViewModelsAsync ===");
this.LogDebug("About to initialize ProcessViewModel (including CPU topology)...");
try
{
// Use the full initialization method instead of just LoadProcesses
var processTask = this.processViewModel.InitializeAsync();
var processResult = await Task.WhenAny(processTask, Task.Delay(15000)); // 15 second timeout for full initialization
if (processResult != processTask)
{
this.LogDebug("ProcessViewModel.InitializeAsync() timed out, trying fallback...");
// Fallback: just load processes without full initialization
await this.processViewModel.LoadProcesses();
this.LogDebug($"ProcessViewModel fallback (LoadProcesses only) completed, process count: {this.processViewModel.Processes?.Count ?? 0}, filtered count: {this.processViewModel.FilteredProcesses?.Count ?? 0}");
}
else
{
await processTask; // Ensure we get any exceptions
this.LogDebug($"ProcessViewModel initialized successfully (including CPU topology), process count: {this.processViewModel.Processes?.Count ?? 0}, filtered count: {this.processViewModel.FilteredProcesses?.Count ?? 0}");
}
}
catch (Exception processEx)
{
this.LogDebug($"ProcessViewModel initialization failed: {processEx.Message}, trying fallback...");
// Fallback: just load processes without full initialization
await this.processViewModel.LoadProcesses();
this.LogDebug($"ProcessViewModel fallback (LoadProcesses only) completed after exception, process count: {this.processViewModel.Processes?.Count ?? 0}, filtered count: {this.processViewModel.FilteredProcesses?.Count ?? 0}");
}
this.LogDebug("Detecting persisted system tweak state...");
await this.systemTweaksViewModel.LoadAsync();
this.initializedSections.Add("Tweaks");
this.LogDebug("System tweak state detected successfully");
this.LogDebug("Skipping inactive view initialization until each page is opened.");
// Initialize keyboard shortcuts after window is loaded
this.Loaded += this.OnWindowLoaded;
this.LogDebug("Keyboard shortcuts event handler attached");
this.LogDebug("=== LoadViewModelsAsync completed successfully ===");
}
catch (Exception ex)
{
this.LogDebug($"=== ERROR in LoadViewModelsAsync: {ex} ===");
throw; // Re-throw to be handled by initialization error handler
}
}
private async Task InitializeServicesAsync()
{
this.LogDebug("=== Starting InitializeServicesAsync ===");
this.LogDebug("About to initialize settings...");
await this.InitializeSettingsAsync();
this.LogDebug("Settings initialized successfully");
this.LogDebug("About to initialize system tray...");
try
{
var systemTrayTask = this.InitializeSystemTrayAsync();
var systemTrayResult = await Task.WhenAny(systemTrayTask, Task.Delay(5000)); // 5 second timeout
if (systemTrayResult != systemTrayTask)
{
this.LogDebug("System tray initialization timed out, continuing with basic tray setup...");
// Initialize basic system tray without context menu updates (Initialize() is idempotent)
await this.InitializeBasicSystemTrayAsync();
this.LogDebug("Basic system tray initialized (without context menu)");
}
else
{
await systemTrayTask; // Ensure we get any exceptions
this.LogDebug("System tray initialized successfully");
}
}
catch (Exception systemTrayEx)
{
this.LogDebug($"System tray initialization failed: {systemTrayEx.Message}, using basic tray...");
// Fallback: basic system tray initialization
try
{
await this.InitializeBasicSystemTrayAsync();
this.LogDebug("Fallback system tray initialized");
}
catch (Exception fallbackEx)
{
this.LogDebug($"Even fallback system tray failed: {fallbackEx.Message}");
}
}
this.LogDebug("About to initialize notifications...");
this.InitializeNotifications();
this.LogDebug("Notifications initialized successfully");
this.LogDebug("About to initialize monitoring...");
await this.InitializeMonitoringAsync();
this.LogDebug("Monitoring initialized successfully");
if (this.skipProcessMonitoringDuringStartup || !this.settingsService.Settings.EnableAutomationMonitoring)
{
this.LogDebug(this.skipProcessMonitoringDuringStartup
? "Skipping process monitoring manager startup (temporary bypass enabled)"
: "Automation monitoring remains disabled by saved setting.");
this.systemTrayService.UpdateMonitoringStatus(false);
this.mainWindowViewModel.UpdateProcessMonitoringStatus(false, this.Localize("SystemTray_StatusDisabled", "Automation Disabled"));
}
else
{
this.LogDebug("About to start process monitoring manager...");
try
{
var monitoringTask = this.StartProcessMonitoringManagerAsync();
var timeoutTask = Task.Delay(8000); // 8 second timeout
var completedTask = await Task.WhenAny(monitoringTask, timeoutTask);
if (completedTask == timeoutTask)
{
this.LogDebug("Process monitoring manager startup timed out after 8 seconds, continuing without monitoring...");
}
else
{
try
{
await monitoringTask; // Ensure we get any exceptions
this.LogDebug("Process monitoring manager started successfully");
}
catch (Exception taskEx)
{
this.LogDebug($"Process monitoring manager task failed: {taskEx.Message}");
}
}
}
catch (Exception monitoringEx)
{
this.LogDebug($"Process monitoring manager startup failed: {monitoringEx.Message}, continuing without monitoring...");
}
}
this.LogDebug("=== InitializeServicesAsync completed successfully ===");
}
private async Task InitializeSettingsAsync()
{
try
{
await this.settingsService.LoadSettingsAsync();
// Apply initial settings
var settings = this.settingsService.Settings;
var useDarkTheme = settings.HasUserThemePreference
? settings.UseDarkTheme
: this.themeService.GetSystemUsesDarkTheme();
if (!settings.HasUserThemePreference && settings.UseDarkTheme != useDarkTheme)
{
settings.UseDarkTheme = useDarkTheme;
await this.settingsService.UpdateSettingsAsync(settings);
}
this.themeService.ApplyTheme(useDarkTheme);
this.mainWindowViewModel.IsDarkTheme = useDarkTheme;
DwmHelper.ApplyWindowCaptionTheme(this, useDarkTheme);
if (settings.StartMinimized)
{
this.WindowState = WindowState.Minimized;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to load settings: {ex.Message}");
}
}
private async Task InitializeSystemTrayAsync()
{
try
{
this.systemTrayService.Initialize();
this.systemTrayService.Show();
// Subscribe to tray events
this.UnsubscribeSystemTrayEvents();
this.systemTrayService.ShowMainWindowRequested += this.OnShowMainWindowRequested;
this.systemTrayService.DashboardRequested += this.OnDashboardRequested;
this.systemTrayService.ExitRequested += this.OnExitRequested;
this.systemTrayService.MonitoringToggleRequested += this.OnMonitoringToggleRequested;
this.systemTrayService.SettingsRequested += this.OnSettingsRequested;
this.systemTrayService.PowerPlanChangeRequested += this.OnPowerPlanChangeRequested;
this.systemTrayService.ProfileApplicationRequested += this.OnProfileApplicationRequested;
this.systemTrayService.PerformanceDashboardRequested += this.OnPerformanceDashboardRequested;
// Update settings and tooltip
this.systemTrayService.UpdateSettings(this.settingsService.Settings);
this.systemTrayService.ApplyTheme(this.themeService.IsDarkTheme);
this.systemTrayService.UpdateTooltip("ThreadPilot - Process & Power Plan Manager");
// Initialize system tray context menu with current data
await this.UpdateSystemTrayContextMenuAsync();
this.trayPowerPlanService ??= this.serviceProvider.GetRequiredService<IPowerPlanService>();
this.trayPowerPlanService.PowerPlanChanged -= this.OnPowerPlanChangedForTray;
this.trayPowerPlanService.PowerPlanChanged += this.OnPowerPlanChangedForTray;
// Start periodic system tray updates
this.StartSystemTrayUpdateTimer();
}
catch (Exception ex)
{
// Log error but don't fail startup
System.Diagnostics.Debug.WriteLine($"Failed to initialize system tray: {ex.Message}");
}
}
private async Task InitializeBasicSystemTrayAsync()
{
try
{
this.LogDebug("Initializing basic system tray (without full context menu)...");
// Initialize basic tray icon (this is idempotent)
this.systemTrayService.Initialize();
this.systemTrayService.Show();
// Subscribe to essential tray events only
this.UnsubscribeSystemTrayEvents();
this.systemTrayService.ShowMainWindowRequested += this.OnShowMainWindowRequested;
this.systemTrayService.DashboardRequested += this.OnDashboardRequested;
this.systemTrayService.ExitRequested += this.OnExitRequested;
// Update basic settings and tooltip
this.systemTrayService.UpdateSettings(this.settingsService.Settings);
this.systemTrayService.ApplyTheme(this.themeService.IsDarkTheme);
this.systemTrayService.UpdateTooltip("ThreadPilot - Process & Power Plan Manager (Basic Mode)");
this.LogDebug("Basic system tray initialization completed");
}
catch (Exception ex)
{
this.LogDebug($"Failed to initialize basic system tray: {ex.Message}");
throw;
}
}
private void OnShowMainWindowRequested(object? sender, EventArgs e)
{
this.ShowWindowFromTray();
}
private void OnExitRequested(object? sender, EventArgs e)
{
TaskSafety.FireAndForget(this.OnExitRequestedAsync(), ex =>
{
this.LogDebug($"OnExitRequested failed: {ex.Message}");
});
}
private async Task OnExitRequestedAsync()
{
await this.PerformGracefulShutdownAsync();
}
private void OnDashboardRequested(object? sender, EventArgs e)
{
this.ShowWindowFromTray("Process");
}
private async Task PerformGracefulShutdownAsync(bool validateUnsavedChanges = true)
{
if (this.isPerformingShutdown)
{
return;
}
if (validateUnsavedChanges && !await this.HandleUnsavedSettingsBeforeExitAsync())
{
return;
}
this.isPerformingShutdown = true;
try
{
this.LogDebug("Starting graceful shutdown...");
this.selfResourceManagementService.RestoreForegroundMode();
// 1. Stop monitoring services
try
{
this.LogDebug("Stopping process monitoring manager...");
await this.processMonitorManagerService.StopAsync();
this.LogDebug("Process monitoring manager stopped");
}
catch (Exception ex)
{
this.LogDebug($"Error stopping process monitoring: {ex.Message}");
}
// 2. Cleanup applied CPU masks (like CPU Set Setter's ClearAllProcessMasksNoSave)
if (this.settingsService.Settings.ClearMasksOnClose)
{
try
{
this.LogDebug("Clearing all applied CPU masks...");
var processService = this.serviceProvider.GetRequiredService<IProcessService>();
await processService.ClearAllAppliedMasksAsync();
this.LogDebug("CPU masks cleared");
}
catch (Exception ex)
{
this.LogDebug($"Error clearing CPU masks: {ex.Message}");
}
// Also reset priorities
try
{
this.LogDebug("Resetting all process priorities...");
var processService = this.serviceProvider.GetRequiredService<IProcessService>();
await processService.ResetAllProcessPrioritiesAsync();
this.LogDebug("Process priorities reset");
}
catch (Exception ex)
{
this.LogDebug($"Error resetting priorities: {ex.Message}");
}
}
// 3. Restore default power plan if configured
if (this.settingsService.Settings.RestoreDefaultPowerPlanOnExit)
{
try
{
var targetDefaultPowerPlanGuid = this.settingsService.Settings.DefaultPowerPlanId;
try
{
await this.processPowerPlanAssociationService.LoadConfigurationAsync();
var (associationDefaultPowerPlanGuid, _) = await this.processPowerPlanAssociationService.GetDefaultPowerPlanAsync();
if (!string.IsNullOrWhiteSpace(associationDefaultPowerPlanGuid))
{
targetDefaultPowerPlanGuid = associationDefaultPowerPlanGuid;
}
}
catch (Exception associationEx)
{
this.LogDebug($"Could not read default power plan from association config: {associationEx.Message}");
}
if (string.IsNullOrWhiteSpace(targetDefaultPowerPlanGuid))
{
this.LogDebug("No default power plan configured for restore on exit");
}
else
{
this.LogDebug("Restoring default power plan...");
var powerPlanService = this.serviceProvider.GetRequiredService<IPowerPlanService>();
await powerPlanService.SetActivePowerPlanByGuidAsync(targetDefaultPowerPlanGuid);
this.LogDebug("Default power plan restored");
}
}
catch (Exception ex)
{
this.LogDebug($"Error restoring power plan: {ex.Message}");
}
}
// 4. Save settings
try
{
this.LogDebug("Saving settings...");
await this.settingsService.SaveSettingsAsync();
this.LogDebug("Settings saved");
}
catch (Exception ex)
{
this.LogDebug($"Error saving settings: {ex.Message}");
}
// 5. Dispose tray service
try
{
this.LogDebug("Disposing system tray...");
this.systemTrayService.Dispose();
this.LogDebug("System tray disposed");
}
catch (Exception ex)
{
this.LogDebug($"Error disposing tray: {ex.Message}");
}
this.LogDebug("Graceful shutdown completed");
}
catch (Exception ex)
{
this.LogDebug($"Error during graceful shutdown: {ex.Message}");
}
finally
{
// Ensure application exits
System.Windows.Application.Current.Shutdown();
}
}
private async Task<bool> HandleUnsavedSettingsBeforeExitAsync()
{
if (!this.settingsViewModel.HasPendingChanges)
{
return true;
}
var result = await this.ShowUnsavedSettingsDialogAsync();
if (result == MessageBoxResult.Cancel)
{
return false;
}
if (result == MessageBoxResult.Yes)
{
var saved = await this.settingsViewModel.SaveIfDirtyAsync();
return saved;
}
await this.settingsViewModel.DiscardPendingChangesAsync();
return true;
}
private async Task HandleWindowCloseAsync()
{
if (!await this.HandleUnsavedSettingsBeforeExitAsync())
{
return;
}
if (this.settingsService.Settings.CloseToTray)
{
this.WindowState = WindowState.Minimized;
return;
}
await this.PerformGracefulShutdownAsync(validateUnsavedChanges: false);
}
private void OnMonitoringToggleRequested(object? sender, MonitoringToggleEventArgs e)
{
TaskSafety.FireAndForget(this.OnMonitoringToggleRequestedAsync(e), ex =>
{
this.LogDebug($"OnMonitoringToggleRequested failed: {ex.Message}");
});
}
private async Task OnMonitoringToggleRequestedAsync(MonitoringToggleEventArgs e)
{
try
{
if (e.EnableMonitoring)
{
await this.processMonitorManagerService.SetAutomationMonitoringEnabledAsync(true);
await this.notificationService.ShowSuccessNotificationAsync(
this.Localize("Notification_ProcessMonitoringEnabled", "Process Monitoring Enabled"),
this.Localize("Notification_AutomationMonitoringEnabledMessage", "Process rule automation and power plan management have been enabled."));
}
else
{
if (new MonitoringDisableDialog { Owner = this }.ShowDialog() != true)
{
this.systemTrayService.UpdateMonitoringStatus(true, this.processMonitorService.IsWmiAvailable);
return;
}
await this.processMonitorManagerService.SetAutomationMonitoringEnabledAsync(false);
await this.notificationService.ShowNotificationAsync(
this.Localize("Notification_ProcessMonitoringDisabled", "Process Monitoring Disabled"),
this.Localize("Notification_AutomationMonitoringDisabledMessage", "Automation monitoring is disabled until you enable it again. Automatic process rules will not run."),
Models.NotificationType.Warning);
}
}
catch (Exception ex)
{
await this.notificationService.ShowErrorNotificationAsync(
this.Localize("Notification_AutomationMonitoringErrorTitle", "Automation Monitoring Error"),
this.Localize("Notification_AutomationMonitoringToggleFailed", "Failed to toggle automation monitoring."),
ex);
}
}
private void OnSettingsRequested(object? sender, EventArgs e)
{
try
{
this.ShowWindowFromTray("Settings");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to open settings: {ex.Message}");
}
}
private void OnPowerPlanChangeRequested(object? sender, PowerPlanChangeRequestedEventArgs e)
{
TaskSafety.FireAndForget(this.OnPowerPlanChangeRequestedAsync(e), ex =>
{
this.LogDebug($"OnPowerPlanChangeRequested failed: {ex.Message}");
});
}
private async Task OnPowerPlanChangeRequestedAsync(PowerPlanChangeRequestedEventArgs e)
{
try
{
var powerPlanService = this.serviceProvider.GetRequiredService<IPowerPlanService>();
var success = await powerPlanService.SetActivePowerPlanByGuidAsync(e.PowerPlanGuid);
if (success)
{
this.systemTrayService.ShowBalloonTip(
"ThreadPilot",
$"Power plan changed to {e.PowerPlanName}", 2000);
}
else
{
this.systemTrayService.ShowBalloonTip(
"ThreadPilot Error",
$"Failed to change power plan to {e.PowerPlanName}", 3000);
}
}
catch (Exception ex)
{
this.systemTrayService.ShowBalloonTip(
"ThreadPilot Error",
$"Error changing power plan: {ex.Message}", 3000);
}
}
private void OnProfileApplicationRequested(object? sender, ProfileApplicationRequestedEventArgs e)
{
TaskSafety.FireAndForget(this.OnProfileApplicationRequestedAsync(e), ex =>
{
this.LogDebug($"OnProfileApplicationRequested failed: {ex.Message}");
});
}
private async Task OnProfileApplicationRequestedAsync(ProfileApplicationRequestedEventArgs e)
{
try
{
var processService = this.serviceProvider.GetRequiredService<IProcessService>();
var selectedProcess = this.processViewModel.SelectedProcess;
if (selectedProcess != null)
{
var success = await processService.LoadProcessProfile(e.ProfileName, selectedProcess);
if (success)
{
this.systemTrayService.ShowBalloonTip(
"ThreadPilot",
$"Profile '{e.ProfileName}' applied to {selectedProcess.Name}", 2000);
}
else
{
this.systemTrayService.ShowBalloonTip(
"ThreadPilot Error",
$"Failed to apply profile '{e.ProfileName}'", 3000);
}
}
else
{
this.systemTrayService.ShowBalloonTip(
"ThreadPilot",
"No process selected for profile application", 2000);
}
}
catch (Exception ex)
{
this.systemTrayService.ShowBalloonTip(
"ThreadPilot Error",
$"Error applying profile: {ex.Message}", 3000);
}
}
private void OnPerformanceDashboardRequested(object? sender, EventArgs e)
{
try
{
this.ShowWindowFromTray("Performance");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to open performance dashboard: {ex.Message}");
}
}
private async Task InitializeKeyboardShortcutsAsync()
{
try
{
// Set window handle for global hotkey registration
var windowInteropHelper = new System.Windows.Interop.WindowInteropHelper(this);
var handle = windowInteropHelper.EnsureHandle();
if (this.keyboardShortcutService is KeyboardShortcutService service)
{
service.SetWindowHandle(handle);
}
// Subscribe to shortcut activation events
this.keyboardShortcutService.ShortcutActivated -= this.OnShortcutActivated;
this.keyboardShortcutService.ShortcutActivated += this.OnShortcutActivated;
// Load shortcuts from settings - with error handling
try
{
await this.keyboardShortcutService.LoadShortcutsFromSettingsAsync();
}
catch (Exception settingsEx)
{
System.Diagnostics.Debug.WriteLine($"Failed to load shortcuts from settings, using defaults: {settingsEx.Message}");
// Continue with default shortcuts if settings loading fails
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to initialize keyboard shortcuts: {ex.Message}");
// Don't let keyboard shortcut initialization failure prevent the app from starting
}
}
private void OnShortcutActivated(object? sender, ShortcutActivatedEventArgs e)
{
try
{
System.Windows.Application.Current.Dispatcher.InvokeAsync(async () =>
{
await this.HandleShortcutActionAsync(e.ActionName);
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error handling shortcut {e.ActionName}: {ex.Message}");
}
}
private async Task HandleShortcutActionAsync(string actionName)
{
switch (actionName)
{
case ShortcutActions.ShowMainWindow:
if (this.IsVisible && this.WindowState != WindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
this.ApplyAppRefreshPolicy(AppActivityState.TrayHidden);
}
else
{
this.ShowWindowFromTray();
}
break;
case ShortcutActions.ToggleMonitoring:
// Toggle monitoring - implementation can be added later
await this.notificationService.ShowNotificationAsync("Keyboard Shortcut", "Toggle monitoring shortcut activated");
break;
case ShortcutActions.PowerPlanHighPerformance:
// Switch to high performance power plan - implementation can be added later
await this.notificationService.ShowNotificationAsync("Keyboard Shortcut", "High Performance power plan shortcut activated");
break;
case ShortcutActions.OpenTweaks:
this.ShowWindowFromTray("Tweaks");
break;
case ShortcutActions.OpenSettings:
this.ShowWindowFromTray("Settings");
break;
case ShortcutActions.RefreshProcessList:
// Refresh process list - implementation can be added later
await this.notificationService.ShowNotificationAsync("Keyboard Shortcut", "Refresh process list shortcut activated");
break;
case ShortcutActions.ExitApplication:
this.Close();
break;
}
}
private async Task UpdateSystemTrayContextMenuAsync()
{
try
{
await this.systemTrayStatusUpdater.UpdateContextMenuAsync(
this.systemTrayService,
action => this.Dispatcher.InvokeAsync(action).Task);
}
catch (Exception ex)
{
this.LogDebug($"Failed to update system tray context menu: {ex.Message}");
}
}
private void OnPowerPlanChangedForTray(object? sender, PowerPlanChangedEventArgs e)
{
TaskSafety.FireAndForget(this.UpdateSystemTrayContextMenuAsync(), ex =>
{
this.LogDebug($"Failed to refresh tray menu after power plan change: {ex.Message}");
});
}
private void StartSystemTrayUpdateTimer()
{
try
{
this.systemTrayUpdateTimer?.Stop();
this.systemTrayUpdateTimer?.Dispose();
this.systemTrayUpdateTimer = null;
if (!this.systemTrayStatusUpdater.ShouldRunPerformanceStatusUpdates)
{
return;
}
this.systemTrayUpdateFailureStreak = 0;
this.systemTrayUpdateTimer = new System.Timers.Timer(SystemTrayUpdateBaseIntervalMs);
this.systemTrayUpdateTimer.Elapsed += async (s, e) =>
{
if (this.isSystemTrayUpdatesSuspended)
{
return;
}
if (Interlocked.Exchange(ref this.isSystemTrayUpdateInProgress, 1) == 1)
{
return;
}
try
{
var updateSucceeded = await this.UpdateSystemTrayStatusAsync();
this.ApplySystemTrayUpdateBackoff(updateSucceeded);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in system tray update timer: {ex.Message}");
this.ApplySystemTrayUpdateBackoff(updateSucceeded: false);
}
finally
{
Interlocked.Exchange(ref this.isSystemTrayUpdateInProgress, 0);
}
};
this.systemTrayUpdateTimer.AutoReset = true;
if (!this.isSystemTrayUpdatesSuspended &&
this.IsVisible &&
this.WindowState != WindowState.Minimized)
{
this.systemTrayUpdateTimer.Start();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to start system tray update timer: {ex.Message}");
}
}
private void ApplySystemTrayUpdateBackoff(bool updateSucceeded)
{
if (this.systemTrayUpdateTimer == null)
{
return;
}
if (updateSucceeded)
{
this.systemTrayUpdateFailureStreak = 0;
if (Math.Abs(this.systemTrayUpdateTimer.Interval - SystemTrayUpdateBaseIntervalMs) > 1)
{
this.systemTrayUpdateTimer.Interval = SystemTrayUpdateBaseIntervalMs;
}
return;
}
this.systemTrayUpdateFailureStreak = Math.Min(4, this.systemTrayUpdateFailureStreak + 1);
var exponentialDelay = SystemTrayUpdateBaseIntervalMs * Math.Pow(2, this.systemTrayUpdateFailureStreak);
var nextIntervalMs = Math.Min(SystemTrayUpdateMaxIntervalMs, exponentialDelay);
if (Math.Abs(this.systemTrayUpdateTimer.Interval - nextIntervalMs) > 1)
{
this.systemTrayUpdateTimer.Interval = nextIntervalMs;
}
}
private async Task<bool> UpdateSystemTrayStatusAsync()
{
try
{
return await this.systemTrayStatusUpdater.UpdateStatusAsync(
this.systemTrayService,
action => this.Dispatcher.InvokeAsync(action).Task);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to update system tray status: {ex.Message}");
return false;
}
}
private void InitializeNotifications()
{
try
{
// Subscribe to settings changes to update notification service
this.settingsService.SettingsChanged += this.OnSettingsChanged;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to initialize notifications: {ex.Message}");
}
}
private async Task InitializeMonitoringAsync()
{
try
{
// Subscribe to monitoring status changes
this.processMonitorService.MonitoringStatusChanged += this.OnMonitoringStatusChanged;
// Update tray with initial monitoring status
this.systemTrayService.UpdateMonitoringStatus(
this.processMonitorService.IsMonitoring,
this.processMonitorService.IsWmiAvailable);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to initialize monitoring: {ex.Message}");
}
}
private async Task StartProcessMonitoringManagerAsync()
{
try
{
this.LogDebug("Subscribing to process monitor manager events...");
// Subscribe to process monitor manager events
this.processMonitorManagerService.ServiceStatusChanged += this.OnProcessMonitorManagerStatusChanged;
this.LogDebug("Starting process monitoring manager service...");
// Start the process monitoring manager service with internal timeout
var startTask = this.processMonitorManagerService.StartAsync();
var timeoutTask = Task.Delay(6000); // 6 second internal timeout
var completedTask = await Task.WhenAny(startTask, timeoutTask);
if (completedTask == timeoutTask)
{
this.LogDebug("ProcessMonitorManagerService.StartAsync() timed out internally");
throw new TimeoutException("Process monitoring manager service startup timed out");
}
await startTask; // Get any exceptions
this.LogDebug("Process monitoring manager service started, showing notification...");
if (!this.isSilentStartupMode)
{
await this.notificationService.ShowSuccessNotificationAsync(
"ThreadPilot Started",
"Process monitoring and power plan management is now active");
}
this.LogDebug(this.isSilentStartupMode
? "Startup success notification skipped for silent startup"
: "Success notification shown");
}
catch (Exception ex)
{
this.LogDebug($"Failed to start process monitoring manager: {ex.Message}");
try
{
await this.notificationService.ShowErrorNotificationAsync(
"Startup Error",
"Failed to start process monitoring manager",
ex);
}
catch (Exception notificationEx)
{
this.LogDebug($"Failed to show error notification: {notificationEx.Message}");
}
throw; // Re-throw to be caught by outer handler
}
}
private void OnSettingsChanged(object? sender, ApplicationSettingsChangedEventArgs e)
{
// Update tray service with new settings
this.systemTrayService.UpdateSettings(e.NewSettings);
var useDarkTheme = e.NewSettings.HasUserThemePreference
? e.NewSettings.UseDarkTheme
: this.themeService.GetSystemUsesDarkTheme();
this.themeService.ApplyTheme(useDarkTheme);
this.mainWindowViewModel.IsDarkTheme = useDarkTheme;
this.systemTrayService.ApplyTheme(useDarkTheme);
DwmHelper.ApplyWindowCaptionTheme(this, useDarkTheme);
this.ApplySelfResourcePolicy(this.lastAppliedRefreshState ?? this.GetForegroundActivityState(), e.NewSettings);
}
private string Localize(string key, string fallback) =>
Application.Current.TryFindResource(key) as string ?? fallback;
private void OnMonitoringStatusChanged(object? sender, MonitoringStatusEventArgs e)
{
if (this.Dispatcher.CheckAccess())
{
this.systemTrayService.UpdateMonitoringStatus(e.IsMonitoring, e.IsWmiAvailable);
}
else
{
this.Dispatcher.InvokeAsync(() =>
this.systemTrayService.UpdateMonitoringStatus(e.IsMonitoring, e.IsWmiAvailable));
}
// Show notification if there's an error
if (e.Error != null && this.settingsService.Settings.EnableErrorNotifications)
{
this.notificationService.ShowErrorNotificationAsync(
"Automation Monitoring Error",
e.StatusMessage ?? "An error occurred with automation monitoring.",
e.Error);
}
}
private void OnProcessMonitorManagerStatusChanged(object? sender, ServiceStatusEventArgs e)
{
// Update main window status
this.mainWindowViewModel.UpdateProcessMonitoringStatus(e.IsRunning, e.Status);
// Show notification for critical status changes
if (!e.IsRunning && e.Error != null && this.settingsService.Settings.EnableErrorNotifications)
{
this.notificationService.ShowErrorNotificationAsync(
"Automation Monitoring Error",
e.Details ?? "Automation monitoring encountered an error.",
e.Error);
}
}
protected override void OnStateChanged(EventArgs e)
{
try
{
if (this.WindowState == WindowState.Minimized)
{
var activityState = AppActivityState.Minimized;
if (this.settingsService.Settings.MinimizeToTray)
{
this.ShowInTaskbar = false;
this.Hide();
this.systemTrayService.Show();
activityState = AppActivityState.TrayHidden;
}
this.ApplyAppRefreshPolicy(activityState);
}
else if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized)
{
this.ShowInTaskbar = true;
this.EnsureDashboardVisibleOnScreen();
this.ApplyAppRefreshPolicy(this.GetForegroundActivityState());
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error handling window state change: {ex.Message}");
}
base.OnStateChanged(e);
}
private void SuspendHiddenModeRefreshes()
{
this.isSystemTrayUpdatesSuspended = true;
this.systemTrayUpdateTimer?.Stop();
Interlocked.Exchange(ref this.isSystemTrayUpdateInProgress, 0);
this.powerPlanViewModel.PauseAutoRefresh();
}
private void ResumeForegroundRefreshes()
{
this.isSystemTrayUpdatesSuspended = false;
this.systemTrayUpdateFailureStreak = 0;
this.systemTrayUpdateTimer?.Stop();
if (!this.systemTrayStatusUpdater.ShouldRunPerformanceStatusUpdates)
{
return;
}
if (this.systemTrayUpdateTimer != null)
{
this.systemTrayUpdateTimer.Interval = SystemTrayUpdateBaseIntervalMs;
}
this.systemTrayUpdateTimer?.Start();
_ = this.Dispatcher.InvokeAsync(async () =>
{
try
{
var updateSucceeded = await this.UpdateSystemTrayStatusAsync();
this.ApplySystemTrayUpdateBackoff(updateSucceeded);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to refresh tray status after resume: {ex.Message}");
}
});
}
private AppActivityState GetForegroundActivityState()
{
if (this.ProcessManagementTab.Visibility == Visibility.Visible)
{
return AppActivityState.ForegroundProcessView;
}
if (this.PerformanceViewControl.Visibility == Visibility.Visible)
{
return AppActivityState.ForegroundDiagnosticsView;
}
return this.PowerPlanViewControl.Visibility == Visibility.Visible
? AppActivityState.ForegroundPowerPlanView
: AppActivityState.ForegroundOtherTab;
}
private void ApplyAppRefreshPolicy(AppActivityState state)
{
if (!AppRefreshPolicy.ShouldApplyTransition(this.lastAppliedRefreshState, state))
{
return;
}
this.lastAppliedRefreshState = state;
var decision = AppRefreshPolicy.Evaluate(state);
var isHiddenState = state is AppActivityState.Minimized or AppActivityState.TrayHidden;
var isProcessViewActive = state == AppActivityState.ForegroundProcessView;
if (isHiddenState)
{
this.isSystemTrayUpdatesSuspended = true;
this.systemTrayUpdateTimer?.Stop();
Interlocked.Exchange(ref this.isSystemTrayUpdateInProgress, 0);
}
else
{
this.ResumeForegroundRefreshes();
}
this.processViewModel.SetProcessViewActive(isProcessViewActive);
this.processViewModel.ApplyRefreshDecision(decision);
if (decision.PowerPlanUiRefreshEnabled)
{
this.powerPlanViewModel.ResumeAutoRefresh();
}
else
{
this.powerPlanViewModel.PauseAutoRefresh();
}
if (decision.PerformanceUiMonitoringEnabled)
{
_ = this.GetPerformanceViewModel().ActivateDiagnosticsAsync();
}
else if (this.performanceViewModel != null)
{
_ = this.performanceViewModel.SuspendBackgroundMonitoringAsync();
}
this.ApplySelfResourcePolicy(state);
}
private void ApplySelfResourcePolicy(AppActivityState state, ApplicationSettingsModel? settings = null)
{
var currentSettings = settings ?? this.settingsService.Settings;
var isHiddenState = state is AppActivityState.Minimized or AppActivityState.TrayHidden;
if (SelfResourcePolicy.ShouldApplyLowImpactMode(isHiddenState, currentSettings.EnableSelfLowImpactMode))
{
this.selfResourceManagementService.ApplyLowImpactMode(SelfResourcePolicy.ShouldLimitAffinity(
isHiddenState,
currentSettings.EnableSelfLowImpactMode,
currentSettings.EnableSelfAffinityLimit));
return;
}
this.selfResourceManagementService.RestoreForegroundMode();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
try
{
DwmHelper.ApplyWindowCaptionTheme(this, this.themeService.IsDarkTheme);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to apply window caption theme: {ex.Message}");
}
this.EnsureDashboardVisibleOnScreen();
}
[System.Diagnostics.Conditional("DEBUG")]
private void LogDebug(string message)
{
try
{
var timestampedMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [MainWindow] {message}";
System.Diagnostics.Debug.WriteLine(timestampedMessage);
File.AppendAllText(this.debugLogPath, timestampedMessage + Environment.NewLine);
}
catch
{
// Swallow logging failures to avoid impacting runtime behavior
}
}
private void OnWindowLoaded(object? sender, RoutedEventArgs e)
{
TaskSafety.FireAndForget(this.OnWindowLoadedAsync(), ex =>
{
this.LogDebug($"OnWindowLoaded failed: {ex.Message}");
});
}
private async Task OnWindowLoadedAsync()
{
this.Loaded -= this.OnWindowLoaded;
this.EnsureDashboardVisibleOnScreen();
await this.InitializeKeyboardShortcutsAsync();
}
private void OnOpenRulesRequested(object? sender, EventArgs e)
{
this.SelectMainTab("Rules");
}
private void ShowWindowFromTray(string? tabTag = null)
{
this.ShowInTaskbar = true;
this.EnsureDashboardVisibleOnScreen();
if (!this.IsVisible)
{
this.Show();
}
else
{
this.Visibility = Visibility.Visible;
}
if (this.WindowState == WindowState.Minimized)
{
this.WindowState = WindowState.Normal;
}
this.EnsureDashboardVisibleOnScreen();
this.ShowInTaskbar = true;
if (tabTag != null)
{
this.SelectMainTab(tabTag);
}
// Force foreground restoration when invoked from tray context menu.
this.Topmost = true;
this.Activate();
this.Focus();
this.Topmost = false;
this.Activate();
this.Focus();
var processViewWillBeActive = tabTag == null
? this.ProcessManagementTab.Visibility == Visibility.Visible
: string.Equals(tabTag, "Process", StringComparison.Ordinal);
this.ApplyAppRefreshPolicy(processViewWillBeActive
? AppActivityState.ForegroundProcessView
: AppActivityState.ForegroundOtherTab);
}
internal bool EnsureDashboardVisibleOnScreen()
{
return WindowPlacementHelper.TryCorrectWindowPlacement(this);
}
private void SelectMainTab(string tag)
{
if (string.IsNullOrEmpty(tag))
{
return;
}
if (string.Equals(tag, "Performance", StringComparison.Ordinal))
{
this.GetPerformanceViewModel();
}
this.ApplySectionVisibility(tag);
if (string.Equals(tag, "Performance", StringComparison.Ordinal))
{
this.TryShowPerformanceIntro();
}
}
private void TryShowPerformanceIntro()
{
if (this.isPerformanceIntroVisible || !this.isInitializationComplete)
{
return;
}
try
{
var settings = this.settingsService.Settings;
if (settings.HasSeenPerformanceIntro)
{
return;
}
this.isPerformanceIntroVisible = true;
this.PerformanceIntroOverlay.Visibility = Visibility.Visible;
}
catch (Exception ex)
{
this.LogDebug($"Failed to show Performance intro overlay: {ex.Message}");
}
}
private void TryShowStartupMinimizedSuggestion()
{
if (!this.showStartupMinimizedSuggestionOnReady
|| this.isSilentStartupMode
|| !this.isInitializationComplete
|| this.isElevationWarningVisible)
{
return;
}
try
{
if (!StartupMinimizedSuggestionPolicy.ShouldShow(
this.settingsService.Settings,
StartupWindowBehavior.Resolve(isAutostart: false, startMinimized: false)))
{
return;
}
this.StartupMinimizedSuggestionOverlay.Visibility = Visibility.Visible;
}
catch (Exception ex)
{
this.LogDebug($"Failed to show startup minimized suggestion: {ex.Message}");
}
}
private async Task PersistStartupMinimizedSuggestionSeenAsync()
{
try
{
var settings = this.settingsService.Settings;
if (settings.HasSeenStartupMinimizedSuggestion)
{
return;
}
settings.HasSeenStartupMinimizedSuggestion = true;
await this.settingsService.UpdateSettingsAsync(settings);
}
catch (Exception ex)
{
this.LogDebug($"Failed to persist startup minimized suggestion state: {ex.Message}");
}
}
private void HideStartupMinimizedSuggestion()
{
this.showStartupMinimizedSuggestionOnReady = false;
this.StartupMinimizedSuggestionOverlay.Visibility = Visibility.Collapsed;
}
private async void StartupSuggestionOpenSettings_Click(object sender, RoutedEventArgs e)
{
await this.PersistStartupMinimizedSuggestionSeenAsync();
this.HideStartupMinimizedSuggestion();
this.SelectMainTab("Settings");
}
private async void StartupSuggestionDontShowAgain_Click(object sender, RoutedEventArgs e)
{
await this.PersistStartupMinimizedSuggestionSeenAsync();
this.HideStartupMinimizedSuggestion();
}
private void HidePerformanceIntro()
{
this.isPerformanceIntroVisible = false;
this.PerformanceIntroOverlay.Visibility = Visibility.Collapsed;
}
private Task<MessageBoxResult> ShowUnsavedSettingsDialogAsync()
{
if (!this.Dispatcher.CheckAccess())
{
return this.Dispatcher.InvokeAsync(this.ShowUnsavedSettingsDialogAsync).Task.Unwrap();
}
if (this.unsavedSettingsDialogCompletionSource != null)
{
return Task.FromResult(MessageBoxResult.Cancel);
}
this.unsavedSettingsDialogCompletionSource = new TaskCompletionSource<MessageBoxResult>(
TaskCreationOptions.RunContinuationsAsynchronously);
this.UnsavedSettingsOverlay.Visibility = Visibility.Visible;
return this.unsavedSettingsDialogCompletionSource.Task;
}
private void CompleteUnsavedSettingsDialog(MessageBoxResult result)
{
var completionSource = this.unsavedSettingsDialogCompletionSource;
if (completionSource == null)
{
return;
}
this.unsavedSettingsDialogCompletionSource = null;
this.UnsavedSettingsOverlay.Visibility = Visibility.Collapsed;
completionSource.TrySetResult(result);
}
private void UnsavedSettingsSave_Click(object sender, RoutedEventArgs e)
{
this.CompleteUnsavedSettingsDialog(MessageBoxResult.Yes);
}
private void UnsavedSettingsDiscard_Click(object sender, RoutedEventArgs e)
{
this.CompleteUnsavedSettingsDialog(MessageBoxResult.No);
}
private void UnsavedSettingsCancel_Click(object sender, RoutedEventArgs e)
{
this.CompleteUnsavedSettingsDialog(MessageBoxResult.Cancel);
}
private async void PerformanceIntroContinue_Click(object sender, RoutedEventArgs e)
{
try
{
var settings = this.settingsService.Settings;
if (!settings.HasSeenPerformanceIntro)
{
settings.HasSeenPerformanceIntro = true;
await this.settingsService.UpdateSettingsAsync(settings);
}
}
catch (Exception ex)
{
this.LogDebug($"Failed to persist Performance intro state: {ex.Message}");
}
finally
{
this.HidePerformanceIntro();
}
}
// Elevation Warning Modal Management
private bool isElevationWarningVisible = false;
private double previousElevationAppContentOpacity = 1;
private double previousElevationBackdropBlurRadius = 0;
private void TryShowElevationWarning()
{
if (this.isElevationWarningVisible || !this.isInitializationComplete)
{
return;
}
try
{
var settings = this.settingsService.Settings;
// Only show if user is not admin AND hasn't dismissed the warning yet
if (this.elevationService?.IsRunningAsAdministrator() == true || settings.HasSeenElevationWarning)
{
return;
}
this.isElevationWarningVisible = true;
var elevationOverlay = this.FindName("ElevationWarningOverlay") as Grid;
if (elevationOverlay != null)
{
elevationOverlay.Visibility = Visibility.Visible;
}
// Apply blur and disable interaction
this.previousElevationAppContentOpacity = this.UIContent.Opacity;
this.UIContent.IsHitTestVisible = false;
this.UIContent.Opacity = 0.74;
var elevationBlur = this.FindName("ElevationWarningBlur") as BlurEffect;
if (elevationBlur != null)
{
this.previousElevationBackdropBlurRadius = elevationBlur.Radius;
elevationBlur.Radius = 16;
}
}
catch (Exception ex)
{
this.LogDebug($"Failed to show elevation warning overlay: {ex.Message}");
}
}
private void HideElevationWarning()
{
this.isElevationWarningVisible = false;
var elevationOverlay = this.FindName("ElevationWarningOverlay") as Grid;
if (elevationOverlay != null)
{
elevationOverlay.Visibility = Visibility.Collapsed;
}
// Restore interaction and remove blur
this.UIContent.IsHitTestVisible = true;
this.UIContent.Opacity = this.previousElevationAppContentOpacity;
var elevationBlur = this.FindName("ElevationWarningBlur") as BlurEffect;
if (elevationBlur != null)
{
elevationBlur.Radius = this.previousElevationBackdropBlurRadius;
}
this.TryShowStartupMinimizedSuggestion();
}
private void ElevationWarningDismiss_Click(object sender, RoutedEventArgs e)
{
try
{
var settings = this.settingsService.Settings;
if (!settings.HasSeenElevationWarning)
{
settings.HasSeenElevationWarning = true;
_ = this.settingsService.UpdateSettingsAsync(settings);
}
}
catch (Exception ex)
{
this.LogDebug($"Failed to persist elevation warning dismiss state: {ex.Message}");
}
finally
{
this.HideElevationWarning();
}
}
private async void ElevationWarningRequestElevation_Click(object sender, RoutedEventArgs e)
{
try
{
if (this.elevationService != null)
{
var success = await this.elevationService.RequestElevationIfNeeded();
if (success)
{
System.Diagnostics.Debug.WriteLine("Elevation requested successfully from warning dialog");
}
}
}
catch (Exception ex)
{
this.LogDebug($"Failed to request elevation from warning dialog: {ex.Message}");
}
finally
{
// Hide the warning after attempting elevation (regardless of success)
this.HideElevationWarning();
}
}
private void ApplySectionVisibility(string tag)
{
this.ProcessManagementTab.Visibility = tag == "Process" ? Visibility.Visible : Visibility.Collapsed;
this.CoreMasksTab.Visibility = tag == "Masks" ? Visibility.Visible : Visibility.Collapsed;
this.PowerPlanViewControl.Visibility = tag == "Power" ? Visibility.Visible : Visibility.Collapsed;
this.AssociationView.Visibility = tag == "Rules" ? Visibility.Visible : Visibility.Collapsed;
this.PerformanceViewControl.Visibility = tag == "Performance" ? Visibility.Visible : Visibility.Collapsed;
this.LogViewerViewControl.Visibility = tag == "Logs" ? Visibility.Visible : Visibility.Collapsed;
this.SystemTweaksView.Visibility = tag == "Tweaks" ? Visibility.Visible : Visibility.Collapsed;
this.SettingsView.Visibility = tag == "Settings" ? Visibility.Visible : Visibility.Collapsed;
this.NavProcess.IsActive = tag == "Process";
this.NavMasks.IsActive = tag == "Masks";
this.NavPower.IsActive = tag == "Power";
this.NavRules.IsActive = tag == "Rules";
this.NavPerf.IsActive = tag == "Performance";
this.NavLogs.IsActive = tag == "Logs";
this.NavTweaks.IsActive = tag == "Tweaks";
this.NavSettings.IsActive = tag == "Settings";
if (!this.IsVisible)
{
this.ApplyAppRefreshPolicy(AppActivityState.TrayHidden);
return;
}
if (this.WindowState == WindowState.Minimized)
{
this.ApplyAppRefreshPolicy(AppActivityState.Minimized);
return;
}
var activityState = tag switch
{
"Process" => AppActivityState.ForegroundProcessView,
"Performance" => AppActivityState.ForegroundDiagnosticsView,
"Power" => AppActivityState.ForegroundPowerPlanView,
_ => AppActivityState.ForegroundOtherTab,
};
this.ApplyAppRefreshPolicy(activityState);
}
private async Task InitializeSectionAsync(string tag)
{
if (!this.initializedSections.Add(tag))
{
return;
}
try
{
switch (tag)
{
case "Rules":
await this.associationViewModel.InitializeAsync();
break;
case "Tweaks":
await this.systemTweaksViewModel.LoadAsync();
break;
case "Settings":
await this.settingsViewModel.InitializeAsync();
break;
}
}
catch
{
this.initializedSections.Remove(tag);
throw;
}
}
private void NavMenuItem_Click(object sender, RoutedEventArgs e)
{
TaskSafety.FireAndForget(this.NavMenuItem_ClickAsync(sender, e), ex =>
{
this.LogDebug($"NavMenuItem_Click failed: {ex.Message}");
});
}
private async Task NavMenuItem_ClickAsync(object sender, RoutedEventArgs e)
{
if (!await this.navigationBehavior.TryEnterAsync())
{
return;
}
try
{
var invokedItem = sender as Wpf.Ui.Controls.NavigationViewItem;
if (invokedItem == null)
{
return;
}
var tag = invokedItem.Tag?.ToString();
if (string.IsNullOrEmpty(tag))
{
return;
}
if (!this.IsLoaded)
{
return;
}
var canNavigate = await NavigationBehavior.EnsureCanNavigateAsync(
tag,
this.settingsViewModel,
this.ShowUnsavedSettingsDialogAsync);
if (!canNavigate)
{
return;
}
if (string.Equals(tag, "Performance", StringComparison.Ordinal))
{
this.GetPerformanceViewModel();
}
if (!string.Equals(tag, "Logs", StringComparison.Ordinal))
{
await this.logViewerViewModel.SetActiveAsync(false);
}
await this.InitializeSectionAsync(tag);
this.ApplySectionVisibility(tag);
if (string.Equals(tag, "Logs", StringComparison.Ordinal))
{
await this.logViewerViewModel.SetActiveAsync(true);
}
if (string.Equals(tag, "Performance", StringComparison.Ordinal))
{
this.TryShowPerformanceIntro();
}
}
finally
{
this.navigationBehavior.Exit();
}
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (this.isPerformingShutdown)
{
base.OnClosing(e);
return;
}
if (this.isPerformanceIntroVisible)
{
e.Cancel = true;
System.Windows.MessageBox.Show(
"Please complete the Performance introduction before closing the application.\n\nClick 'Continue to Performance' to proceed.",
"Performance Introduction Required",
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
e.Cancel = true;
TaskSafety.FireAndForget(this.HandleWindowCloseAsync(), ex =>
{
this.LogDebug($"Window close handling failed: {ex.Message}");
this.Dispatcher.InvokeAsync(() =>
{
this.isPerformingShutdown = true;
System.Windows.Application.Current?.Shutdown();
});
});
}
protected override void OnClosed(EventArgs e)
{
try
{
this.Loaded -= this.OnWindowLoaded;
this.processViewModel.OpenRulesRequested -= this.OnOpenRulesRequested;
this.settingsService.SettingsChanged -= this.OnSettingsChanged;
this.processMonitorService.MonitoringStatusChanged -= this.OnMonitoringStatusChanged;
this.processMonitorManagerService.ServiceStatusChanged -= this.OnProcessMonitorManagerStatusChanged;
this.keyboardShortcutService.ShortcutActivated -= this.OnShortcutActivated;
if (this.trayPowerPlanService != null)
{
this.trayPowerPlanService.PowerPlanChanged -= this.OnPowerPlanChangedForTray;
this.trayPowerPlanService = null;
}
this.UnsubscribeSystemTrayEvents();
this.systemTrayUpdateTimer?.Stop();
this.systemTrayUpdateTimer?.Dispose();
this.initializationTimeoutTimer?.Stop();
this.initializationTimeoutTimer?.Dispose();
this.performanceViewModel?.Dispose();
this.settingsViewModel.Dispose();
this.powerPlanViewModel.Dispose();
this.associationViewModel.Dispose();
this.logViewerViewModel.Dispose();
this.systemTweaksViewModel.Dispose();
this.mainWindowViewModel.Dispose();
this.selfResourceManagementService.RestoreForegroundMode();
this.navigationBehavior.Dispose();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error disposing timers: {ex.Message}");
}
base.OnClosed(e);
}
private void UnsubscribeSystemTrayEvents()
{
this.systemTrayService.ShowMainWindowRequested -= this.OnShowMainWindowRequested;
this.systemTrayService.DashboardRequested -= this.OnDashboardRequested;
this.systemTrayService.ExitRequested -= this.OnExitRequested;
this.systemTrayService.MonitoringToggleRequested -= this.OnMonitoringToggleRequested;
this.systemTrayService.SettingsRequested -= this.OnSettingsRequested;
this.systemTrayService.PowerPlanChangeRequested -= this.OnPowerPlanChangeRequested;
this.systemTrayService.ProfileApplicationRequested -= this.OnProfileApplicationRequested;
this.systemTrayService.PerformanceDashboardRequested -= this.OnPerformanceDashboardRequested;
}
}
}