Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Simplify export logic and add window-level cancel

Richie McIlroy8642e5e4e2f9
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rs
index 46d61dc7b..c8c7e460e 100644
--- a/apps/desktop/src-tauri/src/export.rs
+++ b/apps/desktop/src-tauri/src/export.rs
@@ -286,6 +286,13 @@ pub fn cancel_export(export_id: String) -> bool {
cancel_export_by_id(&export_id)
}
+#[tauri::command]
+#[specta::specta]
+#[instrument(skip(window))]
+pub fn cancel_current_window_exports(window: tauri::Window) {
+ cancel_exports_for_window(window.label());
+}
+
fn retain_export_session() {
let active_exports = ACTIVE_EXPORT_SESSIONS.fetch_add(1, Ordering::AcqRel) + 1;
info!(active_exports, "Export session guard started");
@@ -418,7 +425,7 @@ async fn run_out_of_process_export(
force_ffmpeg: bool,
cancel_token: CancellationToken,
) -> Result<PathBuf, String> {
- let mode = initial_export_worker_mode();
+ let mode = ExportWorkerMode::HardwareOptimized;
match run_out_of_process_export_attempt(
project_path,
settings,
@@ -430,7 +437,7 @@ async fn run_out_of_process_export(
.await
{
Ok(path) => Ok(path),
- Err(e) if e != "Export cancelled" && !mode.is_software_safe() => {
+ Err(e) if e != "Export cancelled" => {
error!(
error = %e,
"Export worker failed, retrying with software rendering and encoding"
@@ -449,14 +456,6 @@ async fn run_out_of_process_export(
}
}
-fn initial_export_worker_mode() -> ExportWorkerMode {
- if should_start_export_worker_in_software_safe_mode() {
- ExportWorkerMode::SoftwareSafe
- } else {
- ExportWorkerMode::HardwareOptimized
- }
-}
-
async fn run_out_of_process_export_attempt(
project_path: &Path,
settings: &ExportSettings,
@@ -901,19 +900,7 @@ fn is_frame_decode_error(error: &str) -> bool {
}
fn should_force_ffmpeg_export(_project_path: &Path, settings: &ExportSettings) -> bool {
- settings.force_ffmpeg_decoder() || should_use_windows_release_export_workaround()
-}
-
-fn should_force_ffmpeg_preview() -> bool {
- should_use_windows_release_export_workaround()
-}
-
-fn should_start_export_worker_in_software_safe_mode() -> bool {
- should_use_windows_release_export_workaround()
-}
-
-fn should_use_windows_release_export_workaround() -> bool {
- cfg!(all(target_os = "windows", not(debug_assertions)))
+ settings.force_ffmpeg_decoder()
}
fn should_use_out_of_process_export() -> bool {
@@ -1411,7 +1398,7 @@ async fn generate_export_preview_inner(
.map_err(|e| format!("Failed to create render constants: {e}"))?,
);
- let force_ffmpeg = should_force_ffmpeg_preview();
+ let force_ffmpeg = false;
info!(
project_path = %project_path.display(),
force_ffmpeg,
@@ -1625,7 +1612,7 @@ mod tests {
#[cfg(target_os = "windows")]
#[test]
- fn windows_exports_force_ffmpeg_in_release_builds_without_explicit_setting() {
+ fn windows_exports_do_not_force_ffmpeg_without_explicit_setting() {
let dir = tempdir().unwrap();
let gif_settings = ExportSettings::Gif(cap_export::gif::GifExportSettings {
@@ -1634,18 +1621,7 @@ mod tests {
quality: None,
});
- assert_eq!(
- should_force_ffmpeg_export(dir.path(), &gif_settings),
- !cfg!(debug_assertions)
- );
- }
-
- #[test]
- fn windows_release_safe_mode_gate_matches_target() {
- assert_eq!(
- should_start_export_worker_in_software_safe_mode(),
- cfg!(all(target_os = "windows", not(debug_assertions)))
- );
+ assert!(!should_force_ffmpeg_export(dir.path(), &gif_settings));
}
}
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index c5d37b91a..8d7a60850 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -4171,6 +4171,7 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
export::begin_export_session,
export::end_export_session,
export::cancel_export,
+ export::cancel_current_window_exports,
export::export_video,
export::export_video_with_id,
export::export_video_to_file,
@@ -4830,13 +4831,6 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
if app_is_exiting(app) {
return;
}
- if export::export_session_active() {
- warn!(
- window = label,
- "Skipping Destroyed cleanup during active export"
- );
- return;
- }
let window_id = CapWindowId::from_str(label).ok();
let is_editor_window = matches!(
window_id,
@@ -4846,6 +4840,13 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
if is_editor_window {
export::cancel_exports_for_window(label);
}
+ if export::export_session_active() {
+ warn!(
+ window = label,
+ "Skipping Destroyed cleanup during active export"
+ );
+ return;
+ }
if let Some(window_id) = window_id {
if matches!(window_id, CapWindowId::Camera) {
tracing::warn!("Camera window Destroyed event received!");
diff --git a/apps/desktop/src/utils/export.ts b/apps/desktop/src/utils/export.ts
index f4ec4af22..790deaecb 100644
--- a/apps/desktop/src/utils/export.ts
+++ b/apps/desktop/src/utils/export.ts
@@ -18,7 +18,6 @@ export function createExportTask(
settings: ExportSettings,
onProgress: (progress: FramesRendered) => void,
) {
- const exportId = createExportId();
const progress = new Channel<FramesRendered>((e) => {
onProgress(e);
});
@@ -35,24 +34,17 @@ export function createExportTask(
};
const cancel = () => {
if (closed) return;
- void cancelExport(exportId);
+ void cancelCurrentWindowExports();
closeProgress();
};
- const promise = invoke<string>("export_video_with_id", {
- projectPath,
- progress,
- settings,
- exportId,
- }).finally(closeProgress);
+ const promise = commands
+ .exportVideo(projectPath, progress, settings)
+ .finally(closeProgress);
return { promise, cancel };
}
-function createExportId() {
- return globalThis.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random()}`;
-}
-
-async function cancelExport(exportId: string) {
- await invoke<boolean>("cancel_export", { exportId }).catch((error) => {
+async function cancelCurrentWindowExports() {
+ await invoke("cancel_current_window_exports").catch((error) => {
console.error("Failed to cancel export", error);
});
}