Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ features = [
"Win32_System_LibraryLoader",
"Win32_System_Ole",
"Win32_Security",
"Win32_System_Registry",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
Expand Down
31 changes: 30 additions & 1 deletion src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::{ffi::c_void, ptr};

use crate::utils::Lazy;
use windows_sys::core::PCSTR;
use windows_sys::Win32::Foundation::{BOOL, HWND, NTSTATUS, S_OK};
use windows_sys::Win32::Foundation::{BOOL, ERROR_SUCCESS, HWND, NTSTATUS, S_OK};
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
use windows_sys::Win32::System::Registry::{HKEY_CURRENT_USER, RRF_RT_REG_DWORD, RegGetValueW};
use windows_sys::Win32::System::SystemInformation::OSVERSIONINFOW;
use windows_sys::Win32::UI::Accessibility::{HCF_HIGHCONTRASTON, HIGHCONTRASTA};
use windows_sys::Win32::UI::Controls::SetWindowTheme;
Expand Down Expand Up @@ -55,6 +56,9 @@ static DARK_MODE_SUPPORTED: Lazy<bool> = Lazy::new(|| {
static DARK_THEME_NAME: Lazy<Vec<u16>> = Lazy::new(|| util::encode_wide("DarkMode_Explorer"));
static LIGHT_THEME_NAME: Lazy<Vec<u16>> = Lazy::new(|| util::encode_wide(""));

static PERSONALIZE_KEY: Lazy<Vec<u16>> = Lazy::new(|| util::encode_wide(r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"));
static APPS_USE_LIGHT_THEME: Lazy<Vec<u16>> = Lazy::new(|| util::encode_wide("AppsUseLightTheme"));

/// Attempt to set a theme on a window, if necessary.
/// Returns the theme that was picked
pub fn try_theme(hwnd: HWND, preferred_theme: Option<Theme>) -> Theme {
Expand Down Expand Up @@ -128,6 +132,13 @@ pub 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
type ShouldAppsUseDarkMode = unsafe extern "system" fn() -> bool;
static SHOULD_APPS_USE_DARK_MODE: Lazy<Option<ShouldAppsUseDarkMode>> = Lazy::new(|| unsafe {
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: PCSTR = 132 as PCSTR;
Expand All @@ -154,6 +165,24 @@ 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,
PERSONALIZE_KEY.as_ptr(),
APPS_USE_LIGHT_THEME.as_ptr(),
RRF_RT_REG_DWORD,
ptr::null_mut(),
&mut data as *mut _ as _,
&mut data_size,
)
};

if status == ERROR_SUCCESS { Some(data != 0) } else { None }
}

fn is_high_contrast() -> bool {
let mut hc = HIGHCONTRASTA { cbSize: 0, dwFlags: 0, lpszDefaultScheme: ptr::null_mut() };

Expand Down