Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DWMWA_CLOAK support on Windows #3576

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Unreleased` header.
- Add the `OwnedDisplayHandle` type for allowing safe display handle usage outside of trivial cases.
- **Breaking:** Rename `TouchpadMagnify` to `PinchGesture`, `SmartMagnify` to `DoubleTapGesture` and `TouchpadRotate` to `RotationGesture` to represent the action rather than the intent.
- on iOS, add detection support for `PinchGesture`, `DoubleTapGesture` and `RotationGesture`.
- on Windows: add `with_system_backdrop`, `with_border_color`, `with_title_background_color`, `with_title_text_color` and `with_corner_preference`
- on Windows: add `with_cloaked`, `with_system_backdrop`, `with_border_color`, `with_title_background_color`, `with_title_text_color` and `with_corner_preference`
- On Windows, Remove `WS_CAPTION`, `WS_BORDER` and `WS_EX_WINDOWEDGE` styles for child windows without decorations.
- **Breaking:** Removed `EventLoopError::AlreadyRunning`, which can't happen as it is already prevented by the type system.
- Added `EventLoop::builder`, which is intended to replace the (now deprecated) `EventLoopBuilder::new`.
Expand Down
21 changes: 21 additions & 0 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ pub trait WindowExtWindows {
///
/// Supported starting with Windows 11 Build 22000.
fn set_corner_preference(&self, preference: CornerPreference);

/// Cloaks the window such that it is not visible to the user. The window is still composed by DWM.
///
/// Not supported on Windows 7 and earlier.
fn set_cloaked(&self, cloaked: bool);
Comment on lines +252 to +255
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like content protected? Or what it means here? Like it's not visible, but you can screen capture it? Like overlay, etc?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description was copy pasted from the winapi docs, but basically DWMWA_CLOAK makes a window invisible.

This is explained in detail here: https://devblogs.microsoft.com/oldnewthing/20200302-00/?p=103507

The shell has a concept called cloaking, in which a window is given all the trappings of visibility, without actually being presented to the user. As far as the app can tell, it seems to be visible: It still has the WS_VISIBLE window style, its coordinates are still within the bounds of the monitor, it still gets WM_PAINT messages, it has a non-empty clipping region, all the ways a traditional Win32 app has of detecting whether it is on screen say, “Yup, you’re on screen. Everything is just fine!”

From the user's perspective, an invisible window will not show up on the taskbar, but a cloaked window will.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: A cloaked window will not appear on Taskbar. Cloaking is particularly useful when you want a window to complete its UI layout, sizing, etc. without flickering in front of the user.

}

impl WindowExtWindows for Window {
Expand Down Expand Up @@ -298,6 +303,11 @@ impl WindowExtWindows for Window {
fn set_corner_preference(&self, preference: CornerPreference) {
self.window.set_corner_preference(preference)
}

#[inline]
fn set_cloaked(&self, cloaked: bool) {
self.window.set_cloaked(cloaked)
}
}

/// Additional methods on `WindowAttributes` that are specific to Windows.
Expand Down Expand Up @@ -388,6 +398,11 @@ pub trait WindowAttributesExtWindows {
///
/// Supported starting with Windows 11 Build 22000.
fn with_corner_preference(self, corners: CornerPreference) -> Self;

/// Cloaks the window such that it is not visible to the user. The window is still composed by DWM.
///
/// Not supported on Windows 7 and earlier.
fn with_cloaked(self, cloaked: bool) -> Self;
}

impl WindowAttributesExtWindows for WindowAttributes {
Expand Down Expand Up @@ -474,6 +489,12 @@ impl WindowAttributesExtWindows for WindowAttributes {
self.platform_specific.corner_preference = Some(corners);
self
}

#[inline]
fn with_cloaked(mut self, cloaked: bool) -> Self {
self.platform_specific.cloaked = cloaked;
self
}
}

/// Additional methods on `MonitorHandle` that are specific to Windows.
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct PlatformSpecificWindowAttributes {
pub title_background_color: Option<Color>,
pub title_text_color: Option<Color>,
pub corner_preference: Option<CornerPreference>,
pub cloaked: bool,
}

impl Default for PlatformSpecificWindowAttributes {
Expand All @@ -62,6 +63,7 @@ impl Default for PlatformSpecificWindowAttributes {
title_background_color: None,
title_text_color: None,
corner_preference: None,
cloaked: false,
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use std::{

use windows_sys::Win32::{
Foundation::{
HWND, LPARAM, OLE_E_WRONGCOMPOBJ, POINT, POINTS, RECT, RPC_E_CHANGED_MODE, S_OK, WPARAM,
BOOL, FALSE, HWND, LPARAM, OLE_E_WRONGCOMPOBJ, POINT, POINTS, RECT, RPC_E_CHANGED_MODE,
S_OK, TRUE, WPARAM,
},
Graphics::{
Dwm::{
DwmEnableBlurBehindWindow, DwmSetWindowAttribute, DWMWA_BORDER_COLOR,
DWMWA_CAPTION_COLOR, DWMWA_SYSTEMBACKDROP_TYPE, DWMWA_TEXT_COLOR,
DWMWA_CAPTION_COLOR, DWMWA_CLOAK, DWMWA_SYSTEMBACKDROP_TYPE, DWMWA_TEXT_COLOR,
DWMWA_WINDOW_CORNER_PREFERENCE, DWM_BB_BLURREGION, DWM_BB_ENABLE, DWM_BLURBEHIND,
DWM_SYSTEMBACKDROP_TYPE, DWM_WINDOW_CORNER_PREFERENCE,
},
Expand Down Expand Up @@ -1126,6 +1127,19 @@ impl Window {
);
}
}

#[inline]
pub fn set_cloaked(&self, cloaked: bool) {
let cloaked = if cloaked { TRUE } else { FALSE };
unsafe {
DwmSetWindowAttribute(
self.hwnd(),
DWMWA_CLOAK,
&cloaked as *const BOOL as *const _,
mem::size_of::<BOOL>() as u32,
);
}
}
}

impl Drop for Window {
Expand Down Expand Up @@ -1335,6 +1349,7 @@ impl<'a> InitData<'a> {
if let Some(corner) = self.attributes.platform_specific.corner_preference {
win.set_corner_preference(corner);
}
win.set_cloaked(self.attributes.platform_specific.cloaked);
}
}
unsafe fn init(
Expand Down