diff --git a/Ink Canvas/Helpers/ThemeHelper.cs b/Ink Canvas/Helpers/ThemeHelper.cs new file mode 100644 index 00000000..c6dbf0de --- /dev/null +++ b/Ink Canvas/Helpers/ThemeHelper.cs @@ -0,0 +1,92 @@ +using iNKORE.UI.WPF.Modern; +using Microsoft.Win32; +using System; +using System.Windows; + +namespace Ink_Canvas.Helpers +{ + public static class ThemeHelper + { + public static bool IsSystemThemeLight() + { + try + { + var registryKey = Registry.CurrentUser; + var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); + if (themeKey != null) + { + var value = themeKey.GetValue("AppsUseLightTheme"); + if (value != null) + { + bool result = (int)value == 1; + themeKey.Close(); + return result; + } + themeKey.Close(); + } + } + catch + { + } + return true; + } + + public static bool IsSystemThemeLightLegacy() + { + try + { + var registryKey = Registry.CurrentUser; + var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); + if (themeKey != null) + { + int keyValue = (int)themeKey.GetValue("SystemUsesLightTheme"); + themeKey.Close(); + return keyValue == 1; + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine(ex); + } + return false; + } + + public static ElementTheme GetEffectiveTheme(Settings settings) + { + if (settings.Appearance.Theme == 0) + return ElementTheme.Light; + if (settings.Appearance.Theme == 1) + return ElementTheme.Dark; + + return IsSystemThemeLight() ? ElementTheme.Light : ElementTheme.Dark; + } + + public static void ApplyTheme(FrameworkElement element, Settings settings) + { + if (element == null || settings == null) return; + try + { + ThemeManager.SetRequestedTheme(element, GetEffectiveTheme(settings)); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"应用主题失败: {ex.Message}", LogHelper.LogType.Error); + } + } + + public static void ApplyTheme(FrameworkElement element, Settings settings, Action onThemeApplied) + { + if (element == null || settings == null) return; + try + { + var theme = GetEffectiveTheme(settings); + ThemeManager.SetRequestedTheme(element, theme); + onThemeApplied?.Invoke(theme == ElementTheme.Dark ? "Dark" : "Light"); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"应用主题失败: {ex.Message}", LogHelper.LogType.Error); + } + } + } +} diff --git a/Ink Canvas/MainWindow.xaml b/Ink Canvas/MainWindow.xaml index fbcf4f01..a91db569 100644 --- a/Ink Canvas/MainWindow.xaml +++ b/Ink Canvas/MainWindow.xaml @@ -1911,76 +1911,76 @@ Label="{i18n:I18n Key=Board_InsertImage}" IconGeometry="F1 M24,24z M0,0z M19,3H5C3.9,3 3,3.9 3,5v14c0,1.1 0.9,2 2,2h14c1.1,0 2-0.9 2-2V5C21,3.9 20.1,3 19,3zM19,19H5V5h14V19z M17,7c-1.1,0-2,0.9-2,2s0.9,2 2,2 2-0.9 2-2S18.1,7 17,7zM7,17l2.5-3.01 1.96,2.36 2.54-3.21L17,17H7z" ButtonMouseUp="InsertImageOptions_MouseUp" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { - ThemeManager.SetRequestedTheme(this, ElementTheme.Light); - } - else if (settings.Appearance.Theme == 1) // 深色主题 - { - ThemeManager.SetRequestedTheme(this, ElementTheme.Dark); - SetDarkThemeBorder(); - } - else // 跟随系统主题 - { - bool isSystemLight = IsSystemThemeLight(); - if (isSystemLight) + if (theme == "Dark") SetDarkThemeBorder(); + if (parentControl != null) { - ThemeManager.SetRequestedTheme(this, ElementTheme.Light); + UpdateTimeDisplay(); } - else - { - ThemeManager.SetRequestedTheme(this, ElementTheme.Dark); - SetDarkThemeBorder(); - } - } - - // 刷新数字和冒号显示的颜色 - if (parentControl != null) - { - UpdateTimeDisplay(); - } + }); } catch (Exception ex) { @@ -399,31 +379,6 @@ namespace Ink_Canvas.Windows } } - private bool IsSystemThemeLight() - { - var light = false; - try - { - var registryKey = Microsoft.Win32.Registry.CurrentUser; - var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - if (themeKey != null) - { - var value = themeKey.GetValue("AppsUseLightTheme"); - if (value != null) - { - light = (int)value == 1; - } - themeKey.Close(); - } - } - catch - { - // 如果读取注册表失败,默认为浅色主题 - light = true; - } - return light; - } - private bool IsLightTheme() { try diff --git a/Ink Canvas/Windows/NamesInputWindow.xaml.cs b/Ink Canvas/Windows/NamesInputWindow.xaml.cs index 8941dda7..67937b08 100644 --- a/Ink Canvas/Windows/NamesInputWindow.xaml.cs +++ b/Ink Canvas/Windows/NamesInputWindow.xaml.cs @@ -69,37 +69,7 @@ namespace Ink_Canvas private void ApplyTheme(Settings settings) { - try - { - if (settings.Appearance.Theme == 0) // 浅色主题 - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light); - ApplyThemeResources("Light"); - } - else if (settings.Appearance.Theme == 1) // 深色主题 - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark); - ApplyThemeResources("Dark"); - } - else // 跟随系统主题 - { - bool isSystemLight = IsSystemThemeLight(); - if (isSystemLight) - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light); - ApplyThemeResources("Light"); - } - else - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark); - ApplyThemeResources("Dark"); - } - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"应用名单导入窗口主题出错: {ex.Message}", LogHelper.LogType.Error); - } + ThemeHelper.ApplyTheme(this, settings, ApplyThemeResources); } private void ApplyThemeResources(string theme) @@ -133,29 +103,5 @@ namespace Ink_Canvas } } - private bool IsSystemThemeLight() - { - var light = false; - try - { - var registryKey = Microsoft.Win32.Registry.CurrentUser; - var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - if (themeKey != null) - { - var value = themeKey.GetValue("AppsUseLightTheme"); - if (value != null) - { - light = (int)value == 1; - } - themeKey.Close(); - } - } - catch - { - // 如果无法读取注册表,默认使用浅色主题 - light = true; - } - return light; - } } } diff --git a/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs b/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs index 16813087..0ea23c2c 100644 --- a/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs +++ b/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs @@ -1,4 +1,4 @@ -using Ink_Canvas.Helpers; +using Ink_Canvas.Helpers; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -333,39 +333,11 @@ 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); - ApplyThemeResources("Light"); - } - else if (settings.Appearance.Theme == 1) // 深色主题 - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark); - ApplyThemeResources("Dark"); - SetDarkThemeBorder(); - } - else // 跟随系统主题 - { - bool isSystemLight = IsSystemThemeLight(); - if (isSystemLight) - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light); - ApplyThemeResources("Light"); - } - else - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark); - ApplyThemeResources("Dark"); - SetDarkThemeBorder(); - } - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"应用新点名UI窗口主题出错: {ex.Message}", LogHelper.LogType.Error); - } + ApplyThemeResources(theme); + if (theme == "Dark") SetDarkThemeBorder(); + }); } /// @@ -412,29 +384,6 @@ namespace Ink_Canvas } } - private bool IsSystemThemeLight() - { - var light = false; - try - { - var registryKey = Microsoft.Win32.Registry.CurrentUser; - var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - if (themeKey != null) - { - var value = themeKey.GetValue("AppsUseLightTheme"); - if (value != null) - { - light = (int)value == 1; - } - themeKey.Close(); - } - } - catch - { - light = true; - } - return light; - } #endregion #region 点名模式逻辑 diff --git a/Ink Canvas/Windows/OperatingGuideWindow.xaml.cs b/Ink Canvas/Windows/OperatingGuideWindow.xaml.cs index f65be003..2b1ccfcb 100644 --- a/Ink Canvas/Windows/OperatingGuideWindow.xaml.cs +++ b/Ink Canvas/Windows/OperatingGuideWindow.xaml.cs @@ -1,5 +1,4 @@ using Ink_Canvas.Helpers; -using iNKORE.UI.WPF.Modern; using System; using System.Windows; using System.Windows.Input; @@ -23,52 +22,16 @@ namespace Ink_Canvas e.Handled = true; } - /// - /// 刷新主题 - /// public void RefreshTheme() { try { - // 根据当前主题设置窗口主题 - bool isDarkTheme = MainWindow.Settings.Appearance.Theme == 1 || - (MainWindow.Settings.Appearance.Theme == 2 && !IsSystemThemeLight()); - - if (isDarkTheme) - { - ThemeManager.SetRequestedTheme(this, ElementTheme.Dark); - } - else - { - ThemeManager.SetRequestedTheme(this, ElementTheme.Light); - } - - // 强制刷新UI + ThemeHelper.ApplyTheme(this, MainWindow.Settings); InvalidateVisual(); } catch (Exception) { } } - - /// - /// 检查系统主题是否为浅色 - /// - 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; - } } } \ No newline at end of file diff --git a/Ink Canvas/Windows/PPTQuickPanel.xaml.cs b/Ink Canvas/Windows/PPTQuickPanel.xaml.cs index 6f8cfd6a..2ea42ea9 100644 --- a/Ink Canvas/Windows/PPTQuickPanel.xaml.cs +++ b/Ink Canvas/Windows/PPTQuickPanel.xaml.cs @@ -1835,21 +1835,8 @@ namespace Ink_Canvas.Windows { try { - bool isDarkTheme = false; - - if (settings.Appearance.Theme == 0) // 浅色主题 - { - isDarkTheme = false; - } - else if (settings.Appearance.Theme == 1) // 深色主题 - { - isDarkTheme = true; - } - else // 跟随系统主题 - { - bool isSystemLight = IsSystemThemeLight(); - isDarkTheme = !isSystemLight; - } + bool isDarkTheme = settings.Appearance.Theme == 1 || + (settings.Appearance.Theme == 2 && !ThemeHelper.IsSystemThemeLight()); if (isDarkTheme) { @@ -1884,31 +1871,6 @@ namespace Ink_Canvas.Windows } } - private bool IsSystemThemeLight() - { - var light = false; - try - { - var registryKey = Microsoft.Win32.Registry.CurrentUser; - var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - if (themeKey != null) - { - var value = themeKey.GetValue("AppsUseLightTheme"); - if (value != null) - { - light = (int)value == 1; - } - themeKey.Close(); - } - } - catch - { - // 如果读取注册表失败,默认为浅色主题 - light = true; - } - return light; - } - #endregion #region 公开方法 diff --git a/Ink Canvas/Windows/PPTTimeCapsule.xaml.cs b/Ink Canvas/Windows/PPTTimeCapsule.xaml.cs index d6897e9d..e8a016b3 100644 --- a/Ink Canvas/Windows/PPTTimeCapsule.xaml.cs +++ b/Ink Canvas/Windows/PPTTimeCapsule.xaml.cs @@ -819,21 +819,8 @@ namespace Ink_Canvas.Windows { try { - bool isDarkTheme = false; - - if (settings.Appearance.Theme == 0) // 浅色主题 - { - isDarkTheme = false; - } - else if (settings.Appearance.Theme == 1) // 深色主题 - { - isDarkTheme = true; - } - else // 跟随系统主题 - { - bool isSystemLight = IsSystemThemeLight(); - isDarkTheme = !isSystemLight; - } + bool isDarkTheme = settings.Appearance.Theme == 1 || + (settings.Appearance.Theme == 2 && !ThemeHelper.IsSystemThemeLight()); if (isDarkTheme) { @@ -864,31 +851,6 @@ namespace Ink_Canvas.Windows } } - private bool IsSystemThemeLight() - { - var light = false; - try - { - var registryKey = Microsoft.Win32.Registry.CurrentUser; - var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - if (themeKey != null) - { - var value = themeKey.GetValue("AppsUseLightTheme"); - if (value != null) - { - light = (int)value == 1; - } - themeKey.Close(); - } - } - catch - { - // 如果读取注册表失败,默认为浅色主题 - light = true; - } - return light; - } - /// /// 获取当前倒计时状态 /// diff --git a/Ink Canvas/Windows/RandWindow.xaml.cs b/Ink Canvas/Windows/RandWindow.xaml.cs index faa9a39f..0fbaab29 100644 --- a/Ink Canvas/Windows/RandWindow.xaml.cs +++ b/Ink Canvas/Windows/RandWindow.xaml.cs @@ -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) diff --git a/Ink Canvas/Windows/RollCallHistoryWindow.xaml.cs b/Ink Canvas/Windows/RollCallHistoryWindow.xaml.cs index 5348081d..be871601 100644 --- a/Ink Canvas/Windows/RollCallHistoryWindow.xaml.cs +++ b/Ink Canvas/Windows/RollCallHistoryWindow.xaml.cs @@ -145,37 +145,7 @@ namespace Ink_Canvas private void ApplyTheme(Settings settings) { - try - { - if (settings.Appearance.Theme == 0) // 浅色主题 - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light); - ApplyThemeResources("Light"); - } - else if (settings.Appearance.Theme == 1) // 深色主题 - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark); - ApplyThemeResources("Dark"); - } - else // 跟随系统主题 - { - bool isSystemLight = IsSystemThemeLight(); - if (isSystemLight) - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Light); - ApplyThemeResources("Light"); - } - else - { - iNKORE.UI.WPF.Modern.ThemeManager.SetRequestedTheme(this, iNKORE.UI.WPF.Modern.ElementTheme.Dark); - ApplyThemeResources("Dark"); - } - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"应用历史记录窗口主题出错: {ex.Message}", LogHelper.LogType.Error); - } + ThemeHelper.ApplyTheme(this, settings, ApplyThemeResources); } private void ApplyThemeResources(string theme) @@ -209,29 +179,6 @@ namespace Ink_Canvas } } - private bool IsSystemThemeLight() - { - var light = false; - try - { - var registryKey = Microsoft.Win32.Registry.CurrentUser; - var themeKey = registryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - if (themeKey != null) - { - var value = themeKey.GetValue("AppsUseLightTheme"); - if (value != null) - { - light = (int)value == 1; - } - themeKey.Close(); - } - } - catch - { - light = true; - } - return light; - } } } diff --git a/Ink Canvas/Windows/TimerControl.xaml.cs b/Ink Canvas/Windows/TimerControl.xaml.cs index 69a8a9e1..e9634cbf 100644 --- a/Ink Canvas/Windows/TimerControl.xaml.cs +++ b/Ink Canvas/Windows/TimerControl.xaml.cs @@ -346,63 +346,11 @@ namespace Ink_Canvas.Windows 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); - SetDarkThemeBorder(); - } - 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); - SetDarkThemeBorder(); - } - } - - // 刷新数字和冒号显示的颜色 + if (theme == "Dark") SetDarkThemeBorder(); UpdateDigitDisplays(); - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"应用新计时器UI倒计时窗口主题出错: {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"); - if (themeKey != null) - { - var value = themeKey.GetValue("AppsUseLightTheme"); - if (value != null) - { - light = (int)value == 1; - } - themeKey.Close(); - } - } - catch - { - // 如果读取注册表失败,默认为浅色主题 - light = true; - } - return light; + }); } private void UpdateDigitDisplays()