Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): capture a focused window as a display crop without what's in front, so macOS doesn't badge it

Richie McIlroy3455c601a415
diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rs
index 46461fc96..dbbd703f3 100644
--- a/crates/recording/src/sources/screen_capture/macos.rs
+++ b/crates/recording/src/sources/screen_capture/macos.rs
@@ -49,9 +49,16 @@ fn capturable_window<'a>(
Some(window_id)
}
+/// The window alone, as a crop of `display` that leaves out every window in
+/// front of it (and the ones the recording excludes anyway). Filters that
+/// target the window itself, desktop-independent or display-including, make
+/// macOS badge it as shared: its traffic lights become a purple capture
+/// symbol, which would be in the recording.
fn window_content_filter(
window_id: &WindowId,
+ display: &Display,
content: &sc::ShareableContent,
+ excluded_windows: &[WindowId],
capture_phase: &'static str,
) -> Option<arc::R<sc::ContentFilter>> {
let Ok(native_id) = window_id.to_string().parse::<u32>() else {
@@ -60,33 +67,34 @@ fn window_content_filter(
};
let windows = content.windows();
- let Some(window) = windows.iter().find(|window| window.id() == native_id) else {
+ if !windows.iter().any(|window| window.id() == native_id) {
warn!(
%window_id,
capture_phase,
"Capture window missing from ScreenCaptureKit shareable content"
);
return None;
- };
-
- Some(sc::ContentFilter::with_desktop_independent_window(window))
-}
+ }
-/// What the stream reads. A display capture crops to `crop_bounds`; a window
-/// capture reads the window itself, inside its rounded corners, where
-/// `crop_bounds` is the window already trimmed by `WINDOW_CORNER_TRIM`.
-fn source_rect(captures_window: bool, crop_bounds: super::CropBounds) -> cg::Rect {
- let (x, y) = if captures_window {
- (super::WINDOW_CORNER_TRIM, super::WINDOW_CORNER_TRIM)
- } else {
- (crop_bounds.position().x(), crop_bounds.position().y())
- };
- cg::Rect::new(
- x,
- y,
- crop_bounds.size().width(),
- crop_bounds.size().height(),
- )
+ let in_front = Window::from_id_direct(window_id)
+ .map(|window| window.raw_handle().windows_in_front())
+ .unwrap_or_default();
+ let mut left_out =
+ excluded_shareable_windows(excluded_windows, &content.retained(), capture_phase);
+ left_out.extend(
+ windows
+ .iter()
+ .filter(|window| in_front.contains(&window.id()))
+ .map(|window| window.retained()),
+ );
+ info!(
+ capture_phase,
+ in_front = in_front.len(),
+ "Capturing the window as a display crop without what is in front of it"
+ );
+ display
+ .raw_handle()
+ .as_content_filter_excluding_windows(content.retained(), left_out)
}
fn excluded_shareable_windows(
@@ -286,11 +294,16 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {
let window_filter = capturable_window(&self.config, &self.excluded_windows, "initial")
.map(|id| {
- window_content_filter(id, &self.shareable_content, "initial")
- .ok_or_else(|| SourceError::WindowGone(id.clone()))
+ window_content_filter(
+ id,
+ &display,
+ &self.shareable_content,
+ &self.excluded_windows,
+ "initial",
+ )
+ .ok_or_else(|| SourceError::WindowGone(id.clone()))
})
.transpose()?;
- let captures_window = window_filter.is_some();
let content_filter = match window_filter {
Some(filter) => filter,
@@ -340,7 +353,12 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {
if let Some(crop_bounds) = self.config.crop_bounds {
debug!("crop bounds: {:?}", crop_bounds);
- settings.set_src_rect(source_rect(captures_window, crop_bounds));
+ settings.set_src_rect(cg::Rect::new(
+ crop_bounds.position().x(),
+ crop_bounds.position().y(),
+ crop_bounds.size().width(),
+ crop_bounds.size().height(),
+ ));
}
cap_fail::fail_err!(
"macos::ScreenCaptureActor::new",
@@ -1026,12 +1044,16 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt
let window_filter = capturable_window(&params.config, &params.excluded_windows, "restart")
.map(|id| {
- window_content_filter(id, &shareable_content, "restart").ok_or_else(|| {
- anyhow!("Capture window {id} is gone; not falling back to the display")
- })
+ window_content_filter(
+ id,
+ &display,
+ &shareable_content,
+ &params.excluded_windows,
+ "restart",
+ )
+ .ok_or_else(|| anyhow!("Capture window {id} is gone; not falling back to the display"))
})
.transpose()?;
- let captures_window = window_filter.is_some();
let content_filter = match window_filter {
Some(filter) => filter,
@@ -1068,7 +1090,12 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt
settings.set_color_space_name(cg::color_space::names::srgb());
if let Some(crop_bounds) = params.config.crop_bounds {
- settings.set_src_rect(source_rect(captures_window, crop_bounds));
+ settings.set_src_rect(cg::Rect::new(
+ crop_bounds.position().x(),
+ crop_bounds.position().y(),
+ crop_bounds.size().width(),
+ crop_bounds.size().height(),
+ ));
}
let expected_width = params.video_info.width as usize;
diff --git a/crates/scap-targets/src/platform/macos.rs b/crates/scap-targets/src/platform/macos.rs
index 77e2202a2..4dbb7385c 100644
--- a/crates/scap-targets/src/platform/macos.rs
+++ b/crates/scap-targets/src/platform/macos.rs
@@ -318,6 +318,34 @@ impl WindowImpl {
ret
}
+ /// The window numbers of the on-screen windows in front of this one,
+ /// frontmost first: what a capture of this window alone leaves out.
+ pub fn windows_in_front(&self) -> Vec<u32> {
+ use core_graphics::window::{
+ kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenAboveWindow,
+ };
+
+ let Some(windows) = core_graphics::window::copy_window_info(
+ kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenAboveWindow,
+ self.0,
+ ) else {
+ return vec![];
+ };
+ windows
+ .iter()
+ .filter_map(|window| {
+ let window_dict =
+ unsafe { CFDictionary::<CFString, *const c_void>::from_void(*window) };
+ unsafe {
+ window_dict
+ .find(kCGWindowNumber)
+ .and_then(|v| CFNumber::from_void(*v).to_i64().map(|v| v as u32))
+ }
+ })
+ .filter(|number| *number != self.0)
+ .collect()
+ }
+
pub fn list_containing_cursor() -> Vec<Self> {
let Some(cursor) = get_cursor_position() else {
return vec![];