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 <daxpedda@gmail.com>

* Fix dark mode success reply

* Replace magic integers with constants

Co-authored-by: daxpedda <daxpedda@gmail.com>
This commit is contained in:
Christian Duerr 2020-03-07 18:56:33 +00:00 committed by GitHub
parent e707052f66
commit cbb60d29a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 18 deletions

View file

@ -11,6 +11,7 @@
- On Wayland, Hide CSD for fullscreen windows. - On Wayland, Hide CSD for fullscreen windows.
- On Windows, ignore spurious mouse move messages. - On Windows, ignore spurious mouse move messages.
- **Breaking:** Move `ModifiersChanged` variant from `DeviceEvent` to `WindowEvent`. - **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) # 0.21.0 (2020-02-04)

View file

@ -6,9 +6,10 @@ use std::os::windows::ffi::OsStrExt;
use winapi::{ use winapi::{
shared::{ shared::{
basetsd::SIZE_T, basetsd::SIZE_T,
minwindef::{BOOL, DWORD, UINT, ULONG, WORD}, minwindef::{BOOL, DWORD, FALSE, UINT, ULONG, WORD},
ntdef::{LPSTR, NTSTATUS, NT_SUCCESS, PVOID, WCHAR}, ntdef::{LPSTR, NTSTATUS, NT_SUCCESS, PVOID, WCHAR},
windef::HWND, windef::HWND,
winerror::S_OK,
}, },
um::{libloaderapi, uxtheme, winuser}, um::{libloaderapi, uxtheme, winuser},
}; };
@ -44,9 +45,8 @@ lazy_static! {
}; };
let status = (rtl_get_version)(&mut vi as _); 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) Some(vi.dwBuildNumber)
} else { } else {
None None
@ -82,22 +82,15 @@ pub fn try_dark_mode(hwnd: HWND) -> bool {
LIGHT_THEME_NAME.as_ptr() LIGHT_THEME_NAME.as_ptr()
}; };
unsafe { let status = unsafe { uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null()) };
assert_eq!(
0,
uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null())
);
set_dark_mode_for_window(hwnd, is_dark_mode) status == S_OK && set_dark_mode_for_window(hwnd, is_dark_mode)
}
is_dark_mode
} else { } else {
false 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, // Uses Windows undocumented API SetWindowCompositionAttribute,
// as seen in win32-darkmode example linked at top of file. // 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 _, cbData: std::mem::size_of_val(&is_dark_mode_bigbool) as _,
}; };
assert_eq!( let status = set_window_composition_attribute(hwnd, &mut data as *mut _);
1,
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<u16> { fn widestring(src: &'static str) -> Vec<u16> {