rustimal 0.6.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Blurred, see-through window backdrop from the Windows compositor (DWM).
//!
//! Windows 11 22H2+ gets the system acrylic backdrop. Windows 10 gets the
//! accent blur, which, unlike Windows 10 acrylic, doesn't lag when the window
//! is dragged. Either way rustimal draws its own tint on top.

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;
}

/// Undocumented, so it isn't in the import library; look it up at runtime.
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],
    };
    // SAFETY: `info` is a correctly sized OSVERSIONINFOW.
    if unsafe { RtlGetVersion(&mut info) } == 0 { info.build } else { 0 }
}

/// Turn on the blurred backdrop. Returns false if the window can't have one.
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();
    // SAFETY: plain DWM/user32 calls on our own window with correctly sized arguments.
    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;

/// Stop Windows from drawing its own minimize/maximize/close buttons.
///
/// rustimal draws its own title bar buttons. With the DWM frame extended into
/// the client area (needed for the blurred backdrop), Windows 11 also paints
/// the native caption buttons for any window that has `WS_SYSMENU`, so every
/// button showed up twice. winit always sets that style and puts it back
/// whenever the window is shown, maximized or restored, so this is cheap to
/// call again: it only touches the window if the style came back.
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();
    // SAFETY: plain user32 calls on our own window.
    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);
    }
}