Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
# Get package version
- name: Get Package Version
run: |
$version = [system.diagnostics.fileversioninfo]::getversioninfo("ContextMenuManager\bin\Release\net10.0-windows10.0.19041.0\ContextMenuManager.dll").fileversion
$version = [system.diagnostics.fileversioninfo]::getversioninfo("ContextMenuManager\bin\Release\ContextMenuManager.dll").fileversion
echo "release_version=$version" | out-file -filepath $env:github_env -encoding utf-8 -append

# Publish the project
Expand Down
2 changes: 1 addition & 1 deletion ContextMenuManager/App.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Application
<Application
x:Class="ContextMenuManager.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down
51 changes: 50 additions & 1 deletion ContextMenuManager/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ContextMenuManager.Methods;
using ContextMenuManager.Methods;
using iNKORE.UI.WPF.Modern;
using iNKORE.UI.WPF.Modern.Common;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Text;
Expand All @@ -25,6 +27,12 @@ protected override void OnStartup(StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnMainWindowClose;

// 初始化主题管理器,根据系统主题设置应用主题
UpdateApplicationTheme();

// 监听系统主题变化
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;

RegisterAppDomainExceptions();
RegisterDispatcherUnhandledException();

Expand All @@ -39,6 +47,20 @@ protected override void OnStartup(StartupEventArgs e)
Updater.PeriodicUpdate();
}

private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category == UserPreferenceCategory.General)
{
UpdateApplicationTheme();
}
}

private static void UpdateApplicationTheme()
{
var isDarkMode = IsSystemDarkModeEnabled();
ThemeManager.Current.ApplicationTheme = isDarkMode ? ApplicationTheme.Dark : ApplicationTheme.Light;
}

private void RegisterExitEvents()
{
AppDomain.CurrentDomain.ProcessExit += (s, e) =>
Expand Down Expand Up @@ -93,6 +115,9 @@ protected virtual void Dispose(bool disposing)
_disposed = true;
}

// 取消注册系统主题变化监听器
SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;

AppConfig.CleanDirectory();
}

Expand All @@ -108,5 +133,29 @@ public void OnSecondAppStarted()
Current?.MainWindow.Show();
Current?.MainWindow.Focus();
}

/// <summary>
/// 检测系统是否启用深色模式
/// </summary>
private static bool IsSystemDarkModeEnabled()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
if (key != null)
{
var value = key.GetValue("AppsUseLightTheme");
if (value is int intValue)
{
return intValue == 0; // 0 = Dark mode, 1 = Light mode
}
}
}
catch
{
// 如果读取注册表失败,默认使用浅色模式
}
return false; // 默认浅色模式
}
}
}
7 changes: 4 additions & 3 deletions ContextMenuManager/ContextMenuManager.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
<TargetFramework>net10.0-windows</TargetFramework>
<OutputType>WinExe</OutputType>
<UseWPF>true</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
Expand All @@ -11,6 +11,7 @@
<NoWarn>$(NoWarn);WFO1000;CA1416</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down Expand Up @@ -55,7 +56,7 @@

<Copy
SourceFiles="$(ProjectDir)Properties\Resources\Texts\AppLanguageDic.ini"
DestinationFiles="$(SolutionDir)Languages\zh-CN.ini"
DestinationFiles="$(TargetDir)Config\Languages\zh-CN.ini"
OverwriteReadOnlyFiles="true" />

<ItemGroup>
Expand All @@ -77,4 +78,4 @@
<Delete Files="$(TargetDir)Config\Dictionaries\Web\AppLanguageDic.ini" />
</Target>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
<TargetFramework>net10.0-windows</TargetFramework>
<PublishDir>bin\Publish\</PublishDir>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
Expand Down
Loading