LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Local: clicking the folder being read again keeps the running read instead of restarting it
Clément Ld6be9f63424a
diff --git a/crates/ui-egui/src/headless.rs b/crates/ui-egui/src/headless.rsindex 96d22e326..7225e0709 100644--- a/crates/ui-egui/src/headless.rs+++ b/crates/ui-egui/src/headless.rs@@ -894,6 +894,39 @@ mod tests {/// The folder scan runs in the background: the request returns at once and the review opens/// when the scan finishes (a folder on a network share must not freeze the window).+ /// Clicking the folder being read again keeps the running read (and its progress).+ #[test]+ fn browsing_the_same_folder_again_does_not_restart() {+ let mut h = demo([1200.0, 900.0]);+ let t = Duration::from_secs(10);+ let dir = std::env::temp_dir().join(format!("lc-browse-again-{}", std::process::id()));+ std::fs::create_dir_all(&dir).unwrap();+ let img = lightcraft_raster::Rgba8::from_fn(8, 8, |x, y| [(x * 9) as u8, (y * 12) as u8, 80, 255]);+ let o = lightcraft_engine::export::ExportOptions { format: lightcraft_engine::export::ExportFormat::Png, ..Default::default() };+ std::fs::write(dir.join("a.png"), lightcraft_engine::export::encode_image(&img, &o).unwrap()).unwrap();+ let gate = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));+ let started = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));+ let (g, n) = (gate.clone(), started.clone());+ h.app.session.media.file_probe = Some(std::sync::Arc::new(move |_: &str| {+ n.fetch_add(1, std::sync::atomic::Ordering::Relaxed);+ while !g.load(std::sync::atomic::Ordering::Relaxed) {+ std::thread::sleep(Duration::from_millis(5));+ }+ Ok(lightcraft_engine::media::ProbeInfo { format: "PNG".into(), ..Default::default() })+ }));+ let browse =+ |h: &mut Headless| h.request("engine.execute", json!({"command": "library.browse", "params": {"path": dir.to_string_lossy()}}), t);+ assert_eq!(browse(&mut h)["result"]["scanning"], true);+ std::thread::sleep(Duration::from_millis(200));+ assert_eq!(browse(&mut h)["result"]["scanning"], true);+ std::thread::sleep(Duration::from_millis(200));+ assert_eq!(started.load(std::sync::atomic::Ordering::Relaxed), 1, "the second click must not start another read");+ gate.store(true, std::sync::atomic::Ordering::Relaxed);+ h.settle(SETTLE);+ assert_eq!(h.app.session.visible_cloned().len(), 1);+ let _ = std::fs::remove_dir_all(&dir);+ }+#[test]fn add_folder_scans_in_the_background() {let mut h = demo([1200.0, 900.0]);diff --git a/crates/ui-egui/src/import.rs b/crates/ui-egui/src/import.rsindex 8bf00e3ec..719b235a6 100644--- a/crates/ui-egui/src/import.rs+++ b/crates/ui-egui/src/import.rs@@ -126,12 +126,18 @@ pub fn browse(app: &mut LightcraftApp, path: &str, subfolders: Option<bool>) ->if app.scan.as_ref().is_some_and(|t| !t.browse) || app.import.as_ref().is_some_and(|t| !t.browse) {return Err("an import is running".into());}+ let dir_s = dir.to_string_lossy().trim_end_matches(['/', '\\']).to_string();+ let subfolders = subfolders.unwrap_or_else(|| app.session.browse.as_ref().is_some_and(|b| b.subfolders));+ let running = app.scan.as_ref().is_some_and(|t| t.browse) || app.import.as_ref().is_some_and(|t| t.browse);+ if running && app.session.browse.as_ref().is_some_and(|b| b.path == dir_s && b.subfolders == subfolders) {+ // already reading this folder: clicking it again must not restart the progress+ app.session.source = lightcraft_engine::LibrarySource::Folder;+ return Ok(json!({"path": dir_s, "subfolders": subfolders, "scanning": true}));+ }if let Some(t) = app.scan.take() {t.progress.cancel.store(true, Ordering::Relaxed);}app.import = None;- let dir_s = dir.to_string_lossy().trim_end_matches(['/', '\\']).to_string();- let subfolders = subfolders.unwrap_or_else(|| app.session.browse.as_ref().is_some_and(|b| b.subfolders));app.session.browse = Some(lightcraft_engine::Browse { path: dir_s.clone(), subfolders });app.session.source = lightcraft_engine::LibrarySource::Folder;let (input, _) = ScanInput::new(&mut app.session, std::slice::from_ref(&dir_s));