Autosave only arms for a project this window opened or saved
Browse files at this commita80334d6f47f4f16fc917eef51b31c63ea810bbdParent: 5134b1d- fix
- autosave only arms for a project this window opened or saved
3 changed files +93 -3
- crates/app/src/main.rs +12 -0
- crates/app/src/player/library.rs +79 -1
- crates/app/src/render.rs +2 -2
crates/app/src/main.rsmodified+12 -0
| @@ -409,6 +409,17 @@ struct Player { | |||
| 409 | /// `project_path` is never touched by either path, only a manual save | 409 | /// `project_path` is never touched by either path, only a manual save |
| 410 | /// writes the real file. | 410 | /// writes the real file. |
| 411 | recovery_sidecar: Option<PathBuf>, | 411 | recovery_sidecar: Option<PathBuf>, |
| 412 | + /// Whether `project_path` names a file this window itself put there -- | ||
| 413 | + /// loaded from a `.edith` ([`Player::install_project`]) or written by a | ||
| 414 | + /// manual save ([`Player::save_project`]) -- rather than one merely | ||
| 415 | + /// derived beside an imported media file. [`Player::autosave_tick`] | ||
| 416 | + /// requires it: a path derived from `media.mp4` names `media.edith` | ||
| 417 | + /// whether or not that file is the user's own saved project, and writing | ||
| 418 | + /// a sidecar beside a *foreign* `.edith` the user never opened is what | ||
| 419 | + /// let a scratch timeline's autosave outdate and "recover" over a real | ||
| 420 | + /// one. `mark_dirty` does not touch this -- an edit earns nothing an open | ||
| 421 | + /// or save has not already granted. | ||
| 422 | + autosave_armed: bool, | ||
| 412 | /// Which stroke means what, and what every shortcut on screen is called. | 423 | /// Which stroke means what, and what every shortcut on screen is called. |
| 413 | /// The one place either question is answered. | 424 | /// The one place either question is answered. |
| 414 | keymap: Keymap, | 425 | keymap: Keymap, |
| @@ -842,6 +853,7 @@ fn main() { | |||
| 842 | autosave_last_edit: None, | 853 | autosave_last_edit: None, |
| 843 | autosave_last_run: None, | 854 | autosave_last_run: None, |
| 844 | recovery_sidecar: None, | 855 | recovery_sidecar: None, |
| 856 | + autosave_armed: false, | ||
| 845 | keymap: keymap.clone(), | 857 | keymap: keymap.clone(), |
| 846 | keys_open: false, | 858 | keys_open: false, |
| 847 | keys_search: String::new(), | 859 | keys_search: String::new(), |
crates/app/src/player/library.rsmodified+79 -1
| @@ -982,6 +982,10 @@ impl Player { | |||
| 982 | // opening another clip is not a change of mind about that. | 982 | // opening another clip is not a change of mind about that. |
| 983 | self.export_path = retarget(&export_path(path), self.format); | 983 | self.export_path = retarget(&export_path(path), self.format); |
| 984 | self.project_path = project_path(path); | 984 | self.project_path = project_path(path); |
| 985 | + // Derived, not opened: `project_path` here is a guess at where | ||
| 986 | + // a save would land, not a file this window has any business | ||
| 987 | + // writing an autosave beside until a manual save earns it. | ||
| 988 | + self.autosave_armed = false; | ||
| 985 | self.name = file_name(path).into(); | 989 | self.name = file_name(path).into(); |
| 986 | self.reset_after_reseek(); | 990 | self.reset_after_reseek(); |
| 987 | let name = file_name(path); | 991 | let name = file_name(path); |
| @@ -1260,6 +1264,9 @@ impl Player { | |||
| 1260 | self.auto_proxies_on = session.auto_proxies(); | 1264 | self.auto_proxies_on = session.auto_proxies(); |
| 1261 | } | 1265 | } |
| 1262 | self.project_path = path.to_path_buf(); | 1266 | self.project_path = path.to_path_buf(); |
| 1267 | + // Opened from the `.edith` itself: the file this window will | ||
| 1268 | + // autosave beside is the one the user actually chose. | ||
| 1269 | + self.autosave_armed = true; | ||
| 1263 | self.name = file_name(path).into(); | 1270 | self.name = file_name(path).into(); |
| 1264 | // A copied clip names its source by index, which means a | 1271 | // A copied clip names its source by index, which means a |
| 1265 | // different file -- or none -- in another project. | 1272 | // different file -- or none -- in another project. |
| @@ -1328,6 +1335,10 @@ impl Player { | |||
| 1328 | } | 1335 | } |
| 1329 | self.autosave_dirty = false; | 1336 | self.autosave_dirty = false; |
| 1330 | self.autosave_last_edit = None; | 1337 | self.autosave_last_edit = None; |
| 1338 | + // A manual save is exactly the event this flag exists to | ||
| 1339 | + // require: whatever `project_path` was derived from, it now | ||
| 1340 | + // names a file the user just chose to write. | ||
| 1341 | + self.autosave_armed = true; | ||
| 1331 | format!("SAVED {}", file_name(&self.project_path)) | 1342 | format!("SAVED {}", file_name(&self.project_path)) |
| 1332 | } | 1343 | } |
| 1333 | Some(Err(e)) => format!("SAVE FAILED: {e}"), | 1344 | Some(Err(e)) => format!("SAVE FAILED: {e}"), |
| @@ -1381,6 +1392,17 @@ impl Player { | |||
| 1381 | self.recovery_sidecar = None; | 1392 | self.recovery_sidecar = None; |
| 1382 | } | 1393 | } |
| 1383 | 1394 | ||
| 1395 | + /// Answers the recovery notice with anything but `enter`: the sidecar is | ||
| 1396 | + /// discarded, on disk as well as in memory. Left behind, an unread | ||
| 1397 | + /// autosave from a scratch session would keep outdating the real project | ||
| 1398 | + /// it sits beside and reappear as "recovery" on every open of that file | ||
| 1399 | + /// (reference-editor behaviour: a declined backup is discarded, not kept | ||
| 1400 | + /// around to ask again). Pure at the path level so it needs no `Player` | ||
| 1401 | + /// to test -- a missing sidecar is not an error, only a no-op. | ||
| 1402 | + pub(crate) fn discard_sidecar(sidecar: &Path) { | ||
| 1403 | + let _ = std::fs::remove_file(sidecar); | ||
| 1404 | + } | ||
| 1405 | + | ||
| 1384 | /// Whether `project_path` has a sidecar beside it that outdates the file | 1406 | /// Whether `project_path` has a sidecar beside it that outdates the file |
| 1385 | /// itself: the sidecar exists, the project exists, and the sidecar's mtime | 1407 | /// itself: the sidecar exists, the project exists, and the sidecar's mtime |
| 1386 | /// is strictly the younger of the two. Pure and file-system-only -- no | 1408 | /// is strictly the younger of the two. Pure and file-system-only -- no |
| @@ -1416,6 +1438,15 @@ impl Player { | |||
| 1416 | .detach(); | 1438 | .detach(); |
| 1417 | } | 1439 | } |
| 1418 | 1440 | ||
| 1441 | + /// Whether a tick is allowed to write at all: armed (`project_path` names | ||
| 1442 | + /// a file this window opened or saved -- see `autosave_armed`), dirty, | ||
| 1443 | + /// and not mid-export. Pure and split out from [`autosave_tick`](Self::autosave_tick) | ||
| 1444 | + /// for the same reason [`recovery_offer`](Self::recovery_offer) is: a test | ||
| 1445 | + /// can ask it with three bools, with no `Player` to build. | ||
| 1446 | + fn autosave_gate(armed: bool, dirty: bool, exporting: bool) -> bool { | ||
| 1447 | + armed && dirty && !exporting | ||
| 1448 | + } | ||
| 1449 | + | ||
| 1419 | /// One tick of the loop above. Writes the sidecar when an edit has been | 1450 | /// One tick of the loop above. Writes the sidecar when an edit has been |
| 1420 | /// idle for [`AUTOSAVE_DEBOUNCE`] (the debounce) or dirty edits have gone | 1451 | /// idle for [`AUTOSAVE_DEBOUNCE`] (the debounce) or dirty edits have gone |
| 1421 | /// unwritten for [`AUTOSAVE_PERIOD`] (the periodic safety net for a run | 1452 | /// unwritten for [`AUTOSAVE_PERIOD`] (the periodic safety net for a run |
| @@ -1425,7 +1456,7 @@ impl Player { | |||
| 1425 | /// write is not something a person can do anything about right now, and a | 1456 | /// write is not something a person can do anything about right now, and a |
| 1426 | /// modal over it would be worse than the risk it is guarding against. | 1457 | /// modal over it would be worse than the risk it is guarding against. |
| 1427 | fn autosave_tick(&mut self, cx: &mut Context<Self>) { | 1458 | fn autosave_tick(&mut self, cx: &mut Context<Self>) { |
| 1428 | - if !self.autosave_dirty || self.exporting().is_some() { | 1459 | + if !Self::autosave_gate(self.autosave_armed, self.autosave_dirty, self.exporting().is_some()) { |
| 1429 | return; | 1460 | return; |
| 1430 | } | 1461 | } |
| 1431 | let now = Instant::now(); | 1462 | let now = Instant::now(); |
| @@ -1683,4 +1714,51 @@ mod autosave_tests { | |||
| 1683 | 1714 | ||
| 1684 | std::fs::remove_dir_all(&dir).unwrap(); | 1715 | std::fs::remove_dir_all(&dir).unwrap(); |
| 1685 | } | 1716 | } |
| 1717 | + | ||
| 1718 | + /// The bug this module exists to close: a plain media open derives | ||
| 1719 | + /// `project_path` (a guess at where a save would land) but never arms | ||
| 1720 | + /// autosave for it -- so a dirty, never-saved scratch session writes no | ||
| 1721 | + /// sidecar beside whatever `.edith` that guess happens to name, even if | ||
| 1722 | + /// one already exists as somebody's real saved project. | ||
| 1723 | + #[test] | ||
| 1724 | + fn unarmed_session_never_writes_even_when_dirty() { | ||
| 1725 | + assert!(!Player::autosave_gate(false, true, false)); | ||
| 1726 | + } | ||
| 1727 | + | ||
| 1728 | + /// Both doors that earn the write: opening the `.edith` itself | ||
| 1729 | + /// (`install_project`) and a manual save (`save_project`), each setting | ||
| 1730 | + /// `autosave_armed = true` before this gate is ever asked again. | ||
| 1731 | + #[test] | ||
| 1732 | + fn armed_and_dirty_session_writes() { | ||
| 1733 | + assert!(Player::autosave_gate(true, true, false)); | ||
| 1734 | + // Still withheld while an export is running, armed or not. | ||
| 1735 | + assert!(!Player::autosave_gate(true, true, true)); | ||
| 1736 | + // Still withheld while clean, armed or not. | ||
| 1737 | + assert!(!Player::autosave_gate(true, false, false)); | ||
| 1738 | + } | ||
| 1739 | + | ||
| 1740 | + /// Declining the recovery notice (any key but `enter`, in | ||
| 1741 | + /// [`render::key handler`](crate::render)) discards the sidecar on disk, | ||
| 1742 | + /// not just the in-memory offer -- otherwise the same stale sidecar | ||
| 1743 | + /// outdates the project again on the very next open. | ||
| 1744 | + #[test] | ||
| 1745 | + fn declining_recovery_deletes_the_sidecar() { | ||
| 1746 | + let dir = std::env::temp_dir().join(format!( | ||
| 1747 | + "edith-autosave-decline-test-{}-{:?}", | ||
| 1748 | + std::process::id(), | ||
| 1749 | + std::thread::current().id() | ||
| 1750 | + )); | ||
| 1751 | + std::fs::create_dir_all(&dir).unwrap(); | ||
| 1752 | + let sidecar = dir.join("p.edith.autosave"); | ||
| 1753 | + std::fs::write(&sidecar, b"stale").unwrap(); | ||
| 1754 | + | ||
| 1755 | + Player::discard_sidecar(&sidecar); | ||
| 1756 | + assert!(!sidecar.exists()); | ||
| 1757 | + | ||
| 1758 | + // A missing sidecar (already recovered, or declined twice) is a | ||
| 1759 | + // no-op, not an error. | ||
| 1760 | + Player::discard_sidecar(&sidecar); | ||
| 1761 | + | ||
| 1762 | + std::fs::remove_dir_all(&dir).unwrap(); | ||
| 1763 | + } | ||
| 1686 | } | 1764 | } |
crates/app/src/render.rsmodified+2 -2
| @@ -216,8 +216,8 @@ impl Render for Player { | |||
| 216 | if this.recovery_sidecar.is_some() { | 216 | if this.recovery_sidecar.is_some() { |
| 217 | if key == "enter" { | 217 | if key == "enter" { |
| 218 | this.recover_from_sidecar(cx); | 218 | this.recover_from_sidecar(cx); |
| 219 | - } else { | 219 | + } else if let Some(sidecar) = this.recovery_sidecar.take() { |
| 220 | - this.recovery_sidecar = None; | 220 | + Player::discard_sidecar(&sidecar); |
| 221 | } | 221 | } |
| 222 | } | 222 | } |
| 223 | // Any key answers the message on the bar and brings the next of | 223 | // Any key answers the message on the bar and brings the next of |