Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Issue #345: Find Missing Photos in the activity stack

Andrey Vyrodov1c89caa4a160
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index af75d1958..f2ba2f258 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -868,7 +868,8 @@ impl LightcraftApp {
let undo0 = app.session.undo.len();
app.import = Some(import::ImportTask::new(paths, params, undo0, false).auto());
};
- if let Err(e) = tasks::spawn(self, LABEL, work, done) {
+ // listed every few seconds: quiet, no row in the activity stack
+ if let Err(e) = tasks::spawn(self, LABEL, None, work, done) {
log::warn!("{e}");
}
}
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 2c63a3d30..c9b397f60 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -1212,7 +1212,7 @@ pub fn run_ui_command(app: &mut LightcraftApp, id: &str, p: &Value) -> Option<Re
}
};
app.ui.last_find_missing = None;
- if let Err(e) = crate::tasks::spawn(app, LABEL, work, done) {
+ if let Err(e) = crate::tasks::spawn(app, LABEL, Some("findMissing"), work, done) {
return Some(Err(e));
}
if p.get("wait").and_then(Value::as_bool).unwrap_or(false) {
diff --git a/crates/ui-egui/src/tasks.rs b/crates/ui-egui/src/tasks.rs
index 7e0d053e6..fb5a1f031 100644
--- a/crates/ui-egui/src/tasks.rs
+++ b/crates/ui-egui/src/tasks.rs
@@ -1,7 +1,8 @@
//! Background tasks for commands whose file-system work can take long on a slow or offline drive
//! (Find Missing Photos walking a folder, listing the auto-import folder…): the work runs on a
//! worker thread and its result is applied on the UI thread between frames, so the window keeps
-//! answering. Imports, scans, exports and preview builds have their own tasks with progress.
+//! answering. Imports, scans, exports and preview builds have their own tasks with progress. A task the user started
+//! (Find Missing Photos) shows in the activity stack while it runs; quiet ones (the auto-import listing) don't.
use std::sync::mpsc::{Receiver, TryRecvError, channel};
@@ -13,6 +14,8 @@ type Finish = Box<dyn FnOnce(&mut LightcraftApp, &egui::Context) + Send>;
struct Task {
label: String,
rx: Receiver<Finish>,
+ /// Its row in the activity stack (none for quiet tasks); goes with the task.
+ _guard: Option<lightcraft_engine::activity::TaskGuard>,
}
/// The background tasks in flight.
@@ -39,10 +42,12 @@ impl Tasks {
}
/// Run `work` on a worker thread, then `done(app, ctx, result)` on the UI thread (in the browser
-/// build, which has no threads, `work` runs at once and `done` on the next frame).
+/// build, which has no threads, `work` runs at once and `done` on the next frame). With `kind`, the
+/// activity stack shows `label` meanwhile, without a count and without ✕ (the work can't be stopped).
pub fn spawn<T: Send + 'static>(
app: &mut LightcraftApp,
label: &str,
+ kind: Option<&'static str>,
work: impl FnOnce() -> T + Send + 'static,
done: impl FnOnce(&mut LightcraftApp, &egui::Context, T) + Send + 'static,
) -> Result<(), String> {
@@ -59,7 +64,8 @@ pub fn spawn<T: Send + 'static>(
std::thread::Builder::new().name(format!("lc-task-{label}")).spawn(job).map_err(|e| format!("{label}: could not start: {e}"))?;
#[cfg(target_arch = "wasm32")]
job();
- app.tasks.running.push(Task { label: label.to_string(), rx });
+ let guard = kind.map(|k| app.session.activity.start(k, label, lightcraft_engine::activity::Cancel::No));
+ app.tasks.running.push(Task { label: label.to_string(), rx, _guard: guard });
Ok(())
}
diff --git a/crates/ui-egui/src/tests_activity.rs b/crates/ui-egui/src/tests_activity.rs
index db4b58297..05479e683 100644
--- a/crates/ui-egui/src/tests_activity.rs
+++ b/crates/ui-egui/src/tests_activity.rs
@@ -329,3 +329,34 @@ fn final_merge_shows_a_row_and_cancels() {
assert_eq!(toast, "HDR merge cancelled");
let _ = std::fs::remove_dir_all(dir);
}
+
+#[test]
+fn find_missing_shows_an_indeterminate_row() {
+ let mut h = demo();
+ let dir = std::env::temp_dir().join(format!("lc-activity-findmissing-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ // run directly, not through a request (which runs frames): the search of an empty folder is over at once, but its
+ // result is applied between frames, and the row is there until then
+ let r = crate::menus::run_ui_command(&mut h.app, "file.findMissing", &json!({"folder": dir.to_string_lossy()})).unwrap().unwrap();
+ assert_eq!(r["background"], true, "{r}");
+ let rows = h.app.session.activity.list();
+ assert_eq!(rows.len(), 1, "{rows:?}");
+ assert_eq!((rows[0].kind, rows[0].label.as_str()), ("findMissing", "Find Missing Photos"));
+ assert_eq!((rows[0].total, rows[0].cancellable), (0, false), "indeterminate, no ✕");
+ assert!(h.step_until(T, |h| h.app.tasks.is_empty()));
+ assert!(h.app.session.activity.list().is_empty(), "the row goes with the task");
+ let _ = std::fs::remove_dir_all(dir);
+}
+
+#[test]
+fn auto_import_listing_has_no_row() {
+ let mut h = demo();
+ let dir = std::env::temp_dir().join(format!("lc-activity-autoimport-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ h.app.session.import_defaults.auto_folder = Some(dir.to_string_lossy().to_string());
+ // the watched folder is listed every few seconds, quietly
+ assert!(h.step_until(Duration::from_secs(10), |h| h.app.tasks.is_running("Auto Import")));
+ assert!(h.app.session.activity.list().is_empty(), "{:?}", h.app.session.activity.list());
+ h.app.session.import_defaults.auto_folder = None;
+ let _ = std::fs::remove_dir_all(dir);
+}