Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(close): a project that cannot be saved asks to quit without saving, not traps the window (#375)

Jareer91973407874d
diff --git a/src/crates/concat-host/src/projects.rs b/src/crates/concat-host/src/projects.rs
index 98dc51db..fa570741 100644
--- a/src/crates/concat-host/src/projects.rs
+++ b/src/crates/concat-host/src/projects.rs
@@ -260,8 +260,9 @@ pub fn save(path: &str, document: &serde_json::Value) -> Result<(), String> {
///
/// The temporary file is this call's own (`create_new`), so two saves -
/// including ones in different processes on the same folder - never write one
-/// another's bytes, and the rename over the manifest is atomic on every
-/// platform. What a folder shared between processes does not get is the
+/// another's bytes, and the rename over the manifest is atomic on a local
+/// disk (not always on FAT or a network share, where it is still one call
+/// that replaces or fails). What a folder shared between processes does not get is the
/// order: each process counts tickets on its own, so a stale snapshot in one
/// process can still land after a newer document written by another.
/// Ordering across processes would need a lock both honour, and a project
@@ -353,8 +354,24 @@ impl Temporary {
fn commit(&mut self, manifest: &Path) -> Result<(), String> {
// Closed first, so the name is free for Windows to replace.
self.file.take();
- std::fs::rename(&self.path, manifest)
- .map_err(|error| format!("could not replace {}: {error}", manifest.display()))?;
+ // A scanner, a sync client or a backup tool holding the manifest a
+ // moment makes Windows refuse the replace (access denied, sharing
+ // violation). That passes; a close that failed on it would not.
+ let mut wait = std::time::Duration::from_millis(50);
+ let mut tries = 0;
+ loop {
+ match std::fs::rename(&self.path, manifest) {
+ Ok(()) => break,
+ Err(error) if tries < 5 && transient(&error) => {
+ tries += 1;
+ std::thread::sleep(wait);
+ wait *= 2;
+ }
+ Err(error) => {
+ return Err(format!("could not replace {}: {error}", manifest.display()));
+ }
+ }
+ }
self.renamed = true;
Ok(())
}
@@ -371,6 +388,15 @@ impl Drop for Temporary {
}
}
+/// Whether a failed replace is another program holding the file for a
+/// moment: on Windows, access denied (5) or a sharing violation (32).
+/// Elsewhere a rename is not refused that way, so nothing is retried.
+fn transient(error: &std::io::Error) -> bool {
+ cfg!(windows)
+ && (error.kind() == std::io::ErrorKind::PermissionDenied
+ || matches!(error.raw_os_error(), Some(5 | 32)))
+}
+
static NEXT_TEMPORARY: AtomicU64 = AtomicU64::new(0);
/// Writes `encoded` to the project's manifest: a temporary of its own,
diff --git a/src/crates/concat/locales/en.json b/src/crates/concat/locales/en.json
index dc52d09b..a20f72b4 100644
--- a/src/crates/concat/locales/en.json
+++ b/src/crates/concat/locales/en.json
@@ -106,6 +106,9 @@
"common.colourRange": "Colour range",
"common.copy": "Copy",
"common.custom": "Custom",
+ "close.quitWithoutSaving": "Quit without saving",
+ "close.unsavedMessage": "Concat could not save this project: {0}\n\nQuit anyway and lose the changes since the last save, or cancel to keep the window open and try again once the disk is back.",
+ "close.unsavedTitle": "Quit without saving?",
"common.delete": "Delete",
"common.duration": "Duration",
"common.effect": "effect",
diff --git a/src/crates/concat/src/lib.rs b/src/crates/concat/src/lib.rs
index 32976be6..1f47a1ff 100644
--- a/src/crates/concat/src/lib.rs
+++ b/src/crates/concat/src/lib.rs
@@ -500,6 +500,7 @@ fn start(project: Option<std::path::PathBuf>, smoke: bool) -> Result<(), slint::
// open, so the window stays up. Its notice is published
// here or nobody would see it.
log::warn!("close: not closing, project could not be saved: {error}");
+ shell.studio.borrow_mut().offer_quit_without_saving(error);
shell.studio.borrow_mut().refresh_art();
shell.studio.borrow().publish(&app, &shell.models);
}
@@ -520,6 +521,7 @@ fn start(project: Option<std::path::PathBuf>, smoke: bool) -> Result<(), slint::
if let Err(error) = outcome {
saved.set(false);
log::warn!("close: not closing, project could not be saved: {error}");
+ shell.studio.borrow_mut().offer_quit_without_saving(error);
shell.studio.borrow_mut().refresh_art();
shell.studio.borrow().publish(&app, &shell.models);
}
@@ -1863,6 +1865,7 @@ fn start(project: Option<std::path::PathBuf>, smoke: bool) -> Result<(), slint::
// Unsaved work is worth more than the window: the
// project stays open and the reason is shown.
log::warn!("close: not closing, project could not be saved: {error}");
+ shell.studio.borrow_mut().offer_quit_without_saving(error);
shell.studio.borrow_mut().refresh_art();
shell.studio.borrow().publish(&app, &shell.models);
return;
diff --git a/src/crates/concat/src/panes/settings.rs b/src/crates/concat/src/panes/settings.rs
index db1a162c..1472f601 100644
--- a/src/crates/concat/src/panes/settings.rs
+++ b/src/crates/concat/src/panes/settings.rs
@@ -865,6 +865,7 @@ fn quit_for_switch(after: impl FnOnce() + Send + 'static) {
);
if let Err(error) = outcome {
log::warn!("close: not switching, project could not be saved: {error}");
+ studio.offer_quit_without_saving(error);
studio.refresh_art();
studio.publish(app, models);
}
diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rs
index f9d68951..8777ac1f 100644
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@ -1886,6 +1886,11 @@ pub enum Confirm {
RemoveTrack { track_id: String },
/// Exporting over a file that is already at `path`.
ReplaceExport { path: String },
+ /// Quitting though the project could not be saved, for `error`. A
+ /// close that cannot write the project keeps the window, and without
+ /// this a disk unplugged, full or read-only made a window that would
+ /// never close.
+ QuitWithoutSaving { error: String },
}
/// One clip on the clipboard: the clip as it was when copied, and the row
@@ -2257,6 +2262,11 @@ impl Studio {
crate::panes::export::ExportMsg::Overwrite,
));
}
+ Some(Confirm::QuitWithoutSaving { .. }) => {
+ log::warn!("close: quitting without saving, as asked");
+ self.release_project();
+ slint::quit_event_loop().ok();
+ }
None => {}
}
}
@@ -2292,6 +2302,12 @@ impl Studio {
message: tf("export.replaceFileMessage", &[path]).into(),
action: t("export.replace").into(),
},
+ Confirm::QuitWithoutSaving { error } => crate::ui::ConfirmData {
+ open: true,
+ title: t("close.unsavedTitle").into(),
+ message: tf("close.unsavedMessage", &[error]).into(),
+ action: t("close.quitWithoutSaving").into(),
+ },
}
}
@@ -7269,6 +7285,19 @@ impl Studio {
return Err(error);
}
}
+ self.release_project();
+ Ok(())
+ }
+
+ /// Asks whether to quit though the project could not be saved: the
+ /// window's ways out call it when `close_project` refused.
+ pub fn offer_quit_without_saving(&mut self, error: String) {
+ self.confirm = Some(Confirm::QuitWithoutSaving { error });
+ }
+
+ /// Lets the open project go, unsaved: what `close_project` does once
+ /// the save has landed, and what quitting without saving does instead.
+ fn release_project(&mut self) {
self.host.cutouts.cancel();
self.host.enhancers.cancel();
self.host.reversers.cancel();
@@ -7315,7 +7344,6 @@ impl Studio {
.set_clips(std::path::PathBuf::new(), Vec::new());
self.on_start = true;
self.recents = projects::list(&self.host.dirs.config);
- Ok(())
}
/// Posters for the recents that have none yet, decoded on a worker.