Skip to content

Commit

Permalink
feat: implement set_content_protection, closes #550 (#551)
Browse files Browse the repository at this point in the history
* feat: implement set_content_protection

* fmt

* Update content_protection.rs

* add changefile

* Apply suggestions from code review

* remove dedicated example

* Update src/platform_impl/windows/window.rs

* fmt

* Delete content_protection.rs
  • Loading branch information
JonasKruckenberg committed Sep 4, 2022
1 parent 457385a commit 802146f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/content-protection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "minor"
---

Add `Window::set_content_protection` for macOS and Windows.
6 changes: 6 additions & 0 deletions examples/window_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/android/ndk_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 802146f

Please sign in to comment.