From 9fb6e59e36da493105aaaeebed3ebd5268b43bf4 Mon Sep 17 00:00:00 2001 From: Timotej <20747466+Timic3@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:21:26 +0200 Subject: [PATCH] fix(desktop): set window caption color if Windows version >= 11 (#3939) --- .../src-tauri/Cargo.lock | 10 +++++++ .../src-tauri/Cargo.toml | 1 + .../src-tauri/src/win/window.rs | 27 ++++++++++++------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.lock b/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.lock index a08bf3377..e28f8ee94 100644 --- a/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.lock +++ b/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.lock @@ -1274,6 +1274,7 @@ dependencies = [ "tauri-plugin-window-state", "url", "windows 0.52.0", + "winver", ] [[package]] @@ -4415,6 +4416,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "winver" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e0e7162b9e282fd75a0a832cce93994bdb21208d848a418cd05a5fdd9b9ab33" +dependencies = [ + "windows 0.48.0", +] + [[package]] name = "wry" version = "0.24.6" diff --git a/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.toml b/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.toml index 06d0e466e..7186c111c 100644 --- a/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.toml +++ b/packages/hoppscotch-selfhost-desktop/src-tauri/Cargo.toml @@ -41,6 +41,7 @@ windows = { version = "0.52.0", features = [ "Win32_Foundation", "Win32_UI_Controls", ] } +winver = "1" [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/packages/hoppscotch-selfhost-desktop/src-tauri/src/win/window.rs b/packages/hoppscotch-selfhost-desktop/src-tauri/src/win/window.rs index bff1408c5..c83202b24 100644 --- a/packages/hoppscotch-selfhost-desktop/src-tauri/src/win/window.rs +++ b/packages/hoppscotch-selfhost-desktop/src-tauri/src/win/window.rs @@ -16,6 +16,8 @@ use windows::Win32::Graphics::Dwm::DwmSetWindowAttribute; use windows::Win32::Foundation::HWND; use windows::Win32::Graphics::Dwm::{DWMWA_USE_IMMERSIVE_DARK_MODE}; +use winver::WindowsVersion; + fn hex_color_to_colorref(color: HexColor) -> COLORREF { // TODO: Remove this unsafe, This operation doesn't need to be unsafe! unsafe { @@ -42,18 +44,25 @@ fn update_bg_color(hwnd: &HWND, bg_color: HexColor) { ptr::addr_of!(use_dark_mode) as *const c_void, size_of::().try_into().unwrap() ).unwrap(); + } - DwmSetWindowAttribute( - HWND(hwnd.0), - DWMWA_CAPTION_COLOR, - ptr::addr_of!(final_color) as *const c_void, - size_of::().try_into().unwrap() - ).unwrap(); + let version = WindowsVersion::detect().unwrap(); + if version >= WindowsVersion::new(10, 0, 22000) { + unsafe { + DwmSetWindowAttribute( + HWND(hwnd.0), + DWMWA_CAPTION_COLOR, + ptr::addr_of!(final_color) as *const c_void, + size_of::().try_into().unwrap() + ).unwrap(); + } + } - let flags = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON; - let mask = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON | WTNCA_NOSYSMENU | WTNCA_NOMIRRORHELP; - let options = WinThemeAttribute { flag: flags, mask }; + let flags = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON; + let mask = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON | WTNCA_NOSYSMENU | WTNCA_NOMIRRORHELP; + let options = WinThemeAttribute { flag: flags, mask }; + unsafe { SetWindowThemeAttribute( HWND(hwnd.0), WTA_NONCLIENT,