screencapturekit/shareable_content/
window.rs1use crate::cg::CGRect;
2use crate::utils::ffi_string::ffi_string_owned;
3use core::fmt;
4use std::ffi::c_void;
5
6use super::SCRunningApplication;
7
8#[repr(transparent)]
28pub struct SCWindow(*const c_void);
29
30impl PartialEq for SCWindow {
31 fn eq(&self, other: &Self) -> bool {
32 self.0 == other.0
33 }
34}
35
36impl Eq for SCWindow {}
37
38impl std::hash::Hash for SCWindow {
39 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
40 self.0.hash(state);
41 }
42}
43
44impl SCWindow {
45 pub(crate) unsafe fn from_ptr(ptr: *const c_void) -> Self {
47 Self(ptr)
48 }
49
50 pub(crate) unsafe fn from_retained_ptr(ptr: *const c_void) -> Option<Self> {
56 if ptr.is_null() {
57 None
58 } else {
59 Some(unsafe { Self::from_ptr(ptr) })
60 }
61 }
62
63 pub(crate) fn as_ptr(&self) -> *const c_void {
65 self.0
66 }
67
68 pub fn owning_application(&self) -> Option<SCRunningApplication> {
70 unsafe {
71 let app_ptr = crate::ffi::sc_window_get_owning_application(self.0);
72 SCRunningApplication::from_retained_ptr(app_ptr)
73 }
74 }
75
76 pub fn window_id(&self) -> u32 {
78 unsafe { crate::ffi::sc_window_get_window_id(self.0) }
79 }
80
81 pub fn frame(&self) -> CGRect {
83 let mut x = 0.0;
84 let mut y = 0.0;
85 let mut width = 0.0;
86 let mut height = 0.0;
87 unsafe {
88 crate::ffi::sc_window_get_frame_packed(self.0, &mut x, &mut y, &mut width, &mut height);
89 }
90 CGRect::new(x, y, width, height)
91 }
92
93 pub fn title(&self) -> Option<String> {
95 unsafe { ffi_string_owned(|| crate::ffi::sc_window_get_title_owned(self.0)) }
96 }
97
98 pub fn window_layer(&self) -> i32 {
100 #[allow(clippy::cast_possible_truncation)]
102 unsafe {
103 crate::ffi::sc_window_get_window_layer(self.0) as i32
104 }
105 }
106
107 pub fn is_on_screen(&self) -> bool {
109 unsafe { crate::ffi::sc_window_is_on_screen(self.0) }
110 }
111
112 #[cfg(feature = "macos_13_0")]
118 pub fn is_active(&self) -> bool {
119 unsafe { crate::ffi::sc_window_is_active(self.0) }
120 }
121}
122
123crate::utils::retained::sc_retained!(
124 SCWindow,
125 retain = crate::ffi::sc_window_retain,
126 release = crate::ffi::sc_window_release,
127);
128
129impl fmt::Debug for SCWindow {
130 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131 let mut debug = f.debug_struct("SCWindow");
132 debug
133 .field("window_id", &self.window_id())
134 .field("title", &self.title())
135 .field("frame", &self.frame())
136 .field("window_layer", &self.window_layer())
137 .field("is_on_screen", &self.is_on_screen());
138 #[cfg(feature = "macos_13_0")]
139 debug.field("is_active", &self.is_active());
140 debug.finish()
141 }
142}
143
144impl fmt::Display for SCWindow {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 write!(
147 f,
148 "Window {} \"{}\" ({})",
149 self.window_id(),
150 self.title().unwrap_or_else(|| String::from("<untitled>")),
151 self.frame()
152 )
153 }
154}
155
156unsafe impl Send for SCWindow {}
160unsafe impl Sync for SCWindow {}