diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bd417c95e..75d927c6e8 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 c50c4f21af..3c7c16eebf 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()) - ); - - set_dark_mode_for_window(hwnd, is_dark_mode) - } + let status = unsafe { uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null()) }; - 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 {