Files
ThreadPilot/ViewModels/PowerPlanViewModel.cs
T
PrimeBuild-pc 1ab85b21bd Release 1.1.1: installer, elevation & onboarding
Bump release to v1.1.1 and add release/build packaging, installer and onboarding enhancements. Key changes:

- Version bump across csproj, README and release assets to 1.1.1.
- CI/workflow: copy bundled Powerplans into single-file output, install Inno Setup, build Inno Setup installer, include installer artifacts in GitHub release, and stage unified package zips with uninstall/license files.
- Installer: update InstallScript (setup.iss) to point to staged artifacts, include Powerplans folder, and output installer to artifacts/release/installer.
- App startup/elevation: requireAdministrator manifest and App.xaml.cs now requests elevation early, terminating non-elevated instance when elevation is requested/declined.
- UX: add Performance first-run onboarding overlay (modal with blurred background), updated loading overlay visuals, and related MainWindow logic to show/hide intro and persist seen state.
- Settings & config: implement export/import configuration bundle (JSON) with Save/Open dialogs, add ReplaceConfigurationAsync and CloneConfiguration in association service, and adjust settings view labels.
- Power plan features: add PowerPlanService.AddCustomPowerPlanFileAsync and UI/command to add .pow files; adjust Import/Apply button labels.
- Performance tweaks: reduce power plan change delay from 1000ms to 250ms in config, models and viewmodels.
- Theme detection: improve ThemeService to resolve registry values and fallback to system theme helper.
- Misc: various logging/status improvements, packaging script fixes (build-release/build-msix), and added package-release-zips script.

These changes prepare the project for a formal 1.1.1 release with installer distribution, improved elevation handling and onboarding, plus enhanced import/export and custom power plan support.
2026-04-14 03:22:06 +02:00

230 lines
7.9 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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace ThreadPilot.ViewModels
{
using System;
using System.Collections.ObjectModel;
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;
public partial class PowerPlanViewModel : BaseViewModel
{
private readonly IPowerPlanService powerPlanService;
private System.Timers.Timer? refreshTimer;
[ObservableProperty]
private ObservableCollection<PowerPlanModel> powerPlans = new();
[ObservableProperty]
private ObservableCollection<PowerPlanModel> customPowerPlans = new();
[ObservableProperty]
private PowerPlanModel? selectedPowerPlan;
[ObservableProperty]
private PowerPlanModel? selectedCustomPlan;
[ObservableProperty]
private PowerPlanModel? activePowerPlan;
public PowerPlanViewModel(
ILogger<PowerPlanViewModel> logger,
IPowerPlanService powerPlanService,
IEnhancedLoggingService? enhancedLoggingService = null)
: base(logger, enhancedLoggingService)
{
this.powerPlanService = powerPlanService;
this.SetupRefreshTimer();
}
private void SetupRefreshTimer()
{
this.refreshTimer = new System.Timers.Timer(10000); // PERFORMANCE OPTIMIZATION: Increased to 10 second refresh - power plans change infrequently
this.refreshTimer.Elapsed += async (s, e) =>
{
try
{
// Marshal timer callback to UI thread to prevent cross-thread access exceptions
await System.Windows.Application.Current.Dispatcher.InvokeAsync(async () =>
{
await this.RefreshPowerPlansCommand.ExecuteAsync(null);
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Power plan refresh timer error: {ex.Message}");
}
};
this.refreshTimer.Start();
}
[RelayCommand]
public async Task LoadPowerPlans()
{
try
{
this.SetStatus("Loading power plans...");
this.PowerPlans = await this.powerPlanService.GetPowerPlansAsync();
this.CustomPowerPlans = await this.powerPlanService.GetCustomPowerPlansAsync();
this.ActivePowerPlan = await this.powerPlanService.GetActivePowerPlan();
this.ClearStatus();
}
catch (Exception ex)
{
this.SetStatus($"Error loading power plans: {ex.Message}", false);
}
}
[RelayCommand]
private async Task RefreshPowerPlans()
{
if (this.IsBusy)
{
return;
}
try
{
var currentPlans = await this.powerPlanService.GetPowerPlansAsync();
var currentActive = await this.powerPlanService.GetActivePowerPlan();
var customPlans = await this.powerPlanService.GetCustomPowerPlansAsync();
// Update power plans
this.PowerPlans = new ObservableCollection<PowerPlanModel>(currentPlans);
this.CustomPowerPlans = new ObservableCollection<PowerPlanModel>(customPlans);
this.ActivePowerPlan = currentActive;
// Update selected plan if it exists
if (this.SelectedPowerPlan != null)
{
this.SelectedPowerPlan = this.PowerPlans.FirstOrDefault(p => p.Guid == this.SelectedPowerPlan.Guid);
}
}
catch (Exception ex)
{
this.SetStatus($"Error refreshing power plans: {ex.Message}", false);
}
}
[RelayCommand]
private async Task SetActivePlan()
{
if (this.SelectedPowerPlan == null)
{
return;
}
try
{
this.SetStatus($"Setting active power plan to {this.SelectedPowerPlan.Name}...");
var success = await this.powerPlanService.SetActivePowerPlan(this.SelectedPowerPlan);
if (success)
{
this.ActivePowerPlan = this.SelectedPowerPlan;
await this.RefreshPowerPlans();
this.ClearStatus();
}
else
{
this.SetStatus($"Failed to set power plan {this.SelectedPowerPlan.Name}", false);
}
}
catch (Exception ex)
{
this.SetStatus($"Error setting power plan: {ex.Message}", false);
}
}
[RelayCommand]
private async Task ImportCustomPlan()
{
if (this.SelectedCustomPlan == null)
{
return;
}
try
{
this.SetStatus($"Importing custom power plan {this.SelectedCustomPlan.Name}...");
var success = await this.powerPlanService.ImportCustomPowerPlan(this.SelectedCustomPlan.FilePath);
if (success)
{
await this.RefreshPowerPlans();
this.ClearStatus();
}
else
{
this.SetStatus($"Failed to import power plan {this.SelectedCustomPlan.Name}", false);
}
}
catch (Exception ex)
{
this.SetStatus($"Error importing power plan: {ex.Message}", false);
}
}
[RelayCommand]
private async Task AddCustomPlanFile()
{
try
{
var dialog = new OpenFileDialog
{
Title = "Select custom power plan",
Filter = "Power Plan Files (*.pow)|*.pow|All Files (*.*)|*.*",
FilterIndex = 1,
CheckFileExists = true,
CheckPathExists = true,
Multiselect = false,
};
if (dialog.ShowDialog() != true)
{
return;
}
this.SetStatus("Adding custom power plan file...");
var success = await this.powerPlanService.AddCustomPowerPlanFileAsync(dialog.FileName);
if (success)
{
await this.RefreshPowerPlans();
this.SetStatus("Custom power plan added to library.", false);
}
else
{
this.SetStatus("Failed to add custom power plan file.", false);
}
}
catch (Exception ex)
{
this.SetStatus($"Error adding custom power plan file: {ex.Message}", false);
}
}
}
}