Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions apps/desktop-gpui/src/app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6775,8 +6775,13 @@ pub fn prepare_for_screenshot_capture(cx: &mut App) -> bool {
/// the Tauri app -- and open the screenshot editor on the new bundle, the
/// `automationShouldOpenScreenshotEditor` default. A failure reveals the main
/// window if the picker had hidden it, since nothing else will.
pub fn screenshot_finished(captured: Option<PathBuf>, cx: &mut App) {
let Some(png) = captured else {
pub fn screenshot_finished(captured: Option<crate::screenshot::CapturedScreenshot>, cx: &mut App) {
let Some(crate::screenshot::CapturedScreenshot {
png,
source,
behaviour,
}) = captured
else {
let hidden = std::mem::take(&mut cx.global_mut::<AppWindows>().main_hidden_for_picker);
if hidden && RecordingSession::global(cx).read(cx).phase == Phase::Idle {
show_main_window(cx);
Expand All @@ -6794,7 +6799,6 @@ pub fn screenshot_finished(captured: Option<PathBuf>, cx: &mut App) {
main.update(cx, |view, window, cx| view.refresh_open_library(window, cx))
.ok();

let behaviour = crate::store::GeneralSettings::load().post_screenshot_behaviour;
if behaviour.copies() {
let copied = platform::copy_image_to_clipboard(png.clone(), cx);
cx.spawn(async move |_| {
Expand All @@ -6810,7 +6814,7 @@ pub fn screenshot_finished(captured: Option<PathBuf>, cx: &mut App) {
// recording hands off to the video editor; `screenshot_editor_closed`
// brings the main window back.
cx.global_mut::<AppWindows>().main_hidden_for_picker = false;
open_screenshot_editor(png, cx);
open_screenshot_editor_with_source(png, source, cx);
return;
}

Expand All @@ -6832,6 +6836,14 @@ pub fn screenshot_finished(captured: Option<PathBuf>, cx: &mut App) {
/// Must be reached through `cx.defer` from anything inside an entity update:
/// opening a window paints it synchronously and would double-lease the caller.
pub fn open_screenshot_editor(path: PathBuf, cx: &mut App) {
open_screenshot_editor_with_source(path, None, cx);
}

fn open_screenshot_editor_with_source(
path: PathBuf,
source: Option<screenshot_editor::LoadedSource>,
cx: &mut App,
) {
#[cfg(target_os = "linux")]
if defer_window_until_capture_safe(cx) {
return;
Expand Down Expand Up @@ -6935,7 +6947,7 @@ pub fn open_screenshot_editor(path: PathBuf, cx: &mut App) {
// only showed up once Cap was clicked.
cx.defer(move |cx| bring_screenshot_editor_forward(handle, cx));
prewarm_shared_wgpu_device(cx);
screenshot_editor::load_screenshot_project(key, handle, cx);
screenshot_editor::load_screenshot_project(key, handle, source, cx);
}

fn bring_screenshot_editor_forward(handle: WindowHandle<ScreenshotEditorWindow>, cx: &mut App) {
Expand Down
179 changes: 154 additions & 25 deletions apps/desktop-gpui/src/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ use anyhow::{Context as _, anyhow};
use cap_recording::sources::screen_capture::ScreenCaptureTarget;
use gpui::App;

use crate::app_windows;
use crate::{app_windows, screenshot_editor::LoadedSource};

pub struct CapturedScreenshot {
pub png: PathBuf,
pub source: Option<LoadedSource>,
pub behaviour: crate::store::PostScreenshotBehaviour,
}

pub fn take_screenshot(target: ScreenCaptureTarget, cx: &mut App) {
if matches!(target, ScreenCaptureTarget::CameraOnly) {
Expand All @@ -28,7 +34,10 @@ pub fn take_screenshot(target: ScreenCaptureTarget, cx: &mut App) {
return;
}
tracing::info!(kind = target.kind_str(), "taking a screenshot");

let behaviour = crate::store::GeneralSettings::load().post_screenshot_behaviour;
if behaviour.opens_editor() {
app_windows::prewarm_shared_wgpu_device(cx);
}
let settle = app_windows::prepare_for_screenshot_capture(cx);

let task = gpui_tokio::Tokio::spawn_result(cx, async move {
Expand All @@ -38,15 +47,16 @@ pub fn take_screenshot(target: ScreenCaptureTarget, cx: &mut App) {
// the compositor needs a beat to actually drop them from screen.
tokio::time::sleep(Duration::from_millis(150)).await;
}
capture_and_write(target).await
capture_and_write(target, behaviour.opens_editor()).await
});

cx.spawn(async move |cx| {
let captured = match task.await {
Ok(png) => {
tracing::info!(path = %png.display(), "screenshot captured");
Some(png)
}
Ok((png, source)) => Some(CapturedScreenshot {
png,
source,
behaviour,
}),
Err(error) => {
tracing::error!("screenshot capture failed: {error:#}");
None
Expand All @@ -57,16 +67,12 @@ pub fn take_screenshot(target: ScreenCaptureTarget, cx: &mut App) {
.detach();
}

/// Capture, then write the `.cap` bundle. Returns the PNG path, which is what
/// the Tauri command returns (`NewScreenshotAdded.path` is the PNG, not the
/// bundle).
async fn capture_and_write(target: ScreenCaptureTarget) -> anyhow::Result<PathBuf> {
let image = cap_recording::screenshot::capture_screenshot(target.clone())
.await
.context("capturing the screenshot")?;

crate::app_sounds::AppSound::Notification.play();

fn create_capture_project(
target: &ScreenCaptureTarget,
) -> anyhow::Result<(
cap_project::RecordingMeta,
cap_project::ProjectConfiguration,
)> {
let base = crate::library::screenshots_dir();
std::fs::create_dir_all(&base)
.with_context(|| format!("creating screenshots dir {}", base.display()))?;
Expand All @@ -93,7 +99,7 @@ async fn capture_and_write(target: ScreenCaptureTarget) -> anyhow::Result<PathBu
let meta = cap_project::RecordingMeta {
platform: Some(cap_project::Platform::default()),
project_path: bundle.clone(),
pretty_name,
pretty_name: pretty_name.clone(),
sharing: None,
inner: cap_project::RecordingMetaInner::Studio(Box::new(
cap_project::StudioRecordingMeta::SingleSegment {
Expand All @@ -113,7 +119,7 @@ async fn capture_and_write(target: ScreenCaptureTarget) -> anyhow::Result<PathBu
upload: None,
};
meta.save_for_project()
.map_err(|e| anyhow!("saving screenshot meta: {e}"))?;
.map_err(|error| anyhow!("saving screenshot meta: {error}"))?;

let mut config = cap_project::ProjectConfiguration::default();
config.background.source = cap_project::BackgroundSource::Color {
Expand All @@ -123,17 +129,52 @@ async fn capture_and_write(target: ScreenCaptureTarget) -> anyhow::Result<PathBu
config.background.shadow = 0.0;
config
.write(&bundle)
.map_err(|e| anyhow!("saving screenshot project config: {e}"))?;
.map_err(|error| anyhow!("saving screenshot project config: {error}"))?;

Ok((meta, config))
}

let png = bundle.join("original.png");
write_png(png.clone(), image).await?;
Ok(png)
async fn capture_and_write(
target: ScreenCaptureTarget,
retain_rgba: bool,
) -> anyhow::Result<(PathBuf, Option<LoadedSource>)> {
let image = cap_recording::screenshot::capture_screenshot(target.clone())
.await
.context("capturing the screenshot")?;
crate::app_sounds::AppSound::Notification.play();
let (meta, config) = create_capture_project(&target)?;
let png = meta.project_path.join("original.png");
let width = image.width();
let height = image.height();
let rgba = write_png(png.clone(), image, retain_rgba).await?;
tracing::info!(path = %png.display(), "screenshot captured");
let source = if let Some(rgba) = rgba {
let cap_project::RecordingMetaInner::Studio(studio_meta) = &meta.inner else {
return Err(anyhow!("screenshot metadata is not a studio project"));
};
Some(LoadedSource {
rgba: std::sync::Arc::new(rgba),
width,
height,
pretty_name: meta.pretty_name.clone(),
config,
studio_meta: (**studio_meta).clone(),
meta,
})
} else {
None
};
Ok((png, source))
}

/// The Tauri encode settings (`recording.rs:2987-3016`): RGB stays RGB, a
/// capture past 8MP trades compression for speed, adaptive filtering either
/// way. Blocking work, so off the runtime workers.
async fn write_png(path: PathBuf, image: image::DynamicImage) -> anyhow::Result<()> {
async fn write_png(
path: PathBuf,
image: image::DynamicImage,
retain_rgba: bool,
) -> anyhow::Result<Option<Vec<u8>>> {
tokio::task::spawn_blocking(move || {
let width = image.width();
let height = image.height();
Expand All @@ -146,6 +187,7 @@ async fn write_png(path: PathBuf, image: image::DynamicImage) -> anyhow::Result<
} else {
image::codecs::png::CompressionType::Default
};
let rgba = retain_rgba.then(|| image.to_rgba8().into_raw());
let data = image.into_bytes();

let file =
Expand All @@ -159,8 +201,95 @@ async fn write_png(path: PathBuf, image: image::DynamicImage) -> anyhow::Result<
compression,
image::codecs::png::FilterType::Adaptive,
)
.context("encoding the screenshot PNG")
.context("encoding the screenshot PNG")?;
Ok(rgba)
})
.await
.context("the PNG encode task died")?
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn retained_capture_pixels_match_saved_rgb_and_alpha() {
let directory = tempfile::tempdir().unwrap();
let images = [
image::DynamicImage::ImageRgb8(image::RgbImage::from_fn(4, 2, |x, y| {
image::Rgb([x as u8 * 40, y as u8 * 80, 173])
})),
image::DynamicImage::ImageRgba8(image::RgbaImage::from_fn(4, 2, |x, y| {
image::Rgba([x as u8 * 40, y as u8 * 80, 173, x as u8 * 70])
})),
];
for (index, image) in images.into_iter().enumerate() {
let path = directory.path().join(format!("{index}.png"));
let expected = image.to_rgba8().into_raw();
let retained = write_png(path.clone(), image, true).await.unwrap().unwrap();
let saved = image::open(path).unwrap().to_rgba8().into_raw();
assert_eq!(retained, expected);
assert_eq!(saved, retained);
}
}

#[tokio::test]
async fn oversized_capture_pixels_can_still_be_saved() {
let directory = tempfile::tempdir().unwrap();
for (width, height) in [(16_385, 2), (4, 16_385)] {
let path = directory.path().join("original.png");
let image = image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(
width,
height,
image::Rgba([13, 71, 129, 255]),
));
let retained = write_png(path.clone(), image, true).await.unwrap().unwrap();
let saved = image::open(&path).unwrap().to_rgba8();
assert_eq!(saved.dimensions(), (width, height));
assert_eq!(saved.as_raw(), &retained);
assert!(matches!(
crate::screenshot_editor::load_source(directory.path()),
Err(message) if message == format!("Image dimensions exceed maximum: {width}x{height}")
));
assert!(path.exists());
}
}

#[tokio::test]
async fn copy_and_save_only_encode_without_retaining_pixels() {
let directory = tempfile::tempdir().unwrap();
for (index, image) in [
image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(
4,
2,
image::Rgb([13, 71, 129]),
)),
image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(
4,
2,
image::Rgba([13, 71, 129, 83]),
)),
]
.into_iter()
.enumerate()
{
let path = directory.path().join(format!("{index}.png"));
let expected = image.to_rgba8();
assert!(
write_png(path.clone(), image, false)
.await
.unwrap()
.is_none()
);
assert_eq!(image::open(path).unwrap().to_rgba8(), expected);
}
}

#[tokio::test]
async fn failed_capture_save_does_not_deliver_pixels() {
let directory = tempfile::tempdir().unwrap();
let image = image::DynamicImage::ImageRgba8(image::RgbaImage::new(4, 2));
let result = write_png(directory.path().to_path_buf(), image, true).await;
assert!(result.is_err());
}
}
Loading
Loading