From 2c4c8204fd593f9fc46ac3b74290559ffc76a2f9 Mon Sep 17 00:00:00 2001 From: Kyzer <36684609+kyzer1023@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:28:03 +0800 Subject: [PATCH] feat(cli): add window-only recording option --- apps/cli/README.md | 14 ++ apps/cli/skill/cap/SKILL.md | 14 ++ apps/cli/src/guide.rs | 2 +- apps/cli/src/record.rs | 185 +++++++++++++++++++++- crates/recording/src/instant_recording.rs | 11 +- crates/recording/src/studio_recording.rs | 23 +++ 6 files changed, 242 insertions(+), 7 deletions(-) diff --git a/apps/cli/README.md b/apps/cli/README.md index 817a528b395..afd6658a7e0 100644 --- a/apps/cli/README.md +++ b/apps/cli/README.md @@ -101,3 +101,17 @@ notification, and open-editor actions are desktop-only and are skipped on the CL rules with `cap automations list --json`. Run `cap --help` or `cap --help` for full flag documentation. + +## Window-only recording (Focus) + +`cap record start --window --window-only --detach --json` captures the selected +window's content using the existing Focus capture engine on macOS and Windows. +Without `--window-only`, `--window` retains its existing screen-region behavior. +The option requires `--window` and is unavailable on Linux. Studio supports a +separate camera track; Instant with `--window-only` rejects `--camera` because that +combination would fall back to screen-region capture. Stop detached recordings +with `cap record stop --id --json`. + +Focus does not guarantee smooth background WebGL rendering or complete input or +privacy isolation. Keyboard/cursor controls, application-specific audio, browser +throttling, and MCP recording tools are separate concerns. diff --git a/apps/cli/skill/cap/SKILL.md b/apps/cli/skill/cap/SKILL.md index 68f6f60c095..d2caa477a60 100644 --- a/apps/cli/skill/cap/SKILL.md +++ b/apps/cli/skill/cap/SKILL.md @@ -122,3 +122,17 @@ To generate a short, cinematic 3D product-demo video from a URL, use the install `cap-demo` skill (installed alongside this one). It scouts the page, records it with virtual input, and treats it with Cap's 3D camera, a brand-matched background, and music. Currently macOS-only. + +## Window-only recording (Focus) + +`cap record start --window --window-only --detach --json` captures the selected +window's content using the existing Focus capture engine on macOS and Windows. +Without `--window-only`, `--window` retains its existing screen-region behavior. +The option requires `--window` and is unavailable on Linux. Studio supports a +separate camera track; Instant with `--window-only` rejects `--camera` because that +combination would fall back to screen-region capture. Stop detached recordings +with `cap record stop --id --json`. + +Focus does not guarantee smooth background WebGL rendering or complete input or +privacy isolation. Keyboard/cursor controls, application-specific audio, browser +throttling, and MCP recording tools are separate concerns. diff --git a/apps/cli/src/guide.rs b/apps/cli/src/guide.rs index f7ecefbff8c..803b711c206 100644 --- a/apps/cli/src/guide.rs +++ b/apps/cli/src/guide.rs @@ -169,7 +169,7 @@ fn build() -> Guide { CommandDoc { requires_duration: false, notes: Some( - "The `started` then `stopped` sequence applies to the foreground run. With `--detach` the stream emits only `started` (or `error`) and returns immediately with recordingId+pid; the `stopped` event is delivered by `cap record stop`.", + "--window captures a screen region by default. Add --window-only for Focus (window content only) on macOS/Windows; requires --window, and rejects --mode instant with --camera. Studio can record a separate camera track. Focus does not guarantee background rendering or input/privacy isolation. The `started` then `stopped` sequence applies to the foreground run. With `--detach` the stream emits only `started` (or `error`) and returns immediately with recordingId+pid; the `stopped` event is delivered by `cap record stop`.", ), ..cmd( "record start", diff --git a/apps/cli/src/record.rs b/apps/cli/src/record.rs index 9b09815225a..355f11fa9cc 100644 --- a/apps/cli/src/record.rs +++ b/apps/cli/src/record.rs @@ -39,6 +39,13 @@ use crate::{ pub struct RecordParams { #[command(flatten)] target: RecordTargets, + #[arg( + long, + requires = "window", + conflicts_with = "screen", + help = "Capture only the window content (Focus) on macOS/Windows, instead of its screen region; Instant mode cannot combine this with --camera" + )] + window_only: bool, /// Recording mode to use #[arg(long, value_enum, default_value_t = RecordMode::Studio)] mode: RecordMode, @@ -66,6 +73,17 @@ pub struct RecordParams { impl RecordParams { fn validate(&self) -> Result<(), String> { + if self.window_only { + if self.target.window.is_none() || self.target.screen.is_some() { + return Err("--window-only requires --window and cannot use --screen".to_string()); + } + if !cfg!(any(target_os = "macos", windows)) { + return Err("--window-only is supported only on macOS and Windows".to_string()); + } + if self.mode == RecordMode::Instant && self.camera.is_some() { + return Err("--window-only cannot combine --mode instant with --camera; use Studio mode or omit --camera".to_string()); + } + } if self.duration.is_some_and(|duration| { // `> u64::MAX` would panic in `Duration::from_secs_f64` (used by `wait_for_stop`). !duration.is_finite() || duration <= 0.0 || duration > u64::MAX as f64 @@ -88,6 +106,9 @@ impl RecordParams { args.push("--window".to_string()); args.push(id.to_string()); } + if self.window_only { + args.push("--window-only".to_string()); + } args.push("--mode".to_string()); args.push(self.mode.to_string()); if let Some(camera) = &self.camera { @@ -422,6 +443,7 @@ impl SessionRunArgs { } async fn session_worker(params: RecordParams, recording_id: &str) -> Result<(), String> { + params.validate()?; let target = resolve_target(¶ms)?; let path = params .path @@ -755,9 +777,11 @@ async fn start_recording( let target_for_shareable_content = target.clone(); let camera_only_target = matches!(target, ScreenCaptureTarget::CameraOnly); let mut studio_builder = studio_recording::Actor::builder(path.clone(), target.clone()) - .with_system_audio(params.system_audio); - let mut instant_builder = - instant_recording::Actor::builder(path, target).with_system_audio(params.system_audio); + .with_system_audio(params.system_audio) + .with_window_only(params.window_only); + let mut instant_builder = instant_recording::Actor::builder(path, target) + .with_system_audio(params.system_audio) + .with_window_only(params.window_only); let mut camera_active = false; let camera_recorded = !(cfg!(target_os = "macos") && params.mode == RecordMode::Instant && !camera_only_target); @@ -1274,8 +1298,11 @@ struct RecordTargets { /// ID of the screen to capture #[arg(long, group = "target")] screen: Option, - /// ID of the window to capture - #[arg(long, group = "target")] + #[arg( + long, + group = "target", + help = "ID of the window to capture (screen region by default; use --window-only for Focus)" + )] window: Option, } @@ -1332,6 +1359,153 @@ fn emit_record_event(format: OutputFormat, event: &RecordEvent<'_>) -> Result<() #[cfg(test)] mod tests { use super::*; + use clap::Parser; + + fn parse_record(args: &[&str]) -> RecordParams { + let cli = crate::Cli::try_parse_from(args).unwrap(); + let Some(crate::Commands::Record(record)) = cli.command else { + panic!("expected record command"); + }; + match record.command { + Some(crate::RecordCommands::Start(start)) => start.params, + Some(crate::RecordCommands::SessionRun(worker)) => worker.params, + None => record.args.params, + _ => panic!("expected recording parameters"), + } + } + + #[test] + fn window_only_requires_a_window_target() { + for args in [ + vec!["cap", "record", "start", "--window-only"], + vec!["cap", "record", "start", "--screen", "1", "--window-only"], + vec![ + "cap", + "record", + "start", + "--screen", + "1", + "--window", + "247", + "--window-only", + ], + ] { + assert!(crate::Cli::try_parse_from(&args).is_err(), "{args:?}"); + } + } + + #[test] + fn window_only_validates_mode_camera_and_platform() { + for mode in ["studio", "instant"] { + for camera in [false, true] { + let mut args = vec![ + "cap", + "record", + "start", + "--window", + "247", + "--window-only", + "--mode", + mode, + ]; + if camera { + args.extend(["--camera", "synthetic-camera"]); + } + let params = parse_record(&args); + let supported = cfg!(any(target_os = "macos", windows)); + assert_eq!( + params.validate().is_ok(), + supported && !(mode == "instant" && camera) + ); + let mut default_params = params.clone(); + default_params.window_only = false; + assert!(default_params.validate().is_ok()); + let mut missing_target = params; + missing_target.target.window = None; + assert!( + missing_target + .validate() + .unwrap_err() + .contains("requires --window") + ); + } + } + } + + #[tokio::test] + async fn window_only_rejects_instant_camera_before_creating_a_project() { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join("unused.cap"); + let mut params = parse_record(&[ + "cap", + "record", + "start", + "--window", + "247", + "--window-only", + "--mode", + "instant", + "--camera", + "synthetic-camera", + ]); + params.path = Some(path.clone()); + for detach in [false, true] { + let start = RecordStart { + params: params.clone(), + detach, + format: OutputFormat::Json, + }; + let error = start.run_inner(OutputFormat::Json).await.unwrap_err(); + assert!(error.contains("--window-only")); + assert!(!path.exists()); + } + let error = session_worker(params, "synthetic-session") + .await + .unwrap_err(); + assert!(error.contains("--window-only")); + assert!(!path.exists()); + } + + #[test] + fn window_only_survives_detached_worker_round_trip() { + for mode in ["studio", "instant"] { + for enabled in [false, true] { + let mut args = vec![ + "cap", + "record", + "start", + "--window", + "247", + "--mode", + mode, + "--detach", + "--duration", + "2", + "--fps", + "30", + ]; + if enabled { + args.push("--window-only"); + } + let params = parse_record(&args); + let serialized = params.to_cli_args(); + assert_eq!(serialized.iter().any(|arg| arg == "--window-only"), enabled); + let mut worker_args = vec![ + "cap", + "record", + "__session-run", + "--session-id", + "synthetic-session", + ]; + worker_args.extend(serialized.iter().map(String::as_str)); + let worker = parse_record(&worker_args); + assert_eq!(worker.window_only, enabled); + assert_eq!(worker.to_cli_args(), serialized); + assert_eq!(worker.validate(), params.validate()); + } + } + assert!(parse_record(&["cap", "record", "--window", "247", "--window-only"]).window_only); + } #[cfg(unix)] #[tokio::test] @@ -1347,6 +1521,7 @@ mod tests { screen: None, window: None, }, + window_only: false, mode: RecordMode::Studio, camera: None, mic: None, diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs index 7b48083b99e..dca3715ec86 100644 --- a/crates/recording/src/instant_recording.rs +++ b/crates/recording/src/instant_recording.rs @@ -1610,7 +1610,16 @@ mod tests { let id: scap_targets::WindowId = "247".parse().unwrap(); let window = ScreenCaptureTarget::Window { id: id.clone() }; - assert_eq!(instant_capture_window(&window, true, false), Some(id)); + let builder = ActorBuilder::new(PathBuf::new(), window.clone()); + assert_eq!( + instant_capture_window(&window, builder.window_only, false), + None + ); + let builder = builder.with_window_only(true); + assert_eq!( + instant_capture_window(&window, builder.window_only, false), + Some(id) + ); assert_eq!(instant_capture_window(&window, true, true), None); assert_eq!(instant_capture_window(&window, false, false), None); assert_eq!( diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs index 93fa6ea1e26..66d150d8df9 100644 --- a/crates/recording/src/studio_recording.rs +++ b/crates/recording/src/studio_recording.rs @@ -3870,6 +3870,29 @@ mod tests { ChannelVideoSourceConfig, Muxer, SetupCtx, TaskPool, VideoFrame, VideoMuxer, }; + #[test] + fn studio_window_only_builder_selects_isolated_capture() { + let id: scap_targets::WindowId = "247".parse().unwrap(); + let target = screen_capture::ScreenCaptureTarget::Window { id: id.clone() }; + let builder = ActorBuilder::new(PathBuf::new(), target.clone()); + assert_eq!( + crate::window_only_target(&target, builder.window_only), + None + ); + let builder = builder.with_window_only(true); + assert_eq!( + crate::window_only_target(&target, builder.window_only), + Some(id) + ); + assert_eq!( + crate::window_only_target( + &screen_capture::ScreenCaptureTarget::CameraOnly, + builder.window_only + ), + None + ); + } + fn sidecar_response() -> CursorActorResponse { CursorActorResponse { cursors: [(