Skip to content

Commit cbdfe2e

Browse files
authored
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.
1 parent 68583e2 commit cbdfe2e

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

v2rayN/v2rayN/ViewModels/ThemeSettingViewModel.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MaterialDesignColors;
22
using MaterialDesignColors.ColorManipulation;
33
using MaterialDesignThemes.Wpf;
4+
using Microsoft.Win32;
45

56
namespace v2rayN.ViewModels;
67

@@ -24,7 +25,7 @@ public ThemeSettingViewModel()
2425
{
2526
_config = AppManager.Instance.Config;
2627

27-
RegisterSystemColorSet(_config, Application.Current.MainWindow, ModifyTheme);
28+
RegisterSystemColorSet(_config, ModifyTheme);
2829

2930
BindingUI();
3031
RestoreUI();
@@ -158,25 +159,15 @@ public void ChangePrimaryColor(System.Windows.Media.Color color)
158159
_paletteHelper.SetTheme(theme);
159160
}
160161

161-
public void RegisterSystemColorSet(Config config, Window window, Action updateFunc)
162+
public static void RegisterSystemColorSet(Config config, Action updateFunc)
162163
{
163-
var helper = new WindowInteropHelper(window);
164-
var hwndSource = HwndSource.FromHwnd(helper.EnsureHandle());
165-
hwndSource.AddHook((IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
164+
SystemEvents.UserPreferenceChanged += (s, e) =>
166165
{
167-
if (config.UiItem.CurrentTheme == nameof(ETheme.FollowSystem))
166+
if ((e.Category == UserPreferenceCategory.Color || e.Category == UserPreferenceCategory.General)
167+
&& config.UiItem.CurrentTheme == nameof(ETheme.FollowSystem))
168168
{
169-
const int WM_SETTINGCHANGE = 0x001A;
170-
if (msg == WM_SETTINGCHANGE)
171-
{
172-
if (wParam == IntPtr.Zero && Marshal.PtrToStringUni(lParam) == "ImmersiveColorSet")
173-
{
174-
updateFunc?.Invoke();
175-
}
176-
}
169+
updateFunc?.Invoke();
177170
}
178-
179-
return IntPtr.Zero;
180-
});
171+
};
181172
}
182173
}

0 commit comments

Comments
 (0)