From e790027c55cb21fd203d80481359f5404d84db1f Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:19:26 -0500 Subject: [PATCH] fix(recording): record the whole window in macOS Focus mode --- crates/recording/src/cursor.rs | 18 +++--- .../src/sources/screen_capture/macos.rs | 63 +++++++++++++++++-- .../src/sources/screen_capture/mod.rs | 54 +--------------- crates/verify/tests/recording.rs | 14 +---- 4 files changed, 70 insertions(+), 79 deletions(-) diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rs index e8aa1c71aed..207d54856c3 100644 --- a/crates/recording/src/cursor.rs +++ b/crates/recording/src/cursor.rs @@ -65,17 +65,13 @@ fn macos_window_crop( .raw_handle() .logical_bounds()?; let display_position = display.raw_handle().logical_position(); - // The video reads the window inside its rounded corners; the cursor - // is placed against the same trimmed frame. - Some(CursorCropBounds::new_macos( - crate::sources::screen_capture::trim_window_corners(LogicalBounds::new( - LogicalPosition::new( - window_bounds.position().x() - display_position.x(), - window_bounds.position().y() - display_position.y(), - ), - window_bounds.size(), - )), - )) + Some(CursorCropBounds::new_macos(LogicalBounds::new( + LogicalPosition::new( + window_bounds.position().x() - display_position.x(), + window_bounds.position().y() - display_position.y(), + ), + window_bounds.size(), + ))) } #[cfg(windows)] diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rs index b0f02e0fed1..5b89b221416 100644 --- a/crates/recording/src/sources/screen_capture/macos.rs +++ b/crates/recording/src/sources/screen_capture/macos.rs @@ -87,12 +87,12 @@ fn configure_window_capture(settings: &mut sc::StreamCfg) { } } -/// 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`. +/// What the stream reads. A display capture crops to `crop_bounds` on the +/// display; a window capture reads the same size from the window's own +/// top-left corner, so Focus records every pixel a screen region would. 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) + (0.0, 0.0) } else { (crop_bounds.position().x(), crop_bounds.position().y()) }; @@ -1488,6 +1488,61 @@ mod tests { } } + fn window_crop( + x: f64, + y: f64, + width: f64, + height: f64, + scale: f64, + ) -> super::super::CropBounds { + super::super::even_pixel_crop( + LogicalBounds::new(LogicalPosition::new(x, y), LogicalSize::new(width, height)), + scale, + ) + } + + fn rect(rect: cg::Rect) -> (f64, f64, f64, f64) { + ( + rect.origin.x, + rect.origin.y, + rect.size.width, + rect.size.height, + ) + } + + #[test] + fn focus_reads_the_whole_window_from_its_top_left() { + let crop = window_crop(100., 50., 920., 436., 1.0); + assert_eq!(rect(source_rect(true, crop)), (0., 0., 920., 436.)); + assert_eq!(rect(source_rect(false, crop)), (100., 50., 920., 436.)); + } + + #[test] + fn focus_reads_as_many_pixels_as_a_screen_region_at_every_scale() { + for (scale, width, height) in [ + (1.0, 701., 461.), + (2.0, 845., 753.5), + (1.5, 1280., 800.), + (2.0, 1512., 949.), + ] { + let crop = window_crop(37., 25., width, height, scale); + let (_, _, focus_width, focus_height) = rect(source_rect(true, crop)); + let (_, _, region_width, region_height) = rect(source_rect(false, crop)); + assert_eq!((focus_width, focus_height), (region_width, region_height)); + assert!(width - focus_width < 2. / scale, "{scale}x {width} pt wide"); + assert!( + height - focus_height < 2. / scale, + "{scale}x {height} pt tall" + ); + } + } + + #[test] + fn focus_reads_a_partly_offscreen_window_from_its_top_left() { + let crop = window_crop(-1440., -120., 1440., 900., 2.0); + assert_eq!(rect(source_rect(true, crop)), (0., 0., 1440., 900.)); + } + #[test] fn initial_capture_uses_window_filter_for_non_excluded_window() { let config = window_config(Some(window_id("247"))); diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs index 4dd193ca657..3207534d41e 100644 --- a/crates/recording/src/sources/screen_capture/mod.rs +++ b/crates/recording/src/sources/screen_capture/mod.rs @@ -393,13 +393,6 @@ pub struct Config { #[cfg(target_os = "macos")] pub type CropBounds = LogicalBounds; -/// macOS hands a single window over with its rounded corners transparent, -/// which the encoder fills with black. Window-only capture reads this many -/// points in from every edge: a corner's curve clears 0.29 of its radius in, -/// and this covers radii up to 27 pt. -#[cfg(target_os = "macos")] -pub const WINDOW_CORNER_TRIM: f64 = 8.0; - /// `bounds` shrunk to whole, even pixel dimensions at `scale`. The encoder /// needs even frames; a region an odd number of pixels across would be read /// whole and squeezed a pixel smaller, resampling, and so softening, every @@ -414,22 +407,6 @@ pub fn even_pixel_crop(bounds: CropBounds, scale: f64) -> CropBounds { ) } -/// `bounds` with [`WINDOW_CORNER_TRIM`] taken off every side, or as it was -/// when the window is too small to trim. -#[cfg(target_os = "macos")] -pub fn trim_window_corners(bounds: CropBounds) -> CropBounds { - let size = bounds.size(); - let trim = WINDOW_CORNER_TRIM; - if size.width() <= trim * 4. || size.height() <= trim * 4. { - return bounds; - } - let position = bounds.position(); - LogicalBounds::new( - LogicalPosition::new(position.x() + trim, position.y() + trim), - LogicalSize::new(size.width() - 2. * trim, size.height() - 2. * trim), - ) -} - #[cfg(windows)] pub type CropBounds = PhysicalBounds; @@ -559,13 +536,6 @@ impl ScreenCaptureConfig { let target_refresh = validated_refresh_rate(display.refresh_rate()); let fps = std::cmp::max(1, std::cmp::min(max_fps, target_refresh)); - // The capture reads the window inside its corners (see - // `WINDOW_CORNER_TRIM`), so the output and the cursor crop match that. - #[cfg(target_os = "macos")] - let crop_bounds = match window { - Some(_) => crop_bounds.map(trim_window_corners), - None => crop_bounds, - }; // Read exactly as many pixels as the frame holds (see `even_pixel_crop`). #[cfg(target_os = "macos")] let crop_bounds = crop_bounds @@ -1240,22 +1210,9 @@ mod tests { } #[cfg(all(test, target_os = "macos"))] -mod window_corner_tests { +mod even_pixel_crop_tests { use super::*; - #[test] - fn window_capture_reads_inside_the_rounded_corners() { - let window = LogicalBounds::new( - LogicalPosition::new(100., 50.), - LogicalSize::new(920., 436.), - ); - let trimmed = trim_window_corners(window); - assert_eq!(trimmed.position().x(), 108.); - assert_eq!(trimmed.position().y(), 58.); - assert_eq!(trimmed.size().width(), 904.); - assert_eq!(trimmed.size().height(), 420.); - } - #[test] fn crops_read_whole_even_pixels() { let odd = LogicalBounds::new(LogicalPosition::new(8., 8.), LogicalSize::new(1690., 753.)); @@ -1271,13 +1228,4 @@ mod window_corner_tests { assert_eq!(retina.size().width() * 2.0, 1690.); assert_eq!(retina.size().height() * 2.0, 1506.); } - - #[test] - fn a_tiny_window_is_left_whole() { - let window = LogicalBounds::new(LogicalPosition::new(0., 0.), LogicalSize::new(30., 200.)); - let kept = trim_window_corners(window); - assert_eq!(kept.position().x(), 0.); - assert_eq!(kept.size().width(), 30.); - assert_eq!(kept.size().height(), 200.); - } } diff --git a/crates/verify/tests/recording.rs b/crates/verify/tests/recording.rs index aad2d2af535..39cd5c30c50 100644 --- a/crates/verify/tests/recording.rs +++ b/crates/verify/tests/recording.rs @@ -3204,7 +3204,7 @@ const START_COVERAGE_SECS: f64 = 0.3; /// no magenta from the occluder in front of it (the screen key is blue, the /// noise grey, so strong positive Cr is the occluder alone), no black column /// or row along an edge (the window drawn smaller than its frame), and the -/// window's own size, less the rounded corners macOS trims off. +/// whole window, every edge, at its own size. fn check_focus(case: &mut Case, track: &Track, stimulus: &[StimulusEvent]) -> anyhow::Result<()> { let Some((width, height, scale)) = stimulus.iter().find_map(|event| match event { StimulusEvent::Ready { @@ -3218,15 +3218,7 @@ fn check_focus(case: &mut Case, track: &Track, stimulus: &[StimulusEvent]) -> an case.problem("missing", "focus: the stimulus never reported its size"); return Ok(()); }; - #[cfg(target_os = "macos")] - let trim = - 2 * (cap_recording::sources::screen_capture::WINDOW_CORNER_TRIM * scale).round() as u32; - #[cfg(not(target_os = "macos"))] - let trim = { - let _ = scale; - 0 - }; - let expected = (width.saturating_sub(trim), height.saturating_sub(trim)); + let expected = (width, height); let mut frames = 0usize; let mut sizes = BTreeMap::new(); @@ -3288,7 +3280,7 @@ fn check_focus(case: &mut Case, track: &Track, stimulus: &[StimulusEvent]) -> an case.problem( "focus-size", format!( - "expected {}x{} (the window less {trim} px of corners), recorded {sizes:?}", + "expected {}x{} (the whole window), recorded {sizes:?}", expected.0, expected.1 ), );