mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-15 23:02:32 +00:00
DES-10: a save waits for the open-time checks of linked files
File > Save, Save As, Save a Copy and the saves before close and quit,
asked for while the open-time link or migration checks of the document
run, wait for them and then write, so the first save of a document saved
before linked storage already writes links. The UI defers the save and
re-issues it once links.checking() turns false; the status line counts
the files left ("Checking N linked images…"), Esc saves at once.
Recovery snapshots and the control channel's file.save don't wait.
links.progress reports how many files are left.
This commit is contained in:
1 parent
bcb0711a7c
commit
7ac9cafa7e
7 files changed
+153
-16
No files matched your search
@@ -29,11 +29,11 @@ use crate::{DocState, Result, Session};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![cmd!(noundo "links.progress", "Link Checks", [], None,
|
||||
"{} — apply what the background checks of opened documents' linked files found (the UI calls this while `checking`) → {checking: the active document's files are still being checked, notice?: a message for the status line, given once}",
|
||||
"{} — apply what the background checks of opened documents' linked files found (the UI calls this while `checking`) → {checking: the active document's files are still being checked, remaining: how many of them, notice?: a message for the status line, given once}",
|
||||
always, |s, _| {
|
||||
s.poll_link_checks();
|
||||
let Ok(st) = s.doc_mut() else { return Ok(json!({"checking": false})) };
|
||||
let mut out = json!({"checking": st.links.checking()});
|
||||
let Ok(st) = s.doc_mut() else { return Ok(json!({"checking": false, "remaining": 0})) };
|
||||
let mut out = json!({"checking": st.links.checking(), "remaining": st.links.remaining()});
|
||||
if let Some(notice) = st.links.take_notice() {
|
||||
out["notice"] = Value::String(notice);
|
||||
}
|
||||
@@ -97,6 +97,14 @@ impl OpenLinks {
|
||||
self.job.is_some()
|
||||
}
|
||||
|
||||
/// How many files the background check has still to look at, the one it is on included.
|
||||
pub fn remaining(&self) -> usize {
|
||||
self.job.as_ref().map_or(0, |handle| {
|
||||
let state = lock(&handle.0);
|
||||
state.work.len().saturating_add(usize::from(state.busy))
|
||||
})
|
||||
}
|
||||
|
||||
/// The status-line message about images that will be saved as links, once.
|
||||
pub fn take_notice(&mut self) -> Option<String> {
|
||||
self.notice.take()
|
||||
@@ -180,6 +188,8 @@ struct JobState {
|
||||
work: Vec<Work>,
|
||||
results: Vec<Outcome>,
|
||||
done: bool,
|
||||
/// A piece of work taken from `work` is being done.
|
||||
busy: bool,
|
||||
}
|
||||
|
||||
impl Job {
|
||||
@@ -383,6 +393,7 @@ fn run_all(job: &Job, roots: Option<&Arc<crate::file_access::AutomationRoots>>)
|
||||
let mut state = lock(job);
|
||||
let next = state.work.pop();
|
||||
state.done |= next.is_none();
|
||||
state.busy = next.is_some();
|
||||
next
|
||||
};
|
||||
let Some(work) = next else { break };
|
||||
@@ -396,7 +407,11 @@ fn run_all(job: &Job, roots: Option<&Arc<crate::file_access::AutomationRoots>>)
|
||||
job.changed.notify_all();
|
||||
}
|
||||
});
|
||||
lock(job).done = true;
|
||||
{
|
||||
let mut state = lock(job);
|
||||
state.done = true;
|
||||
state.busy = false;
|
||||
}
|
||||
job.changed.notify_all();
|
||||
}
|
||||
|
||||
|
||||
@@ -232,8 +232,8 @@ fn migration_links_confirmed_images_and_drops_the_copy_only_after_confirmation()
|
||||
assert_eq!((kept.storage, kept.link_hash.as_deref(), kept.data.embedded_bytes().is_some()), (Storage::Embedded, None, true));
|
||||
assert!(!s.doc().unwrap().is_dirty());
|
||||
let r = s.execute("links.progress", &json!({})).unwrap();
|
||||
assert_eq!(r, json!({"checking": false, "notice": "1 image will be saved as a link to its file (Links ▸ Embed keeps a copy)"}));
|
||||
assert_eq!(s.execute("links.progress", &json!({})).unwrap(), json!({"checking": false}), "told once");
|
||||
assert_eq!(r, json!({"checking": false, "remaining": 0, "notice": "1 image will be saved as a link to its file (Links ▸ Embed keeps a copy)"}));
|
||||
assert_eq!(s.execute("links.progress", &json!({})).unwrap(), json!({"checking": false, "remaining": 0}), "told once");
|
||||
|
||||
let key = left.data.key();
|
||||
s.execute("file.save", &json!({})).unwrap();
|
||||
|
||||
@@ -129,6 +129,7 @@ impl DesignApp {
|
||||
close_requested_pending: false,
|
||||
quit_without_asking: false,
|
||||
quitting: None,
|
||||
waiting_save: None,
|
||||
cursors: cursors::Images::default(),
|
||||
live_preview: LivePreview::default(),
|
||||
updates_rt: updates_ui::Runtime::default(),
|
||||
|
||||
@@ -492,16 +492,60 @@ impl DesignApp {
|
||||
/// Links panel's statuses fill in.
|
||||
pub(crate) fn poll_link_checks(&mut self, ctx: &egui::Context) {
|
||||
// Running any command ends a text composition in progress: ask only while files are checked.
|
||||
if !self.session.active().is_some_and(|st| st.links.checking()) {
|
||||
if !self.session.active().is_some_and(|st| st.links.checking()) && self.waiting_save.is_none() {
|
||||
return;
|
||||
}
|
||||
let Ok(progress) = self.session.execute("links.progress", &json!({})) else { return };
|
||||
if let Some(notice) = progress["notice"].as_str() {
|
||||
self.status(notice.to_string());
|
||||
}
|
||||
if progress["checking"].as_bool() == Some(true) {
|
||||
if progress["checking"].as_bool() == Some(true) || self.waiting_save.is_some() {
|
||||
ctx.request_repaint_after(std::time::Duration::from_millis(400));
|
||||
}
|
||||
let now = self.waiting_save.is_some() && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape));
|
||||
self.run_waiting_save(now);
|
||||
}
|
||||
|
||||
/// The status line while a save waits for the checks of linked files ([`crate::menus::save_when_checked`]).
|
||||
pub(crate) fn show_save_wait(&mut self, remaining: usize) {
|
||||
self.status(crate::i18n::tr(&self.ui.language, "Checking {n} linked images…").replace("{n}", &remaining.max(1).to_string()));
|
||||
}
|
||||
|
||||
/// Run the save waiting for the checks of its document's linked files once they are done, or
|
||||
/// at once when `now` (Esc). It saves its own document, which is made active for that and
|
||||
/// the active one again after. A document closed meanwhile drops it.
|
||||
pub(crate) fn run_waiting_save(&mut self, now: bool) {
|
||||
let Some(uid) = self.waiting_save.as_ref().map(|w| w.uid) else { return };
|
||||
let Some(st) = self.session.documents().iter().find(|d| d.uid == uid) else {
|
||||
self.waiting_save = None;
|
||||
return;
|
||||
};
|
||||
if st.links.checking() && !now {
|
||||
let remaining = st.links.remaining();
|
||||
self.show_save_wait(remaining);
|
||||
return;
|
||||
}
|
||||
let Some(waiting) = self.waiting_save.take() else { return };
|
||||
let active = self.session.active().map(|d| d.uid);
|
||||
let index_of = |app: &Self, uid: u64| app.session.documents().iter().position(|d| d.uid == uid);
|
||||
if active != Some(uid)
|
||||
&& let Some(i) = index_of(self, uid)
|
||||
&& let Err(e) = self.run("file.activate", json!({"index": i}))
|
||||
{
|
||||
self.status_error("Save", e);
|
||||
return;
|
||||
}
|
||||
// The count of files left goes; what the save reports stays.
|
||||
self.status(String::new());
|
||||
let saved = (waiting.save)(self);
|
||||
if active != Some(uid)
|
||||
&& let Some(i) = active.and_then(|a| index_of(self, a))
|
||||
{
|
||||
let _ = self.run("file.activate", json!({"index": i}));
|
||||
}
|
||||
if let Err(e) = saved {
|
||||
self.status_error("Save", e);
|
||||
}
|
||||
}
|
||||
|
||||
/// Deliver window actions to the host without persisting them in UI preferences.
|
||||
|
||||
@@ -143,6 +143,12 @@ struct ShotRequest {
|
||||
slot: Option<craft_ui::capture::CaptureSlot>,
|
||||
}
|
||||
|
||||
/// A save that waits for the checks of document `uid`'s linked files ([`DesignApp::waiting_save`]).
|
||||
pub(crate) struct WaitingSave {
|
||||
pub(crate) uid: u64,
|
||||
pub(crate) save: Box<dyn FnOnce(&mut DesignApp) -> Result<Value, String>>,
|
||||
}
|
||||
|
||||
pub struct DesignApp {
|
||||
pending_pdf: Option<ImportedFile>,
|
||||
/// The files of the batch after `pending_pdf`, which wait for its Import Options.
|
||||
@@ -306,6 +312,9 @@ pub struct DesignApp {
|
||||
pub(crate) quit_without_asking: bool,
|
||||
/// No document is discarded until every unsaved document has an up-to-date approval.
|
||||
quitting: Option<QuitState>,
|
||||
/// A save asked for while the open-time checks of the document's linked files run: it runs
|
||||
/// once they are done, so the first save already writes the links they confirm.
|
||||
pub(crate) waiting_save: Option<WaitingSave>,
|
||||
/// The tool cursor bitmaps handed to the OS.
|
||||
pub cursors: cursors::Images,
|
||||
/// A panel's live preview (a slider being dragged), committed when it is let go.
|
||||
|
||||
@@ -36,11 +36,26 @@ fn rerun_with_path(app: &mut DesignApp, purpose: &str, id: &str, p: &Value, key:
|
||||
fn save_as_dialog(app: &mut DesignApp, after: impl FnOnce(&mut DesignApp) -> Result<Value, String> + 'static) -> Result<Value, String> {
|
||||
let name = app.session.active().map(|d| format!("{}.designcraft", d.doc.title)).unwrap_or_default();
|
||||
app.ask_save(&name, Bound::Document, move |app, path| {
|
||||
app.run("file.saveAs", json!({"path": path}))?;
|
||||
after(app)
|
||||
save_when_checked(app, move |app| {
|
||||
app.run("file.saveAs", json!({"path": path}))?;
|
||||
after(app)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Run `save` (a save of the active document) now, or, while the open-time checks of its linked
|
||||
/// files run, once they are done ([`DesignApp::run_waiting_save`]): a document saved before
|
||||
/// linked storage then writes the links the checks confirm in its first save, not a copy of every
|
||||
/// image. The status line counts the files left; Esc saves at once. A later save replaces one
|
||||
/// still waiting.
|
||||
pub(crate) fn save_when_checked(app: &mut DesignApp, save: impl FnOnce(&mut DesignApp) -> Result<Value, String> + 'static) -> Result<Value, String> {
|
||||
let Some(st) = app.session.active().filter(|st| st.links.checking()) else { return save(app) };
|
||||
let (uid, remaining) = (st.uid, st.links.remaining());
|
||||
app.waiting_save = Some(crate::WaitingSave { uid, save: Box::new(save) });
|
||||
app.show_save_wait(remaining);
|
||||
Ok(json!({"waiting": true}))
|
||||
}
|
||||
|
||||
/// Save the active document (a download in a browser; Save As for one never saved), then run
|
||||
/// `after` once it is written. A save dialog answers on a later frame; Cancel runs nothing.
|
||||
#[track_caller]
|
||||
@@ -50,8 +65,10 @@ pub(crate) fn save_then(app: &mut DesignApp, after: impl FnOnce(&mut DesignApp)
|
||||
return after(app);
|
||||
}
|
||||
if app.session.active().is_some_and(|d| d.path.is_some()) {
|
||||
app.run("file.save", json!({}))?;
|
||||
return after(app);
|
||||
return save_when_checked(app, |app| {
|
||||
app.run("file.save", json!({}))?;
|
||||
after(app)
|
||||
});
|
||||
}
|
||||
save_as_dialog(app, after)
|
||||
}
|
||||
@@ -177,12 +194,15 @@ fn run_ui_part1(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value
|
||||
"app.save" | "app.saveDialog" if app.services.download.is_some() => download_document(app),
|
||||
"app.save" => {
|
||||
if app.session.active().is_some_and(|d| d.path.is_some()) {
|
||||
return Some(app.run("file.save", json!({})));
|
||||
return Some(save_when_checked(app, |app| app.run("file.save", json!({}))));
|
||||
}
|
||||
return run_ui(app, "app.saveDialog", p);
|
||||
}
|
||||
"app.saveDialog" => match p.get("path").and_then(Value::as_str) {
|
||||
Some(path) => app.run("file.saveAs", json!({"path": path})),
|
||||
Some(path) => {
|
||||
let path = path.to_owned();
|
||||
save_when_checked(app, move |app| app.run("file.saveAs", json!({"path": path})))
|
||||
}
|
||||
None => save_as_dialog(app, |_| Ok(Value::Null)),
|
||||
},
|
||||
"app.quit" => {
|
||||
@@ -190,10 +210,13 @@ fn run_ui_part1(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value
|
||||
Ok(Value::Null)
|
||||
}
|
||||
"app.saveCopyDialog" => match p.get("path").and_then(Value::as_str) {
|
||||
Some(path) => app.run("file.saveACopy", json!({"path": path})),
|
||||
Some(path) => {
|
||||
let path = path.to_owned();
|
||||
save_when_checked(app, move |app| app.run("file.saveACopy", json!({"path": path})))
|
||||
}
|
||||
None => {
|
||||
let name = app.session.active().map(|d| format!("{} copy.designcraft", d.doc.title)).unwrap_or_default();
|
||||
app.ask_save(&name, Bound::Document, |app, path| app.run("file.saveACopy", json!({"path": path})))
|
||||
app.ask_save(&name, Bound::Document, |app, path| save_when_checked(app, move |app| app.run("file.saveACopy", json!({"path": path}))))
|
||||
}
|
||||
},
|
||||
"app.exportPng" => export_png(app, p),
|
||||
|
||||
@@ -2523,3 +2523,48 @@ fn the_eyedropper_samples_an_image_still_decoding() {
|
||||
let ctx = egui::Context::default();
|
||||
assert_eq!(canvas::color_at(&mut app, &ctx, at), Some([255, 0, 0]));
|
||||
}
|
||||
|
||||
/// A save asked for while the open-time checks of linked files run waits for them (the status
|
||||
/// line counts them), then writes; Esc writes at once.
|
||||
#[test]
|
||||
fn a_save_waits_for_the_checks_of_linked_files() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let image = dir.path().join("art.png");
|
||||
std::fs::write(&image, designcraft_render::Rendered { width: 10, height: 10, pixels: vec![200; 400] }.to_png()).unwrap();
|
||||
let doc = dir.path().join("doc.designcraft");
|
||||
let mut first = DesignApp::new(Session::new(), Services::default());
|
||||
first.run("file.new", json!({})).unwrap();
|
||||
first.run("file.place", json!({"path": image.to_string_lossy()})).unwrap();
|
||||
first.run("file.saveAs", json!({"path": doc.to_string_lossy()})).unwrap();
|
||||
|
||||
let reopened = |dir: &std::path::Path| {
|
||||
let mut app = DesignApp::new(Session::new(), Services::default());
|
||||
app.run("file.open", json!({"path": dir.join("doc.designcraft").to_string_lossy()})).unwrap();
|
||||
assert!(app.session.active().unwrap().links.checking(), "the open checks its linked file in the background");
|
||||
app.run("frame.create", json!({"rect": [36, 36, 136, 136]})).unwrap();
|
||||
app
|
||||
};
|
||||
let mut app = reopened(dir.path());
|
||||
let before = std::fs::read(&doc).unwrap();
|
||||
assert_eq!(app.run("app.save", json!({})).unwrap()["waiting"], true);
|
||||
assert_eq!(std::fs::read(&doc).unwrap(), before, "nothing is written while the check runs");
|
||||
assert!(app.ui.status.contains("Checking 1 linked image"), "{}", app.ui.status);
|
||||
let ctx = egui::Context::default();
|
||||
for _ in 0..1000 {
|
||||
if app.waiting_save.is_none() {
|
||||
break;
|
||||
}
|
||||
app.poll_link_checks(&ctx);
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
assert!(app.waiting_save.is_none(), "the save ran once the check was done");
|
||||
assert!(!app.session.active().unwrap().is_dirty());
|
||||
assert_ne!(std::fs::read(&doc).unwrap(), before);
|
||||
|
||||
let mut app = reopened(dir.path());
|
||||
let before = std::fs::read(&doc).unwrap();
|
||||
app.run("app.save", json!({})).unwrap();
|
||||
app.run_waiting_save(true);
|
||||
assert!(app.waiting_save.is_none() && !app.session.active().unwrap().is_dirty(), "Esc saves without waiting");
|
||||
assert_ne!(std::fs::read(&doc).unwrap(), before);
|
||||
}
|
||||
Reference in new issue
Block a user