StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use windows::Win32::Foundation::{FALSE, HWND, TRUE}; |
| 2 | use windows::Win32::Graphics::Dwm::{DwmSetWindowAttribute, DWMWA_CLOAK}; |
| 3 | use windows_core::BOOL; |
| 4 | use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle}; |
| 5 | use winit::window::Window; |
| 6 | |
| 7 | #[derive(Debug, thiserror::Error)] |
| 8 | pub enum Error { |
| 9 | #[error("Invalid WindowHandle")] |
| 10 | InvalidWindowHandle, |
| 11 | #[error("Unknown error")] |
| 12 | Other(#[from] windows::core::Error), |
| 13 | } |
| 14 | |
| 15 | /// Extension trait for Windows specific logic on a [`winit::window::Window`]. |
| 16 | pub trait WindowExt { |
| 17 | /// "Cloaks" the window. A cloaked window is one that is invisible, but can still be drawn to. |
| 18 | fn set_cloaked(&self, cloaked: bool) -> Result<(), Error>; |
| 19 | } |
| 20 | |
| 21 | impl WindowExt for Window { |
| 22 | fn set_cloaked(&self, cloaked: bool) -> Result<(), Error> { |
| 23 | let Ok(RawWindowHandle::Win32(handle)) = self |
| 24 | .window_handle() |
| 25 | .map(|window_handle| window_handle.as_raw()) |
| 26 | else { |
| 27 | return Err(Error::InvalidWindowHandle); |
| 28 | }; |
| 29 | |
| 30 | let value = if cloaked { TRUE } else { FALSE }; |
| 31 | unsafe { |
| 32 | DwmSetWindowAttribute( |
| 33 | HWND(handle.hwnd.get() as _), |
| 34 | DWMWA_CLOAK, |
| 35 | &value as *const BOOL as *const _, |
| 36 | size_of::<BOOL>() as u32, |
| 37 | )? |
| 38 | } |
| 39 | |
| 40 | Ok(()) |
| 41 | } |
| 42 | } |
| 43 |