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

@ -54,7 +54,7 @@ pub(crate) use self::version::NSAppKitVersion;
pub(crate) use self::view::{NSTrackingRectTag, NSView};
pub(crate) use self::window::{
NSBackingStoreType, NSWindow, NSWindowButton, NSWindowLevel, NSWindowOcclusionState,
NSWindowStyleMask, NSWindowTitleVisibility,
NSWindowSharingType, NSWindowStyleMask, NSWindowTitleVisibility,
};
#[link(name = "AppKit", kind = "framework")]

View file

@ -84,6 +84,9 @@ extern_methods!(
#[sel(setMovable:)]
pub fn setMovable(&self, movable: bool);
#[sel(setSharingType:)]
pub fn setSharingType(&self, sharingType: NSWindowSharingType);
#[sel(setOpaque:)]
pub fn setOpaque(&self, opaque: bool);
@ -349,3 +352,16 @@ pub enum NSBackingStoreType {
unsafe impl Encode for NSBackingStoreType {
const ENCODING: Encoding = NSUInteger::ENCODING;
}
#[allow(dead_code)]
#[repr(usize)] // NSUInteger
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NSWindowSharingType {
NSWindowSharingNone = 0,
NSWindowSharingReadOnly = 1,
NSWindowSharingReadWrite = 2,
}
unsafe impl Encode for NSWindowSharingType {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

View file

@ -45,7 +45,8 @@ use objc2::{declare_class, msg_send, msg_send_id, sel, ClassType};
use super::appkit::{
NSApp, NSAppKitVersion, NSAppearance, NSApplicationPresentationOptions, NSBackingStoreType,
NSColor, NSCursor, NSFilenamesPboardType, NSRequestUserAttentionType, NSResponder, NSScreen,
NSWindow, NSWindowButton, NSWindowLevel, NSWindowStyleMask, NSWindowTitleVisibility,
NSWindow, NSWindowButton, NSWindowLevel, NSWindowSharingType, NSWindowStyleMask,
NSWindowTitleVisibility,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -305,6 +306,10 @@ impl WinitWindow {
this.setTitle(&NSString::from_str(&attrs.title));
this.setAcceptsMouseMovedEvents(true);
if attrs.content_protected {
this.setSharingType(NSWindowSharingType::NSWindowSharingNone);
}
if pl_attrs.titlebar_transparent {
this.setTitlebarAppearsTransparent(true);
}
@ -1123,6 +1128,14 @@ impl WinitWindow {
}
#[inline]
pub fn set_content_protected(&self, protected: bool) {
self.setSharingType(if protected {
NSWindowSharingType::NSWindowSharingNone
} else {
NSWindowSharingType::NSWindowSharingReadOnly
})
}
pub fn title(&self) -> String {
self.title_().to_string()
}