modified ThemeService

This commit is contained in:
2026-08-09 20:11:14 +05:00
parent 71ad8879ac
commit 1053932cfd
+19 -2
View File
@@ -17,13 +17,24 @@ public sealed class ThemeService : IThemeService, IDisposable
@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
private readonly AppSettings _settings;
private readonly Func<AppTheme> _detectSystemTheme;
private readonly List<Window> _windows = [];
private ResourceDictionary? _palette;
private AppTheme _current;
public ThemeService(AppSettings settings)
: this(settings, DetectSystemTheme)
{
}
/// <summary>
/// Takes the source of the system theme explicitly: in tests it is not the registry
/// that provides it.
/// </summary>
internal ThemeService(AppSettings settings, Func<AppTheme> detectSystemTheme)
{
_settings = settings;
_detectSystemTheme = detectSystemTheme;
_settings.PropertyChanged += OnSettingsChanged;
SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged;
Apply();
@@ -90,7 +101,7 @@ public sealed class ThemeService : IThemeService, IDisposable
private void Apply()
{
AppTheme theme = _settings.Theme == AppTheme.System ? DetectSystemTheme() : _settings.Theme;
AppTheme theme = _settings.Theme == AppTheme.System ? _detectSystemTheme() : _settings.Theme;
if (_palette is not null && theme == _current)
{
return;
@@ -100,7 +111,7 @@ public sealed class ThemeService : IThemeService, IDisposable
var next = new ResourceDictionary
{
Source = new Uri($"pack://application:,,,/Themes/{theme}.xaml", UriKind.Absolute),
Source = PaletteUri(theme),
};
ICollection<ResourceDictionary> dictionaries = Application.Current.Resources.MergedDictionaries;
@@ -119,6 +130,12 @@ public sealed class ThemeService : IThemeService, IDisposable
}
}
// The assembly name in the address is there on purpose: without it the dictionary
// is looked up in the assembly the process started from, which is not always the
// application itself
internal static Uri PaletteUri(AppTheme theme) =>
new($"pack://application:,,,/CursorLang;component/Themes/{theme}.xaml", UriKind.Absolute);
private void ApplyTitleBar(Window window) =>
WindowThemeNative.SetDarkTitleBar(
new WindowInteropHelper(window).Handle,