Skip to content

Commit 93787f8

Browse files
fix(desktop): add win version checks for DWM attr api (hoppscotch#5010)
fix: add win version checks for DWM attr api This adds version checking before using Windows 11-specific DWM APIs. Closes HFE-821 The desktop app crashes on startup on older Windows versions (pre-Windows 11) due to unsupported DWM API calls for dark mode and caption styling. According to docs at https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute, both `DWMWA_USE_IMMERSIVE_DARK_MODE` and `DWMWA_CAPTION_COLOR` attributes are only supported starting with Windows 11 Build 22000. > DWMWA_USE_IMMERSIVE_DARK_MODE: [...] This value is supported starting > with Windows 11 Build 22000" and > DWMWA_CAPTION_COLOR: [...] This value is supported starting > with Windows 11 Build 22000. See hoppscotch#4984 for more details, for reports of app crashing immediately on startup with these errors: ``` Failed to set dark mode: Error { code: HRESULT(0x80070057), message: "The parameter is incorrect." } note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace [0417/150158.530:ERROR:window_impl.cc(122)] Failed to unregister class Chrome_WidgetWin_0. Error = 1412 ``` The tests were all over the place, both attributes (sometimes!) seems to be present on Windows 10 1809 and even earlier, only if it was installed with network access, so perhaps this is due to Windows updates? Other times, they weren't, especially on VMs. The issue is reproducible on Windows Server 2019 Datacenter (v10.0.17763), which is equivalent to Windows 10 version 1809. This version is too old to support DWMWA_USE_IMMERSIVE_DARK_MODE, which is only **officially supported** starting with Windows 11 Build 22000 according to Microsoft's documentation. So at the moment, relying on official docs seems to be the right call, and themes are definitely something app shouldn't crash for regardless.
1 parent 1867f23 commit 93787f8

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

packages/hoppscotch-desktop/plugin-workspace/tauri-plugin-appload/src/ui/windows/posit.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ use windows::Win32::{
1212
};
1313
use winver::WindowsVersion;
1414

15+
// Windows 11 Build 22000 is the minimum version required for
16+
// DWMWA_USE_IMMERSIVE_DARK_MODE and DWMWA_CAPTION_COLOR.
17+
//
18+
// According to Microsoft documentation:
19+
// https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
20+
//
21+
// "DWMWA_USE_IMMERSIVE_DARK_MODE: Use with DwmSetWindowAttribute.
22+
// [...] This value is supported starting with Windows 11 Build 22000."
23+
//
24+
// "DWMWA_CAPTION_COLOR: Use with DwmSetWindowAttribute.
25+
// [...] This value is supported starting with Windows 11 Build 22000."
26+
const MIN_WIN11_BUILD: u32 = 22000;
27+
1528
#[derive(Debug)]
1629
pub struct WindowsWindow<R: Runtime> {
1730
window: WebviewWindow<R>,
@@ -51,30 +64,32 @@ impl<R: Runtime> WindowsWindow<R> {
5164
}
5265

5366
fn set_dark_mode(&self) {
54-
unsafe {
55-
let use_dark_mode = BOOL::from(true);
56-
DwmSetWindowAttribute(
57-
self.hwnd,
58-
DWMWA_USE_IMMERSIVE_DARK_MODE,
59-
ptr::addr_of!(use_dark_mode) as *const c_void,
60-
size_of::<BOOL>().try_into().unwrap(),
61-
)
62-
.expect("Failed to set dark mode");
67+
if let Some(version) = WindowsVersion::detect() {
68+
if version.major() >= 10 && version.build() >= MIN_WIN11_BUILD {
69+
unsafe {
70+
let use_dark_mode = BOOL::from(true);
71+
let _ = DwmSetWindowAttribute(
72+
self.hwnd,
73+
DWMWA_USE_IMMERSIVE_DARK_MODE,
74+
ptr::addr_of!(use_dark_mode) as *const c_void,
75+
size_of::<BOOL>().try_into().unwrap(),
76+
);
77+
}
78+
}
6379
}
6480
}
6581

6682
fn set_caption_color(&self, color: HexColor) {
6783
if let Some(version) = WindowsVersion::detect() {
68-
if version >= WindowsVersion::new(10, 0, 22000) {
84+
if version.major() >= 10 && version.build() >= MIN_WIN11_BUILD {
6985
unsafe {
7086
let color_ref = self.hex_color_to_colorref(color);
71-
DwmSetWindowAttribute(
87+
let _ = DwmSetWindowAttribute(
7288
self.hwnd,
7389
DWMWA_CAPTION_COLOR,
7490
ptr::addr_of!(color_ref) as *const c_void,
7591
size_of::<COLORREF>().try_into().unwrap(),
76-
)
77-
.expect("Failed to set caption color");
92+
);
7893
}
7994
}
8095
}

packages/hoppscotch-desktop/src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)