diff --git a/crates/engine/src/cmd/native_links.rs b/crates/engine/src/cmd/native_links.rs index 22011f1cd..e6ffdef82 100644 --- a/crates/engine/src/cmd/native_links.rs +++ b/crates/engine/src/cmd/native_links.rs @@ -29,11 +29,11 @@ use crate::{DocState, Result, Session}; pub fn specs() -> Vec { 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 { self.notice.take() @@ -180,6 +188,8 @@ struct JobState { work: Vec, results: Vec, 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>) 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>) job.changed.notify_all(); } }); - lock(job).done = true; + { + let mut state = lock(job); + state.done = true; + state.busy = false; + } job.changed.notify_all(); } diff --git a/crates/engine/src/cmd/native_links/tests.rs b/crates/engine/src/cmd/native_links/tests.rs index 73921136a..58c5a48cd 100644 --- a/crates/engine/src/cmd/native_links/tests.rs +++ b/crates/engine/src/cmd/native_links/tests.rs @@ -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(); diff --git a/crates/images/src/tests.rs b/crates/images/src/tests.rs index 5995daf4d..6abb35310 100644 --- a/crates/images/src/tests.rs +++ b/crates/images/src/tests.rs @@ -383,6 +383,7 @@ fn decode_bytes_per_pixel_counts_the_stored_samples_and_their_copy() { assert_eq!(decode_bytes_per_pixel(&tiff_of::(1, 1, &[0; 4], false)), 14); assert_eq!(decode_bytes_per_pixel(&tiff_of::(1, 1, &[0; 5], true)), 15); assert_eq!(decode_bytes_per_pixel(&tiff_of::(1, 1, &[0; 4], false)), 18); + assert_eq!(decode_bytes_per_pixel(&tiff_of::(1, 1, &[0; 5], true)), 20); // Two copies of the 16-bit samples while they are turned upright. assert_eq!(decode_bytes_per_pixel(&tiff_of::(1, 1, &[0; 3], false)), 12); // A progressive JPEG's coefficients beside its 8-bit RGB output. @@ -405,6 +406,18 @@ fn decode_bytes_per_pixel_counts_the_stored_samples_and_their_copy() { assert_eq!(decode_bytes_per_pixel(&dos), 18); } +/// 16-bit CMYK with an alpha sample, which the `tiff` encoder has no type for. +struct Cmyka16; +impl tiff::encoder::colortype::ColorType for Cmyka16 { + type Inner = u16; + const TIFF_VALUE: tiff::tags::PhotometricInterpretation = tiff::tags::PhotometricInterpretation::CMYK; + const BITS_PER_SAMPLE: &[u16] = &[16; 5]; + const SAMPLE_FORMAT: &[tiff::tags::SampleFormat] = &[tiff::tags::SampleFormat::Uint; 5]; + fn horizontal_predict(row: &[u16], result: &mut Vec) { + result.extend_from_slice(row); + } +} + /// Indexed colour (PhotometricInterpretation 3), which the `tiff` encoder has no type for. struct Palette; impl tiff::encoder::colortype::ColorType for Palette { diff --git a/crates/render/src/images.rs b/crates/render/src/images.rs index 4b5c7f83d..447dd904d 100644 --- a/crates/render/src/images.rs +++ b/crates/render/src/images.rs @@ -15,7 +15,7 @@ //! remembered apart from failed decodes: it is asked for again once //! [`designcraft_doc::asset_data::link_generation`] has advanced (a relink, an update). -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Condvar, Mutex}; @@ -996,6 +996,90 @@ pub(crate) fn warm(wants: Vec) -> usize { run_jobs(workers, jobs) } +/// Each asset's full pixel size and the levels it is drawn at. +type DrawnLevels = HashMap<(SourceKey, u32), ((u32, u32), Vec)>; + +/// Whether every drawing of `k` in `levels` finds its level cached, or a finer one to halve. +fn drawn_cached(c: &Cache, k: &(SourceKey, u32), levels: &DrawnLevels) -> bool { + levels.get(k).is_some_and(|(_, ls)| ls.iter().all(|l| (0..=*l).any(|f| c.map.contains_key(&(k.0, k.1, f))))) +} + +/// Whether `k` failed to decode, or its linked bytes couldn't be had under the links of +/// `generation`. +fn failed(c: &Cache, k: &(SourceKey, u32), generation: u64) -> bool { + c.bad.contains_key(k) || c.unavailable.get(&k.0).is_some_and(|(_, g, _)| *g == generation) +} + +/// What decoding the drawings `wanted` of the asset `key` adds to the cache and holds at its +/// peak, and whether that is their reduced JPEG estimate (else a full decode's, which keeps only +/// the levels drawn). +fn estimate(key: (SourceKey, u32), wanted: &[Want], levels: &DrawnLevels) -> (usize, usize, bool) { + fault(key.0); + if let Some((size, decoding)) = reduced_estimates(wanted) { + return (size, decoding, true); + } + let full = wanted.first().and_then(|w| Some(decode_sizes(&w.bytes.load().ok()?, w.page))).unwrap_or((0, 0)); + let kept = levels.get(&key).map_or(full.0, |(size, ls)| { + let mut ls = ls.clone(); + ls.sort_unstable(); + ls.dedup(); + ls.iter().fold(0usize, |n, l| { + let (w, h) = halved(*size, *l); + n.saturating_add((w as usize).saturating_mul(h as usize).saturating_mul(4)) + }) + }); + (kept, full.1, false) +} + +/// The bytes each asset in `index` holds in the cache, when it is cached or failed, and the bytes +/// of everything else cached. An asset some drawing of which finds no level to draw is not held: +/// it is decoded again, and the cached levels its decode estimate covers count with that +/// estimate, not with everything else. The estimate of an asset in `reduced` covers the levels +/// from the one read from the JPEG blocks to each one drawn, as [`reduced_estimates`] counts +/// them; a full decode's covers the levels drawn. The levels of the assets in `index` become the +/// most recently used, so later stores evict other images first. +fn held_in_view( + c: &mut Cache, + index: &HashMap<(SourceKey, u32), usize>, + levels: &DrawnLevels, + reduced: &HashSet<(SourceKey, u32)>, + generation: u64, +) -> (HashMap<(SourceKey, u32), usize>, usize) { + let cached: HashSet<(SourceKey, u32)> = index.keys().filter(|k| drawn_cached(c, k, levels)).copied().collect(); + let covered = |k: &(SourceKey, u32), level: u8| { + let Some((_, ls)) = levels.get(k) else { return level == 0 }; + if reduced.contains(k) { + ls.iter().any(|l| (*l).min(designcraft_images::MAX_REDUCED_SHRINK) <= level && level <= *l) + } else { + ls.contains(&level) + } + }; + c.clock += 1; + let clock = c.clock; + let mut held: HashMap<(SourceKey, u32), usize> = HashMap::new(); + let mut in_view = 0usize; + for (k, e) in c.map.iter_mut() { + let asset = (k.0, k.1); + if !index.contains_key(&asset) { + continue; + } + e.stamp = clock; + let n = size_of(&e.pm); + if cached.contains(&asset) { + *held.entry(asset).or_default() += n; + in_view = in_view.saturating_add(n); + } else if covered(&asset, k.2) { + in_view = in_view.saturating_add(n); + } + } + for k in index.keys() { + if failed(c, k, generation) { + held.entry(*k).or_default(); + } + } + (held, c.bytes.saturating_sub(in_view)) +} + /// [`warm`]'s decodes: the bytes to reserve for each and the drawings it serves. Empty when /// fewer than two images need decoding. fn plan(wants: Vec) -> Vec<(usize, Vec)> { @@ -1013,7 +1097,7 @@ fn plan(wants: Vec) -> Vec<(usize, Vec)> { } } // Each asset's full pixel size and the level [`mip`] takes for each drawing of it. - let levels: HashMap<(SourceKey, u32), ((u32, u32), Vec)> = assets + let levels: DrawnLevels = assets .iter() .filter_map(|(key, wanted)| { let w = wanted.first()?; @@ -1021,31 +1105,20 @@ fn plan(wants: Vec) -> Vec<(usize, Vec)> { Some((*key, (size, wanted.iter().map(|w| level_for(size, w.width_pt, w.on_screen)).collect()))) }) .collect(); - // The bytes each asset in view holds in the cache, when it is cached or failed, and the bytes - // of everything else cached. Its levels become the most recently used, so the stores below - // evict other images first. - let (held, others) = with(|c| { - c.clock += 1; - let clock = c.clock; - let mut held: HashMap<(SourceKey, u32), usize> = HashMap::new(); - for (k, e) in c.map.iter_mut() { - if index.contains_key(&(k.0, k.1)) { - e.stamp = clock; - *held.entry((k.0, k.1)).or_default() += size_of(&e.pm); - } - } - // An asset is cached when every drawing of it finds its level, or a finer one to halve. - held.retain(|k, _| levels.get(k).is_some_and(|(_, ls)| ls.iter().all(|l| (0..=*l).any(|f| c.map.contains_key(&(k.0, k.1, f)))))); - let in_view = held.values().sum::(); - let generation = designcraft_doc::asset_data::link_generation(); - for k in index.keys() { - if c.bad.contains_key(k) || c.unavailable.get(&k.0).is_some_and(|(_, g, _)| *g == generation) { - held.entry(*k).or_default(); - } - } - (held, c.bytes.saturating_sub(in_view)) - }); + let generation = designcraft_doc::asset_data::link_generation(); + // The assets that are neither cached nor failed. + let to_decode: HashSet<(SourceKey, u32)> = + with(|c| index.keys().filter(|k| !failed(c, k, generation) && !drawn_cached(c, k, &levels)).copied().collect()); // One image to decode gains nothing from threads (and is not worth sizing). + if to_decode.len() < 2 { + return Vec::new(); + } + let estimates: HashMap<(SourceKey, u32), (usize, usize, bool)> = + assets.iter().filter(|(key, _)| to_decode.contains(key)).map(|(key, wanted)| (*key, estimate(*key, wanted, &levels))).collect(); + let reduced: HashSet<(SourceKey, u32)> = estimates.iter().filter(|(_, e)| e.2).map(|(k, _)| *k).collect(); + // Another render may have stored or evicted levels since: an asset evicted in between is sized + // below, and its cached levels count as a full decode's. + let (held, others) = with(|c| held_in_view(c, &index, &levels, &reduced, generation)); if assets.iter().filter(|(key, _)| !held.contains_key(key)).count() < 2 { return Vec::new(); } @@ -1055,21 +1128,10 @@ fn plan(wants: Vec) -> Vec<(usize, Vec)> { if held.contains_key(&key) { continue; } - fault(key.0); - let (size, decoding) = reduced_estimates(&wanted).unwrap_or_else(|| { - let full = wanted.first().and_then(|w| Some(decode_sizes(&w.bytes.load().ok()?, w.page))).unwrap_or((0, 0)); - // Only the levels drawn are kept. - let kept = levels.get(&key).map_or(full.0, |(size, ls)| { - let mut ls = ls.clone(); - ls.sort_unstable(); - ls.dedup(); - ls.iter().fold(0usize, |n, l| { - let (w, h) = halved(*size, *l); - n.saturating_add((w as usize).saturating_mul(h as usize).saturating_mul(4)) - }) - }); - (kept, full.1) - }); + let (size, decoding, _) = match estimates.get(&key) { + Some(e) => *e, + None => estimate(key, &wanted, &levels), + }; // A store past the budget evicts down to three quarters of it: the images in view must // fit in that, unless they and the rest fit in the budget. A decode that alone holds // more than the budget is left to the render too. @@ -1308,9 +1370,11 @@ pub(crate) mod tests { let reserved = decoding * 3 / 2; assert!(admits(0, reserved) && !admits(reserved, reserved) && admits(reserved, decode_peak_bytes(&small.load().unwrap(), 0) * 3 / 2)); assert!(admits(0, usize::MAX), "an image over half the budget still decodes alone"); + // Whether the small image's decode overlaps a large one depends on the threads, so the + // most decoding at once is one large reservation, or one large and the small one. + let small_reserved = decode_peak_bytes(&small.load().unwrap(), 0) * 3 / 2; let peak = warm(vec![want(&d), want(&e), want(&small)]); - assert!(peak <= BUDGET / 2, "{peak} bytes decoding at once"); - assert!(peak >= reserved, "the large images were decoded ahead"); + assert!(peak == reserved || peak == reserved + small_reserved, "{peak} bytes decoding at once"); let me = std::thread::current().id(); for img in [&d, &e, &small] { let on = decoders(img, since); @@ -1389,6 +1453,38 @@ pub(crate) mod tests { assert_eq!(warm(vec![small(&a), small(&b)]), 0, "nothing left to decode"); } + #[test] + fn levels_of_an_image_in_view_count_once_when_it_is_decoded_again() { + // A JPEG drawn at levels 3 and 5 has levels 4 and 6 cached: the drawing at level 3 finds + // nothing to halve, so it is decoded again. Another image is cached outside the view. + let (jpeg, other) = (AssetData::embedded(vec![1]), AssetData::embedded(vec![2])); + let (id, other_id) = (jpeg.key(), other.key()); + let mut c = Cache::default(); + let put = |c: &mut Cache, key: (SourceKey, u32, u8), side: u16| { + let pm = Arc::new(Pixmap::new(side, side)); + c.bytes += size_of(&pm); + c.map.insert(key, Entry { pm, stamp: 0 }); + }; + put(&mut c, (id, 0, 4), 16); + put(&mut c, (id, 0, 6), 4); + put(&mut c, (other_id, 0, 0), 8); + let index = HashMap::from([((id, 0), 0)]); + let levels = HashMap::from([((id, 0), ((256, 256), vec![3, 5]))]); + let reduced = HashSet::from([(id, 0)]); + let (held, others) = held_in_view(&mut c, &index, &levels, &reduced, 0); + assert!(held.is_empty(), "level 3 has nothing to halve, so the JPEG is decoded"); + // Its reduced estimate counts levels 3 to 5, so level 4 counts with it; level 6 stays + // cached beside it. + assert_eq!(others, 8 * 8 * 4 + 4 * 4 * 4); + // A full decode keeps only the levels drawn: level 4 stays with the other images too. + let (_, others) = held_in_view(&mut c, &index, &levels, &HashSet::new(), 0); + assert_eq!(others, 8 * 8 * 4 + 16 * 16 * 4 + 4 * 4 * 4); + // With level 3 cached, every drawing finds its level and the JPEG is held with all three. + put(&mut c, (id, 0, 3), 32); + let (held, others) = held_in_view(&mut c, &index, &levels, &reduced, 0); + assert_eq!((held.get(&(id, 0)).copied(), others), (Some(32 * 32 * 4 + 16 * 16 * 4 + 4 * 4 * 4), 8 * 8 * 4)); + } + #[test] fn screen_levels_of_a_jpeg_need_no_full_size_decode() { let bytes = jpeg(1024, 768); diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index b048b68d4..45c296883 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -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(), diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 7e67ddb7d..09aa13f53 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -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. diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 5c30ede0c..0896eb19c 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -143,6 +143,12 @@ struct ShotRequest { slot: Option, } +/// 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 Result>, +} + pub struct DesignApp { pending_pdf: Option, /// 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, + /// 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, /// 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. diff --git a/crates/ui-egui/src/menus/run.rs b/crates/ui-egui/src/menus/run.rs index 455fbe9d9..ee571d398 100644 --- a/crates/ui-egui/src/menus/run.rs +++ b/crates/ui-egui/src/menus/run.rs @@ -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 + 'static) -> Result { 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 + 'static) -> Result { + 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 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 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), diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index b4dca3da8..abac3fe3a 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -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); +}