Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/windowing/winit/windows/window_ext.rs
1use windows::Win32::Foundation::{FALSE, HWND, TRUE};
2use windows::Win32::Graphics::Dwm::{DwmSetWindowAttribute, DWMWA_CLOAK};
3use windows_core::BOOL;
4use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
5use winit::window::Window;
6 
7#[derive(Debug, thiserror::Error)]
8pub 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`].
16pub 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 
21impl 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