DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Recent files: look them up on disk at most every 3 s, not every frame; Open Recent names the file without an undocumented param (#706)
aiongg1cd3b38af1f8
diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rsindex 4c9e7e36b..0dfdaa052 100644--- a/crates/engine/src/cmd/file.rs+++ b/crates/engine/src/cmd/file.rs@@ -187,7 +187,6 @@ fn has_recent(s: &Session) -> std::result::Result<(), String> {if s.prefs.recent_files.is_empty() { Err("no recent files".into()) } else { Ok(()) }}-/// One File › Open Recent row: the path, its file name and whether it is still there./// A recent file's first-page picture as straight RGBA, for the start screen.pub struct RecentPreview {pub width: u32,@@ -213,6 +212,7 @@ pub fn recent_preview(path: &str) -> Option<RecentPreview> {}}+/// One File › Open Recent row: the path, its file name and whether it is still there.fn recent_entry(path: &str) -> Value {let name = std::path::Path::new(path).file_name().map_or_else(|| path.to_string(), |n| n.to_string_lossy().to_string());#[cfg(not(target_arch = "wasm32"))]diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rsindex 3ce1dbefd..4d3262282 100644--- a/crates/ui-egui/src/chrome.rs+++ b/crates/ui-egui/src/chrome.rs@@ -766,8 +766,9 @@ fn recent_files(app: &mut DesignApp, ui: &mut egui::Ui) {let mut open = None;ui.horizontal_wrapped(|ui| {for (i, path) in rows {- let modified = std::fs::metadata(&path).ok().filter(|m| m.is_file()).map(|m| m.modified().ok());- let there = modified.is_some();+ let stat = recent_file_stat(ui.ctx(), &path);+ let there = stat.exists;+ let modified = there.then_some(stat.modified);let (cr, resp) = ui.allocate_exact_size(vec2(140.0, 170.0), Sense::click());let resp = resp.on_hover_text(&path);ui.painter().rect_filled(cr, 6.0, if resp.hovered() { t.hover } else { t.panel });@@ -805,7 +806,38 @@ fn recent_files(app: &mut DesignApp, ui: &mut egui::Ui) {}}-/// The texture of a recent file's saved first-page picture, read again when the file changes.+/// How often a recent file is looked up on disk again, in ms: the start screen and the Open Recent+/// menu draw every frame and must not touch the file system each time.+const RECENT_STAT_MS: f64 = 3000.0;++/// A recent file as last seen on disk.+#[derive(Clone, Debug, PartialEq)]+pub(crate) struct RecentStat {+ pub exists: bool,+ pub modified: Option<std::time::SystemTime>,+ checked: f64,+}++/// Whether recent file `path` is there and when it last changed, looked up again at most every+/// [`RECENT_STAT_MS`].+pub(crate) fn recent_file_stat(ctx: &egui::Context, path: &str) -> RecentStat {+ recent_file_stat_at(ctx, path, crate::now_ms())+}++fn recent_file_stat_at(ctx: &egui::Context, path: &str, now: f64) -> RecentStat {+ let key = egui::Id::new(("recent_stat", path));+ if let Some(st) = ctx.data(|d| d.get_temp::<RecentStat>(key))+ && (0.0..RECENT_STAT_MS).contains(&(now - st.checked))+ {+ return st;+ }+ let meta = std::fs::metadata(path).ok().filter(std::fs::Metadata::is_file);+ let st = RecentStat { exists: meta.is_some(), modified: meta.and_then(|m| m.modified().ok()), checked: now };+ ctx.data_mut(|d| d.insert_temp(key, st.clone()));+ st+}++/// The texture of a recent file's saved first-page picture, decoded once per file version.fn recent_preview_texture(ctx: &egui::Context, path: &str, modified: Option<std::time::SystemTime>) -> Option<egui::TextureHandle> {type Cached = (Option<std::time::SystemTime>, Option<egui::TextureHandle>);let key = egui::Id::new(("recent_preview", path));@@ -879,6 +911,22 @@ mod tests {const LAST_CONTROL: &str = "Jump to Next Column";const FIRST_CONTROL: &str = "Rotate 90° Counterclockwise";+ /// The start screen and Open Recent menu look a recent file up on disk at most every few+ /// seconds, not every frame.+ #[test]+ fn recent_files_are_looked_up_on_disk_every_few_seconds() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("a.designcraft");+ std::fs::write(&path, b"x").unwrap();+ let path = path.to_string_lossy().to_string();+ let ctx = egui::Context::default();+ let first = super::recent_file_stat_at(&ctx, &path, 10_000.0);+ assert!(first.exists && first.modified.is_some());+ std::fs::remove_file(&path).unwrap();+ assert_eq!(super::recent_file_stat_at(&ctx, &path, 10_000.0 + super::RECENT_STAT_MS - 1.0), first, "cached");+ assert!(!super::recent_file_stat_at(&ctx, &path, 10_000.0 + super::RECENT_STAT_MS).exists, "looked up again");+ }+fn app() -> crate::DesignApp {let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());app.ui.control_bar = true;diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex fc7a3e634..01cf46122 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -983,6 +983,12 @@ impl DesignApp {Some(path) => std::path::Path::new(path).file_name().map_or_else(|| path.to_string(), |n| n.to_string_lossy().to_string()),None => params.get("name").and_then(Value::as_str).unwrap_or_default().to_string(),};+ self.open_named_file(cmd, params, file)+ }++ /// [`Self::open_file`] for a command whose params don't name the file: `file` is shown in the+ /// alert or warnings.+ pub fn open_named_file(&mut self, cmd: &str, params: Value, file: String) -> Result<Value, String> {let r = self.run(cmd, params);if let Err(e) = &r {let title = if cmd == "file.place" { "Can't Place the File" } else { "Can't Open the File" };diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex b9744f4b7..1221a1d31 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -778,7 +778,7 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,.and_then(|i| app.session.prefs.recent_files.get(i)).map(|path| recent_label(path)).unwrap_or_default();- app.open_file("file.openRecent", json!({"index": index, "name": name}))+ app.open_named_file("file.openRecent", json!({"index": index}), name)}"app.placeDialog" => app.pick_and_open("place"),"app.placeWithOptions" => {@@ -2159,7 +2159,7 @@ fn recent_menu_items(app: &mut DesignApp, ui: &mut egui::Ui) {Item::Cmd { label, id, params, .. } => {let path = params["index"].as_u64().and_then(|i| app.session.prefs.recent_files.get(i as usize)).cloned().unwrap_or_default();let mut text = egui::RichText::new(label);- if !std::path::Path::new(&path).is_file() {+ if !crate::chrome::recent_file_stat(ui.ctx(), &path).exists {text = text.color(dim);}if ui.button(crate::rtl::widget(ui, text)).on_hover_text(&path).clicked() {