use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
use winit::window::Window;
use uzor::layout::window::CornerStyle;
#[cfg(target_os = "windows")]
#[link(name = "dwmapi")]
extern "system" {
fn DwmSetWindowAttribute(
hwnd: isize,
dw_attribute: u32,
pv_attribute: *const u32,
cb_attribute: u32,
) -> i32;
}
#[cfg(target_os = "windows")]
const DWMWA_WINDOW_CORNER_PREFERENCE: u32 = 33;
#[cfg(target_os = "windows")]
const DWMWA_BORDER_COLOR: u32 = 34;
#[cfg(target_os = "windows")]
const DWMWA_COLOR_DEFAULT: u32 = 0xFFFF_FFFF;
pub fn extract_hwnd(window: &Window) -> Option<isize> {
let handle = window.window_handle().ok()?;
if let RawWindowHandle::Win32(h) = handle.as_ref() {
Some(h.hwnd.get())
} else {
None
}
}
pub fn set_dwm_corner_preference(hwnd: isize, style: CornerStyle) {
#[cfg(target_os = "windows")]
{
let value: u32 = match style {
CornerStyle::Default => 0,
CornerStyle::Sharp => 1,
CornerStyle::Rounded => 2,
CornerStyle::RoundedSmall => 3,
};
unsafe {
DwmSetWindowAttribute(
hwnd,
DWMWA_WINDOW_CORNER_PREFERENCE,
&value as *const u32,
std::mem::size_of::<u32>() as u32,
);
}
}
#[cfg(not(target_os = "windows"))]
{
let _ = (hwnd, style);
}
}
pub fn set_dwm_border_color(hwnd: isize, color: Option<u32>) {
#[cfg(target_os = "windows")]
{
let colorref: u32 = match color {
None => DWMWA_COLOR_DEFAULT,
Some(argb) => {
let r = (argb >> 16) & 0xFF;
let g = (argb >> 8) & 0xFF;
let b = argb & 0xFF;
(b << 16) | (g << 8) | r
}
};
unsafe {
DwmSetWindowAttribute(
hwnd,
DWMWA_BORDER_COLOR,
&colorref as *const u32,
std::mem::size_of::<u32>() as u32,
);
}
}
#[cfg(not(target_os = "windows"))]
{
let _ = (hwnd, color);
}
}