Skip to content

Commit e196538

Browse files
authored
fix(windows): use registry value to detect dark mode (#1165)
* fix(windows): respect default app mode via registry in dark mode detection * chore(windows): document Win11 issue with ShouldAppsUseDarkMode
1 parent faa3a75 commit e196538

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

.changes/fix-windows-dark-mode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tao": patch
3+
---
4+
5+
fix(windows): respect default app mode via registry in dark mode detection

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ windows-core = "0.61"
7979
"Win32_System_LibraryLoader",
8080
"Win32_System_Memory",
8181
"Win32_System_Ole",
82+
"Win32_System_Registry",
8283
"Win32_System_SystemServices",
8384
"Win32_System_Threading",
8485
"Win32_System_WindowsProgramming",

src/platform_impl/windows/dark_mode.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ use once_cell::sync::Lazy;
99
use windows::{
1010
core::{s, w, BOOL, PCSTR, PSTR},
1111
Win32::{
12-
Foundation::{HANDLE, HMODULE, HWND, LPARAM, WPARAM},
12+
Foundation::{ERROR_SUCCESS, HANDLE, HMODULE, HWND, LPARAM, WPARAM},
1313
Graphics::Dwm::{DwmSetWindowAttribute, DWMWINDOWATTRIBUTE},
14-
System::LibraryLoader::*,
14+
System::{
15+
LibraryLoader::*,
16+
Registry::{RegGetValueW, HKEY_CURRENT_USER, RRF_RT_REG_DWORD},
17+
},
1518
UI::{Accessibility::*, Input::KeyboardAndMouse::GetActiveWindow, WindowsAndMessaging::*},
1619
},
1720
};
@@ -199,6 +202,12 @@ fn should_use_dark_mode() -> bool {
199202
}
200203

201204
fn should_apps_use_dark_mode() -> bool {
205+
if let Some(apps_use_light_theme) = read_apps_use_light_theme() {
206+
return !apps_use_light_theme;
207+
}
208+
// This undocumented method `ShouldAppsUseDarkMode` may return
209+
// incorrect values on Windows 11.
210+
// See https://github.com/tauri-apps/tao/pull/1165
202211
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: u16 = 132;
203212
type ShouldAppsUseDarkMode = unsafe extern "system" fn() -> bool;
204213
static SHOULD_APPS_USE_DARK_MODE: Lazy<Option<ShouldAppsUseDarkMode>> = Lazy::new(|| unsafe {
@@ -218,6 +227,27 @@ fn should_apps_use_dark_mode() -> bool {
218227
.unwrap_or(false)
219228
}
220229

230+
fn read_apps_use_light_theme() -> Option<bool> {
231+
let mut data: u32 = 0;
232+
let mut data_size = std::mem::size_of::<u32>() as u32;
233+
let status = unsafe {
234+
RegGetValueW(
235+
HKEY_CURRENT_USER,
236+
w!(r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"),
237+
w!("AppsUseLightTheme"),
238+
RRF_RT_REG_DWORD,
239+
None,
240+
Some(&mut data as *mut _ as _),
241+
Some(&mut data_size),
242+
)
243+
};
244+
if status == ERROR_SUCCESS {
245+
Some(data != 0)
246+
} else {
247+
None
248+
}
249+
}
250+
221251
fn is_high_contrast() -> bool {
222252
const HCF_HIGHCONTRASTON: u32 = 1;
223253

0 commit comments

Comments
 (0)