mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
Save: a file comparison wakes the app, waits for its own preview, and ends with its save
- The comparison's thread asks for a frame once it answers, so a save started from the menu finishes without waiting for the next input. - A comparison that answers while a dialog previews a change in the save's own document, or a drag in it is not yet committed, waits for the preview to end, so the file never holds a change that Cancel takes back. Typing in progress is saved as by any Save. - A save with an explicit path that ends while a comparison is pending drops the comparison and frees the flow, so no changed-on-disk question opens with no save left to apply its answer.
This commit is contained in:
@@ -168,11 +168,15 @@ pub(crate) fn checks_pending(app: &VectorcraftApp) -> bool {
|
||||
app.saves.checks.keys().any(|uid| !held(app, *uid))
|
||||
}
|
||||
|
||||
/// Whether document `uid`'s save has to wait: writing it activates its document, which would
|
||||
/// put it under an open dialog or file picker of another document, or cancel a drag in progress
|
||||
/// in the active one.
|
||||
/// Whether document `uid`'s save has to wait: the document shows a dialog's preview or a drag not
|
||||
/// yet committed, which the save would write (typing in progress is saved, as by any Save), or
|
||||
/// writing it activates its document, which would put it under an open dialog or file picker of
|
||||
/// another document, or cancel a drag in progress in the active one.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn held(app: &VectorcraftApp, uid: u64) -> bool {
|
||||
if app.session.documents().iter().any(|d| d.uid == uid && d.interaction.as_ref().is_some_and(|it| it.label != "Typing")) {
|
||||
return true;
|
||||
}
|
||||
let active = app.session.active();
|
||||
active.map(|d| d.uid) != Some(uid)
|
||||
&& (app.any_dialog_open() || app.file_dialog_open() || active.is_some_and(|d| d.interaction.is_some()) || app.canvas.grab.is_some())
|
||||
@@ -224,6 +228,14 @@ fn finish(app: &mut VectorcraftApp, uid: u64, retargets: bool, out: &Result<Valu
|
||||
_ => flow.failed(&uid),
|
||||
}
|
||||
if !matches!(out, Ok(v) if v.get("pending").is_some()) {
|
||||
// A comparison still pending belongs to the save this one ends (a save with an explicit path
|
||||
// ran meanwhile): it is dropped and its flow freed, since its answer would ask about the file
|
||||
// with no save left to apply the answer.
|
||||
if app.saves.checks.remove(&uid).is_some()
|
||||
&& let Some(flow) = app.saves.flows.get_mut(&uid)
|
||||
{
|
||||
let _ = flow.cancel(app.host.files());
|
||||
}
|
||||
terminate(app, uid, out.clone());
|
||||
}
|
||||
}
|
||||
@@ -322,7 +334,8 @@ pub(crate) fn wait_for_checks(app: &mut VectorcraftApp) {
|
||||
resume_checked(app, true);
|
||||
}
|
||||
|
||||
/// Waits for every save's file comparison to answer, leaving each answer for the next [`pump`].
|
||||
/// Waits for every save's file comparison to answer and its thread to end (its wake included),
|
||||
/// leaving each answer for the next [`pump`].
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
pub(crate) fn wait_for_answers(app: &mut VectorcraftApp) {
|
||||
for check in app.saves.checks.values_mut() {
|
||||
@@ -330,6 +343,8 @@ pub(crate) fn wait_for_answers(app: &mut VectorcraftApp) {
|
||||
if let Ok(same) = check.answer.recv() {
|
||||
let _ = send.send(same);
|
||||
}
|
||||
// The thread drops its sender as it ends, after its wake.
|
||||
let _ = check.answer.recv();
|
||||
check.answer = answer;
|
||||
}
|
||||
}
|
||||
@@ -339,7 +354,7 @@ pub(crate) fn wait_for_answers(app: &mut VectorcraftApp) {
|
||||
pub(crate) fn wait_for_contents(app: &VectorcraftApp) -> Option<FileStamp> {
|
||||
let document = app.session.active()?;
|
||||
let path = Path::new(document.path.as_deref()?);
|
||||
app.saves.contents.check(document.uid, path)?.recv().ok().flatten()
|
||||
app.saves.contents.check(document.uid, path, Wake::none())?.recv().ok().flatten()
|
||||
}
|
||||
|
||||
/// The web has no save flows: nothing answers between passes.
|
||||
@@ -395,6 +410,12 @@ fn ask_changed_on_disk(app: &mut VectorcraftApp, key: u64, path: &Path) -> Resul
|
||||
Ok(json!({ "pending": "changedOnDisk" }))
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
/// Asks the window for a frame, from any thread; does nothing without a window.
|
||||
fn ui_wake(app: &VectorcraftApp) -> Wake {
|
||||
app.ui_ctx.clone().map_or_else(Wake::none, |ctx| Wake::new(move || ctx.request_repaint()))
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn proceed(app: &mut VectorcraftApp, step: SaveStep<u64>) -> Result<Value, String> {
|
||||
match step {
|
||||
@@ -402,7 +423,9 @@ fn proceed(app: &mut VectorcraftApp, step: SaveStep<u64>) -> Result<Value, Strin
|
||||
// Before asking, the file is compared with the bytes the app last read or wrote: a file
|
||||
// whose metadata alone changed (a cloud-sync folder sets attributes on it, or puts the same
|
||||
// bytes back as a new file) is written over without asking (#10734). [`pump`] goes on.
|
||||
SaveStep::AskChangedOnDisk { key, path } => match app.saves.contents.check(key, &path) {
|
||||
// The comparison's thread asks for a frame once it answers: a save started from the UI comes
|
||||
// after this frame's repaint decision.
|
||||
SaveStep::AskChangedOnDisk { key, path } => match app.saves.contents.check(key, &path, ui_wake(app)) {
|
||||
Some(answer) => {
|
||||
app.saves.checks.insert(key, Checking { path, answer });
|
||||
Ok(json!({ "pending": "checkingFile" }))
|
||||
|
||||
@@ -15,6 +15,7 @@ use std::sync::mpsc::{self, Receiver};
|
||||
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
||||
use std::thread::JoinHandle;
|
||||
|
||||
use craft::host::Wake;
|
||||
use craft::platform::watch::FileStamp;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
@@ -81,8 +82,9 @@ impl SavedContents {
|
||||
/// Starts comparing document `key`'s file at `path` with the version the app last read or
|
||||
/// wrote, on a thread of its own that first waits for that version's digest. The answer is
|
||||
/// the file's version now when it holds the same bytes, and `None` when the bytes differ, the
|
||||
/// file can't be read, or no digest of `path` was taken. `None` here when no thread starts.
|
||||
pub(crate) fn check(&self, key: u64, path: &Path) -> Option<Receiver<Option<FileStamp>>> {
|
||||
/// file can't be read, or no digest of `path` was taken. `wake` runs once the answer is sent,
|
||||
/// so a frame collects it. `None` here when no thread starts.
|
||||
pub(crate) fn check(&self, key: u64, path: &Path, wake: Wake) -> Option<Receiver<Option<FileStamp>>> {
|
||||
let pending = lock(&self.state).pending.remove(&key);
|
||||
let state = Arc::clone(&self.state);
|
||||
let path = path.to_path_buf();
|
||||
@@ -94,6 +96,7 @@ impl SavedContents {
|
||||
}
|
||||
// The caller may be gone (its document closed): the answer is dropped.
|
||||
let _ = send.send(unchanged(&state, key, &path));
|
||||
wake.wake();
|
||||
};
|
||||
std::thread::Builder::new().name("vectorcraft-file-check".into()).spawn(work).ok().map(|_| answer)
|
||||
}
|
||||
@@ -164,7 +167,7 @@ mod tests {
|
||||
let contents = SavedContents::new();
|
||||
for uid in [1, 2] {
|
||||
contents.note(uid, path, stamp);
|
||||
assert!(contents.check(uid, path).unwrap().recv().unwrap().is_some());
|
||||
assert!(contents.check(uid, path, Wake::none()).unwrap().recv().unwrap().is_some());
|
||||
}
|
||||
// A third document closes while its digest handle is still retained.
|
||||
contents.note(3, path, stamp);
|
||||
@@ -178,8 +181,8 @@ mod tests {
|
||||
assert!(!state.notes.contains_key(&uid));
|
||||
}
|
||||
}
|
||||
assert!(contents.check(1, path).unwrap().recv().unwrap().is_none());
|
||||
assert!(contents.check(3, path).unwrap().recv().unwrap().is_none());
|
||||
assert!(contents.check(2, path).unwrap().recv().unwrap().is_some());
|
||||
assert!(contents.check(1, path, Wake::none()).unwrap().recv().unwrap().is_none());
|
||||
assert!(contents.check(3, path, Wake::none()).unwrap().recv().unwrap().is_none());
|
||||
assert!(contents.check(2, path, Wake::none()).unwrap().recv().unwrap().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -796,3 +796,66 @@ fn a_comparison_waits_while_a_drag_goes_on_in_another_document() {
|
||||
assert!(unwritten, "nothing is written while the drag goes on");
|
||||
assert!(written, "the save finishes once the drag ends");
|
||||
}
|
||||
|
||||
/// A comparison's thread asks for a frame once it answers: a save started from the UI comes after
|
||||
/// that frame's repaint decision, and an idle app draws no other frame.
|
||||
#[test]
|
||||
fn a_comparison_asks_for_a_frame_once_it_answers() {
|
||||
let (d, _path, mut app) = opened_then_put_back("compare-answer-wake", true);
|
||||
let ctx = settled();
|
||||
app.ui_ctx = Some(ctx.clone());
|
||||
assert_eq!(app.run("file.save", json!({})).unwrap()["pending"], "checkingFile");
|
||||
io::save::wait_for_answers(&mut app);
|
||||
let woke = ctx.has_requested_repaint();
|
||||
io::save::wait_for_checks(&mut app);
|
||||
crate::background::wait_all(&mut app);
|
||||
let _ = std::fs::remove_dir_all(d);
|
||||
assert!(woke, "the answer asks for a frame");
|
||||
}
|
||||
|
||||
/// A comparison that answers while a dialog previews a change in the save's own document waits for
|
||||
/// the preview to end, so the file never holds a change the dialog's Cancel takes back.
|
||||
#[test]
|
||||
fn a_comparison_waits_while_its_own_document_shows_a_preview() {
|
||||
let (d, path, mut app) = opened_then_put_back("compare-own-preview", true);
|
||||
let before = std::fs::read(&path).unwrap();
|
||||
assert_eq!(app.run("file.save", json!({})).unwrap()["pending"], "checkingFile");
|
||||
app.begin_interaction("Move").unwrap();
|
||||
io::save::wait_for_answers(&mut app);
|
||||
let ctx = egui::Context::default();
|
||||
io::save::pump(&mut app, &ctx);
|
||||
crate::background::wait_all(&mut app);
|
||||
let unwritten = std::fs::read(&path).unwrap() == before;
|
||||
let previewing = app.session.active().is_some_and(|d| d.interaction.is_some());
|
||||
// Once the preview is canceled, the save goes on and writes the file.
|
||||
app.session.cancel_interaction().unwrap();
|
||||
io::save::pump(&mut app, &ctx);
|
||||
crate::background::wait_all(&mut app);
|
||||
let written = std::fs::read(&path).unwrap() != before;
|
||||
let _ = std::fs::remove_dir_all(d);
|
||||
assert!(previewing, "the preview stays open");
|
||||
assert!(unwritten, "nothing is written while the preview is shown");
|
||||
assert!(written, "the save finishes once the preview ends");
|
||||
}
|
||||
|
||||
/// A save with an explicit path that ends while a comparison is pending drops the comparison: its
|
||||
/// answer would ask about the file with no save left to apply the answer.
|
||||
#[test]
|
||||
fn a_save_that_ends_first_drops_the_pending_comparison() {
|
||||
let (d, path, mut app) = opened_then_put_back("compare-overtaken", true);
|
||||
assert_eq!(app.run("file.save", json!({})).unwrap()["pending"], "checkingFile");
|
||||
app.run("file.saveAs", json!({"path": path})).unwrap();
|
||||
crate::background::wait_all(&mut app);
|
||||
io::save::wait_for_checks(&mut app);
|
||||
crate::background::wait_all(&mut app);
|
||||
let asked = app.any_dialog_open();
|
||||
let pending = io::save::checks_pending(&app);
|
||||
rect(&mut app);
|
||||
let next = app.run("file.save", json!({}));
|
||||
io::save::wait_for_checks(&mut app);
|
||||
crate::background::wait_all(&mut app);
|
||||
let _ = std::fs::remove_dir_all(d);
|
||||
assert!(!asked, "no changed-on-disk question without a save to answer it");
|
||||
assert!(!pending, "the comparison is dropped");
|
||||
assert!(next.is_ok(), "the next save starts: {next:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user