fix(desktop): set window caption color if Windows version >= 11 (#3939)

This commit is contained in:
Timotej
2024-04-16 14:21:26 +02:00
committed by GitHub
parent 1b0802b0e6
commit 9fb6e59e36
3 changed files with 29 additions and 9 deletions

View File

@@ -1274,6 +1274,7 @@ dependencies = [
"tauri-plugin-window-state", "tauri-plugin-window-state",
"url", "url",
"windows 0.52.0", "windows 0.52.0",
"winver",
] ]
[[package]] [[package]]
@@ -4415,6 +4416,15 @@ dependencies = [
"windows-sys 0.48.0", "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]] [[package]]
name = "wry" name = "wry"
version = "0.24.6" version = "0.24.6"

View File

@@ -41,6 +41,7 @@ windows = { version = "0.52.0", features = [
"Win32_Foundation", "Win32_Foundation",
"Win32_UI_Controls", "Win32_UI_Controls",
] } ] }
winver = "1"
[features] [features]
# this feature is used for production builds or when `devPath` points to the filesystem # this feature is used for production builds or when `devPath` points to the filesystem

View File

@@ -16,6 +16,8 @@ use windows::Win32::Graphics::Dwm::DwmSetWindowAttribute;
use windows::Win32::Foundation::HWND; use windows::Win32::Foundation::HWND;
use windows::Win32::Graphics::Dwm::{DWMWA_USE_IMMERSIVE_DARK_MODE}; use windows::Win32::Graphics::Dwm::{DWMWA_USE_IMMERSIVE_DARK_MODE};
use winver::WindowsVersion;
fn hex_color_to_colorref(color: HexColor) -> COLORREF { fn hex_color_to_colorref(color: HexColor) -> COLORREF {
// TODO: Remove this unsafe, This operation doesn't need to be unsafe! // TODO: Remove this unsafe, This operation doesn't need to be unsafe!
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, ptr::addr_of!(use_dark_mode) as *const c_void,
size_of::<BOOL>().try_into().unwrap() size_of::<BOOL>().try_into().unwrap()
).unwrap(); ).unwrap();
}
DwmSetWindowAttribute( let version = WindowsVersion::detect().unwrap();
HWND(hwnd.0), if version >= WindowsVersion::new(10, 0, 22000) {
DWMWA_CAPTION_COLOR, unsafe {
ptr::addr_of!(final_color) as *const c_void, DwmSetWindowAttribute(
size_of::<COLORREF>().try_into().unwrap() HWND(hwnd.0),
).unwrap(); DWMWA_CAPTION_COLOR,
ptr::addr_of!(final_color) as *const c_void,
size_of::<COLORREF>().try_into().unwrap()
).unwrap();
}
}
let flags = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON; let flags = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON;
let mask = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON | WTNCA_NOSYSMENU | WTNCA_NOMIRRORHELP; let mask = WTNCA_NODRAWCAPTION | WTNCA_NODRAWICON | WTNCA_NOSYSMENU | WTNCA_NOMIRRORHELP;
let options = WinThemeAttribute { flag: flags, mask }; let options = WinThemeAttribute { flag: flags, mask };
unsafe {
SetWindowThemeAttribute( SetWindowThemeAttribute(
HWND(hwnd.0), HWND(hwnd.0),
WTA_NONCLIENT, WTA_NONCLIENT,