hex/crates/capture-core/src/screenshot.rs
Kit Langton 4f3a45e03a
feat(screenshots): a recording toolbar, smaller movies with sound, and playback in the editor (#2166)
* Screenshots recordings: quality settings, copy as a file, export size (t1607)

Labs › Screenshots gets a Recording group (Scale Retina to 1x, Max resolution, Frame rate, Quality), each with a hex settings key; defaults 1x, Original, 30 fps, High (HEVC 0.50). The editor copies a recording's movie file, and its export-size pop-up (Original, 1080p, 720p) shows an estimate for the span kept, re-encoding off the UI thread with progress on the pressed button. (t1607)

* Screenshots: ⌘⇧5 recording toolbar with microphone and computer audio mixed into the movie (t1607)

* Screenshots: compact recording toolbar, editor playback and tidier toolbar (t1607)

* Screenshots: simplify the toolbar still, export-size save and playhead tick (t1607)

* docs: the compact recording toolbar and editor playback (t1607)

* Screenshots: filmstrip cells keep the movie's shape; mic-off icon is macOS-only (t1607)

* Screenshots: trim editor goldens for the transport and filmstrip (t1607)

* Screenshots: a trimmed export keeps a whole video track; reencode has a deadline (t1607)

* Screenshots: recording sound isn't silenced; toolbar choices apply at once and save in order (t1607)

* Screenshots: Return and Escape reach an open toolbar menu (t1607)

* Screenshots: a menu-only open of the chosen microphone; no microphone keeps no audio; Annotate may take the record key while the Lab is off (t1607)

* Screenshots: an export lands whole or not at all, and reports only to the recording it came from (t1607)

* Screenshots: keep the exact-device open and partial exports macOS-only (t1607)
2026-11-21 08:26:36 -08:00

73 lines
2.3 KiB
Rust

//! One still image of a source, through the same filter a recording would use.
use screencapturekit::screenshot_manager::{CGImageExt, SCScreenshotManager};
use screencapturekit::shareable_content::SCShareableContent;
use screencapturekit::stream::configuration::SCStreamConfiguration;
use crate::error::Error;
use crate::native::Target;
use crate::plan::{Encoder, Exclusions, Source, SourcePlan};
/// A screenshot, tightly packed RGBA.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Image {
pub width: u32,
pub height: u32,
pub rgba: Vec<u8>,
}
/// Takes one screenshot of `source` with `ScreenCaptureKit`, leaving out `exclude` as a recording
/// would, with its long edge at most `long_edge` pixels (`None`: the source's own pixels). The
/// cursor is drawn.
///
/// # Errors
///
/// Returns an error for a missing source or permission, an empty region, or a failed capture.
pub fn screenshot(
source: &Source,
exclude: &Exclusions,
long_edge: Option<u32>,
) -> Result<Image, Error> {
screenshot_with(source, exclude, long_edge, true)
}
/// [`screenshot`], drawing the cursor only with `cursor`.
///
/// # Errors
///
/// As [`screenshot`].
pub fn screenshot_with(
source: &Source,
exclude: &Exclusions,
long_edge: Option<u32>,
cursor: bool,
) -> Result<Image, Error> {
let content =
SCShareableContent::get().map_err(|error| Error::ShareableContent(error.to_string()))?;
let target = Target::find(
&content,
SourcePlan {
source: *source,
long_edge,
encoder: Encoder::default(),
quality: crate::HEVC_QUALITY,
},
exclude,
)?;
let config = target.configure(SCStreamConfiguration::new().with_shows_cursor(cursor));
let image = SCScreenshotManager::capture_image(&target.filter, &config)
.map_err(|error| Error::Screenshot(error.to_string()))?;
let width = u32::try_from(image.width()).map_err(|_| Error::InvalidDimensions)?;
let height = u32::try_from(image.height()).map_err(|_| Error::InvalidDimensions)?;
if width == 0 || height == 0 {
return Err(Error::Screenshot("empty image".into()));
}
let rgba = image
.rgba_data()
.map_err(|error| Error::Screenshot(error.to_string()))?;
Ok(Image {
width,
height,
rgba,
})
}