refactor(主题): 将主题相关逻辑提取到ThemeHelper类中

重构主题相关代码,将重复的IsSystemThemeLight方法和主题应用逻辑提取到新的ThemeHelper工具类中
简化多个窗口的主题处理代码,统一使用ThemeHelper进行主题管理
This commit is contained in:
PrefacedCorg
2026-05-01 21:54:15 +08:00
parent 1217ef7ef9
commit 267d9b4450
12 changed files with 188 additions and 523 deletions
+2 -46
View File
@@ -79,60 +79,16 @@ namespace Ink_Canvas
private void ApplyTheme(Settings settings)
{
try
ThemeHelper.ApplyTheme(this, settings, theme =>
{
if (settings.Appearance.Theme == 0) // 浅色主题
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light);
}
else if (settings.Appearance.Theme == 1) // 深色主题
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark);
}
else // 跟随系统主题
{
bool isSystemLight = IsSystemThemeLight();
if (isSystemLight)
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light);
}
else
{
iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark);
}
}
// 根据主题设置窗口背景
if (settings.RandSettings.SelectedBackgroundIndex <= 0)
{
// 没有自定义背景时,使用主题背景色
if (Application.Current.FindResource("RandWindowBackground") is SolidColorBrush backgroundBrush)
{
MainBorder.Background = backgroundBrush;
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"应用点名窗口主题出错: {ex.Message}", LogHelper.LogType.Error);
}
}
private bool IsSystemThemeLight()
{
var light = false;
try
{
var registryKey = Microsoft.Win32.Registry.CurrentUser;
var themeKey =
registryKey.OpenSubKey("software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
var keyValue = 0;
if (themeKey != null) keyValue = (int)themeKey.GetValue("SystemUsesLightTheme");
if (keyValue == 1) light = true;
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
return light;
});
}
public RandWindow(Settings settings, bool IsAutoClose)