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 @@ private void SetDataContexts() // 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 @@ private async Task LoadViewModelsAsync() 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 @@ private async Task UpdateSystemTrayContextMenuAsync() { 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 @@ private void StartSystemTrayUpdateTimer() { 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 @@ private async Task UpdateSystemTrayStatusAsync() { 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 @@ 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; @@ -1365,8 +1304,13 @@ private void ResumeForegroundRefreshes() 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 @@ private void ApplyAppRefreshPolicy(AppActivityState state) if (decision.PerformanceUiMonitoringEnabled) { - _ = this.performanceViewModel.ResumeBackgroundMonitoringAsync(); + _ = this.GetPerformanceViewModel().ActivateDiagnosticsAsync(); } - else + else if (this.performanceViewModel != null) { _ = this.performanceViewModel.SuspendBackgroundMonitoringAsync(); } @@ -1541,6 +1485,11 @@ private void SelectMainTab(string tag) return; } + if (string.Equals(tag, "Performance", StringComparison.Ordinal)) + { + this.GetPerformanceViewModel(); + } + this.ApplySectionVisibility(tag); if (string.Equals(tag, "Performance", StringComparison.Ordinal)) @@ -1791,9 +1740,14 @@ private void ApplySectionVisibility(string tag) 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 @@ private async Task NavMenuItem_ClickAsync(object sender, RoutedEventArgs e) return; } + if (string.Equals(tag, "Performance", StringComparison.Ordinal)) + { + this.GetPerformanceViewModel(); + } + this.ApplySectionVisibility(tag); if (string.Equals(tag, "Performance", StringComparison.Ordinal)) @@ -1895,6 +1854,7 @@ protected override void OnClosed(EventArgs e) 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"/>