-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy path07_list_content.rs
More file actions
180 lines (160 loc) · 6.09 KB
/
Copy path07_list_content.rs
File metadata and controls
180 lines (160 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! List Available Content
//!
//! Demonstrates listing all available shareable content using the
//! batched [`SCShareableContent::snapshot`] API — populates every
//! display, window, and running application with their attributes
//! in a single FFI round-trip per category (~15× faster than the
//! per-element accessors when reading multiple attributes per item).
//!
//! For a rare lookup of a single attribute the older
//! `.windows()` / `.displays()` / `.applications()` accessors still work and
//! issue per-element FFI calls; this example uses the batched form because
//! it's reading every attribute on every item.
use screencapturekit::prelude::*;
use screencapturekit::shareable_content::{
ApplicationSnapshot, ContentSnapshot, DisplaySnapshot, WindowSnapshot,
};
type ExampleResult = Result<(), Box<dyn std::error::Error>>;
fn main() -> ExampleResult {
println!("📋 Available Shareable Content\n");
let content = SCShareableContent::get()?;
let ContentSnapshot {
displays,
applications,
windows,
} = content
.snapshot()
.ok_or("Could not collect shareable content snapshot")?;
print_displays(&content, &displays);
print_windows(&applications, &windows);
print_applications(&applications);
print_safari_windows(&applications, &windows);
print_custom_filtering()?;
#[cfg(feature = "macos_14_0")]
print_filter_styles(&content);
Ok(())
}
fn print_displays(content: &SCShareableContent, displays: &[DisplaySnapshot]) {
println!("🖥️ Displays ({}):", displays.len());
for display in displays {
println!(" - ID: {}", display.display_id);
println!(" Size: {}x{}", display.width, display.height);
println!(" Frame: {:?}", display.frame);
print_display_scale_info(content, display);
}
}
#[cfg(feature = "macos_14_0")]
fn print_display_scale_info(content: &SCShareableContent, display: &DisplaySnapshot) {
if let Some(live) = content
.displays()
.into_iter()
.find(|live| live.display_id() == display.display_id)
{
let filter = SCContentFilter::create()
.with_display(&live)
.with_excluding_windows(&[])
.build();
if let Some(info) =
screencapturekit::shareable_content::SCShareableContentInfo::for_filter(&filter)
{
let (width, height) = info.pixel_size();
println!(
" Pixel size: {}x{} ({:.1}x scale)",
width,
height,
info.point_pixel_scale()
);
}
}
}
#[cfg(not(feature = "macos_14_0"))]
const fn print_display_scale_info(_content: &SCShareableContent, _display: &DisplaySnapshot) {}
fn print_windows(applications: &[ApplicationSnapshot], windows: &[WindowSnapshot]) {
println!("\n🪟 Windows (showing first 10 of {}):", windows.len());
for window in windows.iter().take(10) {
let app_name = window
.owning_app_index
.and_then(|i| applications.get(i))
.map_or("", |app| app.application_name.as_str());
println!(
" - {} - {}",
app_name,
window.title.as_deref().unwrap_or("")
);
println!(" Window ID: {}", window.window_id);
println!(
" Size: {}x{}",
window.frame.size.width, window.frame.size.height
);
println!(" Layer: {}", window.window_layer);
println!(" On screen: {}", window.is_on_screen);
}
}
fn print_applications(applications: &[ApplicationSnapshot]) {
println!("\n📱 Applications ({}):", applications.len());
for app in applications {
println!(" - {}", app.application_name);
println!(" Bundle ID: {}", app.bundle_identifier);
println!(" PID: {}", app.process_id);
}
}
fn print_safari_windows(applications: &[ApplicationSnapshot], windows: &[WindowSnapshot]) {
println!("\n🔍 Filter Example - Safari Windows:");
let safari_app_indices: Vec<usize> = applications
.iter()
.enumerate()
.filter(|(_, app)| app.application_name.contains("Safari"))
.map(|(i, _)| i)
.collect();
let safari_windows: Vec<_> = windows
.iter()
.filter(|window| {
window
.owning_app_index
.is_some_and(|i| safari_app_indices.contains(&i))
})
.collect();
println!("Found {} Safari windows", safari_windows.len());
for window in safari_windows {
println!(" - {}", window.title.as_deref().unwrap_or(""));
}
}
fn print_custom_filtering() -> ExampleResult {
println!("\n⚙️ Custom Filtering:");
let filtered = SCShareableContent::create()
.with_on_screen_windows_only(true)
.with_exclude_desktop_windows(true)
.get()?;
let filtered_snapshot = filtered.snapshot().ok_or("snapshot")?;
println!(
"On-screen windows only: {}",
filtered_snapshot.windows.len()
);
Ok(())
}
#[cfg(feature = "macos_14_0")]
fn print_filter_styles(content: &SCShareableContent) {
use screencapturekit::stream::content_filter::SCShareableContentStyle;
println!("\n📊 Content Filter Styles (macOS 14.0+):");
let live_displays = content.displays();
let live_windows = content.windows();
if let Some(display) = live_displays.first() {
let display_filter = SCContentFilter::create()
.with_display(display)
.with_excluding_windows(&[])
.build();
println!(" Display filter style: {:?}", display_filter.style());
}
if let Some(window) = live_windows.first() {
let window_filter = SCContentFilter::create().with_window(window).build();
println!(" Window filter style: {:?}", window_filter.style());
}
println!("\n Style values:");
println!(" None = {:?}", SCShareableContentStyle::None);
println!(" Window = {:?}", SCShareableContentStyle::Window);
println!(" Display = {:?}", SCShareableContentStyle::Display);
println!(
" Application = {:?}",
SCShareableContentStyle::Application
);
}