From e196538f989894fcb92401fced54d7d3a65fc91a Mon Sep 17 00:00:00 2001 From: Sline Date: Sat, 27 Dec 2025 17:10:45 +0800 Subject: [PATCH] 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 --- .changes/fix-windows-dark-mode.md | 5 ++++ Cargo.toml | 1 + src/platform_impl/windows/dark_mode.rs | 34 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 .changes/fix-windows-dark-mode.md diff --git a/.changes/fix-windows-dark-mode.md b/.changes/fix-windows-dark-mode.md new file mode 100644 index 00000000..94112cc8 --- /dev/null +++ b/.changes/fix-windows-dark-mode.md @@ -0,0 +1,5 @@ +--- +"tao": patch +--- + +fix(windows): respect default app mode via registry in dark mode detection \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 1174e762..5595a68e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/src/platform_impl/windows/dark_mode.rs b/src/platform_impl/windows/dark_mode.rs index ddefdb15..2a0189aa 100644 --- a/src/platform_impl/windows/dark_mode.rs +++ b/src/platform_impl/windows/dark_mode.rs @@ -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> = Lazy::new(|| unsafe { @@ -218,6 +227,27 @@ fn should_apps_use_dark_mode() -> bool { .unwrap_or(false) } +fn read_apps_use_light_theme() -> Option { + let mut data: u32 = 0; + let mut data_size = std::mem::size_of::() 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;