Files
ThreadPilot/Services/ThemeService.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

170 lines
6.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 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.Services
{
using System;
using System.Windows;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
public class ThemeService : IThemeService, IDisposable
{
private const string LightThemeDictionaryPath = "Themes/FluentLight.xaml";
private const string DarkThemeDictionaryPath = "Themes/FluentDark.xaml";
private readonly ILogger<ThemeService> logger;
private ResourceDictionary? activeThemeDictionary;
public bool IsDarkTheme { get; private set; }
public ThemeService(ILogger<ThemeService> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
SystemEvents.UserPreferenceChanged += this.OnUserPreferenceChanged;
}
public void ApplyTheme(bool useDarkTheme)
{
var targetUri = new Uri(useDarkTheme ? DarkThemeDictionaryPath : LightThemeDictionaryPath, UriKind.Relative);
var appResources = System.Windows.Application.Current?.Resources;
if (appResources == null)
{
return;
}
try
{
for (int i = appResources.MergedDictionaries.Count - 1; i >= 0; i--)
{
var dictionary = appResources.MergedDictionaries[i];
var source = dictionary.Source?.OriginalString;
if (string.Equals(source, LightThemeDictionaryPath, StringComparison.OrdinalIgnoreCase) ||
string.Equals(source, DarkThemeDictionaryPath, StringComparison.OrdinalIgnoreCase))
{
appResources.MergedDictionaries.RemoveAt(i);
}
}
this.activeThemeDictionary = null;
var nextDictionary = new ResourceDictionary { Source = targetUri };
appResources.MergedDictionaries.Insert(0, nextDictionary);
this.activeThemeDictionary = nextDictionary;
// Keep Wpf.Ui controls aligned with app theme (NavigationView, TitleBar, etc.).
var applicationTheme = useDarkTheme ? ApplicationTheme.Dark : ApplicationTheme.Light;
ApplicationThemeManager.Apply(applicationTheme, WindowBackdropType.Mica, updateAccent: true);
this.IsDarkTheme = useDarkTheme;
}
catch (Exception ex)
{
this.logger.LogError(ex, "Failed to apply theme {ThemeUri}", targetUri);
}
}
public bool GetSystemUsesDarkTheme()
{
try
{
const string personalizeKey = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
using var key = Registry.CurrentUser.OpenSubKey(personalizeKey, writable: false);
if (key != null)
{
var appsThemeValue = key.GetValue("AppsUseLightTheme");
if (TryResolveDarkPreference(appsThemeValue, out var useDarkTheme))
{
return useDarkTheme;
}
// Fallback key used on some Windows configurations.
var systemThemeValue = key.GetValue("SystemUsesLightTheme");
if (TryResolveDarkPreference(systemThemeValue, out useDarkTheme))
{
return useDarkTheme;
}
}
var detectedTheme = ApplicationThemeManager.GetSystemTheme();
if (detectedTheme == SystemTheme.Dark)
{
return true;
}
if (detectedTheme == SystemTheme.Light)
{
return false;
}
}
catch (Exception ex)
{
this.logger.LogWarning(ex, "Failed to read system theme preference, falling back to light theme");
}
return false;
}
private static bool TryResolveDarkPreference(object? value, out bool useDarkTheme)
{
useDarkTheme = false;
switch (value)
{
case int intValue:
useDarkTheme = intValue == 0;
return true;
case long longValue:
useDarkTheme = longValue == 0;
return true;
case string stringValue when int.TryParse(stringValue, out var parsed):
useDarkTheme = parsed == 0;
return true;
default:
return false;
}
}
private void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category != UserPreferenceCategory.Color &&
e.Category != UserPreferenceCategory.General &&
e.Category != UserPreferenceCategory.VisualStyle)
{
return;
}
try
{
this.ApplyTheme(this.GetSystemUsesDarkTheme());
}
catch (Exception ex)
{
this.logger.LogDebug(ex, "Failed to apply theme after system preference change");
}
}
public void Dispose()
{
SystemEvents.UserPreferenceChanged -= this.OnUserPreferenceChanged;
}
}
}