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
This commit is contained in:
Sline
2025-12-27 17:10:45 +08:00
committed by GitHub
parent faa3a75660
commit e196538f98
3 changed files with 38 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"tao": patch
---
fix(windows): respect default app mode via registry in dark mode detection

View File

@@ -79,6 +79,7 @@ windows-core = "0.61"
"Win32_System_LibraryLoader",
"Win32_System_Memory",
"Win32_System_Ole",
"Win32_System_Registry",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",

View File

@@ -9,9 +9,12 @@ use once_cell::sync::Lazy;
use windows::{
core::{s, w, BOOL, PCSTR, PSTR},
Win32::{
Foundation::{HANDLE, HMODULE, HWND, LPARAM, WPARAM},
Foundation::{ERROR_SUCCESS, HANDLE, HMODULE, HWND, LPARAM, WPARAM},
Graphics::Dwm::{DwmSetWindowAttribute, DWMWINDOWATTRIBUTE},
System::LibraryLoader::*,
System::{
LibraryLoader::*,
Registry::{RegGetValueW, HKEY_CURRENT_USER, RRF_RT_REG_DWORD},
},
UI::{Accessibility::*, Input::KeyboardAndMouse::GetActiveWindow, WindowsAndMessaging::*},
},
};
@@ -199,6 +202,12 @@ fn should_use_dark_mode() -> bool {
}
fn should_apps_use_dark_mode() -> bool {
if let Some(apps_use_light_theme) = read_apps_use_light_theme() {
return !apps_use_light_theme;
}
// This undocumented method `ShouldAppsUseDarkMode` may return
// incorrect values on Windows 11.
// See https://github.com/tauri-apps/tao/pull/1165
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: u16 = 132;
type ShouldAppsUseDarkMode = unsafe extern "system" fn() -> bool;
static SHOULD_APPS_USE_DARK_MODE: Lazy<Option<ShouldAppsUseDarkMode>> = Lazy::new(|| unsafe {
@@ -218,6 +227,27 @@ fn should_apps_use_dark_mode() -> bool {
.unwrap_or(false)
}
fn read_apps_use_light_theme() -> Option<bool> {
let mut data: u32 = 0;
let mut data_size = std::mem::size_of::<u32>() as u32;
let status = unsafe {
RegGetValueW(
HKEY_CURRENT_USER,
w!(r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"),
w!("AppsUseLightTheme"),
RRF_RT_REG_DWORD,
None,
Some(&mut data as *mut _ as _),
Some(&mut data_size),
)
};
if status == ERROR_SUCCESS {
Some(data != 0)
} else {
None
}
}
fn is_high_contrast() -> bool {
const HCF_HIGHCONTRASTON: u32 = 1;