diff --git a/.changes/content-protection.md b/.changes/content-protection.md new file mode 100644 index 000000000..2b12f78fb --- /dev/null +++ b/.changes/content-protection.md @@ -0,0 +1,5 @@ +--- +"tao": "minor" +--- + +Add `Window::set_content_protection` for macOS and Windows. \ No newline at end of file diff --git a/examples/window_debug.rs b/examples/window_debug.rs index 3f6966707..8664e19d1 100644 --- a/examples/window_debug.rs +++ b/examples/window_debug.rs @@ -34,10 +34,12 @@ fn main() { eprintln!(" (X) Toggle maximized"); eprintln!(" (T) Toggle always on top"); eprintln!(" (B) Toggle always on bottom"); + eprintln!(" (C) Toggle content protection"); let mut always_on_bottom = false; let mut always_on_top = false; let mut visible = true; + let mut content_protection = false; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; @@ -133,6 +135,10 @@ fn main() { always_on_bottom = !always_on_bottom; window.set_always_on_bottom(always_on_bottom); } + "c" => { + content_protection = !content_protection; + window.set_content_protection(content_protection); + } _ => (), }, Event::WindowEvent { diff --git a/src/platform_impl/android/ndk_glue.rs b/src/platform_impl/android/ndk_glue.rs index a23035f85..682d6ddfb 100644 --- a/src/platform_impl/android/ndk_glue.rs +++ b/src/platform_impl/android/ndk_glue.rs @@ -2,8 +2,8 @@ // Copyright 2021-2022 Tauri Programme within The Commons Conservancy // SPDX-License-Identifier: Apache-2.0 -pub use jni; pub use jni::{ + self, objects::{GlobalRef, JClass, JMap, JObject, JString}, sys::jobject, JNIEnv, diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index cdd35b368..784a92486 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -1253,6 +1253,12 @@ impl UnownedWindow { let state = self.shared_state.lock().unwrap(); state.current_theme } + + pub fn set_content_protection(&self, enabled: bool) { + unsafe { + let _: () = msg_send![*self.ns_window, setSharingType: !enabled as i32]; + } + } } impl WindowExtMacOS for UnownedWindow { diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index d7eebced8..272616c60 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -821,6 +821,19 @@ impl Window { self.window_state.lock().skip_taskbar = skip; unsafe { set_skip_taskbar(self.hwnd(), skip) }; } + + pub fn set_content_protection(&self, enabled: bool) { + unsafe { + SetWindowDisplayAffinity( + self.hwnd(), + if enabled { + WDA_EXCLUDEFROMCAPTURE + } else { + WDA_NONE + }, + ); + } + } } impl Drop for Window { diff --git a/src/window.rs b/src/window.rs index c732902cd..bfb3c5422 100644 --- a/src/window.rs +++ b/src/window.rs @@ -905,6 +905,16 @@ impl Window { pub fn theme(&self) -> Theme { self.window.theme() } + + /// Prevents the window contents from being captured by other apps. + /// + /// ## Platform-specific + /// + /// - **iOS / Android / Linux:** Unsupported. + pub fn set_content_protection(&self, enabled: bool) { + #[cfg(any(target_os = "macos", target_os = "windows"))] + self.window.set_content_protection(enabled); + } } /// Cursor functions.