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 winit-win32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ windows-sys = { workspace = true, features = [
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
"Win32_System_Registry",
"Win32_UI_Accessibility",
"Win32_UI_Controls",
"Win32_UI_HiDpi",
Expand Down
28 changes: 27 additions & 1 deletion winit-win32/src/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::sync::LazyLock;
/// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode
use std::{ffi::c_void, ptr};

use windows_sys::Win32::Foundation::{BOOL, HWND, LPARAM, S_OK, WPARAM};
use windows_sys::Win32::Foundation::{BOOL, ERROR_SUCCESS, HWND, LPARAM, S_OK, WPARAM};
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::UI::Accessibility::{HCF_HIGHCONTRASTON, HIGHCONTRASTA};
use windows_sys::Win32::UI::Controls::SetWindowTheme;
use windows_sys::Win32::UI::Input::KeyboardAndMouse::GetActiveWindow;
Expand Down Expand Up @@ -121,6 +122,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: LazyLock<Option<ShouldAppsUseDarkMode>> =
LazyLock::new(|| unsafe {
Expand Down Expand Up @@ -148,6 +156,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,
w!(r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"),
w!("AppsUseLightTheme"),
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
1 change: 1 addition & 0 deletions winit/src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ changelog entry.

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