From 266834cbdfa4720513e8512f8eefb07fbcae7eff Mon Sep 17 00:00:00 2001 From: Prime Build <162145141+PrimeBuild-pc@users.noreply.github.com> Date: Thu, 21 May 2026 23:47:21 +0200 Subject: [PATCH] Reduce Performance tab to optional diagnostics (#17) Reduce Performance tab to optional diagnostics Hide the Performance/Diagnostics surface from the primary UX by default and remove hidden startup/tray runtime cost. - Hide Diagnostics from primary navigation and tray by default - Lazy-create PerformanceViewModel only when Diagnostics is opened - Stop initializing diagnostics snapshots during app startup - Gate tray CPU/RAM metrics behind the advanced diagnostics flag - Avoid starting the tray lightweight metrics timer when diagnostics are hidden - Lazily allocate PerformanceMonitoringService counters on first metrics use - Stop live diagnostics when inactive, minimized or tray-hidden - Preserve diagnostics internals for future selected-process summary work - Add tests for lazy diagnostics creation, tray metrics gating and explicit diagnostics activation No Process tab redesign, CPU affinity changes, memory priority changes, persistent rules changes, version bump or tag changes. --- MainWindow.Behaviors.partial.cs | 130 ++++++---------- MainWindow.xaml | 14 +- MainWindow.xaml.cs | 30 +++- Services/AppNavigationOptions.cs | 26 ++++ Services/AppRefreshPolicy.cs | 10 +- Services/ISystemTrayService.cs | 5 + Services/PerformanceMonitoringService.cs | 73 +++++++-- Services/ServiceConfiguration.cs | 6 + Services/SystemTrayService.cs | 32 +++- Services/SystemTrayStatusUpdater.cs | 144 ++++++++++++++++++ .../AppRefreshPolicyTests.cs | 7 +- .../DiagnosticsViewModelProviderTests.cs | 70 +++++++++ .../PerformanceViewModelDiagnosticsTests.cs | 120 +++++++++++++++ .../SystemTrayStatusUpdaterTests.cs | 72 +++++++++ ViewModels/DiagnosticsViewModelProvider.cs | 41 +++++ ViewModels/PerformanceViewModel.cs | 131 ++++++++++------ Views/PerformanceView.xaml | 6 +- 17 files changed, 752 insertions(+), 165 deletions(-) create mode 100644 Services/AppNavigationOptions.cs create mode 100644 Services/SystemTrayStatusUpdater.cs create mode 100644 Tests/ThreadPilot.Core.Tests/DiagnosticsViewModelProviderTests.cs create mode 100644 Tests/ThreadPilot.Core.Tests/PerformanceViewModelDiagnosticsTests.cs create mode 100644 Tests/ThreadPilot.Core.Tests/SystemTrayStatusUpdaterTests.cs create mode 100644 ViewModels/DiagnosticsViewModelProvider.cs diff --git a/MainWindow.Behaviors.partial.cs b/MainWindow.Behaviors.partial.cs index 753e7e0..828779d 100644 --- a/MainWindow.Behaviors.partial.cs +++ b/MainWindow.Behaviors.partial.cs @@ -49,9 +49,6 @@ namespace ThreadPilot // Set DataContext for the association view this.AssociationView.DataContext = this.associationViewModel; - // Set DataContext for the performance view - this.PerformanceViewControl.DataContext = this.performanceViewModel; - // Set DataContext for the log viewer view this.LogViewerViewControl.DataContext = this.logViewerViewModel; @@ -340,15 +337,7 @@ namespace ThreadPilot await powerPlanTask; // Ensure we get any exceptions this.LogDebug("PowerPlanViewModel loaded successfully"); - this.LogDebug("About to initialize PerformanceViewModel..."); - var performanceTask = this.performanceViewModel.InitializeAsync(); - var performanceResult = await Task.WhenAny(performanceTask, Task.Delay(5000)); - if (performanceResult != performanceTask) - { - throw new TimeoutException("PerformanceViewModel.InitializeAsync() timed out after 5 seconds"); - } - await performanceTask; // Ensure we get any exceptions - this.LogDebug("PerformanceViewModel initialized successfully"); + this.LogDebug("Skipping optional diagnostics initialization until the diagnostics page is opened."); this.LogDebug("About to load SystemTweaksViewModel..."); var systemTweaksTask = this.systemTweaksViewModel.LoadCommand.ExecuteAsync(null); @@ -1009,57 +998,7 @@ namespace ThreadPilot { try { - // Update power plans in system tray - var powerPlanService = this.serviceProvider.GetRequiredService(); - var powerPlans = await powerPlanService.GetPowerPlansAsync(); - var activePowerPlan = await powerPlanService.GetActivePowerPlan(); - this.systemTrayService.UpdatePowerPlans(powerPlans, activePowerPlan); - - // Update profiles in system tray - var profilesDirectory = StoragePaths.ProfilesDirectory; - var profileNames = new List(); - - if (Directory.Exists(profilesDirectory)) - { - profileNames = Directory.GetFiles(profilesDirectory, "*.json") - .Select(Path.GetFileNameWithoutExtension) - .Where(name => !string.IsNullOrWhiteSpace(name)) - .ToList()!; - } - - this.systemTrayService.UpdateProfiles(profileNames); - - // Update system status (with timeout to prevent hanging) - try - { - var performanceService = this.serviceProvider.GetRequiredService(); - var metricsTask = performanceService.GetSystemMetricsAsync(lightweight: true); - var metricsResult = await Task.WhenAny(metricsTask, Task.Delay(2000)); // 2 second timeout - - if (metricsResult == metricsTask) - { - var currentMetrics = await metricsTask; - this.systemTrayService.UpdateSystemStatus( - activePowerPlan?.Name ?? "Unknown", - currentMetrics?.TotalCpuUsage ?? 0.0, - currentMetrics?.MemoryUsagePercentage ?? 0.0); - } - else - { - // Timeout - use default values - this.systemTrayService.UpdateSystemStatus( - activePowerPlan?.Name ?? "Unknown", - 0.0, 0.0); - } - } - catch (Exception metricsEx) - { - System.Diagnostics.Debug.WriteLine($"Failed to get performance metrics for tray: {metricsEx.Message}"); - // Use default values - this.systemTrayService.UpdateSystemStatus( - activePowerPlan?.Name ?? "Unknown", - 0.0, 0.0); - } + await this.systemTrayStatusUpdater.UpdateContextMenuAsync(this.systemTrayService); } catch (Exception ex) { @@ -1073,6 +1012,12 @@ namespace ThreadPilot { this.systemTrayUpdateTimer?.Stop(); this.systemTrayUpdateTimer?.Dispose(); + this.systemTrayUpdateTimer = null; + + if (!this.systemTrayStatusUpdater.ShouldRunPerformanceStatusUpdates) + { + return; + } this.systemTrayUpdateFailureStreak = 0; this.systemTrayUpdateTimer = new System.Timers.Timer(SystemTrayUpdateBaseIntervalMs); @@ -1150,21 +1095,9 @@ namespace ThreadPilot { try { - var powerPlanService = this.serviceProvider.GetRequiredService(); - var performanceService = this.serviceProvider.GetRequiredService(); - - var activePowerPlan = await powerPlanService.GetActivePowerPlan(); - var currentMetrics = await performanceService.GetSystemMetricsAsync(lightweight: true); - - await this.Dispatcher.InvokeAsync(() => - { - this.systemTrayService.UpdateSystemStatus( - activePowerPlan?.Name ?? "Unknown", - currentMetrics?.TotalCpuUsage ?? 0.0, - currentMetrics?.MemoryUsagePercentage ?? 0.0); - }); - - return true; + return await this.systemTrayStatusUpdater.UpdateStatusAsync( + this.systemTrayService, + action => this.Dispatcher.InvokeAsync(action).Task); } catch (Exception ex) { @@ -1343,6 +1276,12 @@ namespace ThreadPilot this.isSystemTrayUpdatesSuspended = false; this.systemTrayUpdateFailureStreak = 0; this.systemTrayUpdateTimer?.Stop(); + + if (!this.systemTrayStatusUpdater.ShouldRunPerformanceStatusUpdates) + { + return; + } + if (this.systemTrayUpdateTimer != null) { this.systemTrayUpdateTimer.Interval = SystemTrayUpdateBaseIntervalMs; @@ -1365,8 +1304,13 @@ namespace ThreadPilot private AppActivityState GetForegroundActivityState() { - return this.ProcessManagementTab.Visibility == Visibility.Visible - ? AppActivityState.ForegroundProcessView + if (this.ProcessManagementTab.Visibility == Visibility.Visible) + { + return AppActivityState.ForegroundProcessView; + } + + return this.PerformanceViewControl.Visibility == Visibility.Visible + ? AppActivityState.ForegroundDiagnosticsView : AppActivityState.ForegroundOtherTab; } @@ -1408,9 +1352,9 @@ namespace ThreadPilot if (decision.PerformanceUiMonitoringEnabled) { - _ = this.performanceViewModel.ResumeBackgroundMonitoringAsync(); + _ = this.GetPerformanceViewModel().ActivateDiagnosticsAsync(); } - else + else if (this.performanceViewModel != null) { _ = this.performanceViewModel.SuspendBackgroundMonitoringAsync(); } @@ -1541,6 +1485,11 @@ namespace ThreadPilot return; } + if (string.Equals(tag, "Performance", StringComparison.Ordinal)) + { + this.GetPerformanceViewModel(); + } + this.ApplySectionVisibility(tag); if (string.Equals(tag, "Performance", StringComparison.Ordinal)) @@ -1791,9 +1740,14 @@ namespace ThreadPilot return; } - this.ApplyAppRefreshPolicy(string.Equals(tag, "Process", StringComparison.Ordinal) - ? AppActivityState.ForegroundProcessView - : AppActivityState.ForegroundOtherTab); + var activityState = tag switch + { + "Process" => AppActivityState.ForegroundProcessView, + "Performance" => AppActivityState.ForegroundDiagnosticsView, + _ => AppActivityState.ForegroundOtherTab, + }; + + this.ApplyAppRefreshPolicy(activityState); } private void NavMenuItem_Click(object sender, RoutedEventArgs e) @@ -1840,6 +1794,11 @@ namespace ThreadPilot return; } + if (string.Equals(tag, "Performance", StringComparison.Ordinal)) + { + this.GetPerformanceViewModel(); + } + this.ApplySectionVisibility(tag); if (string.Equals(tag, "Performance", StringComparison.Ordinal)) @@ -1895,6 +1854,7 @@ namespace ThreadPilot this.initializationTimeoutTimer?.Stop(); this.initializationTimeoutTimer?.Dispose(); + this.performanceViewModel?.Dispose(); this.selfResourceManagementService.RestoreForegroundMode(); this.navigationBehavior.Dispose(); diff --git a/MainWindow.xaml b/MainWindow.xaml index 5f890ee..6972afe 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -138,7 +138,7 @@ - + @@ -325,14 +325,14 @@ @@ -344,21 +344,21 @@ Margin="0,0,0,6"/>