From 2e6cf1a4332093806a5d277098b6573299256622 Mon Sep 17 00:00:00 2001 From: Jnschrber <115859489+Jnschrber@users.noreply.github.com> Date: Mon, 9 Dec 2024 00:00:12 +0100 Subject: [PATCH] fix(windows): fix appcrash on windows where darkmode is not supported (#1022) --- .changes/dark-mode.md | 5 ++ src/platform_impl/windows/dark_mode.rs | 90 +++++++++++++------------- 2 files changed, 51 insertions(+), 44 deletions(-) create mode 100644 .changes/dark-mode.md diff --git a/.changes/dark-mode.md b/.changes/dark-mode.md new file mode 100644 index 000000000..9f7244a24 --- /dev/null +++ b/.changes/dark-mode.md @@ -0,0 +1,5 @@ +--- +"tao": "patch" +--- + +On Windows, fix crash on older windows versions that doesn't support dark mode. diff --git a/src/platform_impl/windows/dark_mode.rs b/src/platform_impl/windows/dark_mode.rs index 620e01c29..633a89385 100644 --- a/src/platform_impl/windows/dark_mode.rs +++ b/src/platform_impl/windows/dark_mode.rs @@ -42,58 +42,60 @@ static DARK_MODE_SUPPORTED: Lazy = Lazy::new(|| { }); pub fn allow_dark_mode_for_app(is_dark_mode: bool) { - const UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL: u16 = 135; - type AllowDarkModeForApp = unsafe extern "system" fn(bool) -> bool; - static ALLOW_DARK_MODE_FOR_APP: Lazy> = Lazy::new(|| unsafe { - if HMODULE(*HUXTHEME as _).is_invalid() { - return None; - } + if *DARK_MODE_SUPPORTED { + const UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL: u16 = 135; + type AllowDarkModeForApp = unsafe extern "system" fn(bool) -> bool; + static ALLOW_DARK_MODE_FOR_APP: Lazy> = Lazy::new(|| unsafe { + if HMODULE(*HUXTHEME as _).is_invalid() { + return None; + } - GetProcAddress( - HMODULE(*HUXTHEME as _), - PCSTR::from_raw(UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL as usize as *mut _), - ) - .map(|handle| std::mem::transmute(handle)) - }); + GetProcAddress( + HMODULE(*HUXTHEME as _), + PCSTR::from_raw(UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL as usize as *mut _), + ) + .map(|handle| std::mem::transmute(handle)) + }); - #[repr(C)] - enum PreferredAppMode { - Default, - AllowDark, - // ForceDark, - // ForceLight, - // Max, - } - const UXTHEME_SETPREFERREDAPPMODE_ORDINAL: u16 = 135; - type SetPreferredAppMode = unsafe extern "system" fn(PreferredAppMode) -> PreferredAppMode; - static SET_PREFERRED_APP_MODE: Lazy> = Lazy::new(|| unsafe { - if HMODULE(*HUXTHEME as _).is_invalid() { - return None; + #[repr(C)] + enum PreferredAppMode { + Default, + AllowDark, + // ForceDark, + // ForceLight, + // Max, } + const UXTHEME_SETPREFERREDAPPMODE_ORDINAL: u16 = 135; + type SetPreferredAppMode = unsafe extern "system" fn(PreferredAppMode) -> PreferredAppMode; + static SET_PREFERRED_APP_MODE: Lazy> = Lazy::new(|| unsafe { + if HMODULE(*HUXTHEME as _).is_invalid() { + return None; + } - GetProcAddress( - HMODULE(*HUXTHEME as _), - PCSTR::from_raw(UXTHEME_SETPREFERREDAPPMODE_ORDINAL as usize as *mut _), - ) - .map(|handle| std::mem::transmute(handle)) - }); + GetProcAddress( + HMODULE(*HUXTHEME as _), + PCSTR::from_raw(UXTHEME_SETPREFERREDAPPMODE_ORDINAL as usize as *mut _), + ) + .map(|handle| std::mem::transmute(handle)) + }); - if let Some(ver) = *WIN10_BUILD_VERSION { - if ver < 18362 { - if let Some(_allow_dark_mode_for_app) = *ALLOW_DARK_MODE_FOR_APP { - unsafe { _allow_dark_mode_for_app(is_dark_mode) }; + if let Some(ver) = *WIN10_BUILD_VERSION { + if ver < 18362 { + if let Some(_allow_dark_mode_for_app) = *ALLOW_DARK_MODE_FOR_APP { + unsafe { _allow_dark_mode_for_app(is_dark_mode) }; + } + } else if let Some(_set_preferred_app_mode) = *SET_PREFERRED_APP_MODE { + let mode = if is_dark_mode { + PreferredAppMode::AllowDark + } else { + PreferredAppMode::Default + }; + unsafe { _set_preferred_app_mode(mode) }; } - } else if let Some(_set_preferred_app_mode) = *SET_PREFERRED_APP_MODE { - let mode = if is_dark_mode { - PreferredAppMode::AllowDark - } else { - PreferredAppMode::Default - }; - unsafe { _set_preferred_app_mode(mode) }; } - } - refresh_immersive_color_policy_state(); + refresh_immersive_color_policy_state(); + } } fn refresh_immersive_color_policy_state() {