Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: restore GPUI controls after editor deletion

Richie McIlroyd189567c08c8
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 2ea1c7a70..06c93e602 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -8,6 +8,7 @@
//! main window whenever the session comes back to rest.
use std::{
+ collections::HashSet,
path::{Path, PathBuf},
sync::Arc,
};
@@ -59,6 +60,7 @@ pub struct AppWindows {
/// (`windows.rs:3656-3659`). The Tauri app keys its label off an
/// incrementing id; the path is the identity in both.
pub editors: Vec<(PathBuf, WindowHandle<EditorWindow>)>,
+ deleting_editors: HashSet<PathBuf>,
/// One screenshot editor per `.cap` bundle -- the gpui spelling of
/// `ScreenshotEditorWindowIds`, keyed by the bundle directory.
pub screenshot_editors: Vec<(PathBuf, WindowHandle<ScreenshotEditorWindow>)>,
@@ -337,6 +339,7 @@ pub fn init(main: WindowHandle<MainWindow>, session: Entity<RecordingSession>, c
teleprompter: None,
overlays: Vec::new(),
editors: Vec::new(),
+ deleting_editors: HashSet::new(),
screenshot_editors: Vec::new(),
main_hidden_for_picker: false,
editor_hidden_for_picker: None,
@@ -787,6 +790,23 @@ pub fn handle_dock_reopen(cx: &mut App) {
if !cx.has_global::<AppWindows>() {
return;
}
+ let live = cx
+ .windows()
+ .into_iter()
+ .map(|handle| handle.window_id())
+ .collect::<HashSet<_>>();
+ {
+ let windows = cx.global_mut::<AppWindows>();
+ windows
+ .editors
+ .retain(|(_, handle)| live.contains(&handle.window_id()));
+ windows
+ .screenshot_editors
+ .retain(|(_, handle)| live.contains(&handle.window_id()));
+ windows.settings = windows
+ .settings
+ .filter(|handle| live.contains(&handle.window_id()));
+ }
let windows = cx.global::<AppWindows>();
if crate::store::should_show_onboarding() {
if windows.onboarding.is_some() {
@@ -798,30 +818,61 @@ pub fn handle_dock_reopen(cx: &mut App) {
cx.activate(true);
return;
}
- let focus: Option<gpui::AnyWindowHandle> = windows
+ let candidates = windows
.editors
- .first()
- .map(|(_, handle)| (*handle).into())
- .or_else(|| windows.settings.map(Into::into));
+ .iter()
+ .map(|(_, handle)| gpui::AnyWindowHandle::from(*handle))
+ .chain(
+ windows
+ .screenshot_editors
+ .iter()
+ .map(|(_, handle)| (*handle).into()),
+ )
+ .chain(windows.settings.map(Into::into))
+ .collect::<Vec<_>>();
+ let focus = first_registered_reopen_target(candidates, &live);
tracing::info!(focus_existing = focus.is_some(), "dock reopen");
match focus {
Some(handle) => {
- let native = handle
- .update(cx, |_, window, _| platform::native_window(window))
- .ok()
- .flatten();
- cx.spawn(async move |_| {
- if let Some(native) = &native {
- platform::show_native(native);
+ cx.defer(move |cx| {
+ if !cx
+ .windows()
+ .iter()
+ .any(|current| current.window_id() == handle.window_id())
+ {
+ handle_dock_reopen(cx);
+ return;
}
- })
- .detach();
+ let native = match handle.update(cx, |_, window, _| platform::native_window(window))
+ {
+ Ok(native) => native,
+ Err(error) => {
+ tracing::warn!(%error, "could not focus the registered editor window");
+ return;
+ }
+ };
+ cx.spawn(async move |_| {
+ if let Some(native) = &native {
+ platform::show_native(native);
+ }
+ })
+ .detach();
+ });
}
None => show_main_window(cx),
}
cx.activate(true);
}
+fn first_registered_reopen_target(
+ candidates: impl IntoIterator<Item = gpui::AnyWindowHandle>,
+ live: &HashSet<gpui::WindowId>,
+) -> Option<gpui::AnyWindowHandle> {
+ candidates
+ .into_iter()
+ .find(|candidate| live.contains(&candidate.window_id()))
+}
+
/// Open the settings window on a page, and hide the main window.
///
/// The header gear in `new-main/index.tsx` is
@@ -2137,6 +2188,7 @@ fn own_windows(cx: &mut App) -> Vec<OwnWindowHandle> {
teleprompter,
overlays,
editors,
+ deleting_editors: _,
screenshot_editors,
main_hidden_for_picker: _,
editor_hidden_for_picker: _,
@@ -3992,6 +4044,10 @@ pub fn open_editor(project_path: PathBuf, cx: &mut App) {
return;
}
let key = editor_key(&project_path);
+ if cx.global::<AppWindows>().deleting_editors.contains(&key) {
+ tracing::info!(path = %key.display(), "recording deletion is still settling; editor remains closed");
+ return;
+ }
if cx
.global::<AppWindows>()
@@ -4757,30 +4813,43 @@ fn reveal_main_after_editor_close(editors_left: usize, settings_open: bool, idle
/// `restore_main_windows_if_no_editors` (`lib.rs:5777-5792`) -- so the main
/// window comes back only once the last editor has closed, and never over an
/// open Settings window ([`reveal_main_after_editor_close`]).
-pub fn editor_closed(project_path: &Path, cx: &mut App) {
+pub fn editor_closed(project_path: &Path, window_id: gpui::WindowId, cx: &mut App) {
let key = editor_key(project_path);
- let handle = {
- let editors = &mut cx.global_mut::<AppWindows>().editors;
- let index = editors.iter().position(|(path, _)| path == &key);
- index.map(|index| editors.remove(index).1)
+ let Some(handle) =
+ take_editor_window(&mut cx.global_mut::<AppWindows>().editors, &key, window_id)
+ else {
+ return;
};
- if let Some(handle) = handle {
- // `onCleanup(() => { clearTimeout(saveTimer); flushProjectConfig() })`
- // (`ED/context.ts:1246-1252`): a `.cap` closed inside the 250ms save
- // debounce still gets its last edit written.
- if let Ok(pending) = handle.update(cx, |view, _window, _cx| view.pending_save()) {
- pending.borrow_mut().flush();
- }
- let instance = handle
- .update(cx, |view, _window, _cx| view.take_instance())
- .ok()
- .flatten();
- if let Some(instance) = instance {
- gpui_tokio::Tokio::spawn(cx, async move { instance.dispose().await }).detach();
- }
+ // `onCleanup(() => { clearTimeout(saveTimer); flushProjectConfig() })`
+ // (`ED/context.ts:1246-1252`): a `.cap` closed inside the 250ms save
+ // debounce still gets its last edit written.
+ if let Ok(pending) = handle.update(cx, |view, _window, _cx| view.pending_save()) {
+ pending.borrow_mut().flush();
+ }
+ let instance = handle
+ .update(cx, |view, _window, _cx| view.take_instance())
+ .ok()
+ .flatten();
+ if let Some(instance) = instance {
+ gpui_tokio::Tokio::spawn(cx, async move { instance.dispose().await }).detach();
}
+ restore_after_editor_close(&key, cx);
+}
+
+fn take_editor_window(
+ editors: &mut Vec<(PathBuf, WindowHandle<EditorWindow>)>,
+ key: &Path,
+ window_id: gpui::WindowId,
+) -> Option<WindowHandle<EditorWindow>> {
+ let index = editors
+ .iter()
+ .position(|(path, handle)| path == key && handle.window_id() == window_id)?;
+ Some(editors.remove(index).1)
+}
+
+fn restore_after_editor_close(key: &Path, cx: &mut App) {
let session = RecordingSession::global(cx);
if session.read(cx).phase == Phase::Idle
&& session
@@ -4814,6 +4883,147 @@ pub fn editor_closed(project_path: &Path, cx: &mut App) {
}
}
+fn editor_deletion_allowed(
+ project_path: &Path,
+ handle: WindowHandle<EditorWindow>,
+ cx: &App,
+) -> Result<(), String> {
+ if !crate::library::known_recordings_dirs().iter().any(|root| {
+ root.canonicalize()
+ .is_ok_and(|root| project_path != root && project_path.starts_with(root))
+ }) {
+ return Err(
+ "This recording is outside the recordings library. Its files were not changed.".into(),
+ );
+ }
+ let windows = cx.global::<AppWindows>();
+ if windows.deleting_editors.contains(project_path) {
+ return Err("This recording is already being deleted.".into());
+ }
+ if !windows
+ .editors
+ .iter()
+ .any(|(path, current)| path == project_path && *current == handle)
+ {
+ return Err(
+ "The editor changed while deletion was being confirmed. The recording was not deleted."
+ .into(),
+ );
+ }
+ let session = RecordingSession::global(cx);
+ if session.read(cx).phase != Phase::Idle
+ || session.read(cx).editor_recording_target().is_some()
+ || clean_capture_owned(cx)
+ {
+ return Err("Finish or cancel the recording before deleting this project.".into());
+ }
+ let editor = handle
+ .read(cx)
+ .map_err(|_| "The editor has already closed. The recording was not deleted.")?;
+ if let Some(reason) = editor.deletion_blocked_reason() {
+ return Err(reason.into());
+ }
+ Ok(())
+}
+
+pub(crate) fn request_editor_deletion(
+ project_path: PathBuf,
+ handle: WindowHandle<EditorWindow>,
+ cx: &mut App,
+) {
+ let key = editor_key(&project_path);
+ cx.spawn(async move |cx| {
+ if let Err(error) = cx.update(|cx| editor_deletion_allowed(&key, handle, cx)) {
+ platform::alert_dialog("Recording retained", &error);
+ return;
+ }
+ if !platform::confirm_dialog("Cap", "Are you sure you want to delete this recording?", "Yes", "No", false) {
+ return;
+ }
+ let prepared = cx.update(|cx| {
+ editor_deletion_allowed(&key, handle, cx)?;
+ let ownership = cap_recording::upload_resume::UploadLock::acquire(&key)
+ .map_err(|error| format!("This recording is still in use. Finish or cancel its upload and try again: {error}"))?;
+ let windows = cx.global_mut::<AppWindows>();
+ windows.deleting_editors.insert(key.clone());
+ windows.editors.retain(|(_, current)| *current != handle);
+ let result = handle.update(cx, |view, window, cx| {
+ let instance = view.prepare_for_deletion(cx)?;
+ window.remove_window();
+ Ok::<_, String>(instance)
+ });
+ match result {
+ Ok(Ok(instance)) => {
+ restore_after_editor_close(&key, cx);
+ Ok((instance, ownership))
+ }
+ Ok(Err(error)) => {
+ let windows = cx.global_mut::<AppWindows>();
+ windows.deleting_editors.remove(&key);
+ windows.editors.push((key.clone(), handle));
+ Err(error)
+ }
+ Err(error) => {
+ cx.global_mut::<AppWindows>().deleting_editors.remove(&key);
+ Err(format!("Could not close the editor: {error}"))
+ }
+ }
+ });
+ let (instance, ownership) = match prepared {
+ Ok(prepared) => prepared,
+ Err(error) => {
+ platform::alert_dialog("Recording retained", &error);
+ return;
+ }
+ };
+ let deleting = key.clone();
+ let task = cx.update(|cx| gpui_tokio::Tokio::spawn(cx, async move {
+ delete_after_editor_cleanup(
+ async move {
+ if let Some(instance) = instance {
+ for segment in instance.segment_medias.iter() {
+ for audio in [&segment.audio, &segment.system_audio] {
+ if let Err(error) = audio.get().await {
+ tracing::debug!(%error, "audio reader settled with an error before deletion");
+ }
+ }
+ }
+ instance.dispose().await;
+ }
+ drop(ownership);
+ Ok(())
+ },
+ crate::upload::queue::delete_recording(deleting),
+ ).await
+ }));
+ let notice = cx.background_executor().timer(std::time::Duration::from_secs(10));
+ futures_util::pin_mut!(task, notice);
+ let result = match futures_util::future::select(&mut task, &mut notice).await {
+ futures_util::future::Either::Left((result, _)) => result,
+ futures_util::future::Either::Right(_) => {
+ platform::alert_dialog("Deletion is still finishing", "Cap is waiting for recording cleanup to finish before completing this deletion.");
+ task.await
+ }
+ }.unwrap_or_else(|error| Err(format!("Recording deletion failed: {error}")));
+ cx.update(|cx| {
+ cx.global_mut::<AppWindows>().deleting_editors.remove(&key);
+ refresh_library_after_delete(cx);
+ });
+ if let Err(error) = result {
+ tracing::error!(path = %key.display(), %error, "recording deletion failed");
+ platform::alert_dialog("Could not delete recording", &error);
+ }
+ }).detach();
+}
+
+async fn delete_after_editor_cleanup(
+ cleanup: impl Future<Output = Result<(), String>>,
+ delete: impl Future<Output = Result<(), String>>,
+) -> Result<(), String> {
+ cleanup.await?;
+ delete.await
+}
+
/// `addExistingRecordingToEditor` ends with `EditorInstances::remove(window)`
/// and the sidebar reloads the webview (`ClipsSidebar.tsx:513-517`,
/// `import.rs:1892`): the same bundle, re-read from disk. The native
@@ -4824,6 +5034,9 @@ pub fn editor_closed(project_path: &Path, cx: &mut App) {
/// between.
pub fn reload_editor(project_path: &Path, cx: &mut App) {
let key = editor_key(project_path);
+ if cx.global::<AppWindows>().deleting_editors.contains(&key) {
+ return;
+ }
let handle = {
let editors = &mut cx.global_mut::<AppWindows>().editors;
let index = editors.iter().position(|(path, _)| path == &key);
@@ -5415,6 +5628,96 @@ mod tests {
use super::*;
use crate::store::{DEFAULT_EXCLUDED_WINDOW_TITLES, WindowExclusion, default_excluded_windows};
+ #[test]
+ fn dock_reopen_uses_registered_windows_and_falls_back_after_delete() {
+ let editor = WindowHandle::<EditorWindow>::new(1_u64.into());
+ let screenshot = WindowHandle::<ScreenshotEditorWindow>::new(2_u64.into());
+ let settings = WindowHandle::<SettingsWindow>::new(3_u64.into());
+ let candidates = [editor.into(), screenshot.into(), settings.into()];
+ let mut live = HashSet::from([editor.window_id(), settings.window_id()]);
+ assert_eq!(
+ first_registered_reopen_target(candidates, &live),
+ Some(editor.into())
+ );
+ live.remove(&editor.window_id());
+ assert_eq!(
+ first_registered_reopen_target(candidates, &live),
+ Some(settings.into())
+ );
+ live.clear();
+ assert_eq!(first_registered_reopen_target(candidates, &live), None);
+ }
+
+ #[test]
+ fn old_editor_close_preserves_a_reopened_window_for_the_same_project() {
+ let path = PathBuf::from("recording.cap");
+ let old = WindowHandle::<EditorWindow>::new(1_u64.into());
+ let current = WindowHandle::<EditorWindow>::new(2_u64.into());
+ let other = WindowHandle::<EditorWindow>::new(3_u64.into());
+ let mut editors = vec![(path.clone(), current), (PathBuf::from("other.cap"), other)];
+ assert_eq!(
+ take_editor_window(&mut editors, &path, old.window_id()),
+ None
+ );
+ assert_eq!(editors.len(), 2);
+ assert_eq!(
+ take_editor_window(&mut editors, &path, current.window_id()),
+ Some(current)
+ );
+ assert_eq!(editors, vec![(PathBuf::from("other.cap"), other)]);
+ }
+
+ struct DeleteFixture(PathBuf);
+
+ impl DeleteFixture {
+ fn new() -> Self {
+ let directory = std::env::temp_dir()
+ .join(format!("cap-editor-delete-{}", crate::store::new_uuid_v4()));
+ std::fs::create_dir(&directory).unwrap();
+ std::fs::write(directory.join("source.mp4"), b"retained recording bytes").unwrap();
+ Self(directory)
+ }
+ }
+
+ impl Drop for DeleteFixture {
+ fn drop(&mut self) {
+ let _ = std::fs::remove_dir_all(&self.0);
+ }
+ }
+
+ #[tokio::test]
+ async fn recording_files_are_not_deleted_before_editor_cleanup_finishes() {
+ let fixture = DeleteFixture::new();
+ let source = fixture.0.join("source.mp4");
+ let (complete, completed) = tokio::sync::oneshot::channel();
+ let deletion = delete_after_editor_cleanup(
+ async { completed.await.map_err(|error| error.to_string()) },
+ async { std::fs::remove_file(&source).map_err(|error| error.to_string()) },
+ );
+ tokio::pin!(deletion);
+ tokio::select! {
+ biased;
+ _ = &mut deletion => panic!("deletion ran before cleanup completed"),
+ _ = tokio::task::yield_now() => {}
+ }
+ assert_eq!(std::fs::read(&source).unwrap(), b"retained recording bytes");
+ complete.send(()).unwrap();
+ deletion.await.unwrap();
+ assert!(!source.exists());
+ }
+
+ #[tokio::test]
+ async fn failed_editor_cleanup_leaves_original_recording_bytes_untouched() {
+ let fixture = DeleteFixture::new();
+ let source = fixture.0.join("source.mp4");
+ let result = delete_after_editor_cleanup(async { Err("cleanup failed".into()) }, async {
+ std::fs::remove_file(&source).map_err(|error| error.to_string())
+ })
+ .await;
+ assert_eq!(result, Err("cleanup failed".into()));
+ assert_eq!(std::fs::read(source).unwrap(), b"retained recording bytes");
+ }
+
#[cfg(target_os = "linux")]
fn pending_clean_capture(mode: RecordingMode, wayland: bool) -> CleanCaptureUi {
CleanCaptureUi {
diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs
index 55e884f0a..0ce3cf575 100644
--- a/apps/desktop-gpui/src/editor_clips.rs
+++ b/apps/desktop-gpui/src/editor_clips.rs
@@ -340,6 +340,12 @@ impl Default for ClipsState {
}
}
+impl ClipsState {
+ pub(crate) fn is_importing(&self) -> bool {
+ self.importing
+ }
+}
+
impl Drop for ClipsState {
fn drop(&mut self) {
if let Some(cancelled) = &self.import_cancelled {
diff --git a/apps/desktop-gpui/src/editor_export.rs b/apps/desktop-gpui/src/editor_export.rs
index 2deb563b2..ceab87184 100644
--- a/apps/desktop-gpui/src/editor_export.rs
+++ b/apps/desktop-gpui/src/editor_export.rs
@@ -177,6 +177,7 @@ pub struct ExportUi {
pub preview_error: Option<String>,
pub preview_task: Option<gpui::Task<()>>,
pub phase: ExportPhase,
+ close_requested: bool,
pub rendered: u32,
pub total_frames: u32,
pub output_path: Option<PathBuf>,
@@ -227,6 +228,7 @@ impl ExportUi {
preview_error: None,
preview_task: None,
phase: ExportPhase::Idle,
+ close_requested: false,
rendered: 0,
total_frames: 0,
output_path: None,
@@ -337,8 +339,12 @@ impl EditorWindow {
pub(crate) fn close_export(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if let Some(ui) = self.export.as_mut() {
if ui.phase.is_busy() {
+ ui.close_requested = true;
ui.cancel.store(true, Ordering::Relaxed);
+ cx.notify();
+ return;
}
+ ui.sign_in_cancel.store(true, Ordering::Relaxed);
if let Some(image) = ui.preview.take() {
let _ = window.drop_image(image);
}
@@ -348,6 +354,16 @@ impl EditorWindow {
cx.notify();
}
+ fn finish_requested_export_close(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ if self
+ .export
+ .as_ref()
+ .is_some_and(|ui| ui.close_requested && !ui.phase.is_busy())
+ {
+ self.close_export(window, cx);
+ }
+ }
+
fn copy_share_link(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some(link) = self.export.as_ref().and_then(|ui| ui.share_link.clone()) else {
return;
@@ -488,7 +504,7 @@ impl EditorWindow {
let Some(ui) = self.export.as_mut() else {
return;
};
- if ui.phase.is_busy() {
+ if ui.phase.is_busy() || ui.close_requested {
return;
}
if ui.destination == ExportDestination::Link {
@@ -517,7 +533,7 @@ impl EditorWindow {
let force = ui.force_ffmpeg;
let project_path = self.project_path.clone();
let project = self.project.clone();
- ui.cancel.store(false, Ordering::Relaxed);
+ ui.cancel = Arc::new(AtomicBool::new(false));
let cancel = ui.cancel.clone();
ui.phase = if destination == ExportDestination::File {
ExportPhase::ChoosingFile
@@ -550,6 +566,9 @@ impl EditorWindow {
}
cx.notify();
});
+ let _ = this.update_in(cx, |this, window, cx| {
+ this.finish_requested_export_close(window, cx)
+ });
return;
}
chosen
@@ -571,6 +590,9 @@ impl EditorWindow {
true
});
if !started.unwrap_or(false) {
+ let _ = this.update_in(cx, |this, window, cx| {
+ this.finish_requested_export_close(window, cx)
+ });
return;
}
@@ -682,6 +704,9 @@ impl EditorWindow {
});
}
}
+ let _ = this.update_in(cx, |this, window, cx| {
+ this.finish_requested_export_close(window, cx)
+ });
}));
}
@@ -762,7 +787,7 @@ impl EditorWindow {
let Some(ui) = self.export.as_mut() else {
return;
};
- if ui.phase.is_busy() {
+ if ui.phase.is_busy() || ui.close_requested {
return;
}
@@ -788,7 +813,7 @@ impl EditorWindow {
.first()
.map(|org| org.id.clone())
});
- ui.cancel.store(false, Ordering::Relaxed);
+ ui.cancel = Arc::new(AtomicBool::new(false));
let cancel = ui.cancel.clone();
ui.phase = ExportPhase::Starting;
ui.error = None;
@@ -976,6 +1001,9 @@ impl EditorWindow {
show_upload_failure(&this, cx, "Export task failed", false);
}
}
+ let _ = this.update_in(cx, |this, window, cx| {
+ this.finish_requested_export_close(window, cx)
+ });
}));
}
@@ -1882,7 +1910,7 @@ impl EditorWindow {
let Some(ui) = this.export.as_mut() else {
return;
};
- ui.cancel.store(true, Ordering::Relaxed);
+ let cancel = ui.cancel.clone();
cx.spawn_in(window, async move |this, cx| {
let confirmed = platform::confirm_dialog(
"Cancel export?",
@@ -1891,15 +1919,12 @@ impl EditorWindow {
"Keep exporting",
true,
);
- if confirmed {
- let _ = this.update(cx, |this, cx| {
- if let Some(ui) = this.export.as_mut() {
- ui.cancel.store(true, Ordering::Relaxed);
- ui.phase = ExportPhase::Idle;
- }
- cx.notify();
- });
- }
+ let _ = this.update(cx, |this, cx| {
+ if let Some(ui) = this.export.as_mut() {
+ cancel_matching_export(&ui.cancel, &cancel, confirmed);
+ }
+ cx.notify();
+ });
})
.detach();
})),
@@ -1989,6 +2014,12 @@ impl EditorWindow {
}
}
+fn cancel_matching_export(current: &Arc<AtomicBool>, requested: &Arc<AtomicBool>, confirmed: bool) {
+ if confirmed && Arc::ptr_eq(current, requested) {
+ current.store(true, Ordering::Relaxed);
+ }
+}
+
fn show_upload_failure(
this: &gpui::WeakEntity<EditorWindow>,
cx: &mut gpui::AsyncWindowContext,
@@ -2103,7 +2134,29 @@ async fn run_export(
#[cfg(test)]
mod tests {
- use super::ExportPhase;
+ use super::{ExportPhase, cancel_matching_export};
+ use std::sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ };
+
+ #[test]
+ fn declining_cancel_preserves_the_running_export() {
+ let cancel = Arc::new(AtomicBool::new(false));
+ cancel_matching_export(&cancel, &cancel, false);
+ assert!(!cancel.load(Ordering::Relaxed));
+ cancel_matching_export(&cancel, &cancel, true);
+ assert!(cancel.load(Ordering::Relaxed));
+ }
+
+ #[test]
+ fn late_confirmation_cannot_cancel_a_subsequent_export() {
+ let previous = Arc::new(AtomicBool::new(false));
+ let current = Arc::new(AtomicBool::new(false));
+ cancel_matching_export(&current, &previous, true);
+ assert!(!current.load(Ordering::Relaxed));
+ assert!(!previous.load(Ordering::Relaxed));
+ }
#[test]
fn choosing_a_file_blocks_duplicate_exports_without_showing_progress() {
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index 061aef39f..be84d5f9a 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -1136,16 +1136,20 @@ impl PendingProjectSave {
}
pub fn flush(&mut self) {
- let (Some(path), Some(config)) = (self.path.clone(), self.config.take()) else {
- return;
- };
- match config.write(&path) {
- Ok(()) => tracing::debug!(path = %path.display(), "project config written"),
- Err(error) => {
- tracing::error!(path = %path.display(), "failed to persist project config: {error}")
- }
+ if let Err(error) = self.try_flush() {
+ tracing::error!(path = ?self.path, "failed to persist project config: {error}");
}
}
+
+ pub fn try_flush(&mut self) -> std::io::Result<()> {
+ let (Some(path), Some(config)) = (&self.path, &self.config) else {
+ return Ok(());
+ };
+ config.write(path)?;
+ tracing::debug!(path = %path.display(), "project config written");
+ self.config = None;
+ Ok(())
+ }
}
/// Which edge -- or the whole box -- a live drag is moving.
@@ -1516,9 +1520,29 @@ impl EditorWindow {
// `restore_main_windows_if_no_editors` (`lib.rs:5777-5792`). Deferred
// out of the callback -- it fires with the App borrowed.
let path = project_path.clone();
- window.on_window_should_close(cx, move |_window, cx| {
+ let window_id = window.window_handle().window_id();
+ window.on_window_should_close(cx, move |window, cx| {
+ let blocked = window.root::<Self>().flatten().and_then(|editor| {
+ let editor = editor.read(cx);
+ if let Some(reason) = editor.busy_reason() {
+ return Some(reason.to_string());
+ }
+ let saved = editor.pending_save.borrow_mut().try_flush();
+ saved.err().map(|error| {
+ format!(
+ "Could not save the recording. Keep the editor open and try again: {error}"
+ )
+ })
+ });
+ if let Some(message) = blocked {
+ cx.spawn(async move |_| {
+ crate::platform::alert_dialog("Recording retained", &message);
+ })
+ .detach();
+ return false;
+ }
let path = path.clone();
- cx.defer(move |cx| crate::app_windows::editor_closed(&path, cx));
+ cx.defer(move |cx| crate::app_windows::editor_closed(&path, window_id, cx));
true
});
@@ -5448,41 +5472,49 @@ impl EditorWindow {
fn delete_recording(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.set_selection(None, cx);
let path = self.project_path.clone();
- cx.spawn_in(window, async move |_this, cx| {
- let confirmed = crate::platform::confirm_dialog(
- "Cap",
- "Are you sure you want to delete this recording?",
- "Yes",
- "No",
- false,
- );
- if !confirmed {
- return;
- }
- _this
- .update_in(cx, |_this, window, _cx| {
- window.remove_window();
- })
- .ok();
- cx.background_executor()
- .timer(Duration::from_millis(20))
- .await;
- let deleted = cx
- .background_executor()
- .spawn({
- let path = path.clone();
- async move { crate::library::delete_recording_directory(&path) }
- })
- .await;
- if let Err(error) = deleted {
- tracing::error!(path = %path.display(), "deleting the recording failed: {error}");
- return;
- }
- let _ = cx.update(|_window, cx| {
- crate::app_windows::refresh_library_after_delete(cx);
- });
- })
- .detach();
+ let handle = window.window_handle().downcast::<Self>();
+ if let Some(handle) = handle {
+ cx.defer(move |cx| crate::app_windows::request_editor_deletion(path, handle, cx));
+ }
+ }
+
+ pub(crate) fn busy_reason(&self) -> Option<&'static str> {
+ if self
+ .export
+ .as_ref()
+ .is_some_and(|export| export.phase.is_busy())
+ {
+ Some(
+ "Wait for the export or its cancellation to finish before closing or deleting this recording.",
+ )
+ } else if self.clips.is_importing() {
+ Some("Wait for the clip import to finish before closing or deleting this recording.")
+ } else {
+ None
+ }
+ }
+
+ pub(crate) fn deletion_blocked_reason(&self) -> Option<&'static str> {
+ self.busy_reason().or((self.instance.is_none()
+ && !matches!(self.state, LoadState::Failed(_)))
+ .then_some("Wait for the recording to finish opening before deleting it."))
+ }
+
+ pub(crate) fn prepare_for_deletion(
+ &mut self,
+ cx: &mut Context<Self>,
+ ) -> Result<Option<Arc<EditorInstance>>, String> {
+ if let Some(reason) = self.deletion_blocked_reason() {
+ return Err(reason.into());
+ }
+ self.pending_save
+ .borrow_mut()
+ .try_flush()
+ .map_err(|error| format!("Could not save the recording before deleting it: {error}"))?;
+ self.save_task = None;
+ self.stop_playback(cx);
+ self.export = None;
+ Ok(self.take_instance())
}
fn open_add_track(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -8913,6 +8945,36 @@ fn hex_to_color(rgba: [u8; 4]) -> cap_project::Color {
mod tests {
use super::*;
+ #[test]
+ fn failed_predelete_save_keeps_the_pending_edit_for_retry() {
+ let root = std::env::temp_dir().join(format!(
+ "cap-editor-save-delete-{}",
+ crate::store::new_uuid_v4()
+ ));
+ std::fs::create_dir(&root).unwrap();
+ let source = root.join("source.mp4");
+ std::fs::write(&source, b"original recording").unwrap();
+ let config = ProjectConfiguration {
+ aspect_ratio: Some(cap_project::AspectRatio::Square),
+ ..Default::default()
+ };
+ let mut pending = PendingProjectSave {
+ path: Some(source.clone()),
+ config: Some(config),
+ };
+ assert!(pending.try_flush().is_err());
+ assert!(pending.config.is_some());
+ assert_eq!(std::fs::read(&source).unwrap(), b"original recording");
+ pending.path = Some(root.clone());
+ pending.try_flush().unwrap();
+ assert!(pending.config.is_none());
+ let saved: ProjectConfiguration =
+ serde_json::from_slice(&std::fs::read(root.join("project-config.json")).unwrap())
+ .unwrap();
+ assert_eq!(saved.aspect_ratio, Some(cap_project::AspectRatio::Square));
+ std::fs::remove_dir_all(root).unwrap();
+ }
+
#[test]
fn playback_shortcut_is_reserved_for_bare_space_outside_text_fields() {
let space = gpui::Keystroke::parse("space").unwrap();