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"; @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
private readonly AppSettings _settings; private readonly AppSettings _settings;
private readonly Func<AppTheme> _detectSystemTheme;
private readonly List<Window> _windows = []; private readonly List<Window> _windows = [];
private ResourceDictionary? _palette; private ResourceDictionary? _palette;
private AppTheme _current; private AppTheme _current;
public ThemeService(AppSettings settings) 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; _settings = settings;
_detectSystemTheme = detectSystemTheme;
_settings.PropertyChanged += OnSettingsChanged; _settings.PropertyChanged += OnSettingsChanged;
SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged; SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged;
Apply(); Apply();
@@ -90,7 +101,7 @@ public sealed class ThemeService : IThemeService, IDisposable
private void Apply() 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) if (_palette is not null && theme == _current)
{ {
return; return;
@@ -100,7 +111,7 @@ public sealed class ThemeService : IThemeService, IDisposable
var next = new ResourceDictionary var next = new ResourceDictionary
{ {
Source = new Uri($"pack://application:,,,/Themes/{theme}.xaml", UriKind.Absolute), Source = PaletteUri(theme),
}; };
ICollection<ResourceDictionary> dictionaries = Application.Current.Resources.MergedDictionaries; 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) => private void ApplyTitleBar(Window window) =>
WindowThemeNative.SetDarkTitleBar( WindowThemeNative.SetDarkTitleBar(
new WindowInteropHelper(window).Handle, new WindowInteropHelper(window).Handle,