Files
ThreadPilot/ViewModels/MainWindowViewModel.cs
T
PrimeBuild-pc 010dee679f Add docs, themes, services and UI updates
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.
2026-04-10 03:58:51 +02:00

197 lines
7.1 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 CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using ThreadPilot.Models;
using ThreadPilot.Services;
using ThreadPilot.ViewModels;
namespace ThreadPilot
{
public partial class MainWindowViewModel : BaseViewModel
{
private readonly IProcessMonitorManagerService? _processMonitorManagerService;
private readonly INotificationService? _notificationService;
private readonly IElevationService? _elevationService;
private readonly ISecurityService? _securityService;
[ObservableProperty]
private bool isProcessMonitoringActive = false;
[ObservableProperty]
private string processMonitoringStatusText = "Process Monitoring: Inactive";
[ObservableProperty]
private bool isRunningAsAdministrator = false;
[ObservableProperty]
private string elevationStatusText = "Checking elevation status...";
[ObservableProperty]
private bool showElevationPrompt = false;
public MainWindowViewModel(
ILogger<MainWindowViewModel> logger,
IEnhancedLoggingService? enhancedLoggingService = null,
IProcessMonitorManagerService? processMonitorManagerService = null,
INotificationService? notificationService = null,
IElevationService? elevationService = null,
ISecurityService? securityService = null)
: base(logger, enhancedLoggingService)
{
_processMonitorManagerService = processMonitorManagerService;
_notificationService = notificationService;
_elevationService = elevationService;
_securityService = securityService;
}
public override async Task InitializeAsync()
{
await ExecuteAsync(async () =>
{
// Subscribe to service events
if (_processMonitorManagerService != null)
{
_processMonitorManagerService.ServiceStatusChanged += OnServiceStatusChanged;
}
// Initialize status
await UpdateStatusAsync();
UpdateElevationStatus();
await LogUserActionAsync("MainWindow", "Initialized main window", "Application startup");
}, "Initializing main window...");
}
[RelayCommand]
private async Task ToggleProcessMonitoringAsync()
{
if (_processMonitorManagerService == null) return;
await ExecuteAsync(async () =>
{
if (IsProcessMonitoringActive)
{
await _processMonitorManagerService.StopAsync();
await LogUserActionAsync("ProcessMonitoring", "Stopped process monitoring", "User action");
}
else
{
await _processMonitorManagerService.StartAsync();
await LogUserActionAsync("ProcessMonitoring", "Started process monitoring", "User action");
}
}, IsProcessMonitoringActive ? "Stopping monitoring..." : "Starting monitoring...");
}
[RelayCommand]
private async Task RequestElevationAsync()
{
if (_elevationService == null) return;
await ExecuteAsync(async () =>
{
var success = await _elevationService.RequestElevationIfNeeded();
if (success)
{
await LogUserActionAsync("Elevation", "Requested elevation", "User action");
}
else
{
await LogUserActionAsync("Elevation", "Elevation request failed or cancelled", "User action");
}
}, "Requesting elevation...");
}
private async Task UpdateStatusAsync()
{
try
{
// Update process monitoring status
if (_processMonitorManagerService != null)
{
IsProcessMonitoringActive = _processMonitorManagerService.IsRunning;
ProcessMonitoringStatusText = IsProcessMonitoringActive
? "Process Monitoring: Active"
: "Process Monitoring: Inactive";
}
// Update elevation status
UpdateElevationStatus();
}
catch (Exception ex)
{
SetError("Failed to update status", ex);
}
}
private void UpdateElevationStatus()
{
if (_elevationService == null)
{
IsRunningAsAdministrator = false;
ElevationStatusText = "Elevation service not available";
ShowElevationPrompt = false;
return;
}
IsRunningAsAdministrator = _elevationService.IsRunningAsAdministrator();
ElevationStatusText = _elevationService.GetElevationStatus();
ShowElevationPrompt = !IsRunningAsAdministrator;
}
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(() =>
{
IsProcessMonitoringActive = e.IsRunning;
ProcessMonitoringStatusText = $"Process Monitoring: {e.Status}";
});
}
public void UpdateProcessMonitoringStatus(bool isActive, string status)
{
if (System.Windows.Application.Current.Dispatcher.CheckAccess())
{
IsProcessMonitoringActive = isActive;
ProcessMonitoringStatusText = $"Process Monitoring: {status}";
}
else
{
System.Windows.Application.Current.Dispatcher.InvokeAsync(() =>
{
IsProcessMonitoringActive = isActive;
ProcessMonitoringStatusText = $"Process Monitoring: {status}";
});
}
}
protected override void OnDispose()
{
if (_processMonitorManagerService != null)
{
_processMonitorManagerService.ServiceStatusChanged -= OnServiceStatusChanged;
}
base.OnDispose();
}
}
}