Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop): keep recording restart responsive during discarded teardown

Richie McIlroyca4f6cfa44ad
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index b18e96c06..5b1ceaafb 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -2639,10 +2639,23 @@ pub async fn restart_recording(
let _ = CurrentRecordingChanged.emit(&app);
let inputs = recording.inputs().clone();
+ let recording_dir = recording.recording_dir().clone();
- discard_recording(&app, recording).await?;
+ // Cleanup of the discarded recording must not block or abort the restart:
+ // the old recording is already cancelled at this point, and the new one
+ // writes to a fresh directory.
+ if let Some(video_id) = cancel_discarded_recording(&app, recording).await {
+ let app = app.clone();
+ tokio::spawn(async move {
+ if let Err(err) = delete_remote_instant_video(&app, &video_id).await {
+ warn!("Failed to delete remote instant video while restarting: {err}");
+ }
+ });
+ }
- tokio::time::sleep(Duration::from_millis(1000)).await;
+ if let Err(err) = remove_recording_dir(&recording_dir).await {
+ warn!("Failed to delete recording files while restarting: {err}");
+ }
start_recording(app.clone(), state, inputs).await
}
@@ -2660,6 +2673,10 @@ pub async fn delete_recording(app: AppHandle, state: MutableState<'_, App>) -> R
CurrentRecordingChanged.emit(&app).ok();
RecordingStopped {}.emit(&app).ok();
+ if let Some(window) = CapWindowId::RecordingControls.get(&app) {
+ let _ = window.hide();
+ }
+
let delete_result = discard_recording(&app, recording).await;
let settings = GeneralSettingsStore::get(&app)
@@ -2667,10 +2684,6 @@ pub async fn delete_recording(app: AppHandle, state: MutableState<'_, App>) -> R
.flatten()
.unwrap_or_default();
- if let Some(window) = CapWindowId::RecordingControls.get(&app) {
- let _ = window.hide();
- }
-
match settings.post_deletion_behaviour {
PostDeletionBehaviour::DoNothing => {}
PostDeletionBehaviour::ReopenRecordingWindow => {
diff --git a/apps/desktop/src/routes/in-progress-recording.tsx b/apps/desktop/src/routes/in-progress-recording.tsx
index 6e5467149..871420fbf 100644
--- a/apps/desktop/src/routes/in-progress-recording.tsx
+++ b/apps/desktop/src/routes/in-progress-recording.tsx
@@ -109,6 +109,7 @@ function InProgressRecordingInner() {
const [cameraWindowOpen, setCameraWindowOpen] = createSignal(false);
const [startingDismissed, setStartingDismissed] = createSignal(false);
const [stopRequested, setStopRequested] = createSignal(false);
+ const [teardownInFlight, setTeardownInFlight] = createSignal(false);
const [interactiveAreaRef, setInteractiveAreaRef] =
createSignal<HTMLDivElement | null>(null);
let settingsButtonRef: HTMLButtonElement | undefined;
@@ -255,6 +256,10 @@ function InProgressRecordingInner() {
});
createEffect(() => {
+ // While restart/delete teardown is running the query data is stale;
+ // reconciling against it would resurrect the discarded recording's state.
+ if (teardownInFlight()) return;
+
const s = state();
const recording = currentRecording.data as
| CurrentRecording
@@ -445,10 +450,13 @@ function InProgressRecordingInner() {
if (!shouldRestart) return;
- await handleRecordingResult(commands.restartRecording(), undefined);
-
- setState({ variant: "recording" });
- setTime(Date.now());
+ setTeardownInFlight(true);
+ setState({ variant: "initializing" });
+ try {
+ await handleRecordingResult(commands.restartRecording(), undefined);
+ } finally {
+ setTeardownInFlight(false);
+ }
},
}));
@@ -461,9 +469,14 @@ function InProgressRecordingInner() {
if (!shouldDelete) return;
- await commands.deleteRecording();
-
+ setTeardownInFlight(true);
setState({ variant: "stopped" });
+ void getCurrentWindow().hide();
+ try {
+ await commands.deleteRecording();
+ } finally {
+ setTeardownInFlight(false);
+ }
},
}));