Skip to content

Commit da04d8e

Browse files
committed
fix(windows): use registry value to detect dark mode
1 parent da62200 commit da04d8e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

winit-win32/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ windows-sys = { workspace = true, features = [
3939
"Win32_System_SystemServices",
4040
"Win32_System_Threading",
4141
"Win32_System_WindowsProgramming",
42+
"Win32_System_Registry",
4243
"Win32_UI_Accessibility",
4344
"Win32_UI_Controls",
4445
"Win32_UI_HiDpi",

winit-win32/src/dark_mode.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::sync::LazyLock;
33
/// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode
44
use std::{ffi::c_void, ptr};
55

6-
use windows_sys::Win32::Foundation::{BOOL, HWND, LPARAM, S_OK, WPARAM};
6+
use windows_sys::Win32::Foundation::{BOOL, ERROR_SUCCESS, HWND, LPARAM, S_OK, WPARAM};
77
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
8+
use windows_sys::Win32::System::Registry::{HKEY_CURRENT_USER, RRF_RT_REG_DWORD, RegGetValueW};
89
use windows_sys::Win32::UI::Accessibility::{HCF_HIGHCONTRASTON, HIGHCONTRASTA};
910
use windows_sys::Win32::UI::Controls::SetWindowTheme;
1011
use windows_sys::Win32::UI::Input::KeyboardAndMouse::GetActiveWindow;
@@ -121,6 +122,13 @@ pub fn should_use_dark_mode() -> bool {
121122
}
122123

123124
fn should_apps_use_dark_mode() -> bool {
125+
if let Some(apps_use_light_theme) = read_apps_use_light_theme() {
126+
return !apps_use_light_theme;
127+
}
128+
129+
// This undocumented method `ShouldAppsUseDarkMode` may return
130+
// incorrect values on Windows 11.
131+
// See https://github.com/tauri-apps/tao/pull/1165
124132
type ShouldAppsUseDarkMode = unsafe extern "system" fn() -> bool;
125133
static SHOULD_APPS_USE_DARK_MODE: LazyLock<Option<ShouldAppsUseDarkMode>> =
126134
LazyLock::new(|| unsafe {
@@ -148,6 +156,24 @@ fn should_apps_use_dark_mode() -> bool {
148156
.unwrap_or(false)
149157
}
150158

159+
fn read_apps_use_light_theme() -> Option<bool> {
160+
let mut data: u32 = 0;
161+
let mut data_size = std::mem::size_of::<u32>() as u32;
162+
let status = unsafe {
163+
RegGetValueW(
164+
HKEY_CURRENT_USER,
165+
w!(r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"),
166+
w!("AppsUseLightTheme"),
167+
RRF_RT_REG_DWORD,
168+
ptr::null_mut(),
169+
&mut data as *mut _ as _,
170+
&mut data_size,
171+
)
172+
};
173+
174+
if status == ERROR_SUCCESS { Some(data != 0) } else { None }
175+
}
176+
151177
fn is_high_contrast() -> bool {
152178
let mut hc = HIGHCONTRASTA { cbSize: 0, dwFlags: 0, lpszDefaultScheme: ptr::null_mut() };
153179

winit/src/changelog/unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ changelog entry.
4848

4949
- On X11, fix `set_hittest` not working on some window managers.
5050
- On Redox, handle `EINTR` when reading from `event_socket` instead of panicking.
51+
- On Windows, fix Dark/light theme detection sometimes isn't working.

0 commit comments

Comments
 (0)