fix: Fix failure to follow system theme changes (#8584)

Fix the issue where the application failed to sync with system dark/light mode changes in specific scenarios such as triggering system theme switching via scheduled tasks while waking from hibernation, caused by the unreliable HWND hook implementation that missed critical events.
This commit is contained in:
Kazuto Iris 2026-01-05 09:56:33 +08:00 committed by GitHub
parent 68583e20bc
commit cbdfe2e15a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf;
using Microsoft.Win32;
namespace v2rayN.ViewModels;
@ -24,7 +25,7 @@ public class ThemeSettingViewModel : MyReactiveObject
{
_config = AppManager.Instance.Config;
RegisterSystemColorSet(_config, Application.Current.MainWindow, ModifyTheme);
RegisterSystemColorSet(_config, ModifyTheme);
BindingUI();
RestoreUI();
@ -158,25 +159,15 @@ public class ThemeSettingViewModel : MyReactiveObject
_paletteHelper.SetTheme(theme);
}
public void RegisterSystemColorSet(Config config, Window window, Action updateFunc)
public static void RegisterSystemColorSet(Config config, Action updateFunc)
{
var helper = new WindowInteropHelper(window);
var hwndSource = HwndSource.FromHwnd(helper.EnsureHandle());
hwndSource.AddHook((IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
SystemEvents.UserPreferenceChanged += (s, e) =>
{
if (config.UiItem.CurrentTheme == nameof(ETheme.FollowSystem))
if ((e.Category == UserPreferenceCategory.Color || e.Category == UserPreferenceCategory.General)
&& config.UiItem.CurrentTheme == nameof(ETheme.FollowSystem))
{
const int WM_SETTINGCHANGE = 0x001A;
if (msg == WM_SETTINGCHANGE)
{
if (wParam == IntPtr.Zero && Marshal.PtrToStringUni(lParam) == "ImmersiveColorSet")
{
updateFunc?.Invoke();
}
}
updateFunc?.Invoke();
}
return IntPtr.Zero;
});
};
}
}