Add Window::set_content_protected on macOS and Windows (#2525)

* Add `Window::set_content_protect` on macOS and Windows

* Update window.rs

* Add builder variant

* fix import

* fix argument type

* fix import

* fix always visible window on Windows

* update docs
This commit is contained in:
Amr Bashir 2022-11-23 15:51:34 +02:00 committed by GitHub
parent 418cc44e93
commit 65baae75c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 7 deletions

View file

@ -42,11 +42,12 @@ use windows_sys::Win32::{
CreateWindowExW, FlashWindowEx, GetClientRect, GetCursorPos, GetForegroundWindow,
GetSystemMetrics, GetWindowPlacement, GetWindowTextLengthW, GetWindowTextW,
IsWindowVisible, LoadCursorW, PeekMessageW, PostMessageW, RegisterClassExW, SetCursor,
SetCursorPos, SetForegroundWindow, SetWindowPlacement, SetWindowPos, SetWindowTextW,
CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, FLASHWINFO, FLASHW_ALL, FLASHW_STOP,
FLASHW_TIMERNOFG, FLASHW_TRAY, GWLP_HINSTANCE, HTCAPTION, MAPVK_VK_TO_VSC, NID_READY,
PM_NOREMOVE, SM_DIGITIZER, SWP_ASYNCWINDOWPOS, SWP_NOACTIVATE, SWP_NOSIZE,
SWP_NOZORDER, WM_NCLBUTTONDOWN, WNDCLASSEXW,
SetCursorPos, SetForegroundWindow, SetWindowDisplayAffinity, SetWindowPlacement,
SetWindowPos, SetWindowTextW, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, FLASHWINFO,
FLASHW_ALL, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, GWLP_HINSTANCE, HTCAPTION,
MAPVK_VK_TO_VSC, NID_READY, PM_NOREMOVE, SM_DIGITIZER, SWP_ASYNCWINDOWPOS,
SWP_NOACTIVATE, SWP_NOSIZE, SWP_NOZORDER, WDA_EXCLUDEFROMCAPTURE, WDA_NONE,
WM_NCLBUTTONDOWN, WNDCLASSEXW,
},
},
};
@ -738,6 +739,20 @@ impl Window {
unsafe { force_window_active(window.0) };
}
}
#[inline]
pub fn set_content_protected(&self, protected: bool) {
unsafe {
SetWindowDisplayAffinity(
self.hwnd(),
if protected {
WDA_EXCLUDEFROMCAPTURE
} else {
WDA_NONE
},
)
};
}
}
impl Drop for Window {
@ -908,6 +923,10 @@ impl<'a, T: 'static> InitData<'a, T> {
let attributes = self.attributes.clone();
if attributes.content_protected {
win.set_content_protected(true);
}
// Set visible before setting the size to ensure the
// attribute is correctly applied.
win.set_visible(attributes.visible);