Skip to content

Commit cbb60d2

Browse files
chrisduerrdaxpedda
andauthored
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>
1 parent e707052 commit cbb60d2

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- On Wayland, Hide CSD for fullscreen windows.
1212
- On Windows, ignore spurious mouse move messages.
1313
- **Breaking:** Move `ModifiersChanged` variant from `DeviceEvent` to `WindowEvent`.
14+
- On Windows, fix crash at startup on systems that do not properly support Windows' Dark Mode
1415

1516
# 0.21.0 (2020-02-04)
1617

src/platform_impl/windows/dark_mode.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use std::os::windows::ffi::OsStrExt;
66
use winapi::{
77
shared::{
88
basetsd::SIZE_T,
9-
minwindef::{BOOL, DWORD, UINT, ULONG, WORD},
9+
minwindef::{BOOL, DWORD, FALSE, UINT, ULONG, WORD},
1010
ntdef::{LPSTR, NTSTATUS, NT_SUCCESS, PVOID, WCHAR},
1111
windef::HWND,
12+
winerror::S_OK,
1213
},
1314
um::{libloaderapi, uxtheme, winuser},
1415
};
@@ -44,9 +45,8 @@ lazy_static! {
4445
};
4546

4647
let status = (rtl_get_version)(&mut vi as _);
47-
assert!(NT_SUCCESS(status));
4848

49-
if vi.dwMajorVersion == 10 && vi.dwMinorVersion == 0 {
49+
if NT_SUCCESS(status) && vi.dwMajorVersion == 10 && vi.dwMinorVersion == 0 {
5050
Some(vi.dwBuildNumber)
5151
} else {
5252
None
@@ -82,22 +82,15 @@ pub fn try_dark_mode(hwnd: HWND) -> bool {
8282
LIGHT_THEME_NAME.as_ptr()
8383
};
8484

85-
unsafe {
86-
assert_eq!(
87-
0,
88-
uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null())
89-
);
90-
91-
set_dark_mode_for_window(hwnd, is_dark_mode)
92-
}
85+
let status = unsafe { uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null()) };
9386

94-
is_dark_mode
87+
status == S_OK && set_dark_mode_for_window(hwnd, is_dark_mode)
9588
} else {
9689
false
9790
}
9891
}
9992

100-
fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) {
93+
fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) -> bool {
10194
// Uses Windows undocumented API SetWindowCompositionAttribute,
10295
// as seen in win32-darkmode example linked at top of file.
10396

@@ -132,11 +125,12 @@ fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) {
132125
cbData: std::mem::size_of_val(&is_dark_mode_bigbool) as _,
133126
};
134127

135-
assert_eq!(
136-
1,
137-
set_window_composition_attribute(hwnd, &mut data as *mut _)
138-
);
128+
let status = set_window_composition_attribute(hwnd, &mut data as *mut _);
129+
130+
status != FALSE
139131
}
132+
} else {
133+
false
140134
}
141135
}
142136

@@ -205,7 +199,7 @@ fn is_high_contrast() -> bool {
205199
)
206200
};
207201

208-
(ok > 0) && ((HCF_HIGHCONTRASTON & hc.dwFlags) == 1)
202+
ok != FALSE && (HCF_HIGHCONTRASTON & hc.dwFlags) == 1
209203
}
210204

211205
fn widestring(src: &'static str) -> Vec<u16> {

0 commit comments

Comments
 (0)