Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 81 additions & 47 deletions MainWindow.Behaviors.partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -782,23 +782,23 @@ private async Task OnMonitoringToggleRequestedAsync(MonitoringToggleEventArgs e)
{
await this.processMonitorManagerService.StartAsync();
await this.notificationService.ShowSuccessNotificationAsync(
"Monitoring Enabled",
"Process monitoring and power plan management has been enabled");
"Automation Monitoring Enabled",
"Process rule automation and power plan management have been enabled.");
}
else
{
await this.processMonitorManagerService.StopAsync();
await this.notificationService.ShowNotificationAsync(
"Monitoring Disabled",
"Process monitoring and power plan management has been disabled",
"Automation Monitoring Disabled",
"Process rule automation and power plan management have been disabled.",
Models.NotificationType.Warning);
}
}
catch (Exception ex)
{
await this.notificationService.ShowErrorNotificationAsync(
"Monitoring Error",
"Failed to toggle process monitoring",
"Automation Monitoring Error",
"Failed to toggle automation monitoring.",
ex);
}
}
Expand Down Expand Up @@ -969,10 +969,7 @@ private async Task HandleShortcutActionAsync(string actionName)
{
this.ShowInTaskbar = false;
this.Hide();
this.SuspendHiddenModeRefreshes();
this.processViewModel.PauseRefresh();
this.processViewModel.SetProcessViewActive(false);
_ = this.performanceViewModel.SuspendBackgroundMonitoringAsync();
this.ApplyAppRefreshPolicy(AppActivityState.TrayHidden);
}
else
{
Expand Down Expand Up @@ -1273,8 +1270,8 @@ private void OnMonitoringStatusChanged(object? sender, MonitoringStatusEventArgs
if (e.Error != null && this.settingsService.Settings.EnableErrorNotifications)
{
this.notificationService.ShowErrorNotificationAsync(
"Monitoring Error",
e.StatusMessage ?? "An error occurred with process monitoring",
"Automation Monitoring Error",
e.StatusMessage ?? "An error occurred with automation monitoring.",
e.Error);
}
}
Expand All @@ -1288,8 +1285,8 @@ private void OnProcessMonitorManagerStatusChanged(object? sender, ServiceStatusE
if (!e.IsRunning && e.Error != null && this.settingsService.Settings.EnableErrorNotifications)
{
this.notificationService.ShowErrorNotificationAsync(
"Process Monitoring Error",
e.Details ?? "Process monitoring manager encountered an error",
"Automation Monitoring Error",
e.Details ?? "Automation monitoring encountered an error.",
e.Error);
}
}
Expand All @@ -1300,42 +1297,22 @@ protected override void OnStateChanged(EventArgs e)
{
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.SuspendHiddenModeRefreshes();

if (this.processViewModel != null)
{
this.processViewModel.PauseRefresh();
this.processViewModel.SetProcessViewActive(false);
}

if (this.performanceViewModel != null)
{
_ = this.performanceViewModel.SuspendBackgroundMonitoringAsync();
}
this.ApplyAppRefreshPolicy(activityState);
}
else if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized)
{
this.ShowInTaskbar = true;

this.ResumeForegroundRefreshes();

if (this.processViewModel != null)
{
this.processViewModel.SetProcessViewActive(this.ProcessManagementTab.Visibility == Visibility.Visible);
this.processViewModel.ResumeRefresh();
}

if (this.performanceViewModel != null)
{
_ = this.performanceViewModel.ResumeBackgroundMonitoringAsync();
}
this.ApplyAppRefreshPolicy(this.GetForegroundActivityState());
}
}
catch (Exception ex)
Expand Down Expand Up @@ -1364,7 +1341,6 @@ private void ResumeForegroundRefreshes()
this.systemTrayUpdateTimer.Interval = SystemTrayUpdateBaseIntervalMs;
}
this.systemTrayUpdateTimer?.Start();
this.powerPlanViewModel.ResumeAutoRefresh(refreshImmediately: true);

_ = this.Dispatcher.InvokeAsync(async () =>
{
Expand All @@ -1380,6 +1356,52 @@ private void ResumeForegroundRefreshes()
});
}

private AppActivityState GetForegroundActivityState()
{
return this.ProcessManagementTab.Visibility == Visibility.Visible
? AppActivityState.ForegroundProcessView
: AppActivityState.ForegroundOtherTab;
}

private void ApplyAppRefreshPolicy(AppActivityState 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(refreshImmediately: state != AppActivityState.ForegroundOtherTab);
}
else
{
this.powerPlanViewModel.PauseAutoRefresh();
}

if (decision.PerformanceUiMonitoringEnabled)
{
_ = this.performanceViewModel.ResumeBackgroundMonitoringAsync();
}
else
{
_ = this.performanceViewModel.SuspendBackgroundMonitoringAsync();
}
}

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
Expand Down Expand Up @@ -1447,15 +1469,16 @@ private void ShowWindowFromTray(string? tabTag = null)
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.ResumeForegroundRefreshes();
this.processViewModel.SetProcessViewActive(processViewWillBeActive);
this.processViewModel.ResumeRefresh();
_ = this.performanceViewModel.ResumeBackgroundMonitoringAsync();
this.ApplyAppRefreshPolicy(processViewWillBeActive
? AppActivityState.ForegroundProcessView
: AppActivityState.ForegroundOtherTab);

if (tabTag != null)
{
Expand Down Expand Up @@ -1664,10 +1687,21 @@ private void ApplySectionVisibility(string tag)
this.NavTweaks.IsActive = tag == "Tweaks";
this.NavSettings.IsActive = tag == "Settings";

this.processViewModel.SetProcessViewActive(
string.Equals(tag, "Process", StringComparison.Ordinal) &&
this.IsVisible &&
this.WindowState != WindowState.Minimized);
if (!this.IsVisible)
{
this.ApplyAppRefreshPolicy(AppActivityState.TrayHidden);
return;
}

if (this.WindowState == WindowState.Minimized)
{
this.ApplyAppRefreshPolicy(AppActivityState.Minimized);
return;
}

this.ApplyAppRefreshPolicy(string.Equals(tag, "Process", StringComparison.Ordinal)
? AppActivityState.ForegroundProcessView
: AppActivityState.ForegroundOtherTab);
}

private void NavMenuItem_Click(object sender, RoutedEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
Margin="0,0,0,6"/>

<TextBlock x:Name="PerformanceIntroTip1"
Text="1. Start monitoring to collect a live snapshot."
Text="1. Start live metrics to collect a current system snapshot."
Foreground="{DynamicResource TextFillColorSecondaryBrush}"
Margin="0,0,0,4"/>
<TextBlock x:Name="PerformanceIntroTip2"
Expand Down
17 changes: 17 additions & 0 deletions Models/ProcessModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ namespace ThreadPilot.Models
using System.Diagnostics;
using CommunityToolkit.Mvvm.ComponentModel;

public enum ProcessClassification
{
ForegroundApp,
VisibleWindowApp,
BackgroundUser,
System,
ProtectedOrAccessDenied,
Terminated,
Unknown,
}

public partial class ProcessModel : ObservableObject
{
[ObservableProperty]
Expand Down Expand Up @@ -52,6 +63,12 @@ public partial class ProcessModel : ObservableObject
[ObservableProperty]
private bool hasVisibleWindow;

[ObservableProperty]
private bool isForeground;

[ObservableProperty]
private ProcessClassification classification = ProcessClassification.Unknown;

[ObservableProperty]
private bool isIdleServerDisabled;

Expand Down
Loading
Loading