use std::ffi::c_void;
use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
use winit::window::Window;
#[repr(C)]
struct OsVersionInfo {
size: u32,
major: u32,
minor: u32,
build: u32,
platform: u32,
csd: [u16; 128],
}
#[repr(C)]
struct AccentPolicy {
state: u32,
flags: u32,
color: u32,
animation: u32,
}
#[repr(C)]
struct CompositionData {
attribute: u32,
data: *mut c_void,
size: usize,
}
#[repr(C)]
struct Margins {
left: i32,
right: i32,
top: i32,
bottom: i32,
}
#[link(name = "ntdll")]
unsafe extern "system" {
fn RtlGetVersion(info: *mut OsVersionInfo) -> i32;
}
#[link(name = "dwmapi")]
unsafe extern "system" {
fn DwmSetWindowAttribute(hwnd: isize, attribute: u32, value: *const c_void, size: u32) -> i32;
fn DwmExtendFrameIntoClientArea(hwnd: isize, margins: *const Margins) -> i32;
}
#[link(name = "kernel32")]
unsafe extern "system" {
fn GetModuleHandleA(name: *const u8) -> isize;
fn GetProcAddress(module: isize, name: *const u8) -> *const c_void;
}
type SetWindowCompositionAttribute = unsafe extern "system" fn(isize, *mut CompositionData) -> i32;
const DWMWA_USE_IMMERSIVE_DARK_MODE: u32 = 20;
const DWMWA_SYSTEMBACKDROP_TYPE: u32 = 38;
const DWMSBT_TRANSIENTWINDOW: u32 = 3;
const WCA_ACCENT_POLICY: u32 = 19;
const ACCENT_ENABLE_BLURBEHIND: u32 = 3;
fn windows_build() -> u32 {
let mut info = OsVersionInfo {
size: size_of::<OsVersionInfo>() as u32,
major: 0,
minor: 0,
build: 0,
platform: 0,
csd: [0; 128],
};
if unsafe { RtlGetVersion(&mut info) } == 0 { info.build } else { 0 }
}
pub fn apply(window: &Window) -> bool {
let Ok(handle) = window.window_handle() else { return false };
let RawWindowHandle::Win32(h) = handle.as_raw() else { return false };
let hwnd = h.hwnd.get();
let build = windows_build();
unsafe {
let dark: u32 = 1;
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, (&raw const dark).cast(), 4);
if build >= 22621 {
let margins = Margins { left: -1, right: -1, top: -1, bottom: -1 };
DwmExtendFrameIntoClientArea(hwnd, &margins);
let kind = DWMSBT_TRANSIENTWINDOW;
DwmSetWindowAttribute(hwnd, DWMWA_SYSTEMBACKDROP_TYPE, (&raw const kind).cast(), 4) >= 0
} else if build >= 10240 {
let mut accent = AccentPolicy { state: ACCENT_ENABLE_BLURBEHIND, flags: 0, color: 0, animation: 0 };
let mut data = CompositionData {
attribute: WCA_ACCENT_POLICY,
data: (&raw mut accent).cast(),
size: size_of::<AccentPolicy>(),
};
let user32 = GetModuleHandleA(c"user32.dll".as_ptr().cast());
let f = GetProcAddress(user32, c"SetWindowCompositionAttribute".as_ptr().cast());
if f.is_null() {
return false;
}
let f: SetWindowCompositionAttribute = std::mem::transmute(f);
f(hwnd, &mut data) != 0
} else {
false
}
}
}
#[link(name = "user32")]
unsafe extern "system" {
fn GetWindowLongPtrW(hwnd: isize, index: i32) -> isize;
fn SetWindowLongPtrW(hwnd: isize, index: i32, value: isize) -> isize;
fn SetWindowPos(hwnd: isize, after: isize, x: i32, y: i32, cx: i32, cy: i32, flags: u32) -> i32;
}
const GWL_STYLE: i32 = -16;
const WS_SYSMENU: isize = 0x0008_0000;
const SWP_NOSIZE: u32 = 0x0001;
const SWP_NOMOVE: u32 = 0x0002;
const SWP_NOZORDER: u32 = 0x0004;
const SWP_NOACTIVATE: u32 = 0x0010;
const SWP_FRAMECHANGED: u32 = 0x0020;
pub fn hide_caption_buttons(window: &Window) {
let Ok(handle) = window.window_handle() else { return };
let RawWindowHandle::Win32(h) = handle.as_raw() else { return };
let hwnd = h.hwnd.get();
unsafe {
let style = GetWindowLongPtrW(hwnd, GWL_STYLE);
if style & WS_SYSMENU == 0 {
return;
}
SetWindowLongPtrW(hwnd, GWL_STYLE, style & !WS_SYSMENU);
SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
}