010dee679f
Large changeset adding documentation, themes, converters and wiring between UI and services. Key changes: - Rewrote .gitignore to standardize build, IDE, OS and project-specific ignores. - Added comprehensive documentation: ADVANCED_CPU_FEATURES.md, API_REFERENCE.md, ARCHITECTURE_GUIDE.md, DEVELOPER_GUIDE.md, GAME_BOOST_VALIDATION.md, CLAUDE.md, PROJECT_STRUCTURE.md, UI_STYLE_GUIDE.md, TRADEMARK.md. - Added theme resources (Themes/FluentLight.xaml, Themes/FluentDark.xaml) and merged FluentLight into App.xaml resource dictionary. - Added NullToBoolConverter and registered it in App.xaml. - Enabled a DEBUG-only console test mode in App.xaml.cs (--test flag) and guarded AllocConsole with #if DEBUG. - Added new service/support files (AtomicFileWriter, StoragePaths, ThemeService) and numerous updates across Services (CpuTopologyService, CoreMaskService, PowerPlanService, ProcessMonitorManagerService, ProcessService, NotificationService, SystemTrayService, SystemTweaksService, EnhancedLoggingService, etc.). - Updated many ViewModels and Views to support new functionality (CPU topology, power plan and game-boost features) and tests (Tests/TestRunner.cs). - Added build-release.ps1 and various project metadata updates. Overall this commit introduces documentation, theming, new converters and test/debug support, plus extensive service and UI wiring for CPU topology, process monitoring, power plans and game boost features.
600 lines
21 KiB
C#
600 lines
21 KiB
C#
/*
|
|
* ThreadPilot - Advanced Windows Process and Power Plan Manager
|
|
* Copyright (C) 2025 Prime Build
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, version 3 only.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Win32;
|
|
using ThreadPilot.Models;
|
|
using ThreadPilot.Services;
|
|
using ThreadPilot.ViewModels;
|
|
|
|
namespace ThreadPilot.ViewModels
|
|
{
|
|
public partial class ProcessPowerPlanAssociationViewModel : BaseViewModel
|
|
{
|
|
private readonly IProcessPowerPlanAssociationService _associationService;
|
|
private readonly IPowerPlanService _powerPlanService;
|
|
private readonly IProcessService _processService;
|
|
private readonly IProcessMonitorManagerService _monitorManagerService;
|
|
private readonly ICoreMaskService _coreMaskService;
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<ProcessPowerPlanAssociation> associations = new();
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<PowerPlanModel> availablePowerPlans = new();
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<CoreMask> availableCoreMasks = new();
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<string> availablePriorities = new()
|
|
{
|
|
"Idle",
|
|
"BelowNormal",
|
|
"Normal",
|
|
"AboveNormal",
|
|
"High",
|
|
"RealTime"
|
|
};
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<ProcessModel> runningProcesses = new();
|
|
|
|
[ObservableProperty]
|
|
private ProcessPowerPlanAssociation? selectedAssociation;
|
|
|
|
[ObservableProperty]
|
|
private PowerPlanModel? selectedPowerPlan;
|
|
|
|
[ObservableProperty]
|
|
private CoreMask? selectedCoreMask;
|
|
|
|
[ObservableProperty]
|
|
private string? selectedProcessPriority;
|
|
|
|
[ObservableProperty]
|
|
private ProcessModel? selectedProcess;
|
|
|
|
[ObservableProperty]
|
|
private string newExecutableName = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string newExecutablePath = string.Empty;
|
|
|
|
// Properties for the selected executable (read-only display)
|
|
[ObservableProperty]
|
|
private string selectedExecutableDisplayName = "No executable selected";
|
|
|
|
[ObservableProperty]
|
|
private string selectedExecutableFullPath = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private bool hasSelectedExecutable = false;
|
|
|
|
[ObservableProperty]
|
|
private bool matchByPath = false;
|
|
|
|
[ObservableProperty]
|
|
private int priority = 0;
|
|
|
|
[ObservableProperty]
|
|
private string description = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private PowerPlanModel? defaultPowerPlan;
|
|
|
|
[ObservableProperty]
|
|
private bool isMonitoringEnabled = true;
|
|
|
|
[ObservableProperty]
|
|
private bool isEventBasedMonitoringEnabled = true;
|
|
|
|
[ObservableProperty]
|
|
private bool isFallbackPollingEnabled = true;
|
|
|
|
[ObservableProperty]
|
|
private int pollingIntervalSeconds = 5;
|
|
|
|
[ObservableProperty]
|
|
private bool preventDuplicatePowerPlanChanges = true;
|
|
|
|
[ObservableProperty]
|
|
private int powerPlanChangeDelayMs = 1000;
|
|
|
|
[ObservableProperty]
|
|
private string serviceStatus = "Stopped";
|
|
|
|
[ObservableProperty]
|
|
private bool isServiceRunning = false;
|
|
|
|
public ProcessPowerPlanAssociationViewModel(
|
|
ILogger<ProcessPowerPlanAssociationViewModel> logger,
|
|
IProcessPowerPlanAssociationService associationService,
|
|
IPowerPlanService powerPlanService,
|
|
IProcessService processService,
|
|
IProcessMonitorManagerService monitorManagerService,
|
|
ICoreMaskService coreMaskService,
|
|
IEnhancedLoggingService? enhancedLoggingService = null)
|
|
: base(logger, enhancedLoggingService)
|
|
{
|
|
_associationService = associationService ?? throw new ArgumentNullException(nameof(associationService));
|
|
_powerPlanService = powerPlanService ?? throw new ArgumentNullException(nameof(powerPlanService));
|
|
_processService = processService ?? throw new ArgumentNullException(nameof(processService));
|
|
_monitorManagerService = monitorManagerService ?? throw new ArgumentNullException(nameof(monitorManagerService));
|
|
_coreMaskService = coreMaskService ?? throw new ArgumentNullException(nameof(coreMaskService));
|
|
|
|
// Subscribe to events
|
|
_associationService.ConfigurationChanged += OnConfigurationChanged;
|
|
_monitorManagerService.ServiceStatusChanged += OnServiceStatusChanged;
|
|
_monitorManagerService.ProcessPowerPlanChanged += OnProcessPowerPlanChanged;
|
|
|
|
// Initialize
|
|
_ = InitializeAsync();
|
|
}
|
|
|
|
public override async Task InitializeAsync()
|
|
{
|
|
await LoadDataAsync();
|
|
UpdateServiceStatus();
|
|
}
|
|
|
|
partial void OnSelectedAssociationChanged(ProcessPowerPlanAssociation? value)
|
|
{
|
|
if (value == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
PopulateEditorFromAssociation(value);
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task LoadDataAsync()
|
|
{
|
|
try
|
|
{
|
|
SetStatus("Loading data...");
|
|
|
|
// Load associations
|
|
var associationsData = await _associationService.GetAssociationsAsync();
|
|
Associations = new ObservableCollection<ProcessPowerPlanAssociation>(associationsData);
|
|
|
|
// Load power plans
|
|
var powerPlans = await _powerPlanService.GetPowerPlansAsync();
|
|
AvailablePowerPlans = powerPlans;
|
|
|
|
// Load core masks
|
|
await _coreMaskService.InitializeAsync();
|
|
AvailableCoreMasks = _coreMaskService.AvailableMasks;
|
|
|
|
// Load running processes
|
|
var processes = await _processService.GetProcessesAsync();
|
|
RunningProcesses = processes;
|
|
|
|
// Load configuration settings
|
|
var config = _associationService.Configuration;
|
|
IsEventBasedMonitoringEnabled = config.IsEventBasedMonitoringEnabled;
|
|
IsFallbackPollingEnabled = config.IsFallbackPollingEnabled;
|
|
PollingIntervalSeconds = config.PollingIntervalSeconds;
|
|
PreventDuplicatePowerPlanChanges = config.PreventDuplicatePowerPlanChanges;
|
|
PowerPlanChangeDelayMs = config.PowerPlanChangeDelayMs;
|
|
|
|
// Load default power plan
|
|
var (defaultGuid, defaultName) = await _associationService.GetDefaultPowerPlanAsync();
|
|
DefaultPowerPlan = AvailablePowerPlans.FirstOrDefault(p => p.Guid == defaultGuid);
|
|
|
|
ClearStatus();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error loading data: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task AddAssociationAsync()
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(NewExecutableName) || SelectedPowerPlan == null)
|
|
{
|
|
SetStatus("Please select an executable and a power plan", false);
|
|
return;
|
|
}
|
|
|
|
SetStatus("Adding association...");
|
|
|
|
var association = new ProcessPowerPlanAssociation
|
|
{
|
|
ExecutableName = NewExecutableName.Trim(),
|
|
ExecutablePath = NewExecutablePath.Trim(),
|
|
PowerPlanGuid = SelectedPowerPlan.Guid,
|
|
PowerPlanName = SelectedPowerPlan.Name,
|
|
CoreMaskId = SelectedCoreMask?.Id,
|
|
CoreMaskName = SelectedCoreMask?.Name,
|
|
ProcessPriority = SelectedProcessPriority,
|
|
MatchByPath = MatchByPath,
|
|
Priority = Priority,
|
|
Description = Description.Trim(),
|
|
IsEnabled = true
|
|
};
|
|
|
|
var success = await _associationService.AddAssociationAsync(association);
|
|
if (success)
|
|
{
|
|
// Clear form
|
|
NewExecutableName = string.Empty;
|
|
NewExecutablePath = string.Empty;
|
|
SelectedPowerPlan = null;
|
|
SelectedCoreMask = null;
|
|
SelectedProcessPriority = null;
|
|
MatchByPath = false;
|
|
Priority = 0;
|
|
Description = string.Empty;
|
|
SelectedAssociation = null;
|
|
|
|
await LoadDataAsync();
|
|
SetStatus("Rule created and applied successfully.", false);
|
|
}
|
|
else
|
|
{
|
|
SetStatus("Failed to add rule - it may already exist", false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error adding rule: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task UpdateAssociationAsync()
|
|
{
|
|
try
|
|
{
|
|
if (SelectedAssociation == null)
|
|
{
|
|
SetStatus("Please select a rule to update", false);
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(NewExecutableName) || SelectedPowerPlan == null)
|
|
{
|
|
SetStatus("Executable and power plan are required", false);
|
|
return;
|
|
}
|
|
|
|
SetStatus("Updating rule...");
|
|
|
|
ApplyEditorToAssociation(SelectedAssociation);
|
|
|
|
var success = await _associationService.UpdateAssociationAsync(SelectedAssociation);
|
|
if (success)
|
|
{
|
|
await LoadDataAsync();
|
|
SetStatus("Rule updated and applied successfully.", false);
|
|
}
|
|
else
|
|
{
|
|
SetStatus("Failed to update rule", false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error updating rule: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task RemoveAssociationAsync()
|
|
{
|
|
try
|
|
{
|
|
if (SelectedAssociation == null)
|
|
{
|
|
SetStatus("Please select a rule to remove", false);
|
|
return;
|
|
}
|
|
|
|
SetStatus("Removing rule...");
|
|
|
|
var success = await _associationService.RemoveAssociationAsync(SelectedAssociation.Id);
|
|
if (success)
|
|
{
|
|
SelectedAssociation = null;
|
|
await LoadDataAsync();
|
|
SetStatus("Rule removed successfully");
|
|
}
|
|
else
|
|
{
|
|
SetStatus("Failed to remove rule", false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error removing rule: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task SetDefaultPowerPlanAsync()
|
|
{
|
|
try
|
|
{
|
|
if (DefaultPowerPlan == null)
|
|
{
|
|
SetStatus("Please select a default power plan", false);
|
|
return;
|
|
}
|
|
|
|
SetStatus("Setting default power plan...");
|
|
|
|
var success = await _associationService.SetDefaultPowerPlanAsync(DefaultPowerPlan.Guid, DefaultPowerPlan.Name);
|
|
if (success)
|
|
{
|
|
SetStatus("Default power plan set successfully");
|
|
}
|
|
else
|
|
{
|
|
SetStatus("Failed to set default power plan", false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error setting default power plan: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task StartMonitoringAsync()
|
|
{
|
|
try
|
|
{
|
|
SetStatus("Starting monitoring service...");
|
|
await _monitorManagerService.StartAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error starting monitoring: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task StopMonitoringAsync()
|
|
{
|
|
try
|
|
{
|
|
SetStatus("Stopping monitoring service...");
|
|
await _monitorManagerService.StopAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error stopping monitoring: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task SaveConfigurationAsync()
|
|
{
|
|
try
|
|
{
|
|
SetStatus("Saving configuration...");
|
|
|
|
// Update configuration with current settings
|
|
var config = _associationService.Configuration;
|
|
config.IsEventBasedMonitoringEnabled = IsEventBasedMonitoringEnabled;
|
|
config.IsFallbackPollingEnabled = IsFallbackPollingEnabled;
|
|
config.PollingIntervalSeconds = PollingIntervalSeconds;
|
|
config.PreventDuplicatePowerPlanChanges = PreventDuplicatePowerPlanChanges;
|
|
config.PowerPlanChangeDelayMs = PowerPlanChangeDelayMs;
|
|
|
|
var success = await _associationService.SaveConfigurationAsync();
|
|
if (success)
|
|
{
|
|
SetStatus("Configuration saved and active.", false);
|
|
}
|
|
else
|
|
{
|
|
SetStatus("Failed to save configuration", false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error saving configuration: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void UseSelectedProcessForAssociation()
|
|
{
|
|
if (SelectedProcess != null)
|
|
{
|
|
NewExecutableName = SelectedProcess.Name;
|
|
NewExecutablePath = SelectedProcess.ExecutablePath;
|
|
|
|
// Update the selected executable display
|
|
UpdateSelectedExecutableDisplay(SelectedProcess.ExecutablePath, SelectedProcess.Name);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void BrowseExecutable()
|
|
{
|
|
try
|
|
{
|
|
var openFileDialog = new Microsoft.Win32.OpenFileDialog
|
|
{
|
|
Title = "Select Executable File",
|
|
Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*",
|
|
FilterIndex = 1,
|
|
CheckFileExists = true,
|
|
CheckPathExists = true,
|
|
Multiselect = false
|
|
};
|
|
|
|
if (openFileDialog.ShowDialog() == true)
|
|
{
|
|
var selectedFilePath = openFileDialog.FileName;
|
|
|
|
// Validate that it's an executable file
|
|
if (!IsValidExecutable(selectedFilePath))
|
|
{
|
|
SetStatus("Selected file is not a valid executable", false);
|
|
return;
|
|
}
|
|
|
|
// Extract executable name from the full path
|
|
var executableName = Path.GetFileName(selectedFilePath);
|
|
|
|
// Auto-populate the fields
|
|
NewExecutableName = executableName;
|
|
NewExecutablePath = selectedFilePath;
|
|
|
|
// Update the display
|
|
UpdateSelectedExecutableDisplay(selectedFilePath, executableName);
|
|
|
|
SetStatus($"Selected executable: {executableName}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetStatus($"Error selecting executable: {ex.Message}", false);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void ClearSelectedExecutable()
|
|
{
|
|
NewExecutableName = string.Empty;
|
|
NewExecutablePath = string.Empty;
|
|
SelectedExecutableDisplayName = "No executable selected";
|
|
SelectedExecutableFullPath = string.Empty;
|
|
HasSelectedExecutable = false;
|
|
SetStatus("Executable selection cleared");
|
|
}
|
|
|
|
private void OnConfigurationChanged(object? sender, ConfigurationChangedEventArgs e)
|
|
{
|
|
// Reload data when configuration changes - marshal to UI thread to prevent cross-thread access exceptions
|
|
_ = System.Windows.Application.Current.Dispatcher.InvokeAsync(async () => await LoadDataAsync());
|
|
}
|
|
|
|
private void OnServiceStatusChanged(object? sender, ServiceStatusEventArgs e)
|
|
{
|
|
// Marshal UI updates to the UI thread to prevent cross-thread access exceptions
|
|
System.Windows.Application.Current.Dispatcher.InvokeAsync(() =>
|
|
{
|
|
ServiceStatus = e.Status;
|
|
IsServiceRunning = e.IsRunning;
|
|
});
|
|
}
|
|
|
|
private bool IsValidExecutable(string filePath)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
|
|
return false;
|
|
|
|
var extension = Path.GetExtension(filePath);
|
|
return string.Equals(extension, ".exe", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void UpdateSelectedExecutableDisplay(string fullPath, string executableName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fullPath))
|
|
{
|
|
SelectedExecutableDisplayName = "No executable selected";
|
|
SelectedExecutableFullPath = string.Empty;
|
|
HasSelectedExecutable = false;
|
|
}
|
|
else
|
|
{
|
|
SelectedExecutableDisplayName = executableName;
|
|
SelectedExecutableFullPath = fullPath;
|
|
HasSelectedExecutable = true;
|
|
}
|
|
}
|
|
|
|
private void PopulateEditorFromAssociation(ProcessPowerPlanAssociation association)
|
|
{
|
|
NewExecutableName = association.ExecutableName ?? string.Empty;
|
|
NewExecutablePath = association.ExecutablePath ?? string.Empty;
|
|
MatchByPath = association.MatchByPath;
|
|
Priority = association.Priority;
|
|
Description = association.Description ?? string.Empty;
|
|
SelectedProcessPriority = association.ProcessPriority;
|
|
|
|
SelectedPowerPlan = AvailablePowerPlans.FirstOrDefault(p =>
|
|
string.Equals(p.Guid, association.PowerPlanGuid, StringComparison.OrdinalIgnoreCase));
|
|
|
|
SelectedCoreMask = AvailableCoreMasks.FirstOrDefault(m =>
|
|
string.Equals(m.Id, association.CoreMaskId, StringComparison.Ordinal));
|
|
|
|
UpdateSelectedExecutableDisplay(
|
|
NewExecutablePath,
|
|
string.IsNullOrWhiteSpace(NewExecutableName)
|
|
? Path.GetFileName(NewExecutablePath)
|
|
: NewExecutableName);
|
|
}
|
|
|
|
private void ApplyEditorToAssociation(ProcessPowerPlanAssociation association)
|
|
{
|
|
association.ExecutableName = NewExecutableName.Trim();
|
|
association.ExecutablePath = NewExecutablePath.Trim();
|
|
association.MatchByPath = MatchByPath;
|
|
association.Priority = Priority;
|
|
association.Description = Description.Trim();
|
|
association.ProcessPriority = SelectedProcessPriority;
|
|
association.PowerPlanGuid = SelectedPowerPlan?.Guid ?? string.Empty;
|
|
association.PowerPlanName = SelectedPowerPlan?.Name ?? string.Empty;
|
|
association.CoreMaskId = SelectedCoreMask?.Id;
|
|
association.CoreMaskName = SelectedCoreMask?.Name;
|
|
association.UpdatedAt = DateTime.UtcNow;
|
|
}
|
|
|
|
private void OnProcessPowerPlanChanged(object? sender, ProcessPowerPlanChangeEventArgs e)
|
|
{
|
|
// Marshal UI updates to the UI thread to prevent cross-thread access exceptions
|
|
System.Windows.Application.Current.Dispatcher.InvokeAsync(() =>
|
|
{
|
|
// Update status when power plan changes occur
|
|
SetStatus($"Power plan changed: {e.NewPowerPlan?.Name} for {e.Process.Name}");
|
|
});
|
|
}
|
|
|
|
private void UpdateServiceStatus()
|
|
{
|
|
ServiceStatus = _monitorManagerService.Status;
|
|
IsServiceRunning = _monitorManagerService.IsRunning;
|
|
}
|
|
}
|
|
}
|
|
|