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
+92
View File
@@ -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<string> 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);
}
}
}
}
+72 -72
View File
@@ -1911,76 +1911,76 @@
Label="{i18n:I18n Key=Board_InsertImage}" 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" 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" /> ButtonMouseUp="InsertImageOptions_MouseUp" />
<localControls:BoardMenuFrame x:Name="BoardImageOptionsPanel" Visibility="Visible" ClipToBounds="True" <localControls:BoardMenuFrame x:Name="BoardImageOptionsPanel" Visibility="Visible" ClipToBounds="True"
PlacementTarget="{Binding ElementName=BoardInsertImage}" PlacementTarget="{Binding ElementName=BoardInsertImage}"
PanelCornerRadius="5" HeaderCornerRadius="6,6,0,0" TitleFontSize="11" PanelCornerRadius="5" HeaderCornerRadius="6,6,0,0" TitleFontSize="11"
PanelBackground="{DynamicResource FloatBarBackground}" Opacity="1" PanelBackground="{DynamicResource FloatBarBackground}" Opacity="1"
Title="{i18n:I18n Key=Board_SelectImage}" Title="{i18n:I18n Key=Board_SelectImage}"
CloseMouseDown="Border_MouseDown" CloseMouseDown="Border_MouseDown"
CloseMouseUp="CloseImageOptionsPanel_MouseUp"> CloseMouseUp="CloseImageOptionsPanel_MouseUp">
<ikw:SimpleStackPanel Margin="6,4,6,4" Spacing="2"> <ikw:SimpleStackPanel Margin="6,4,6,4" Spacing="2">
<!-- Screenshot Option --> <!-- Screenshot Option -->
<Border MouseDown="Border_MouseDown" MouseUp="ImageOptionScreenshot_MouseUp" <Border MouseDown="Border_MouseDown" MouseUp="ImageOptionScreenshot_MouseUp"
Background="Transparent" CornerRadius="3" Padding="6,4"> Background="Transparent" CornerRadius="3" Padding="6,4">
<Border.Style> <Border.Style>
<Style TargetType="Border"> <Style TargetType="Border">
<Setter Property="Background" Value="Transparent"/> <Setter Property="Background" Value="Transparent"/>
<Style.Triggers> <Style.Triggers>
<Trigger Property="IsMouseOver" Value="True"> <Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#f0f9ff"/> <Setter Property="Background" Value="#f0f9ff"/>
</Trigger> </Trigger>
</Style.Triggers> </Style.Triggers>
</Style> </Style>
</Border.Style> </Border.Style>
<ikw:SimpleStackPanel Orientation="Horizontal" Spacing="6"> <ikw:SimpleStackPanel Orientation="Horizontal" Spacing="6">
<Image Height="16" Width="16"> <Image Height="16" Width="16">
<Image.Source> <Image.Source>
<DrawingImage> <DrawingImage>
<DrawingImage.Drawing> <DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z"> <DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
<GeometryDrawing Brush="#2563eb" <GeometryDrawing Brush="#2563eb"
Geometry="{Binding Source={x:Static icons:XamlGraphicsIconGeometries.ScreenshotIconGeometry}, Converter={StaticResource StringToGeometryConverter}}"/> Geometry="{Binding Source={x:Static icons:XamlGraphicsIconGeometries.ScreenshotIconGeometry}, Converter={StaticResource StringToGeometryConverter}}"/>
</DrawingGroup> </DrawingGroup>
</DrawingImage.Drawing> </DrawingImage.Drawing>
</DrawingImage> </DrawingImage>
</Image.Source> </Image.Source>
</Image> </Image>
<TextBlock Text="{i18n:I18n Key=Board_Screenshot}" FontSize="10" <TextBlock Text="{i18n:I18n Key=Board_Screenshot}" FontSize="10"
Foreground="{DynamicResource TextForeground}" VerticalAlignment="Center"/> Foreground="{DynamicResource TextForeground}" VerticalAlignment="Center"/>
</ikw:SimpleStackPanel> </ikw:SimpleStackPanel>
</Border> </Border>
<!-- Select Image Option --> <!-- Select Image Option -->
<Border MouseDown="Border_MouseDown" MouseUp="ImageOptionSelectFile_MouseUp" <Border MouseDown="Border_MouseDown" MouseUp="ImageOptionSelectFile_MouseUp"
Background="Transparent" CornerRadius="3" Padding="6,4"> Background="Transparent" CornerRadius="3" Padding="6,4">
<Border.Style> <Border.Style>
<Style TargetType="Border"> <Style TargetType="Border">
<Setter Property="Background" Value="Transparent"/> <Setter Property="Background" Value="Transparent"/>
<Style.Triggers> <Style.Triggers>
<Trigger Property="IsMouseOver" Value="True"> <Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#f0f9ff"/> <Setter Property="Background" Value="#f0f9ff"/>
</Trigger> </Trigger>
</Style.Triggers> </Style.Triggers>
</Style> </Style>
</Border.Style> </Border.Style>
<ikw:SimpleStackPanel Orientation="Horizontal" Spacing="6"> <ikw:SimpleStackPanel Orientation="Horizontal" Spacing="6">
<Image Height="16" Width="16"> <Image Height="16" Width="16">
<Image.Source> <Image.Source>
<DrawingImage> <DrawingImage>
<DrawingImage.Drawing> <DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z"> <DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
<GeometryDrawing Brush="#2563eb" <GeometryDrawing Brush="#2563eb"
Geometry="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"/> Geometry="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"/>
</DrawingGroup> </DrawingGroup>
</DrawingImage.Drawing> </DrawingImage.Drawing>
</DrawingImage> </DrawingImage>
</Image.Source> </Image.Source>
</Image> </Image>
<TextBlock Text="{i18n:I18n Key=Board_SelectImage}" FontSize="10" <TextBlock Text="{i18n:I18n Key=Board_SelectImage}" FontSize="10"
Foreground="{DynamicResource TextForeground}" VerticalAlignment="Center"/> Foreground="{DynamicResource TextForeground}" VerticalAlignment="Center"/>
</ikw:SimpleStackPanel> </ikw:SimpleStackPanel>
</Border> </Border>
</ikw:SimpleStackPanel> </ikw:SimpleStackPanel>
</localControls:BoardMenuFrame> </localControls:BoardMenuFrame>
<controls:BoardToolbarButton x:Name="BoardUndo" <controls:BoardToolbarButton x:Name="BoardUndo"
Label="{i18n:I18n Key=Board_Undo}" Label="{i18n:I18n Key=Board_Undo}"
IconGeometry="F1 M24,24z M0,0z M8.71408,16.8493L0.874451,9.00964 8.71408,1.17001 8.71408,7.42358 15.7239,7.42358C16.7074,7.42358 17.6791,7.62744 18.583,8.02124 19.4866,8.41493 20.3023,8.98966 20.9857,9.70849 21.6689,10.4271 22.2069,11.276 22.5726,12.2047 22.9383,13.1333 23.1256,14.126 23.1256,15.1268 23.1256,16.1276 22.9383,17.1203 22.5726,18.0489 22.2069,18.9776 21.6689,19.8264 20.9857,20.5451 20.3023,21.2639 19.4866,21.8387 18.583,22.2324 17.6791,22.6262 16.7074,22.83 15.7239,22.83L10.437,22.83 10.437,19.6579 15.7239,19.6579C16.2679,19.6579 16.8086,19.5453 17.3159,19.3243 17.8235,19.1031 18.29,18.7767 18.6867,18.3594 19.0835,17.942 19.4023,17.4422 19.6211,16.8866 19.8399,16.3308 19.9534,15.7326 19.9534,15.1268 19.9534,14.5209 19.8399,13.9227 19.6211,13.367 19.4023,12.8114 19.0835,12.3115 18.6867,11.8941 18.29,11.4769 17.8235,11.1505 17.3159,10.9293 16.8086,10.7083 16.2679,10.5957 15.7239,10.5957L8.71408,10.5957 8.71408,16.8493z" IconGeometry="F1 M24,24z M0,0z M8.71408,16.8493L0.874451,9.00964 8.71408,1.17001 8.71408,7.42358 15.7239,7.42358C16.7074,7.42358 17.6791,7.62744 18.583,8.02124 19.4866,8.41493 20.3023,8.98966 20.9857,9.70849 21.6689,10.4271 22.2069,11.276 22.5726,12.2047 22.9383,13.1333 23.1256,14.126 23.1256,15.1268 23.1256,16.1276 22.9383,17.1203 22.5726,18.0489 22.2069,18.9776 21.6689,19.8264 20.9857,20.5451 20.3023,21.2639 19.4866,21.8387 18.583,22.2324 17.6791,22.6262 16.7074,22.83 15.7239,22.83L10.437,22.83 10.437,19.6579 15.7239,19.6579C16.2679,19.6579 16.8086,19.5453 17.3159,19.3243 17.8235,19.1031 18.29,18.7767 18.6867,18.3594 19.0835,17.942 19.4023,17.4422 19.6211,16.8866 19.8399,16.3308 19.9534,15.7326 19.9534,15.1268 19.9534,14.5209 19.8399,13.9227 19.6211,13.367 19.4023,12.8114 19.0835,12.3115 18.6867,11.8941 18.29,11.4769 17.8235,11.1505 17.3159,10.9293 16.8086,10.7083 16.2679,10.5957 15.7239,10.5957L8.71408,10.5957 8.71408,16.8493z"
@@ -2793,7 +2793,7 @@
</ikw:SimpleStackPanel> </ikw:SimpleStackPanel>
<!-- 快捷调色盘 - 双行显示模式 --> <!-- 快捷调色盘 - 双行显示模式 -->
<ikw:SimpleStackPanel Name="QuickColorPalettePanel" <ikw:SimpleStackPanel Name="QuickColorPalettePanel"
Visibility="Collapsed" Visibility="Collapsed" d:Visibility="Visible"
Orientation="Vertical" Orientation="Vertical"
HorizontalAlignment="Center" HorizontalAlignment="Center"
Margin="4,0,4,0" Margin="4,0,4,0"
@@ -2829,7 +2829,7 @@
<!-- 快捷调色盘 - 单行显示模式 --> <!-- 快捷调色盘 - 单行显示模式 -->
<ikw:SimpleStackPanel Name="QuickColorPaletteSingleRowPanel" <ikw:SimpleStackPanel Name="QuickColorPaletteSingleRowPanel"
Visibility="Collapsed" Visibility="Collapsed" d:Visibility="Visible"
Orientation="Horizontal" Orientation="Horizontal"
HorizontalAlignment="Center" HorizontalAlignment="Center"
Margin="4,0,4,0" Margin="4,0,4,0"
+2 -17
View File
@@ -1,4 +1,5 @@
using iNKORE.UI.WPF.Modern; using iNKORE.UI.WPF.Modern;
using Ink_Canvas.Helpers;
using Microsoft.Win32; using Microsoft.Win32;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -246,27 +247,11 @@ namespace Ink_Canvas
SetTheme(ThemeDark); SetTheme(ThemeDark);
break; break;
case 2: case 2:
SetTheme(IsSystemThemeLight() ? ThemeLight : ThemeDark); SetTheme(ThemeHelper.IsSystemThemeLightLegacy() ? ThemeLight : ThemeDark);
break; break;
} }
} }
private bool IsSystemThemeLight()
{
try
{
var registryKey = Registry.CurrentUser;
var themeKey = registryKey.OpenSubKey("software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
if (themeKey != null)
{
int keyValue = (int)themeKey.GetValue("SystemUsesLightTheme");
return keyValue == 1;
}
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
return false;
}
private void AutoSwitchFloatingBarIconForTheme(string theme) private void AutoSwitchFloatingBarIconForTheme(string theme)
{ {
try try
@@ -364,34 +364,14 @@ namespace Ink_Canvas.Windows
{ {
try try
{ {
if (settings.Appearance.Theme == 0) // 浅色主题 ThemeHelper.ApplyTheme(this, settings, theme =>
{ {
ThemeManager.SetRequestedTheme(this, ElementTheme.Light); if (theme == "Dark") SetDarkThemeBorder();
} if (parentControl != null)
else if (settings.Appearance.Theme == 1) // 深色主题
{
ThemeManager.SetRequestedTheme(this, ElementTheme.Dark);
SetDarkThemeBorder();
}
else // 跟随系统主题
{
bool isSystemLight = IsSystemThemeLight();
if (isSystemLight)
{ {
ThemeManager.SetRequestedTheme(this, ElementTheme.Light); UpdateTimeDisplay();
} }
else });
{
ThemeManager.SetRequestedTheme(this, ElementTheme.Dark);
SetDarkThemeBorder();
}
}
// 刷新数字和冒号显示的颜色
if (parentControl != null)
{
UpdateTimeDisplay();
}
} }
catch (Exception ex) 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() private bool IsLightTheme()
{ {
try try
+1 -55
View File
@@ -69,37 +69,7 @@ namespace Ink_Canvas
private void ApplyTheme(Settings settings) private void ApplyTheme(Settings settings)
{ {
try ThemeHelper.ApplyTheme(this, settings, ApplyThemeResources);
{
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);
}
} }
private void ApplyThemeResources(string theme) 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;
}
} }
} }
@@ -1,4 +1,4 @@
using Ink_Canvas.Helpers; using Ink_Canvas.Helpers;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -333,39 +333,11 @@ namespace Ink_Canvas
private void ApplyTheme(Settings settings) private void ApplyTheme(Settings settings)
{ {
try ThemeHelper.ApplyTheme(this, settings, theme =>
{ {
if (settings.Appearance.Theme == 0) // 浅色主题 ApplyThemeResources(theme);
{ if (theme == "Dark") SetDarkThemeBorder();
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);
}
} }
/// <summary> /// <summary>
@@ -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 #endregion
#region #region
@@ -1,5 +1,4 @@
using Ink_Canvas.Helpers; using Ink_Canvas.Helpers;
using iNKORE.UI.WPF.Modern;
using System; using System;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
@@ -23,52 +22,16 @@ namespace Ink_Canvas
e.Handled = true; e.Handled = true;
} }
/// <summary>
/// 刷新主题
/// </summary>
public void RefreshTheme() public void RefreshTheme()
{ {
try try
{ {
// 根据当前主题设置窗口主题 ThemeHelper.ApplyTheme(this, MainWindow.Settings);
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
InvalidateVisual(); InvalidateVisual();
} }
catch (Exception) catch (Exception)
{ {
} }
} }
/// <summary>
/// 检查系统主题是否为浅色
/// </summary>
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;
}
} }
} }
+2 -40
View File
@@ -1835,21 +1835,8 @@ namespace Ink_Canvas.Windows
{ {
try try
{ {
bool isDarkTheme = false; bool isDarkTheme = settings.Appearance.Theme == 1 ||
(settings.Appearance.Theme == 2 && !ThemeHelper.IsSystemThemeLight());
if (settings.Appearance.Theme == 0) // 浅色主题
{
isDarkTheme = false;
}
else if (settings.Appearance.Theme == 1) // 深色主题
{
isDarkTheme = true;
}
else // 跟随系统主题
{
bool isSystemLight = IsSystemThemeLight();
isDarkTheme = !isSystemLight;
}
if (isDarkTheme) 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 #endregion
#region #region
+2 -40
View File
@@ -819,21 +819,8 @@ namespace Ink_Canvas.Windows
{ {
try try
{ {
bool isDarkTheme = false; bool isDarkTheme = settings.Appearance.Theme == 1 ||
(settings.Appearance.Theme == 2 && !ThemeHelper.IsSystemThemeLight());
if (settings.Appearance.Theme == 0) // 浅色主题
{
isDarkTheme = false;
}
else if (settings.Appearance.Theme == 1) // 深色主题
{
isDarkTheme = true;
}
else // 跟随系统主题
{
bool isSystemLight = IsSystemThemeLight();
isDarkTheme = !isSystemLight;
}
if (isDarkTheme) 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;
}
/// <summary> /// <summary>
/// 获取当前倒计时状态 /// 获取当前倒计时状态
/// </summary> /// </summary>
+2 -46
View File
@@ -79,60 +79,16 @@ namespace Ink_Canvas
private void ApplyTheme(Settings settings) 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 (settings.RandSettings.SelectedBackgroundIndex <= 0)
{ {
// 没有自定义背景时,使用主题背景色
if (Application.Current.FindResource("RandWindowBackground") is SolidColorBrush backgroundBrush) if (Application.Current.FindResource("RandWindowBackground") is SolidColorBrush backgroundBrush)
{ {
MainBorder.Background = 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) public RandWindow(Settings settings, bool IsAutoClose)
@@ -145,37 +145,7 @@ namespace Ink_Canvas
private void ApplyTheme(Settings settings) private void ApplyTheme(Settings settings)
{ {
try ThemeHelper.ApplyTheme(this, settings, ApplyThemeResources);
{
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);
}
} }
private void ApplyThemeResources(string theme) 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;
}
} }
} }
+3 -55
View File
@@ -346,63 +346,11 @@ namespace Ink_Canvas.Windows
private void ApplyTheme(Settings settings) private void ApplyTheme(Settings settings)
{ {
try ThemeHelper.ApplyTheme(this, settings, theme =>
{ {
if (settings.Appearance.Theme == 0) // 浅色主题 if (theme == "Dark") SetDarkThemeBorder();
{
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();
}
}
// 刷新数字和冒号显示的颜色
UpdateDigitDisplays(); 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() private void UpdateDigitDisplays()