Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): keep excluded windows hidden in macOS window capture

Minit Jainc3d41d2767a4
diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rs
index be29055e6..ad4cd3018 100644
--- a/crates/recording/src/sources/screen_capture/macos.rs
+++ b/crates/recording/src/sources/screen_capture/macos.rs
@@ -28,6 +28,27 @@ use tracing::{debug, error, info, warn};
const MAX_CAPTURE_RESTARTS: u32 = 3;
const RESTART_DELAY: Duration = Duration::from_secs(2);
+// The display filter is what hides excluded windows, so an excluded selection must stay on
+// the display-crop path instead of being captured on its own.
+fn capturable_window<'a>(
+ config: &'a Config,
+ excluded_windows: &[WindowId],
+ capture_phase: &'static str,
+) -> Option<&'a WindowId> {
+ let window_id = config.window.as_ref()?;
+
+ if excluded_windows.contains(window_id) {
+ warn!(
+ %window_id,
+ capture_phase,
+ "Capture window is excluded from recordings, using display crop with exclusions"
+ );
+ return None;
+ }
+
+ Some(window_id)
+}
+
fn window_content_filter(
window_id: &WindowId,
content: &sc::ShareableContent,
@@ -242,10 +263,7 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {
let display = Display::from_id(&self.config.display)
.ok_or_else(|| SourceError::NoDisplay(self.config.display.clone()))?;
- let window_filter = self
- .config
- .window
- .as_ref()
+ let window_filter = capturable_window(&self.config, &self.excluded_windows, "initial")
.and_then(|id| window_content_filter(id, &self.shareable_content, "initial"));
let captures_window = window_filter.is_some();
@@ -986,10 +1004,7 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt
let display = Display::from_id(&params.display_id)
.ok_or_else(|| anyhow!("Display not found during restart: {:?}", params.display_id))?;
- let window_filter = params
- .config
- .window
- .as_ref()
+ let window_filter = capturable_window(&params.config, &params.excluded_windows, "restart")
.and_then(|id| window_content_filter(id, &shareable_content, "restart"));
let captures_window = window_filter.is_some();
@@ -1374,3 +1389,73 @@ impl output_pipeline::AudioSource for SystemAudioSource {
self.inner.audio_info()
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn window_id(id: &str) -> WindowId {
+ id.parse().unwrap()
+ }
+
+ fn window_config(window: Option<WindowId>) -> Config {
+ Config {
+ display: "1".parse().unwrap(),
+ crop_bounds: None,
+ fps: 60,
+ show_cursor: true,
+ window,
+ }
+ }
+
+ #[test]
+ fn initial_capture_uses_window_filter_for_non_excluded_window() {
+ let config = window_config(Some(window_id("247")));
+ let excluded = vec![window_id("12"), window_id("99")];
+
+ assert_eq!(
+ capturable_window(&config, &excluded, "initial"),
+ Some(&window_id("247"))
+ );
+ }
+
+ #[test]
+ fn initial_capture_keeps_excluded_window_on_display_path() {
+ let config = window_config(Some(window_id("247")));
+ let excluded = vec![window_id("12"), window_id("247")];
+
+ assert_eq!(capturable_window(&config, &excluded, "initial"), None);
+ }
+
+ #[test]
+ fn restart_capture_keeps_excluded_window_on_display_path() {
+ let config = window_config(Some(window_id("247")));
+ let excluded = vec![window_id("247")];
+ let (restart_config, restart_excluded) = (config.clone(), excluded.clone());
+
+ assert_eq!(
+ capturable_window(&restart_config, &restart_excluded, "restart"),
+ None
+ );
+ }
+
+ #[test]
+ fn restart_capture_uses_window_filter_for_non_excluded_window() {
+ let config = window_config(Some(window_id("247")));
+ let excluded = vec![window_id("12")];
+ let (restart_config, restart_excluded) = (config.clone(), excluded.clone());
+
+ assert_eq!(
+ capturable_window(&restart_config, &restart_excluded, "restart"),
+ Some(&window_id("247"))
+ );
+ }
+
+ #[test]
+ fn non_window_targets_never_use_window_filter() {
+ let config = window_config(None);
+
+ assert_eq!(capturable_window(&config, &[], "initial"), None);
+ assert_eq!(capturable_window(&config, &[], "restart"), None);
+ }
+}