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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//! Dimension and scaling configuration for stream capture
//!
//! This module provides methods to configure the output dimensions, scaling behavior,
//! and source/destination rectangles for captured streams.
use crate::cg::CGRect;
use super::internal::SCStreamConfiguration;
impl SCStreamConfiguration {
/// Set the output width in pixels
///
/// The width determines the width of captured frames.
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let mut config = SCStreamConfiguration::default();
/// config.set_width(1920);
/// assert_eq!(config.width(), 1920);
/// ```
pub fn set_width(&mut self, width: u32) -> &mut Self {
// FFI expects isize; u32 may wrap on 32-bit platforms (acceptable)
#[allow(clippy::cast_possible_wrap)]
unsafe {
crate::ffi::sc_stream_configuration_set_width(self.as_ptr(), width as isize);
}
self
}
/// Set the output width in pixels (builder pattern)
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let config = SCStreamConfiguration::new()
/// .with_width(1920)
/// .with_height(1080);
/// ```
#[must_use]
pub fn with_width(mut self, width: u32) -> Self {
self.set_width(width);
self
}
/// Get the configured output width in pixels
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let mut config = SCStreamConfiguration::default();
/// config.set_width(1920);
/// assert_eq!(config.width(), 1920);
/// ```
pub fn width(&self) -> u32 {
// FFI returns isize but width is always positive and fits in u32
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
unsafe {
crate::ffi::sc_stream_configuration_get_width(self.as_ptr()) as u32
}
}
/// Set the output height in pixels
///
/// The height determines the height of captured frames.
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let mut config = SCStreamConfiguration::default();
/// config.set_height(1080);
/// assert_eq!(config.height(), 1080);
/// ```
pub fn set_height(&mut self, height: u32) -> &mut Self {
// FFI expects isize; u32 may wrap on 32-bit platforms (acceptable)
#[allow(clippy::cast_possible_wrap)]
unsafe {
crate::ffi::sc_stream_configuration_set_height(self.as_ptr(), height as isize);
}
self
}
/// Set the output height in pixels (builder pattern)
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let config = SCStreamConfiguration::new()
/// .with_width(1920)
/// .with_height(1080);
/// ```
#[must_use]
pub fn with_height(mut self, height: u32) -> Self {
self.set_height(height);
self
}
/// Get the configured output height in pixels
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let mut config = SCStreamConfiguration::default();
/// config.set_height(1080);
/// assert_eq!(config.height(), 1080);
/// ```
pub fn height(&self) -> u32 {
// FFI returns isize but height is always positive and fits in u32
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
unsafe {
crate::ffi::sc_stream_configuration_get_height(self.as_ptr()) as u32
}
}
/// Enable or disable scaling to fit the output dimensions
///
/// When enabled, the source content will be scaled to fit within the
/// configured width and height, potentially changing aspect ratio.
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let mut config = SCStreamConfiguration::default();
/// config.set_scales_to_fit(true);
/// assert!(config.scales_to_fit());
/// ```
pub fn set_scales_to_fit(&mut self, scales_to_fit: bool) -> &mut Self {
unsafe {
crate::ffi::sc_stream_configuration_set_scales_to_fit(self.as_ptr(), scales_to_fit);
}
self
}
/// Enable or disable scaling to fit (builder pattern)
#[must_use]
pub fn with_scales_to_fit(mut self, scales_to_fit: bool) -> Self {
self.set_scales_to_fit(scales_to_fit);
self
}
/// Check if scaling to fit is enabled
pub fn scales_to_fit(&self) -> bool {
unsafe { crate::ffi::sc_stream_configuration_get_scales_to_fit(self.as_ptr()) }
}
/// Set the source rectangle to capture
///
/// Defines which portion of the source content to capture. Coordinates are
/// relative to the source content's coordinate system.
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
/// use screencapturekit::cg::CGRect;
///
/// // Capture only top-left quarter of screen
/// let rect = CGRect::new(0.0, 0.0, 960.0, 540.0);
/// let mut config = SCStreamConfiguration::default();
/// config.set_source_rect(rect);
/// ```
pub fn set_source_rect(&mut self, source_rect: CGRect) -> &mut Self {
unsafe {
crate::ffi::sc_stream_configuration_set_source_rect(
self.as_ptr(),
source_rect.x,
source_rect.y,
source_rect.width,
source_rect.height,
);
}
self
}
/// Set the source rectangle (builder pattern)
#[must_use]
pub fn with_source_rect(mut self, source_rect: CGRect) -> Self {
self.set_source_rect(source_rect);
self
}
/// Get the configured source rectangle
pub fn source_rect(&self) -> CGRect {
unsafe {
let mut x = 0.0;
let mut y = 0.0;
let mut width = 0.0;
let mut height = 0.0;
crate::ffi::sc_stream_configuration_get_source_rect(
self.as_ptr(),
&mut x,
&mut y,
&mut width,
&mut height,
);
CGRect::new(x, y, width, height)
}
}
/// Set the destination rectangle for captured content
///
/// Defines where the captured content will be placed in the output frame.
/// Useful for picture-in-picture or multi-source compositions.
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
/// use screencapturekit::cg::CGRect;
///
/// // Place captured content in top-left corner
/// let rect = CGRect::new(0.0, 0.0, 640.0, 480.0);
/// let mut config = SCStreamConfiguration::default();
/// config.set_destination_rect(rect);
/// ```
pub fn set_destination_rect(&mut self, destination_rect: CGRect) -> &mut Self {
unsafe {
crate::ffi::sc_stream_configuration_set_destination_rect(
self.as_ptr(),
destination_rect.x,
destination_rect.y,
destination_rect.width,
destination_rect.height,
);
}
self
}
/// Set the destination rectangle (builder pattern)
#[must_use]
pub fn with_destination_rect(mut self, destination_rect: CGRect) -> Self {
self.set_destination_rect(destination_rect);
self
}
/// Get the configured destination rectangle
pub fn destination_rect(&self) -> CGRect {
unsafe {
let mut x = 0.0;
let mut y = 0.0;
let mut width = 0.0;
let mut height = 0.0;
crate::ffi::sc_stream_configuration_get_destination_rect(
self.as_ptr(),
&mut x,
&mut y,
&mut width,
&mut height,
);
CGRect::new(x, y, width, height)
}
}
/// Preserve aspect ratio when scaling
///
/// When enabled, the content will be scaled while maintaining its original
/// aspect ratio, potentially adding letterboxing or pillarboxing.
///
/// Note: This property requires macOS 14.0+. On older versions, the setter
/// is a no-op and the getter returns `false`.
///
/// # Examples
///
/// ```
/// use screencapturekit::prelude::*;
///
/// let mut config = SCStreamConfiguration::default();
/// config.set_preserves_aspect_ratio(true);
/// // Returns true on macOS 14.0+, false on older versions
/// let _ = config.preserves_aspect_ratio();
/// ```
pub fn set_preserves_aspect_ratio(&mut self, preserves_aspect_ratio: bool) -> &mut Self {
unsafe {
crate::ffi::sc_stream_configuration_set_preserves_aspect_ratio(
self.as_ptr(),
preserves_aspect_ratio,
);
}
self
}
/// Preserve aspect ratio when scaling (builder pattern)
#[must_use]
pub fn with_preserves_aspect_ratio(mut self, preserves_aspect_ratio: bool) -> Self {
self.set_preserves_aspect_ratio(preserves_aspect_ratio);
self
}
/// Check if aspect ratio preservation is enabled
pub fn preserves_aspect_ratio(&self) -> bool {
unsafe { crate::ffi::sc_stream_configuration_get_preserves_aspect_ratio(self.as_ptr()) }
}
}