From cbb60d29a21401461e23aa86d71c733076046476 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sat, 7 Mar 2020 18:56:33 +0000 Subject: [PATCH] Remove assertions from Windows dark mode code (#1459) * Remove assertions from Windows dark mode code In general, winit should never assert on anything unless it means that it is impossible to continue the execution of the program. There are several assertions in the Windows dark mode code where this is not the case. Based on surface level inspection, all existing assertions could be easily replaced with just simple conditional checks, allowing the execution of the program to proceed with sane default values. Fixes #1458. * Add changelog entry * Format code * Pass dark mode by mutable reference * Format code * Return bool instead of mutable reference * Fix dark mode success reply Co-Authored-By: daxpedda * Fix dark mode success reply * Replace magic integers with constants Co-authored-by: daxpedda --- CHANGELOG.md | 1 + src/platform_impl/windows/dark_mode.rs | 30 +++++++++++--------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bd417c9..75d927c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - On Wayland, Hide CSD for fullscreen windows. - On Windows, ignore spurious mouse move messages. - **Breaking:** Move `ModifiersChanged` variant from `DeviceEvent` to `WindowEvent`. +- On Windows, fix crash at startup on systems that do not properly support Windows' Dark Mode # 0.21.0 (2020-02-04) diff --git a/src/platform_impl/windows/dark_mode.rs b/src/platform_impl/windows/dark_mode.rs index c50c4f21..3c7c16ee 100644 --- a/src/platform_impl/windows/dark_mode.rs +++ b/src/platform_impl/windows/dark_mode.rs @@ -6,9 +6,10 @@ use std::os::windows::ffi::OsStrExt; use winapi::{ shared::{ basetsd::SIZE_T, - minwindef::{BOOL, DWORD, UINT, ULONG, WORD}, + minwindef::{BOOL, DWORD, FALSE, UINT, ULONG, WORD}, ntdef::{LPSTR, NTSTATUS, NT_SUCCESS, PVOID, WCHAR}, windef::HWND, + winerror::S_OK, }, um::{libloaderapi, uxtheme, winuser}, }; @@ -44,9 +45,8 @@ lazy_static! { }; let status = (rtl_get_version)(&mut vi as _); - assert!(NT_SUCCESS(status)); - if vi.dwMajorVersion == 10 && vi.dwMinorVersion == 0 { + if NT_SUCCESS(status) && vi.dwMajorVersion == 10 && vi.dwMinorVersion == 0 { Some(vi.dwBuildNumber) } else { None @@ -82,22 +82,15 @@ pub fn try_dark_mode(hwnd: HWND) -> bool { LIGHT_THEME_NAME.as_ptr() }; - unsafe { - assert_eq!( - 0, - uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null()) - ); + let status = unsafe { uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null()) }; - set_dark_mode_for_window(hwnd, is_dark_mode) - } - - is_dark_mode + status == S_OK && set_dark_mode_for_window(hwnd, is_dark_mode) } else { false } } -fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) { +fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) -> bool { // Uses Windows undocumented API SetWindowCompositionAttribute, // as seen in win32-darkmode example linked at top of file. @@ -132,11 +125,12 @@ fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) { cbData: std::mem::size_of_val(&is_dark_mode_bigbool) as _, }; - assert_eq!( - 1, - set_window_composition_attribute(hwnd, &mut data as *mut _) - ); + let status = set_window_composition_attribute(hwnd, &mut data as *mut _); + + status != FALSE } + } else { + false } } @@ -205,7 +199,7 @@ fn is_high_contrast() -> bool { ) }; - (ok > 0) && ((HCF_HIGHCONTRASTON & hc.dwFlags) == 1) + ok != FALSE && (HCF_HIGHCONTRASTON & hc.dwFlags) == 1 } fn widestring(src: &'static str) -> Vec {